From 3f155b9b753212d0614a844566ce5a27c63d1f9e Mon Sep 17 00:00:00 2001 From: Phenom Date: Mon, 7 Sep 2015 20:19:52 +0200 Subject: [PATCH] Add minimum font size in chat. --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 3 +- retroshare-gui/src/gui/settings/ChatPage.cpp | 2 ++ retroshare-gui/src/gui/settings/ChatPage.ui | 37 ++++++++++++++++++++ retroshare-gui/src/util/HandleRichText.cpp | 24 ++++++++++--- retroshare-gui/src/util/HandleRichText.h | 4 +-- 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 42344c4c8..24728ddc7 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -870,6 +870,7 @@ void ChatWidget::addChatMsg(bool incoming, const QString &name, const QDateTime if (!Settings->valueFromGroup("Chat", "EnableCustomFontSize", true).toBool()) { formatTextFlag |= RSHTML_FORMATTEXT_REMOVE_FONT_SIZE; } + int desiredMinimumFontSize = Settings->valueFromGroup("Chat", "MinimumFontSize", 10).toInt(); if (!Settings->valueFromGroup("Chat", "EnableBold", true).toBool()) { formatTextFlag |= RSHTML_FORMATTEXT_REMOVE_FONT_WEIGHT; } @@ -893,7 +894,7 @@ void ChatWidget::addChatMsg(bool incoming, const QString &name, const QDateTime formatFlag |= CHAT_FORMATMSG_SYSTEM; } - QString formattedMessage = RsHtml().formatText(ui->textBrowser->document(), message, formatTextFlag, backgroundColor, desiredContrast); + QString formattedMessage = RsHtml().formatText(ui->textBrowser->document(), message, formatTextFlag, backgroundColor, desiredContrast, desiredMinimumFontSize); QDateTime dtTimestamp=incoming ? sendTime : recvTime; QString formatMsg = chatStyle.formatMessage(type, name, dtTimestamp, formattedMessage, formatFlag); QString timeStamp = dtTimestamp.toString(Qt::ISODate); diff --git a/retroshare-gui/src/gui/settings/ChatPage.cpp b/retroshare-gui/src/gui/settings/ChatPage.cpp index d3619a014..bee6f0248 100644 --- a/retroshare-gui/src/gui/settings/ChatPage.cpp +++ b/retroshare-gui/src/gui/settings/ChatPage.cpp @@ -115,6 +115,7 @@ ChatPage::save(QString &/*errmsg*/) Settings->setValue("Emoteicons_GroupChat", ui.checkBox_emotegroupchat->isChecked()); Settings->setValue("EnableCustomFonts", ui.checkBox_enableCustomFonts->isChecked()); Settings->setValue("EnableCustomFontSize", ui.checkBox_enableCustomFontSize->isChecked()); + Settings->setValue("MinimumFontSize", ui.minimumFontSize->value()); Settings->setValue("EnableBold", ui.checkBox_enableBold->isChecked()); Settings->setValue("EnableItalics", ui.checkBox_enableItalics->isChecked()); Settings->setValue("MinimumContrast", ui.minimumContrast->value()); @@ -219,6 +220,7 @@ ChatPage::load() ui.checkBox_emotegroupchat->setChecked(Settings->value("Emoteicons_GroupChat", true).toBool()); ui.checkBox_enableCustomFonts->setChecked(Settings->value("EnableCustomFonts", true).toBool()); ui.checkBox_enableCustomFontSize->setChecked(Settings->value("EnableCustomFontSize", true).toBool()); + ui.minimumFontSize->setValue(Settings->value("MinimumFontSize", 10).toInt()); ui.checkBox_enableBold->setChecked(Settings->value("EnableBold", true).toBool()); ui.checkBox_enableItalics->setChecked(Settings->value("EnableItalics", true).toBool()); ui.minimumContrast->setValue(Settings->value("MinimumContrast", 4.5).toDouble()); diff --git a/retroshare-gui/src/gui/settings/ChatPage.ui b/retroshare-gui/src/gui/settings/ChatPage.ui index 43dadb5d2..1b3cfa544 100644 --- a/retroshare-gui/src/gui/settings/ChatPage.ui +++ b/retroshare-gui/src/gui/settings/ChatPage.ui @@ -75,6 +75,43 @@ + + + + 0 + + + + + Minimum font size + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 1 + + + 64 + + + + + diff --git a/retroshare-gui/src/util/HandleRichText.cpp b/retroshare-gui/src/util/HandleRichText.cpp index 980dcbd88..46227433d 100644 --- a/retroshare-gui/src/util/HandleRichText.cpp +++ b/retroshare-gui/src/util/HandleRichText.cpp @@ -376,7 +376,7 @@ static QString saveSpace(const QString text) return savedSpaceText; } -QString RsHtml::formatText(QTextDocument *textDocument, const QString &text, ulong flag, const QColor &backgroundColor, qreal desiredContrast) +QString RsHtml::formatText(QTextDocument *textDocument, const QString &text, ulong flag, const QColor &backgroundColor, qreal desiredContrast, int desiredMinimumFontSize) { if (flag == 0 || text.isEmpty()) { // nothing to do @@ -416,7 +416,7 @@ QString RsHtml::formatText(QTextDocument *textDocument, const QString &text, ulo formattedText = doc.toString(-1); // -1 removes any annoying carriage return misinterpreted by QTextEdit if (flag & RSHTML_OPTIMIZEHTML_MASK) { - optimizeHtml(formattedText, flag, backgroundColor, desiredContrast); + optimizeHtml(formattedText, flag, backgroundColor, desiredContrast, desiredMinimumFontSize); } return formattedText; @@ -734,12 +734,14 @@ static void optimizeHtml(QDomDocument& doc * is passed inside flag. * @param desiredContrast: Minimum contrast between text and background color, * between 1 and 21. + * @param desiredMinimumFontSize: Minimum font size. */ static void styleCreate(QDomDocument& doc , QHash stylesList , unsigned int flag , qreal bglum - , qreal desiredContrast) + , qreal desiredContrast + , int desiredMinimumFontSize) { QDomElement styleElem; do{ @@ -792,6 +794,15 @@ static void styleCreate(QDomDocument& doc QString key = keyvalue.at(0).trimmed(); QString val = keyvalue.at(1).trimmed(); + if (key == "font-size") { + QRegExp re("(\\d+)(\\D*)"); + if (re.indexIn(val) != -1) { + bool ok; int iVal = re.cap(1).toInt(&ok); + if (ok && (iVal < desiredMinimumFontSize)) { + val = QString::number(desiredMinimumFontSize) + re.cap(2); + } + } + } if ((flag & RSHTML_FORMATTEXT_REMOVE_FONT_FAMILY && key == "font-family") || (flag & RSHTML_FORMATTEXT_REMOVE_FONT_SIZE && key == "font-size") || (flag & RSHTML_FORMATTEXT_REMOVE_FONT_WEIGHT && key == "font-weight") || @@ -864,9 +875,12 @@ void RsHtml::optimizeHtml(QTextEdit *textEdit, QString &text, unsigned int flag * is passed inside flag. * @param desiredContrast Minimum contrast between text and background color, * between 1 and 21. + * @param desiredMinimumFontSize Minimum font size. */ void RsHtml::optimizeHtml(QString &text, unsigned int flag /*= 0*/ - , const QColor &backgroundColor /*= Qt::white*/, qreal desiredContrast /*= 1.0*/ + , const QColor &backgroundColor /*= Qt::white*/ + , qreal desiredContrast /*= 1.0*/ + , int desiredMinimumFontSize /*=10*/ ) { @@ -888,7 +902,7 @@ void RsHtml::optimizeHtml(QString &text, unsigned int flag /*= 0*/ QHash knownStyle; ::optimizeHtml(doc, body, stylesList, knownStyle); - ::styleCreate(doc, stylesList, flag, ::getRelativeLuminance(backgroundColor), desiredContrast); + ::styleCreate(doc, stylesList, flag, ::getRelativeLuminance(backgroundColor), desiredContrast, desiredMinimumFontSize); text = doc.toString(-1); // std::cerr << "Optimized text to " << text.length() << " bytes , instead of " << originalLength << std::endl; diff --git a/retroshare-gui/src/util/HandleRichText.h b/retroshare-gui/src/util/HandleRichText.h index d3d69bed5..9b8d91d84 100644 --- a/retroshare-gui/src/util/HandleRichText.h +++ b/retroshare-gui/src/util/HandleRichText.h @@ -59,11 +59,11 @@ public: static void initEmoticons(const QHash< QString, QString >& hash); - QString formatText(QTextDocument *textDocument, const QString &text, ulong flag, const QColor &backgroundColor = Qt::white, qreal desiredContrast = 1.0); + QString formatText(QTextDocument *textDocument, const QString &text, ulong flag, const QColor &backgroundColor = Qt::white, qreal desiredContrast = 1.0, int desiredMinimumFontSize = 10); static bool findAnchors(const QString &text, QStringList& urls); static void optimizeHtml(QTextEdit *textEdit, QString &text, unsigned int flag = 0); - static void optimizeHtml(QString &text, unsigned int flag = 0, const QColor &backgroundColor = Qt::white, qreal desiredContrast = 1.0); + static void optimizeHtml(QString &text, unsigned int flag = 0, const QColor &backgroundColor = Qt::white, qreal desiredContrast = 1.0, int desiredMinimumFontSize = 10); static QString toHtml(QString text, bool realHtml = true); static bool makeEmbeddedImage(const QString &fileName, QString &embeddedImage, const int maxPixels);