Merge pull request #303 from PhenomRetroShare/Fix_CorrectColoredNameContrast

Correct Colored Name Contrast in chat, same way than text.
This commit is contained in:
Cyril Soler 2016-03-12 09:22:28 -05:00
commit 4e2fd4e2c7
7 changed files with 48 additions and 24 deletions

View File

@ -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 &timestamp, const QString &message, unsigned int flag)
QString ChatStyle::formatMessage(enumFormatMessage type
, const QString &name
, const QDateTime &timestamp
, 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("<a name=\"name\">")).append(QString("</a>"));
@ -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() ) {

View File

@ -23,6 +23,7 @@
#ifndef _CHATSTYLE_H
#define _CHATSTYLE_H
#include <QColor>
#include <QString>
#include <QDateTime>
#include <QHash>
@ -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 &timestamp, const QString &message, unsigned int flag = 0);
QString formatMessage(enumFormatMessage type, const QString &name, const QDateTime &timestamp, const QString &message, unsigned int flag = 0, const QColor &backgroundColor = Qt::white);
static bool getAvailableStyles(enumStyleType styleType, QList<ChatStyleInfo> &styles);
static bool getAvailableVariants(const QString &stylePath, QStringList &variants);

View File

@ -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

View File

@ -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);

View File

@ -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)

View File

@ -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 <a> 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("<a href=\"hidden:%1\" title=\"%1\">%2</a>").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);
}

View File

@ -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 &currentElement, EmbedInHtml& embedInfos, ulong flag);