mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-28 08:59:37 -05:00
Merge pull request #303 from PhenomRetroShare/Fix_CorrectColoredNameContrast
Correct Colored Name Contrast in chat, same way than text.
This commit is contained in:
commit
4e2fd4e2c7
@ -265,7 +265,13 @@ static QString getStyle(const QDir &styleDir, const QString &styleVariant, enumG
|
|||||||
return style;
|
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;
|
bool me = false;
|
||||||
QDomDocument doc ;
|
QDomDocument doc ;
|
||||||
@ -343,20 +349,27 @@ QString ChatStyle::formatMessage(enumFormatMessage type, const QString &name, co
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef COLORED_NICKNAMES
|
#ifdef COLORED_NICKNAMES
|
||||||
QColor color;
|
QColor color;
|
||||||
if (flag & CHAT_FORMATMSG_SYSTEM) {
|
QString colorName;
|
||||||
color = Qt::darkBlue;
|
|
||||||
|
if (flag & CHAT_FORMATMSG_SYSTEM) {
|
||||||
|
color = Qt::darkBlue;
|
||||||
} else {
|
} else {
|
||||||
// Calculate color from the name
|
// Calculate color from the name
|
||||||
uint hash = 0;
|
uint hash = 0;
|
||||||
for (int i = 0; i < name.length(); ++i) {
|
for (int i = 0; i < name.length(); ++i) {
|
||||||
hash = (((hash << 1) + (hash >> 14)) ^ ((int) name[i].toLatin1())) & 0x3fff;
|
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
|
#else
|
||||||
Q_UNUSED(flag);
|
Q_UNUSED(flag);
|
||||||
|
Q_UNUSED(backgroundColor);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QString strName = RsHtml::plainText(name).prepend(QString("<a name=\"name\">")).append(QString("</a>"));
|
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("%date%", strDate)
|
||||||
.replace("%time%", strTime)
|
.replace("%time%", strTime)
|
||||||
#ifdef COLORED_NICKNAMES
|
#ifdef COLORED_NICKNAMES
|
||||||
.replace("%color%", color.name())
|
.replace("%color%", colorName)
|
||||||
#endif
|
#endif
|
||||||
.replace("%message%", messageBody ) ;
|
.replace("%message%", messageBody ) ;
|
||||||
if ( !styleOptimized.isEmpty() ) {
|
if ( !styleOptimized.isEmpty() ) {
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#ifndef _CHATSTYLE_H
|
#ifndef _CHATSTYLE_H
|
||||||
#define _CHATSTYLE_H
|
#define _CHATSTYLE_H
|
||||||
|
|
||||||
|
#include <QColor>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
@ -82,7 +83,7 @@ public:
|
|||||||
bool setStylePath(const QString &stylePath, const QString &styleVariant);
|
bool setStylePath(const QString &stylePath, const QString &styleVariant);
|
||||||
bool setStyleFromSettings(enumStyleType styleType);
|
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<ChatStyleInfo> &styles);
|
static bool getAvailableStyles(enumStyleType styleType, QList<ChatStyleInfo> &styles);
|
||||||
static bool getAvailableVariants(const QString &stylePath, QStringList &variants);
|
static bool getAvailableVariants(const QString &stylePath, QStringList &variants);
|
||||||
|
@ -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);
|
QString formattedMessage = RsHtml().formatText(ui->textBrowser->document(), message, formatTextFlag, backgroundColor, desiredContrast, desiredMinimumFontSize);
|
||||||
QDateTime dtTimestamp=incoming ? sendTime : recvTime;
|
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);
|
QString timeStamp = dtTimestamp.toString(Qt::ISODate);
|
||||||
|
|
||||||
//replace Date and Time anchors
|
//replace Date and Time anchors
|
||||||
|
@ -285,7 +285,8 @@ void ImHistoryBrowser::fillItem(QListWidgetItem *itemWidget, HistoryMsg& msg)
|
|||||||
name = QString::fromUtf8(msg.peerName.c_str());
|
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(Qt::DisplayRole, qVariantFromValue(IMHistoryItemPainter(formatMsg)));
|
||||||
itemWidget->setData(ROLE_MSGID, msg.msgId);
|
itemWidget->setData(ROLE_MSGID, msg.msgId);
|
||||||
|
@ -339,14 +339,15 @@ void ChatPage::setPreviewMessages(QString &stylePath, QString styleVariant, QTex
|
|||||||
QString nameIncoming = tr("Incoming");
|
QString nameIncoming = tr("Incoming");
|
||||||
QString nameOutgoing = tr("Outgoing");
|
QString nameOutgoing = tr("Outgoing");
|
||||||
QDateTime timestmp = QDateTime::fromTime_t(time(NULL));
|
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_HINCOMING, nameIncoming, timestmp, tr("Incoming message in history"), 0, backgroundColor));
|
||||||
textBrowser->append(style.formatMessage(ChatStyle::FORMATMSG_HOUTGOING, nameOutgoing, timestmp, tr("Outgoing message in history")));
|
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")));
|
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")));
|
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")));
|
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")));
|
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")));
|
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)
|
void ChatPage::fillPreview(QListWidget *listWidget, QComboBox *comboBox, QTextBrowser *textBrowser)
|
||||||
|
@ -258,7 +258,7 @@ void RsHtml::embedHtml(QTextDocument *textDocument, QDomDocument& doc, QDomEleme
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
QDomNodeList children = currentElement.childNodes();
|
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);
|
QDomNode node = children.item(index);
|
||||||
if(node.isElement()) {
|
if(node.isElement()) {
|
||||||
// child is an element, we skip it if it's an <a> tag
|
// 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();
|
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);
|
QDomNode node = children.item(index);
|
||||||
if (node.isElement()) {
|
if (node.isElement()) {
|
||||||
QDomElement element = node.toElement();
|
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.
|
* @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[in,out] val Name of the original color. Will be modified.
|
||||||
* @param bglum Background's relative luminance as returned by getRelativeLuminance().
|
* @param bglum Background's relative luminance as returned by getRelativeLuminance().
|
||||||
|
* @param desiredContrast Contrast to get.
|
||||||
*/
|
*/
|
||||||
static void findBestColor(QString &val, qreal bglum, qreal desiredContrast)
|
static void findBestColor(QString &val, qreal bglum, qreal desiredContrast)
|
||||||
{
|
{
|
||||||
@ -651,7 +652,7 @@ static void optimizeHtml(QDomDocument& doc
|
|||||||
bool addBR = false;
|
bool addBR = false;
|
||||||
|
|
||||||
QDomNodeList children = currentElement.childNodes();
|
QDomNodeList children = currentElement.childNodes();
|
||||||
for (uint index = 0; index < children.length(); ) {
|
for (uint index = 0; index < (uint)children.length(); ) {
|
||||||
QDomNode node = children.item(index);
|
QDomNode node = children.item(index);
|
||||||
if (node.isElement()) {
|
if (node.isElement()) {
|
||||||
QDomElement element = node.toElement();
|
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);
|
QString html = QString("<a href=\"hidden:%1\" title=\"%1\">%2</a>").arg(encoded, publictext);
|
||||||
cursor.insertHtml(html);
|
cursor.insertHtml(html);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RsHtml::findBestColor(QString &val, const QColor &backgroundColor /*= Qt::white*/, qreal desiredContrast /*= 1.0*/)
|
||||||
|
{
|
||||||
|
::findBestColor(val, ::getRelativeLuminance(backgroundColor), desiredContrast);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -77,6 +77,7 @@ public:
|
|||||||
|
|
||||||
static QString makeQuotedText(RSTextBrowser* browser);
|
static QString makeQuotedText(RSTextBrowser* browser);
|
||||||
static void insertSpoilerText(QTextCursor cursor);
|
static void insertSpoilerText(QTextCursor cursor);
|
||||||
|
static void findBestColor(QString &val, const QColor &backgroundColor = Qt::white, qreal desiredContrast = 1.0);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void embedHtml(QTextDocument *textDocument, QDomDocument &doc, QDomElement ¤tElement, EmbedInHtml& embedInfos, ulong flag);
|
void embedHtml(QTextDocument *textDocument, QDomDocument &doc, QDomElement ¤tElement, EmbedInHtml& embedInfos, ulong flag);
|
||||||
|
Loading…
Reference in New Issue
Block a user