From 2e3e6c55ffce8c97fe48e630f61298fcb14dadaa Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Tue, 14 Jul 2015 09:19:07 +0200 Subject: [PATCH] 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);