From 16c59af9f4490de8691df1b6730a9bec97639e3d Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 12 Mar 2016 13:25:10 +0100 Subject: [PATCH] Correct Colored Name Contrast in chat, same way than text. --- retroshare-gui/src/gui/chat/ChatStyle.cpp | 35 +++++++++++++------ retroshare-gui/src/gui/chat/ChatStyle.h | 3 +- retroshare-gui/src/gui/chat/ChatWidget.cpp | 2 +- .../src/gui/im_history/ImHistoryBrowser.cpp | 3 +- retroshare-gui/src/gui/settings/ChatPage.cpp | 15 ++++---- retroshare-gui/src/util/HandleRichText.cpp | 13 +++++-- retroshare-gui/src/util/HandleRichText.h | 1 + 7 files changed, 48 insertions(+), 24 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatStyle.cpp b/retroshare-gui/src/gui/chat/ChatStyle.cpp index ba24e30b0..e7f5b2c63 100644 --- a/retroshare-gui/src/gui/chat/ChatStyle.cpp +++ b/retroshare-gui/src/gui/chat/ChatStyle.cpp @@ -265,7 +265,13 @@ static QString getStyle(const QDir &styleDir, const QString &styleVariant, enumG return style; } -QString ChatStyle::formatMessage(enumFormatMessage type, const QString &name, const QDateTime ×tamp, const QString &message, unsigned int flag) +QString ChatStyle::formatMessage(enumFormatMessage type + , const QString &name + , const QDateTime ×tamp + , const QString &message + , unsigned int flag + , const QColor &backgroundColor /*= Qt::white*/ + ) { bool me = false; QDomDocument doc ; @@ -343,20 +349,27 @@ QString ChatStyle::formatMessage(enumFormatMessage type, const QString &name, co } #ifdef COLORED_NICKNAMES - QColor color; - if (flag & CHAT_FORMATMSG_SYSTEM) { - color = Qt::darkBlue; + QColor color; + QString colorName; + + if (flag & CHAT_FORMATMSG_SYSTEM) { + color = Qt::darkBlue; } else { - // Calculate color from the name - uint hash = 0; - for (int i = 0; i < name.length(); ++i) { - hash = (((hash << 1) + (hash >> 14)) ^ ((int) name[i].toLatin1())) & 0x3fff; + // Calculate color from the name + uint hash = 0; + for (int i = 0; i < name.length(); ++i) { + hash = (((hash << 1) + (hash >> 14)) ^ ((int) name[i].toLatin1())) & 0x3fff; } - color.setHsv(hash, 255, 150); + color.setHsv(hash, 255, 150); + // Always fix colors + qreal desiredContrast = Settings->valueFromGroup("Chat", "MinimumContrast", 4.5).toDouble(); + colorName = color.name(); + RsHtml::findBestColor(colorName, backgroundColor, desiredContrast); } #else - Q_UNUSED(flag); + Q_UNUSED(flag); + Q_UNUSED(backgroundColor); #endif QString strName = RsHtml::plainText(name).prepend(QString("")).append(QString("")); @@ -381,7 +394,7 @@ QString ChatStyle::formatMessage(enumFormatMessage type, const QString &name, co .replace("%date%", strDate) .replace("%time%", strTime) #ifdef COLORED_NICKNAMES - .replace("%color%", color.name()) + .replace("%color%", colorName) #endif .replace("%message%", messageBody ) ; if ( !styleOptimized.isEmpty() ) { diff --git a/retroshare-gui/src/gui/chat/ChatStyle.h b/retroshare-gui/src/gui/chat/ChatStyle.h index 25dab4654..75e395d58 100644 --- a/retroshare-gui/src/gui/chat/ChatStyle.h +++ b/retroshare-gui/src/gui/chat/ChatStyle.h @@ -23,6 +23,7 @@ #ifndef _CHATSTYLE_H #define _CHATSTYLE_H +#include #include #include #include @@ -82,7 +83,7 @@ public: bool setStylePath(const QString &stylePath, const QString &styleVariant); bool setStyleFromSettings(enumStyleType styleType); - QString formatMessage(enumFormatMessage type, const QString &name, const QDateTime ×tamp, const QString &message, unsigned int flag = 0); + QString formatMessage(enumFormatMessage type, const QString &name, const QDateTime ×tamp, const QString &message, unsigned int flag = 0, const QColor &backgroundColor = Qt::white); static bool getAvailableStyles(enumStyleType styleType, QList &styles); static bool getAvailableVariants(const QString &stylePath, QStringList &variants); diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 7c149a4b5..fecea1c36 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -914,7 +914,7 @@ void ChatWidget::addChatMsg(bool incoming, const QString &name, const RsGxsId gx 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 formatMsg = chatStyle.formatMessage(type, name, dtTimestamp, formattedMessage, formatFlag, backgroundColor); QString timeStamp = dtTimestamp.toString(Qt::ISODate); //replace Date and Time anchors diff --git a/retroshare-gui/src/gui/im_history/ImHistoryBrowser.cpp b/retroshare-gui/src/gui/im_history/ImHistoryBrowser.cpp index a107f82da..17b74a127 100644 --- a/retroshare-gui/src/gui/im_history/ImHistoryBrowser.cpp +++ b/retroshare-gui/src/gui/im_history/ImHistoryBrowser.cpp @@ -285,7 +285,8 @@ void ImHistoryBrowser::fillItem(QListWidgetItem *itemWidget, HistoryMsg& msg) name = QString::fromUtf8(msg.peerName.c_str()); } - QString formatMsg = style.formatMessage(type, name, QDateTime::fromTime_t(msg.sendTime), messageText); + QColor backgroundColor = ui.listWidget->palette().base().color(); + QString formatMsg = style.formatMessage(type, name, QDateTime::fromTime_t(msg.sendTime), messageText, 0, backgroundColor); itemWidget->setData(Qt::DisplayRole, qVariantFromValue(IMHistoryItemPainter(formatMsg))); itemWidget->setData(ROLE_MSGID, msg.msgId); diff --git a/retroshare-gui/src/gui/settings/ChatPage.cpp b/retroshare-gui/src/gui/settings/ChatPage.cpp index bd584b57e..da7168f7d 100644 --- a/retroshare-gui/src/gui/settings/ChatPage.cpp +++ b/retroshare-gui/src/gui/settings/ChatPage.cpp @@ -339,14 +339,15 @@ void ChatPage::setPreviewMessages(QString &stylePath, QString styleVariant, QTex QString nameIncoming = tr("Incoming"); QString nameOutgoing = tr("Outgoing"); QDateTime timestmp = QDateTime::fromTime_t(time(NULL)); + QColor backgroundColor = textBrowser->palette().base().color(); - textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_HINCOMING, nameIncoming, timestmp, tr("Incoming message in history"))); - textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_HOUTGOING, nameOutgoing, timestmp, tr("Outgoing message in history"))); - textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_INCOMING, nameIncoming, timestmp, tr("Incoming message"))); - textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_OUTGOING, nameOutgoing, timestmp, tr("Outgoing message"))); - textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_OOUTGOING, nameOutgoing, timestmp, tr("Outgoing offline message"))); - textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_SYSTEM, tr("System"), timestmp, tr("System message"))); - textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_OUTGOING, tr("UserName"), timestmp, tr("/me is sending a message with /me"))); + textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_HINCOMING, nameIncoming, timestmp, tr("Incoming message in history"), 0, backgroundColor)); + textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_HOUTGOING, nameOutgoing, timestmp, tr("Outgoing message in history"), 0, backgroundColor)); + textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_INCOMING, nameIncoming, timestmp, tr("Incoming message"), 0, backgroundColor)); + textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_OUTGOING, nameOutgoing, timestmp, tr("Outgoing message"), 0, backgroundColor)); + textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_OOUTGOING, nameOutgoing, timestmp, tr("Outgoing offline message"), 0, backgroundColor)); + textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_SYSTEM, tr("System"), timestmp, tr("System message"), 0, backgroundColor)); + textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_OUTGOING, tr("UserName"), timestmp, tr("/me is sending a message with /me"), 0, backgroundColor)); } void ChatPage::fillPreview(QListWidget *listWidget, QComboBox *comboBox, QTextBrowser *textBrowser) diff --git a/retroshare-gui/src/util/HandleRichText.cpp b/retroshare-gui/src/util/HandleRichText.cpp index 886bb60bb..d3adda36b 100644 --- a/retroshare-gui/src/util/HandleRichText.cpp +++ b/retroshare-gui/src/util/HandleRichText.cpp @@ -258,7 +258,7 @@ void RsHtml::embedHtml(QTextDocument *textDocument, QDomDocument& doc, QDomEleme return; QDomNodeList children = currentElement.childNodes(); - for(uint index = 0; index < children.length(); index++) { + for(uint index = 0; index < (uint)children.length(); index++) { QDomNode node = children.item(index); if(node.isElement()) { // child is an element, we skip it if it's an tag @@ -473,7 +473,7 @@ static void findElements(QDomDocument& doc, QDomElement& currentElement, const Q } QDomNodeList children = currentElement.childNodes(); - for (uint index = 0; index < children.length(); index++) { + for (uint index = 0; index < (uint)children.length(); index++) { QDomNode node = children.item(index); if (node.isElement()) { QDomElement element = node.toElement(); @@ -555,6 +555,7 @@ static qreal getContrastRatio(qreal lum1, qreal lum2) * @brief Find a color with the same hue that provides the desired contrast with bglum. * @param[in,out] val Name of the original color. Will be modified. * @param bglum Background's relative luminance as returned by getRelativeLuminance(). + * @param desiredContrast Contrast to get. */ static void findBestColor(QString &val, qreal bglum, qreal desiredContrast) { @@ -651,7 +652,7 @@ static void optimizeHtml(QDomDocument& doc bool addBR = false; QDomNodeList children = currentElement.childNodes(); - for (uint index = 0; index < children.length(); ) { + for (uint index = 0; index < (uint)children.length(); ) { QDomNode node = children.item(index); if (node.isElement()) { QDomElement element = node.toElement(); @@ -1087,3 +1088,9 @@ void RsHtml::insertSpoilerText(QTextCursor cursor) QString html = QString("%2").arg(encoded, publictext); cursor.insertHtml(html); } + +void RsHtml::findBestColor(QString &val, const QColor &backgroundColor /*= Qt::white*/, qreal desiredContrast /*= 1.0*/) +{ + ::findBestColor(val, ::getRelativeLuminance(backgroundColor), desiredContrast); +} + diff --git a/retroshare-gui/src/util/HandleRichText.h b/retroshare-gui/src/util/HandleRichText.h index e832ee02a..b3efbb1c1 100644 --- a/retroshare-gui/src/util/HandleRichText.h +++ b/retroshare-gui/src/util/HandleRichText.h @@ -77,6 +77,7 @@ public: static QString makeQuotedText(RSTextBrowser* browser); static void insertSpoilerText(QTextCursor cursor); + static void findBestColor(QString &val, const QColor &backgroundColor = Qt::white, qreal desiredContrast = 1.0); protected: void embedHtml(QTextDocument *textDocument, QDomDocument &doc, QDomElement ¤tElement, EmbedInHtml& embedInfos, ulong flag);