From dc05a14f4c2c7f2567806fcb197b26be85c96c39 Mon Sep 17 00:00:00 2001 From: Zach H Date: Sat, 11 Jul 2015 20:45:37 -0400 Subject: [PATCH 1/6] Highlight Custom Words --- cockatrice/src/chatview.cpp | 161 +++++++++++++++++++++---------- cockatrice/src/dlg_settings.cpp | 18 +++- cockatrice/src/dlg_settings.h | 3 + cockatrice/src/settingscache.cpp | 8 ++ cockatrice/src/settingscache.h | 3 + 5 files changed, 141 insertions(+), 52 deletions(-) diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview.cpp index 2b967ac4..cb84df83 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview.cpp @@ -161,72 +161,115 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use } cursor.setCharFormat(messageFormat); - int index = -1, bracketFirstIndex = -1, mentionFirstIndex = -1, urlFirstIndex = -1; + int index = -1, bracketFirstIndex = -1, mentionFirstIndex = -1, urlFirstIndex = -1, highlightWordFirstIndex = -1; bool mentionEnabled = settingsCache->getChatMention(); const QRegExp urlStarter = QRegExp("https?://|\\bwww\\."); const QRegExp phraseEnder = QRegExp("\\s"); const QRegExp notALetterOrNumber = QRegExp("[^a-zA-Z0-9]"); + const QStringList highlightedWords = settingsCache->getHighlightWords(); while (message.size()) { - // search for the first [ or @ bracketFirstIndex = message.indexOf('['); mentionFirstIndex = message.indexOf('@'); urlFirstIndex = message.indexOf(urlStarter); + highlightWordFirstIndex = -1; + + foreach (QString word, message.simplified().split(" ")) + { + if (highlightedWords.contains(word, Qt::CaseInsensitive)) + { + highlightWordFirstIndex = message.indexOf(word); + break; + } + } bool startsWithBracket = (bracketFirstIndex != -1); bool startsWithAtSymbol = (mentionFirstIndex != -1); - bool startsWithUrl = (urlFirstIndex != -1); + bool startsWithUrl = (urlFirstIndex != -1); + bool startsWithHighlightWord = (highlightWordFirstIndex != -1); + - if (!startsWithBracket) + if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) { - if (!startsWithAtSymbol) - { - if (!startsWithUrl) - { - // No brackets, mentions, or urls. Send message as normal - cursor.insertText(message); - break; - } - else - { - // There's a URL, lets begin! - index = urlFirstIndex; - } - } - else - { - if (!startsWithUrl) - { - // There's an @ symbol, lets begin! - index = mentionFirstIndex; - } - else - { - // There's both an @ symbol and URL, pick the first one... lets begin! - index = std::min(urlFirstIndex, mentionFirstIndex); - } - } + // No functions need to be run. Send message as normal + cursor.insertText(message); + break; } - else + else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) { - if (!startsWithAtSymbol) - { - // There's a [, look down! - index = bracketFirstIndex; - } - else - { - // There's both a [ and @, pick the first one... look down! - index = std::min(bracketFirstIndex, mentionFirstIndex); - } - - if (startsWithUrl) - { - // If there's a URL, pick the first one... then lets begin! - // Otherwise, just "lets begin!" - index = std::min(index, urlFirstIndex); - } + // Contains a bracket + index = bracketFirstIndex; + } + else if (!startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) + { + // Contains an @ symbol + index = mentionFirstIndex; + } + else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains URL stuff (http or www.) + index = urlFirstIndex; + } + else if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains a word the user wants highlighted + index = highlightWordFirstIndex; + } + else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) + { + // Contains both a bracket and an @ symbol + index = std::min(bracketFirstIndex, mentionFirstIndex); + } + else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains both a bracket and URL stuff + index = std::min(bracketFirstIndex, urlFirstIndex); + } + else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains both a bracket and a word the user wants highlighted + index = std::min(bracketFirstIndex, highlightWordFirstIndex); + } + else if (!startsWithBracket && startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains both an @ symbol and URL stuff + index = std::min(mentionFirstIndex, urlFirstIndex); + } + else if (!startsWithBracket && startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains both an @ symbol and a word the user wants highlighted + index = std::min(mentionFirstIndex, highlightWordFirstIndex); + } + else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains both URL stuff and a word the user wants highlighted + index = std::min(urlFirstIndex, highlightWordFirstIndex); + } + else if (!startsWithBracket && startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains an @ symbol, URL stuff, and a word the user wants highlighted + index = std::min(mentionFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex)); + } + else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains a bracket, URL stuff, and a word the user wants highlighted + index = std::min(bracketFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex)); + } + else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains a bracket, an @ symbol, and a word the user wants highlighted + index = std::min(bracketFirstIndex, std::min(mentionFirstIndex, highlightWordFirstIndex)); + } + else if (startsWithBracket && startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains a bracket, an @ symbol, and URL stuff + index = std::min(bracketFirstIndex, std::min(mentionFirstIndex, urlFirstIndex)); + } + else if (startsWithBracket && startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains a bracket, an @ symbol, URL stuff, and a word the user wants highlighted + index = std::min(highlightWordFirstIndex, std::min(bracketFirstIndex, std::min(mentionFirstIndex, urlFirstIndex))); } if (index > 0) @@ -374,9 +417,27 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use while (fullMentionUpToSpaceOrEnd.size()); } } + else if (index == highlightWordFirstIndex) + { + // You have received a valid mention of custom word!! + int firstSpace = (message.indexOf(" ") == -1 ? message.size() : message.indexOf(" ")); + mentionFormat.setBackground(QBrush(getCustomMentionColor())); + mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); + cursor.insertText(message.mid(0, firstSpace), mentionFormat); + cursor.setCharFormat(defaultFormat); + message = message.mid(firstSpace); + QApplication::alert(this); + if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) + { + QString ref = sender.left(sender.length() - 2); + showSystemPopup(ref); + } + } else { - message = message.mid(1); // Not certain when this would ever be reached, but just incase lets skip the character + // Not certain when this would ever be reached, but just incase lets skip the character + cursor.insertText(message.mid(0), defaultFormat); + message = message.mid(1); } } diff --git a/cockatrice/src/dlg_settings.cpp b/cockatrice/src/dlg_settings.cpp index 86a0fd4a..3159f88d 100644 --- a/cockatrice/src/dlg_settings.cpp +++ b/cockatrice/src/dlg_settings.cpp @@ -591,6 +591,11 @@ MessagesSettingsPage::MessagesSettingsPage() mentionPopups.setChecked(settingsCache->getShowMentionPopup()); connect(&mentionPopups, SIGNAL(stateChanged(int)), settingsCache, SLOT(setShowMentionPopups(int))); + customAlertString = new QLineEdit(); + customAlertString->setPlaceholderText("Word1, Word2, Word3"); + customAlertString->setText(settingsCache->getHighlightWords().join(", ")); + connect(customAlertString, SIGNAL(textChanged(QString)), settingsCache, SLOT(setHighlightWords(QString))); + QGridLayout *chatGrid = new QGridLayout; chatGrid->addWidget(&chatMentionCheckBox, 0, 0); chatGrid->addWidget(&invertMentionForeground, 0, 1); @@ -602,6 +607,12 @@ MessagesSettingsPage::MessagesSettingsPage() chatGrid->addWidget(&mentionPopups, 4, 0); chatGroupBox = new QGroupBox; chatGroupBox->setLayout(chatGrid); + + QGridLayout *highlightNotice = new QGridLayout; + highlightNotice->addWidget(customAlertString, 0, 0); + highlightNotice->addWidget(&customAlertStringLabel, 1, 0); + highlightGroupBox = new QGroupBox; + highlightGroupBox->setLayout(highlightNotice); QSettings settings; messageList = new QListWidget; @@ -628,11 +639,12 @@ MessagesSettingsPage::MessagesSettingsPage() messageShortcuts = new QGroupBox; messageShortcuts->setLayout(messageListLayout); - + QVBoxLayout *mainLayout = new QVBoxLayout; - + mainLayout->addWidget(messageShortcuts); mainLayout->addWidget(chatGroupBox); + mainLayout->addWidget(highlightGroupBox); setLayout(mainLayout); @@ -688,6 +700,7 @@ void MessagesSettingsPage::actRemove() void MessagesSettingsPage::retranslateUi() { chatGroupBox->setTitle(tr("Chat settings")); + highlightGroupBox->setTitle(tr("Custom alert words")); chatMentionCheckBox.setText(tr("Enable chat mentions")); messageShortcuts->setTitle(tr("In-game message macros")); ignoreUnregUsersMainChat.setText(tr("Ignore unregistered users in main chat")); @@ -697,6 +710,7 @@ void MessagesSettingsPage::retranslateUi() messagePopups.setText(tr("Enable desktop notifications for private messages.")); mentionPopups.setText(tr("Enable desktop notification for mentions.")); hexLabel.setText(tr("(Color is hexadecimal)")); + customAlertStringLabel.setText(tr("(Seperate each word with a comma; Words are case insensitive)")); } diff --git a/cockatrice/src/dlg_settings.h b/cockatrice/src/dlg_settings.h index 5b2f06e5..71811434 100644 --- a/cockatrice/src/dlg_settings.h +++ b/cockatrice/src/dlg_settings.h @@ -172,9 +172,12 @@ private: QCheckBox messagePopups; QCheckBox mentionPopups; QGroupBox *chatGroupBox; + QGroupBox *highlightGroupBox; QGroupBox *messageShortcuts; QLineEdit *mentionColor; + QLineEdit *customAlertString; QLabel hexLabel; + QLabel customAlertStringLabel; void storeSettings(); void updateMentionPreview(); diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index 202a0356..ab6a8b38 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -82,6 +82,8 @@ SettingsCache::SettingsCache() masterVolume = settings->value("sound/mastervolume", 100).toInt(); cardInfoViewMode = settings->value("cards/cardinfoviewmode", 0).toInt(); + + highlightWords = settings->value("personal/highlightWords", QStringList()).toStringList(); } void SettingsCache::setCardInfoViewMode(const int _viewMode) { @@ -89,6 +91,12 @@ void SettingsCache::setCardInfoViewMode(const int _viewMode) { settings->setValue("cards/cardinfoviewmode", cardInfoViewMode); } +void SettingsCache::setHighlightWords(const QString _highlightWords) { + // Words are seperated by a comma and you can not use spaces in words + highlightWords = _highlightWords.simplified().replace(" ", "").split(","); + settings->setValue("personal/highlightWords", highlightWords); +} + void SettingsCache::setMasterVolume(int _masterVolume) { masterVolume = _masterVolume; settings->setValue("sound/mastervolume", masterVolume); diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index c838f74e..5e7b2a85 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -85,6 +85,7 @@ private: bool leftJustified; int masterVolume; int cardInfoViewMode; + QStringList highlightWords; public: SettingsCache(); const QByteArray &getMainWindowGeometry() const { return mainWindowGeometry; } @@ -143,6 +144,7 @@ public: int getMasterVolume() const { return masterVolume; } int getCardInfoViewMode() const { return cardInfoViewMode; } QStringList getCountries() const; + QStringList getHighlightWords() const { return highlightWords; } public slots: void setMainWindowGeometry(const QByteArray &_mainWindowGeometry); void setLang(const QString &_lang); @@ -194,6 +196,7 @@ public slots: void setLeftJustified( const int _leftJustified); void setMasterVolume(const int _masterVolume); void setCardInfoViewMode(const int _viewMode); + void setHighlightWords(const QString _highlightWords); }; extern SettingsCache *settingsCache; From 97f298452e01638b899993dd971069b09a9d3154 Mon Sep 17 00:00:00 2001 From: Zach H Date: Sun, 12 Jul 2015 11:00:11 -0400 Subject: [PATCH 2/6] first fixes --- cockatrice/src/chatview.cpp | 5 ----- cockatrice/src/dlg_settings.cpp | 2 +- cockatrice/src/settingscache.cpp | 2 +- cockatrice/src/settingscache.h | 2 +- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview.cpp index cb84df83..4863b2c1 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview.cpp @@ -427,11 +427,6 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use cursor.setCharFormat(defaultFormat); message = message.mid(firstSpace); QApplication::alert(this); - if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) - { - QString ref = sender.left(sender.length() - 2); - showSystemPopup(ref); - } } else { diff --git a/cockatrice/src/dlg_settings.cpp b/cockatrice/src/dlg_settings.cpp index 3159f88d..1a86be51 100644 --- a/cockatrice/src/dlg_settings.cpp +++ b/cockatrice/src/dlg_settings.cpp @@ -710,7 +710,7 @@ void MessagesSettingsPage::retranslateUi() messagePopups.setText(tr("Enable desktop notifications for private messages.")); mentionPopups.setText(tr("Enable desktop notification for mentions.")); hexLabel.setText(tr("(Color is hexadecimal)")); - customAlertStringLabel.setText(tr("(Seperate each word with a comma; Words are case insensitive)")); + customAlertStringLabel.setText(tr("(Seperate each word with a comma, words are case insensitive)")); } diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index ab6a8b38..01f1b960 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -91,7 +91,7 @@ void SettingsCache::setCardInfoViewMode(const int _viewMode) { settings->setValue("cards/cardinfoviewmode", cardInfoViewMode); } -void SettingsCache::setHighlightWords(const QString _highlightWords) { +void SettingsCache::setHighlightWords(const QString &_highlightWords) { // Words are seperated by a comma and you can not use spaces in words highlightWords = _highlightWords.simplified().replace(" ", "").split(","); settings->setValue("personal/highlightWords", highlightWords); diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index 5e7b2a85..12916805 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -196,7 +196,7 @@ public slots: void setLeftJustified( const int _leftJustified); void setMasterVolume(const int _masterVolume); void setCardInfoViewMode(const int _viewMode); - void setHighlightWords(const QString _highlightWords); + void setHighlightWords(const QString &_highlightWords); }; extern SettingsCache *settingsCache; From 87c70466a40dcde45cf57e3c3c36f5096345fcec Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Sun, 12 Jul 2015 23:56:32 +0200 Subject: [PATCH 3/6] Rework message parser --- cockatrice/src/chatview.cpp | 470 +++++++++++++++--------------------- cockatrice/src/chatview.h | 3 + 2 files changed, 199 insertions(+), 274 deletions(-) diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview.cpp index 4863b2c1..87548832 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview.cpp @@ -53,14 +53,15 @@ QTextCursor ChatView::prepareBlock(bool same) QTextCursor cursor(document()->lastBlock()); cursor.movePosition(QTextCursor::End); - if (!same) { + if (same) { + cursor.insertHtml("
"); + } else { QTextBlockFormat blockFormat; if ((evenNumber = !evenNumber)) blockFormat.setBackground(palette().alternateBase()); blockFormat.setBottomMargin(4); cursor.insertBlock(blockFormat); - } else - cursor.insertHtml("
"); + } return cursor; } @@ -120,6 +121,7 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use QTextCursor cursor = prepareBlock(sameSender); lastSender = sender; + // timestamp if (showTimestamps && !sameSender) { QTextCharFormat timeFormat; timeFormat.setForeground(QColor(SERVER_MESSAGE_COLOR)); @@ -128,7 +130,8 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use cursor.setCharFormat(timeFormat); cursor.insertText(QDateTime::currentDateTime().toString("[hh:mm:ss] ")); } - + + // nickname QTextCharFormat senderFormat; if (tabSupervisor && tabSupervisor->getUserInfo() && (sender == QString::fromStdString(tabSupervisor->getUserInfo()->name()))) { senderFormat.setForeground(QBrush(getCustomMentionColor())); @@ -140,7 +143,9 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use } senderFormat.setAnchor(true); senderFormat.setAnchorHref("user://" + QString::number(userLevel) + "_" + sender); - if (!sameSender) { + if (sameSender) { + cursor.insertText(" "); + } else { if (!sender.isEmpty() && tabSupervisor->getUserListsTab()) { const int pixelSize = QFontInfo(cursor.charFormat().font()).pixelSize(); QMap buddyList = tabSupervisor->getUserListsTab()->getBuddyList()->getUsers(); @@ -151,288 +156,38 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use if (!sender.isEmpty()) sender.append(": "); cursor.insertText(sender); - } else - cursor.insertText(" "); - + } + QTextCharFormat messageFormat; if (sender.isEmpty()) { messageFormat.setForeground(Qt::darkGreen); messageFormat.setFontWeight(QFont::Bold); } cursor.setCharFormat(messageFormat); - - int index = -1, bracketFirstIndex = -1, mentionFirstIndex = -1, urlFirstIndex = -1, highlightWordFirstIndex = -1; + bool mentionEnabled = settingsCache->getChatMention(); - const QRegExp urlStarter = QRegExp("https?://|\\bwww\\."); - const QRegExp phraseEnder = QRegExp("\\s"); - const QRegExp notALetterOrNumber = QRegExp("[^a-zA-Z0-9]"); - const QStringList highlightedWords = settingsCache->getHighlightWords(); - + // parse the message while (message.size()) { - bracketFirstIndex = message.indexOf('['); - mentionFirstIndex = message.indexOf('@'); - urlFirstIndex = message.indexOf(urlStarter); - highlightWordFirstIndex = -1; - - foreach (QString word, message.simplified().split(" ")) + QChar c = message.at(0); + switch(c.toLatin1()) { - if (highlightedWords.contains(word, Qt::CaseInsensitive)) - { - highlightWordFirstIndex = message.indexOf(word); + case '[': + checkTag(cursor, message); break; - } - } - - bool startsWithBracket = (bracketFirstIndex != -1); - bool startsWithAtSymbol = (mentionFirstIndex != -1); - bool startsWithUrl = (urlFirstIndex != -1); - bool startsWithHighlightWord = (highlightWordFirstIndex != -1); - - - if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) - { - // No functions need to be run. Send message as normal - cursor.insertText(message); - break; - } - else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) - { - // Contains a bracket - index = bracketFirstIndex; - } - else if (!startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) - { - // Contains an @ symbol - index = mentionFirstIndex; - } - else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) - { - // Contains URL stuff (http or www.) - index = urlFirstIndex; - } - else if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) - { - // Contains a word the user wants highlighted - index = highlightWordFirstIndex; - } - else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) - { - // Contains both a bracket and an @ symbol - index = std::min(bracketFirstIndex, mentionFirstIndex); - } - else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) - { - // Contains both a bracket and URL stuff - index = std::min(bracketFirstIndex, urlFirstIndex); - } - else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) - { - // Contains both a bracket and a word the user wants highlighted - index = std::min(bracketFirstIndex, highlightWordFirstIndex); - } - else if (!startsWithBracket && startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) - { - // Contains both an @ symbol and URL stuff - index = std::min(mentionFirstIndex, urlFirstIndex); - } - else if (!startsWithBracket && startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) - { - // Contains both an @ symbol and a word the user wants highlighted - index = std::min(mentionFirstIndex, highlightWordFirstIndex); - } - else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) - { - // Contains both URL stuff and a word the user wants highlighted - index = std::min(urlFirstIndex, highlightWordFirstIndex); - } - else if (!startsWithBracket && startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) - { - // Contains an @ symbol, URL stuff, and a word the user wants highlighted - index = std::min(mentionFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex)); - } - else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) - { - // Contains a bracket, URL stuff, and a word the user wants highlighted - index = std::min(bracketFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex)); - } - else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) - { - // Contains a bracket, an @ symbol, and a word the user wants highlighted - index = std::min(bracketFirstIndex, std::min(mentionFirstIndex, highlightWordFirstIndex)); - } - else if (startsWithBracket && startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) - { - // Contains a bracket, an @ symbol, and URL stuff - index = std::min(bracketFirstIndex, std::min(mentionFirstIndex, urlFirstIndex)); - } - else if (startsWithBracket && startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) - { - // Contains a bracket, an @ symbol, URL stuff, and a word the user wants highlighted - index = std::min(highlightWordFirstIndex, std::min(bracketFirstIndex, std::min(mentionFirstIndex, urlFirstIndex))); - } - - if (index > 0) - { - cursor.insertText(message.left(index), defaultFormat); - message = message.mid(index); - } - - if (index == bracketFirstIndex) // The message now starts with a bracket ->> [ <<- that symbol - { - if (message.startsWith("[card]")) - { - message = message.mid(6); - int closeTagIndex = message.indexOf("[/card]"); - QString cardName = message.left(closeTagIndex); - if (closeTagIndex == -1) - message.clear(); + case '@': + if(mentionEnabled) + checkMention(cursor, message, sender, userLevel); else - message = message.mid(closeTagIndex + 7); - - appendCardTag(cursor, cardName); - } - else if (message.startsWith("[[")) - { - message = message.mid(2); - int closeTagIndex = message.indexOf("]]"); - QString cardName = message.left(closeTagIndex); - if (closeTagIndex == -1) - message.clear(); - else - message = message.mid(closeTagIndex + 2); - - appendCardTag(cursor, cardName); - } - else if (message.startsWith("[url]")) - { - message = message.mid(5); - int closeTagIndex = message.indexOf("[/url]"); - QString url = message.left(closeTagIndex); - if (closeTagIndex == -1) - message.clear(); - else - message = message.mid(closeTagIndex + 6); - - appendUrlTag(cursor, url); - } - else - { - // Not a valid tag - cursor.insertText("[", defaultFormat); + checkWord(cursor, message); + break; + case ' ': + cursor.insertText(" ", defaultFormat); message = message.mid(1); - } - } - else if (index == urlFirstIndex) // The message now starts with either: www. , http:// , or https:// - { - int urlEndIndex = message.indexOf(phraseEnder, 0); - if (urlEndIndex == -1) - urlEndIndex = message.size(); - QString urlText = message.left(urlEndIndex); - QUrl qUrl(urlText); - if (qUrl.isValid()) - appendUrlTag(cursor, urlText); - else - cursor.insertText(urlText); - if (urlEndIndex == -1) - message.clear(); - else - message = message.mid(urlEndIndex); - } - else if (index == mentionFirstIndex) - { - int firstSpace = message.indexOf(" "); - QString fullMentionUpToSpaceOrEnd = (firstSpace == -1) ? message.mid(1) : message.mid(1, firstSpace - 1); - QString mentionIntact = fullMentionUpToSpaceOrEnd; - - if ((!mentionEnabled && !isModeratorSendingGlobal(userLevel, fullMentionUpToSpaceOrEnd)) || tabSupervisor->getIsLocalGame()) - { - cursor.insertText("@"); - message = message.mid(1); - } - else - { - QMap userList = tabSupervisor->getUserListsTab()->getAllUsersList()->getUsers(); - - do - { - if (isFullMentionAValidUser(userList, fullMentionUpToSpaceOrEnd)) // Is there a user online named this? - { - if (userName.toLower() == fullMentionUpToSpaceOrEnd.toLower()) // Is this user you? - { - // You have received a valid mention!! - mentionFormat.setBackground(QBrush(getCustomMentionColor())); - mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); - cursor.insertText(mention, mentionFormat); - message = message.mid(mention.size()); - QApplication::alert(this); - if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) - { - QString ref = sender.left(sender.length() - 2); - showSystemPopup(ref); - } - } - else - { - QString correctUserName = getNameFromUserList(userList, fullMentionUpToSpaceOrEnd); - UserListTWI *vlu = userList.value(correctUserName); - mentionFormatOtherUser.setAnchorHref("user://" + QString::number(vlu->getUserInfo().user_level()) + "_" + correctUserName); - cursor.insertText("@" + correctUserName, mentionFormatOtherUser); - - message = message.mid(correctUserName.size() + 1); - } - - cursor.setCharFormat(defaultFormat); - break; - } - else if (isModeratorSendingGlobal(userLevel, fullMentionUpToSpaceOrEnd)) - { - // Moderator Sending Global Message - mentionFormat.setBackground(QBrush(getCustomMentionColor())); - mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); - cursor.insertText("@" + fullMentionUpToSpaceOrEnd, mentionFormat); - message = message.mid(fullMentionUpToSpaceOrEnd.size() + 1); - QApplication::alert(this); - if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) - { - QString ref = sender.left(sender.length() - 2); - showSystemPopup(ref); - } - - cursor.setCharFormat(defaultFormat); - break; - } - else if (fullMentionUpToSpaceOrEnd.right(1).indexOf(notALetterOrNumber) == -1 || fullMentionUpToSpaceOrEnd.size() < 2) - { - cursor.insertText("@" + mentionIntact, defaultFormat); - message = message.mid(mentionIntact.size() + 1); - cursor.setCharFormat(defaultFormat); - break; - } - else - { - fullMentionUpToSpaceOrEnd.chop(1); - } - } - while (fullMentionUpToSpaceOrEnd.size()); - } - } - else if (index == highlightWordFirstIndex) - { - // You have received a valid mention of custom word!! - int firstSpace = (message.indexOf(" ") == -1 ? message.size() : message.indexOf(" ")); - mentionFormat.setBackground(QBrush(getCustomMentionColor())); - mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); - cursor.insertText(message.mid(0, firstSpace), mentionFormat); - cursor.setCharFormat(defaultFormat); - message = message.mid(firstSpace); - QApplication::alert(this); - } - else - { - // Not certain when this would ever be reached, but just incase lets skip the character - cursor.insertText(message.mid(0), defaultFormat); - message = message.mid(1); + break; + default: + checkWord(cursor, message); + break; } } @@ -440,6 +195,173 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use verticalScrollBar()->setValue(verticalScrollBar()->maximum()); } +void ChatView::checkTag(QTextCursor &cursor, QString &message) +{ + if (message.startsWith("[card]")) + { + message = message.mid(6); + int closeTagIndex = message.indexOf("[/card]"); + QString cardName = message.left(closeTagIndex); + if (closeTagIndex == -1) + message.clear(); + else + message = message.mid(closeTagIndex + 7); + + appendCardTag(cursor, cardName); + return; + } + + if (message.startsWith("[[")) + { + message = message.mid(2); + int closeTagIndex = message.indexOf("]]"); + QString cardName = message.left(closeTagIndex); + if (closeTagIndex == -1) + message.clear(); + else + message = message.mid(closeTagIndex + 2); + + appendCardTag(cursor, cardName); + return; + } + + if (message.startsWith("[url]")) + { + message = message.mid(5); + int closeTagIndex = message.indexOf("[/url]"); + QString url = message.left(closeTagIndex); + if (closeTagIndex == -1) + message.clear(); + else + message = message.mid(closeTagIndex + 6); + + appendUrlTag(cursor, url); + return; + } + + // no valid tag found + checkWord(cursor, message); +} + +void ChatView::checkMention(QTextCursor &cursor, QString &message, QString &sender, UserLevelFlags userLevel) +{ + const QRegExp notALetterOrNumber = QRegExp("[^a-zA-Z0-9]"); + + int firstSpace = message.indexOf(' '); + QString fullMentionUpToSpaceOrEnd = (firstSpace == -1) ? message.mid(1) : message.mid(1, firstSpace - 1); + QString mentionIntact = fullMentionUpToSpaceOrEnd; + + QMap userList = tabSupervisor->getUserListsTab()->getAllUsersList()->getUsers(); + + while (fullMentionUpToSpaceOrEnd.size()) + { + if (isFullMentionAValidUser(userList, fullMentionUpToSpaceOrEnd)) // Is there a user online named this? + { + if (userName.toLower() == fullMentionUpToSpaceOrEnd.toLower()) // Is this user you? + { + // You have received a valid mention!! + mentionFormat.setBackground(QBrush(getCustomMentionColor())); + mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); + cursor.insertText(mention, mentionFormat); + message = message.mid(mention.size()); + QApplication::alert(this); + if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) + { + QString ref = sender.left(sender.length() - 2); + showSystemPopup(ref); + } + } else { + QString correctUserName = getNameFromUserList(userList, fullMentionUpToSpaceOrEnd); + UserListTWI *vlu = userList.value(correctUserName); + mentionFormatOtherUser.setAnchorHref("user://" + QString::number(vlu->getUserInfo().user_level()) + "_" + correctUserName); + cursor.insertText("@" + correctUserName, mentionFormatOtherUser); + + message = message.mid(correctUserName.size() + 1); + } + + cursor.setCharFormat(defaultFormat); + return; + } + + if (isModeratorSendingGlobal(userLevel, fullMentionUpToSpaceOrEnd)) { + // Moderator Sending Global Message + mentionFormat.setBackground(QBrush(getCustomMentionColor())); + mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); + cursor.insertText("@" + fullMentionUpToSpaceOrEnd, mentionFormat); + message = message.mid(fullMentionUpToSpaceOrEnd.size() + 1); + QApplication::alert(this); + if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) + { + QString ref = sender.left(sender.length() - 2); + showSystemPopup(ref); + } + + cursor.setCharFormat(defaultFormat); + return; + } + + if (fullMentionUpToSpaceOrEnd.right(1).indexOf(notALetterOrNumber) == -1 || fullMentionUpToSpaceOrEnd.size() < 2) + { + cursor.insertText("@" + mentionIntact, defaultFormat); + message = message.mid(mentionIntact.size() + 1); + cursor.setCharFormat(defaultFormat); + return; + } + + fullMentionUpToSpaceOrEnd.chop(1); + } + + // no valid mention found + checkWord(cursor, message); +} + +void ChatView::checkWord(QTextCursor &cursor, QString &message) +{ + // extract the first word + int firstSpace = message.indexOf(' '); + QString fullWordUpToSpaceOrEnd; + if(firstSpace == -1) + { + fullWordUpToSpaceOrEnd = message; + message.clear(); + } else { + fullWordUpToSpaceOrEnd = message.mid(0, firstSpace); + message = message.mid(firstSpace); + } + + // check urls + if (fullWordUpToSpaceOrEnd.startsWith("http://", Qt::CaseInsensitive) || + fullWordUpToSpaceOrEnd.startsWith("https://", Qt::CaseInsensitive) || + fullWordUpToSpaceOrEnd.startsWith("www.", Qt::CaseInsensitive)) + { + QUrl qUrl(fullWordUpToSpaceOrEnd); + if (qUrl.isValid()) + { + appendUrlTag(cursor, fullWordUpToSpaceOrEnd); + return; + } + } + + // check word mentions + const QStringList highlightedWords = settingsCache->getHighlightWords(); + foreach (QString word, highlightedWords) + { + if (fullWordUpToSpaceOrEnd.compare(word, Qt::CaseInsensitive) == 0) + { + // You have received a valid mention of custom word!! + mentionFormat.setBackground(QBrush(getCustomMentionColor())); + mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); + cursor.insertText(fullWordUpToSpaceOrEnd, mentionFormat); + cursor.setCharFormat(defaultFormat); + QApplication::alert(this); + return; + } + } + + // not a special word; just print it + cursor.insertText(fullWordUpToSpaceOrEnd, defaultFormat); +} + bool ChatView::isModeratorSendingGlobal(QFlags userLevelFlag, QString message) { int userLevel = QString::number(userLevelFlag).toInt(); diff --git a/cockatrice/src/chatview.h b/cockatrice/src/chatview.h index 17d73d72..144097f0 100644 --- a/cockatrice/src/chatview.h +++ b/cockatrice/src/chatview.h @@ -44,6 +44,9 @@ private: bool shouldShowSystemPopup(); void showSystemPopup(QString &sender); bool isModeratorSendingGlobal(QFlags userLevelFlag, QString message); + void checkTag(QTextCursor &cursor, QString &message); + void checkMention(QTextCursor &cursor, QString &message, QString &sender, UserLevelFlags userLevel); + void checkWord(QTextCursor &cursor, QString &message); private slots: void openLink(const QUrl &link); void actMessageClicked(); From 2e3e6c55ffce8c97fe48e630f61298fcb14dadaa Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Tue, 14 Jul 2015 09:19:07 +0200 Subject: [PATCH 4/6] Updated with latest changes from #1243 --- cockatrice/src/chatview.cpp | 61 +++++++++++++++++++++++--------- cockatrice/src/chatview.h | 4 +++ cockatrice/src/dlg_settings.cpp | 47 +++++++++++++++++++----- cockatrice/src/dlg_settings.h | 6 ++++ cockatrice/src/settingscache.cpp | 17 +++++++-- cockatrice/src/settingscache.h | 10 ++++-- 6 files changed, 116 insertions(+), 29 deletions(-) diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview.cpp index 87548832..75abadd7 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview.cpp @@ -166,6 +166,8 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use cursor.setCharFormat(messageFormat); bool mentionEnabled = settingsCache->getChatMention(); + highlightedWords = settingsCache->getHighlightWords().split(' ', QString::SkipEmptyParts); + // parse the message while (message.size()) { @@ -318,16 +320,8 @@ void ChatView::checkMention(QTextCursor &cursor, QString &message, QString &send void ChatView::checkWord(QTextCursor &cursor, QString &message) { // extract the first word - int firstSpace = message.indexOf(' '); - QString fullWordUpToSpaceOrEnd; - if(firstSpace == -1) - { - fullWordUpToSpaceOrEnd = message; - message.clear(); - } else { - fullWordUpToSpaceOrEnd = message.mid(0, firstSpace); - message = message.mid(firstSpace); - } + QString rest; + QString fullWordUpToSpaceOrEnd = extractNextWord(message, rest); // check urls if (fullWordUpToSpaceOrEnd.startsWith("http://", Qt::CaseInsensitive) || @@ -338,30 +332,60 @@ void ChatView::checkWord(QTextCursor &cursor, QString &message) if (qUrl.isValid()) { appendUrlTag(cursor, fullWordUpToSpaceOrEnd); + cursor.insertText(rest, defaultFormat); return; } } // check word mentions - const QStringList highlightedWords = settingsCache->getHighlightWords(); foreach (QString word, highlightedWords) { if (fullWordUpToSpaceOrEnd.compare(word, Qt::CaseInsensitive) == 0) { // You have received a valid mention of custom word!! - mentionFormat.setBackground(QBrush(getCustomMentionColor())); - mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); - cursor.insertText(fullWordUpToSpaceOrEnd, mentionFormat); + highlightFormat.setBackground(QBrush(getCustomHighlightColor())); + highlightFormat.setForeground(settingsCache->getChatHighlightForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); + cursor.insertText(fullWordUpToSpaceOrEnd, highlightFormat); cursor.setCharFormat(defaultFormat); + cursor.insertText(rest, defaultFormat); QApplication::alert(this); return; } } // not a special word; just print it - cursor.insertText(fullWordUpToSpaceOrEnd, defaultFormat); + cursor.insertText(fullWordUpToSpaceOrEnd + rest, defaultFormat); } +QString ChatView::extractNextWord(QString &message, QString &rest) +{ + // get the first next space and extract the word + QString word; + int firstSpace = message.indexOf(' '); + if(firstSpace == -1) + { + word = message; + message.clear(); + } else { + word = message.mid(0, firstSpace); + message = message.mid(firstSpace); + } + + // remove any punctution from the end and pass it separately + for (int len = word.size() - 1; len >= 0; --len) + { + if(word.at(len).isLetterOrNumber()) + { + rest = word.mid(len + 1); + return word.mid(0, len + 1); + } + } + + rest = word; + return QString(); +} + + bool ChatView::isModeratorSendingGlobal(QFlags userLevelFlag, QString message) { int userLevel = QString::number(userLevelFlag).toInt(); @@ -387,13 +411,18 @@ void ChatView::showSystemPopup(QString &sender) { emit showMentionPopup(sender); } - QColor ChatView::getCustomMentionColor() { QColor customColor; customColor.setNamedColor("#" + settingsCache->getChatMentionColor()); return customColor.isValid() ? customColor : DEFAULT_MENTION_COLOR; } +QColor ChatView::getCustomHighlightColor() { + QColor customColor; + customColor.setNamedColor("#" + settingsCache->getChatHighlightColor()); + return customColor.isValid() ? customColor : DEFAULT_MENTION_COLOR; +} + /** Returns the correct case version of the provided username, if no correct casing version was found then the provided name is not available and will return an empty QString. diff --git a/cockatrice/src/chatview.h b/cockatrice/src/chatview.h index 144097f0..9371cfe0 100644 --- a/cockatrice/src/chatview.h +++ b/cockatrice/src/chatview.h @@ -27,8 +27,10 @@ private: QString userName; QString mention; QTextCharFormat mentionFormat; + QTextCharFormat highlightFormat; QTextCharFormat mentionFormatOtherUser; QTextCharFormat defaultFormat; + QStringList highlightedWords; bool evenNumber; bool showTimestamps; HoveredItemType hoveredItemType; @@ -41,12 +43,14 @@ private: QString getNameFromUserList(QMap &userList, QString &userName); bool isFullMentionAValidUser(QMap &userList, QString userNameToMatch); QColor getCustomMentionColor(); + QColor getCustomHighlightColor(); bool shouldShowSystemPopup(); void showSystemPopup(QString &sender); bool isModeratorSendingGlobal(QFlags userLevelFlag, QString message); void checkTag(QTextCursor &cursor, QString &message); void checkMention(QTextCursor &cursor, QString &message, QString &sender, UserLevelFlags userLevel); void checkWord(QTextCursor &cursor, QString &message); + QString extractNextWord(QString &message, QString &rest); private slots: void openLink(const QUrl &link); void actMessageClicked(); diff --git a/cockatrice/src/dlg_settings.cpp b/cockatrice/src/dlg_settings.cpp index 1a86be51..5d10d6aa 100644 --- a/cockatrice/src/dlg_settings.cpp +++ b/cockatrice/src/dlg_settings.cpp @@ -580,6 +580,9 @@ MessagesSettingsPage::MessagesSettingsPage() invertMentionForeground.setChecked(settingsCache->getChatMentionForeground()); connect(&invertMentionForeground, SIGNAL(stateChanged(int)), this, SLOT(updateTextColor(int))); + invertHighlightForeground.setChecked(settingsCache->getChatHighlightForeground()); + connect(&invertHighlightForeground, SIGNAL(stateChanged(int)), this, SLOT(updateTextHighlightColor(int))); + mentionColor = new QLineEdit(); mentionColor->setText(settingsCache->getChatMentionColor()); updateMentionPreview(); @@ -592,8 +595,8 @@ MessagesSettingsPage::MessagesSettingsPage() connect(&mentionPopups, SIGNAL(stateChanged(int)), settingsCache, SLOT(setShowMentionPopups(int))); customAlertString = new QLineEdit(); - customAlertString->setPlaceholderText("Word1, Word2, Word3"); - customAlertString->setText(settingsCache->getHighlightWords().join(", ")); + customAlertString->setPlaceholderText("Word1 Word2 Word3"); + customAlertString->setText(settingsCache->getHighlightWords()); connect(customAlertString, SIGNAL(textChanged(QString)), settingsCache, SLOT(setHighlightWords(QString))); QGridLayout *chatGrid = new QGridLayout; @@ -608,9 +611,17 @@ MessagesSettingsPage::MessagesSettingsPage() chatGroupBox = new QGroupBox; chatGroupBox->setLayout(chatGrid); + highlightColor = new QLineEdit(); + highlightColor->setText(settingsCache->getChatHighlightColor()); + updateHighlightPreview(); + connect(highlightColor, SIGNAL(textChanged(QString)), this, SLOT(updateHighlightColor(QString))); + QGridLayout *highlightNotice = new QGridLayout; + highlightNotice->addWidget(highlightColor, 0, 2); + highlightNotice->addWidget(&invertHighlightForeground, 0, 1); + highlightNotice->addWidget(&hexHighlightLabel, 1, 2); highlightNotice->addWidget(customAlertString, 0, 0); - highlightNotice->addWidget(&customAlertStringLabel, 1, 0); + highlightNotice->addWidget(&customAlertStringLabel, 1, 0); highlightGroupBox = new QGroupBox; highlightGroupBox->setLayout(highlightNotice); @@ -660,16 +671,35 @@ void MessagesSettingsPage::updateColor(const QString &value) { } } +void MessagesSettingsPage::updateHighlightColor(const QString &value) { + QColor colorToSet; + colorToSet.setNamedColor("#" + value); + if (colorToSet.isValid()) { + settingsCache->setChatHighlightColor(value); + updateHighlightPreview(); + } +} + void MessagesSettingsPage::updateTextColor(int value) { settingsCache->setChatMentionForeground(value); updateMentionPreview(); } +void MessagesSettingsPage::updateTextHighlightColor(int value) { + settingsCache->setChatHighlightForeground(value); + updateHighlightPreview(); +} + void MessagesSettingsPage::updateMentionPreview() { mentionColor->setStyleSheet("QLineEdit{background:#" + settingsCache->getChatMentionColor() + ";color: " + (settingsCache->getChatMentionForeground() ? "white" : "black") + ";}"); } +void MessagesSettingsPage::updateHighlightPreview() { + highlightColor->setStyleSheet("QLineEdit{background:#" + settingsCache->getChatHighlightColor() + + ";color: " + (settingsCache->getChatHighlightForeground() ? "white" : "black") + ";}"); +} + void MessagesSettingsPage::storeSettings() { QSettings settings; @@ -703,14 +733,15 @@ void MessagesSettingsPage::retranslateUi() highlightGroupBox->setTitle(tr("Custom alert words")); chatMentionCheckBox.setText(tr("Enable chat mentions")); messageShortcuts->setTitle(tr("In-game message macros")); - ignoreUnregUsersMainChat.setText(tr("Ignore unregistered users in main chat")); - ignoreUnregUsersMainChat.setText(tr("Ignore chat room messages sent by unregistered users.")); - ignoreUnregUserMessages.setText(tr("Ignore private messages sent by unregistered users.")); + ignoreUnregUsersMainChat.setText(tr("Ignore chat room messages sent by unregistered users")); + ignoreUnregUserMessages.setText(tr("Ignore private messages sent by unregistered users")); invertMentionForeground.setText(tr("Invert text color")); - messagePopups.setText(tr("Enable desktop notifications for private messages.")); + invertHighlightForeground.setText(tr("Invert text color")); + messagePopups.setText(tr("Enable desktop notifications for private messages")); mentionPopups.setText(tr("Enable desktop notification for mentions.")); hexLabel.setText(tr("(Color is hexadecimal)")); - customAlertStringLabel.setText(tr("(Seperate each word with a comma, words are case insensitive)")); + hexHighlightLabel.setText(tr("(Color is hexadecimal)")); + customAlertStringLabel.setText(tr("(Seperate each word with a space, words are case insensitive)")); } diff --git a/cockatrice/src/dlg_settings.h b/cockatrice/src/dlg_settings.h index 71811434..e3c8b158 100644 --- a/cockatrice/src/dlg_settings.h +++ b/cockatrice/src/dlg_settings.h @@ -160,13 +160,16 @@ private slots: void actAdd(); void actRemove(); void updateColor(const QString &value); + void updateHighlightColor(const QString &value); void updateTextColor(int value); + void updateTextHighlightColor(int value); private: QListWidget *messageList; QAction *aAdd; QAction *aRemove; QCheckBox chatMentionCheckBox; QCheckBox invertMentionForeground; + QCheckBox invertHighlightForeground; QCheckBox ignoreUnregUsersMainChat; QCheckBox ignoreUnregUserMessages; QCheckBox messagePopups; @@ -175,12 +178,15 @@ private: QGroupBox *highlightGroupBox; QGroupBox *messageShortcuts; QLineEdit *mentionColor; + QLineEdit *highlightColor; QLineEdit *customAlertString; QLabel hexLabel; + QLabel hexHighlightLabel; QLabel customAlertStringLabel; void storeSettings(); void updateMentionPreview(); + void updateHighlightPreview(); }; class SoundSettingsPage : public AbstractSettingsPage { diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index 01f1b960..50a0a9cc 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -56,7 +56,9 @@ SettingsCache::SettingsCache() tapAnimation = settings->value("cards/tapanimation", true).toBool(); chatMention = settings->value("chat/mention", true).toBool(); chatMentionForeground = settings->value("chat/mentionforeground", true).toBool(); + chatHighlightForeground = settings->value("chat/highlightforeground", true).toBool(); chatMentionColor = settings->value("chat/mentioncolor", "A6120D").toString(); + chatHighlightColor = settings->value("chat/highlightcolor", "A6120D").toString(); zoneViewSortByName = settings->value("zoneview/sortbyname", true).toBool(); zoneViewSortByType = settings->value("zoneview/sortbytype", true).toBool(); @@ -83,7 +85,7 @@ SettingsCache::SettingsCache() cardInfoViewMode = settings->value("cards/cardinfoviewmode", 0).toInt(); - highlightWords = settings->value("personal/highlightWords", QStringList()).toStringList(); + highlightWords = settings->value("personal/highlightWords", QString()).toString(); } void SettingsCache::setCardInfoViewMode(const int _viewMode) { @@ -92,8 +94,7 @@ void SettingsCache::setCardInfoViewMode(const int _viewMode) { } void SettingsCache::setHighlightWords(const QString &_highlightWords) { - // Words are seperated by a comma and you can not use spaces in words - highlightWords = _highlightWords.simplified().replace(" ", "").split(","); + highlightWords = _highlightWords; settings->setValue("personal/highlightWords", highlightWords); } @@ -322,11 +323,21 @@ void SettingsCache::setChatMentionForeground(int _chatMentionForeground) { settings->setValue("chat/mentionforeground", chatMentionForeground); } +void SettingsCache::setChatHighlightForeground(int _chatHighlightForeground) { + chatHighlightForeground = _chatHighlightForeground; + settings->setValue("chat/highlightforeground", chatHighlightForeground); +} + void SettingsCache::setChatMentionColor(const QString &_chatMentionColor) { chatMentionColor = _chatMentionColor; settings->setValue("chat/mentioncolor", chatMentionColor); } +void SettingsCache::setChatHighlightColor(const QString &_chatHighlightColor) { + chatHighlightColor = _chatHighlightColor; + settings->setValue("chat/highlightcolor", chatHighlightColor); +} + void SettingsCache::setZoneViewSortByName(int _zoneViewSortByName) { zoneViewSortByName = _zoneViewSortByName; diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index 12916805..20483f56 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -65,7 +65,9 @@ private: bool tapAnimation; bool chatMention; QString chatMentionColor; + QString chatHighlightColor; bool chatMentionForeground; + bool chatHighlightForeground; bool zoneViewSortByName, zoneViewSortByType, zoneViewPileView; bool soundEnabled; QString soundPath; @@ -85,7 +87,7 @@ private: bool leftJustified; int masterVolume; int cardInfoViewMode; - QStringList highlightWords; + QString highlightWords; public: SettingsCache(); const QByteArray &getMainWindowGeometry() const { return mainWindowGeometry; } @@ -101,6 +103,7 @@ public: QString getPlayerBgPath() const { return playerBgPath; } QString getCardBackPicturePath() const { return cardBackPicturePath; } QString getChatMentionColor() const { return chatMentionColor; } + QString getChatHighlightColor() const { return chatHighlightColor; } bool getPicDownload() const { return picDownload; } bool getPicDownloadHq() const { return picDownloadHq; } bool getNotificationsEnabled() const { return notificationsEnabled; } @@ -118,6 +121,7 @@ public: bool getTapAnimation() const { return tapAnimation; } bool getChatMention() const { return chatMention; } bool getChatMentionForeground() const { return chatMentionForeground; } + bool getChatHighlightForeground() const { return chatHighlightForeground; } bool getZoneViewSortByName() const { return zoneViewSortByName; } bool getZoneViewSortByType() const { return zoneViewSortByType; } /** @@ -144,7 +148,7 @@ public: int getMasterVolume() const { return masterVolume; } int getCardInfoViewMode() const { return cardInfoViewMode; } QStringList getCountries() const; - QStringList getHighlightWords() const { return highlightWords; } + QString getHighlightWords() const { return highlightWords; } public slots: void setMainWindowGeometry(const QByteArray &_mainWindowGeometry); void setLang(const QString &_lang); @@ -159,6 +163,7 @@ public slots: void setPlayerBgPath(const QString &_playerBgPath); void setCardBackPicturePath(const QString &_cardBackPicturePath); void setChatMentionColor(const QString &_chatMentionColor); + void setChatHighlightColor(const QString &_chatHighlightColor); void setPicDownload(int _picDownload); void setPicDownloadHq(int _picDownloadHq); void setNotificationsEnabled(int _notificationsEnabled); @@ -175,6 +180,7 @@ public slots: void setTapAnimation(int _tapAnimation); void setChatMention(int _chatMention); void setChatMentionForeground(int _chatMentionForeground); + void setChatHighlightForeground(int _chatHighlightForeground); void setZoneViewSortByName(int _zoneViewSortByName); void setZoneViewSortByType(int _zoneViewSortByType); void setZoneViewPileView(int _zoneViewPileView); From efe388bdddd64530cba3242ac83be1bd30cd11b0 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Wed, 15 Jul 2015 11:52:04 +0200 Subject: [PATCH 5/6] Fix server message color; try to detect words inside parentheses, punctuation, etc.. --- cockatrice/src/chatview.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview.cpp index 75abadd7..a31ba1d7 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview.cpp @@ -158,12 +158,13 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use cursor.insertText(sender); } - QTextCharFormat messageFormat; + // use different color for server messages + defaultFormat = QTextCharFormat(); if (sender.isEmpty()) { - messageFormat.setForeground(Qt::darkGreen); - messageFormat.setFontWeight(QFont::Bold); + defaultFormat.setForeground(Qt::darkGreen); + defaultFormat.setFontWeight(QFont::Bold); } - cursor.setCharFormat(messageFormat); + cursor.setCharFormat(defaultFormat); bool mentionEnabled = settingsCache->getChatMention(); highlightedWords = settingsCache->getHighlightWords().split(' ', QString::SkipEmptyParts); @@ -178,17 +179,24 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use checkTag(cursor, message); break; case '@': - if(mentionEnabled) + if(mentionEnabled) { checkMention(cursor, message, sender, userLevel); - else - checkWord(cursor, message); + } else { + cursor.insertText(c, defaultFormat); + message = message.mid(1); + } break; case ' ': - cursor.insertText(" ", defaultFormat); + cursor.insertText(c, defaultFormat); message = message.mid(1); break; default: - checkWord(cursor, message); + if(c.isLetterOrNumber()) { + checkWord(cursor, message); + } else { + cursor.insertText(c, defaultFormat); + message = message.mid(1); + } break; } } @@ -346,7 +354,6 @@ void ChatView::checkWord(QTextCursor &cursor, QString &message) highlightFormat.setBackground(QBrush(getCustomHighlightColor())); highlightFormat.setForeground(settingsCache->getChatHighlightForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); cursor.insertText(fullWordUpToSpaceOrEnd, highlightFormat); - cursor.setCharFormat(defaultFormat); cursor.insertText(rest, defaultFormat); QApplication::alert(this); return; From ef831d2749508cf062d46d358b11e93f6d0ab222 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Wed, 15 Jul 2015 22:44:24 +0200 Subject: [PATCH 6/6] Reworded settings sentence --- cockatrice/src/dlg_settings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cockatrice/src/dlg_settings.cpp b/cockatrice/src/dlg_settings.cpp index 5d10d6aa..c20ca78d 100644 --- a/cockatrice/src/dlg_settings.cpp +++ b/cockatrice/src/dlg_settings.cpp @@ -741,7 +741,7 @@ void MessagesSettingsPage::retranslateUi() mentionPopups.setText(tr("Enable desktop notification for mentions.")); hexLabel.setText(tr("(Color is hexadecimal)")); hexHighlightLabel.setText(tr("(Color is hexadecimal)")); - customAlertStringLabel.setText(tr("(Seperate each word with a space, words are case insensitive)")); + customAlertStringLabel.setText(tr("Separate words with a space, alphanumeric characters only")); }