Commit dc05a14f authored by Zach H's avatar Zach H
Browse files

Highlight Custom Words

parent 9edacd7b
......@@ -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 startsWithHighlightWord = (highlightWordFirstIndex != -1);
if (!startsWithBracket)
{
if (!startsWithAtSymbol)
{
if (!startsWithUrl)
if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord)
{
// No brackets, mentions, or urls. Send message as normal
// No functions need to be run. Send message as normal
cursor.insertText(message);
break;
}
else
else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord)
{
// There's a URL, lets begin!
// 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
else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord)
{
if (!startsWithUrl)
// Contains both a bracket and an @ symbol
index = std::min(bracketFirstIndex, mentionFirstIndex);
}
else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord)
{
// There's an @ symbol, lets begin!
index = mentionFirstIndex;
// Contains both a bracket and URL stuff
index = std::min(bracketFirstIndex, urlFirstIndex);
}
else
else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord)
{
// There's both an @ symbol and URL, pick the first one... lets begin!
index = std::min(urlFirstIndex, mentionFirstIndex);
// 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
else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord)
{
if (!startsWithAtSymbol)
// Contains both URL stuff and a word the user wants highlighted
index = std::min(urlFirstIndex, highlightWordFirstIndex);
}
else if (!startsWithBracket && startsWithAtSymbol && startsWithUrl && startsWithHighlightWord)
{
// There's a [, look down!
index = bracketFirstIndex;
// Contains an @ symbol, URL stuff, and a word the user wants highlighted
index = std::min(mentionFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex));
}
else
else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord)
{
// There's both a [ and @, pick the first one... look down!
index = std::min(bracketFirstIndex, mentionFirstIndex);
// Contains a bracket, URL stuff, and a word the user wants highlighted
index = std::min(bracketFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex));
}
if (startsWithUrl)
else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord)
{
// If there's a URL, pick the first one... then lets begin!
// Otherwise, just "lets begin!"
index = std::min(index, urlFirstIndex);
// 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);
}
}
......
......@@ -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);
......@@ -603,6 +608,12 @@ MessagesSettingsPage::MessagesSettingsPage()
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;
settings.beginGroup("messages");
......@@ -633,6 +644,7 @@ MessagesSettingsPage::MessagesSettingsPage()
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)"));
}
......
......@@ -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();
......
......@@ -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);
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment