mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-01 02:53:56 -07:00
Highlight Custom Words
This commit is contained in:
parent
9edacd7b3e
commit
dc05a14f4c
5 changed files with 141 additions and 52 deletions
|
|
@ -161,72 +161,115 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
|
||||||
}
|
}
|
||||||
cursor.setCharFormat(messageFormat);
|
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();
|
bool mentionEnabled = settingsCache->getChatMention();
|
||||||
const QRegExp urlStarter = QRegExp("https?://|\\bwww\\.");
|
const QRegExp urlStarter = QRegExp("https?://|\\bwww\\.");
|
||||||
const QRegExp phraseEnder = QRegExp("\\s");
|
const QRegExp phraseEnder = QRegExp("\\s");
|
||||||
const QRegExp notALetterOrNumber = QRegExp("[^a-zA-Z0-9]");
|
const QRegExp notALetterOrNumber = QRegExp("[^a-zA-Z0-9]");
|
||||||
|
const QStringList highlightedWords = settingsCache->getHighlightWords();
|
||||||
|
|
||||||
while (message.size())
|
while (message.size())
|
||||||
{
|
{
|
||||||
// search for the first [ or @
|
|
||||||
bracketFirstIndex = message.indexOf('[');
|
bracketFirstIndex = message.indexOf('[');
|
||||||
mentionFirstIndex = message.indexOf('@');
|
mentionFirstIndex = message.indexOf('@');
|
||||||
urlFirstIndex = message.indexOf(urlStarter);
|
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 startsWithBracket = (bracketFirstIndex != -1);
|
||||||
bool startsWithAtSymbol = (mentionFirstIndex != -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)
|
// No functions need to be run. Send message as normal
|
||||||
{
|
cursor.insertText(message);
|
||||||
if (!startsWithUrl)
|
break;
|
||||||
{
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord)
|
||||||
{
|
{
|
||||||
if (!startsWithAtSymbol)
|
// Contains a bracket
|
||||||
{
|
index = bracketFirstIndex;
|
||||||
// There's a [, look down!
|
}
|
||||||
index = bracketFirstIndex;
|
else if (!startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord)
|
||||||
}
|
{
|
||||||
else
|
// Contains an @ symbol
|
||||||
{
|
index = mentionFirstIndex;
|
||||||
// There's both a [ and @, pick the first one... look down!
|
}
|
||||||
index = std::min(bracketFirstIndex, mentionFirstIndex);
|
else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord)
|
||||||
}
|
{
|
||||||
|
// Contains URL stuff (http or www.)
|
||||||
if (startsWithUrl)
|
index = urlFirstIndex;
|
||||||
{
|
}
|
||||||
// If there's a URL, pick the first one... then lets begin!
|
else if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord)
|
||||||
// Otherwise, just "lets begin!"
|
{
|
||||||
index = std::min(index, urlFirstIndex);
|
// 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)
|
if (index > 0)
|
||||||
|
|
@ -374,9 +417,27 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
|
||||||
while (fullMentionUpToSpaceOrEnd.size());
|
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
|
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());
|
mentionPopups.setChecked(settingsCache->getShowMentionPopup());
|
||||||
connect(&mentionPopups, SIGNAL(stateChanged(int)), settingsCache, SLOT(setShowMentionPopups(int)));
|
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;
|
QGridLayout *chatGrid = new QGridLayout;
|
||||||
chatGrid->addWidget(&chatMentionCheckBox, 0, 0);
|
chatGrid->addWidget(&chatMentionCheckBox, 0, 0);
|
||||||
chatGrid->addWidget(&invertMentionForeground, 0, 1);
|
chatGrid->addWidget(&invertMentionForeground, 0, 1);
|
||||||
|
|
@ -602,6 +607,12 @@ MessagesSettingsPage::MessagesSettingsPage()
|
||||||
chatGrid->addWidget(&mentionPopups, 4, 0);
|
chatGrid->addWidget(&mentionPopups, 4, 0);
|
||||||
chatGroupBox = new QGroupBox;
|
chatGroupBox = new QGroupBox;
|
||||||
chatGroupBox->setLayout(chatGrid);
|
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;
|
QSettings settings;
|
||||||
messageList = new QListWidget;
|
messageList = new QListWidget;
|
||||||
|
|
@ -628,11 +639,12 @@ MessagesSettingsPage::MessagesSettingsPage()
|
||||||
|
|
||||||
messageShortcuts = new QGroupBox;
|
messageShortcuts = new QGroupBox;
|
||||||
messageShortcuts->setLayout(messageListLayout);
|
messageShortcuts->setLayout(messageListLayout);
|
||||||
|
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||||
|
|
||||||
mainLayout->addWidget(messageShortcuts);
|
mainLayout->addWidget(messageShortcuts);
|
||||||
mainLayout->addWidget(chatGroupBox);
|
mainLayout->addWidget(chatGroupBox);
|
||||||
|
mainLayout->addWidget(highlightGroupBox);
|
||||||
|
|
||||||
setLayout(mainLayout);
|
setLayout(mainLayout);
|
||||||
|
|
||||||
|
|
@ -688,6 +700,7 @@ void MessagesSettingsPage::actRemove()
|
||||||
void MessagesSettingsPage::retranslateUi()
|
void MessagesSettingsPage::retranslateUi()
|
||||||
{
|
{
|
||||||
chatGroupBox->setTitle(tr("Chat settings"));
|
chatGroupBox->setTitle(tr("Chat settings"));
|
||||||
|
highlightGroupBox->setTitle(tr("Custom alert words"));
|
||||||
chatMentionCheckBox.setText(tr("Enable chat mentions"));
|
chatMentionCheckBox.setText(tr("Enable chat mentions"));
|
||||||
messageShortcuts->setTitle(tr("In-game message macros"));
|
messageShortcuts->setTitle(tr("In-game message macros"));
|
||||||
ignoreUnregUsersMainChat.setText(tr("Ignore unregistered users in main chat"));
|
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."));
|
messagePopups.setText(tr("Enable desktop notifications for private messages."));
|
||||||
mentionPopups.setText(tr("Enable desktop notification for mentions."));
|
mentionPopups.setText(tr("Enable desktop notification for mentions."));
|
||||||
hexLabel.setText(tr("(Color is hexadecimal)"));
|
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 messagePopups;
|
||||||
QCheckBox mentionPopups;
|
QCheckBox mentionPopups;
|
||||||
QGroupBox *chatGroupBox;
|
QGroupBox *chatGroupBox;
|
||||||
|
QGroupBox *highlightGroupBox;
|
||||||
QGroupBox *messageShortcuts;
|
QGroupBox *messageShortcuts;
|
||||||
QLineEdit *mentionColor;
|
QLineEdit *mentionColor;
|
||||||
|
QLineEdit *customAlertString;
|
||||||
QLabel hexLabel;
|
QLabel hexLabel;
|
||||||
|
QLabel customAlertStringLabel;
|
||||||
|
|
||||||
void storeSettings();
|
void storeSettings();
|
||||||
void updateMentionPreview();
|
void updateMentionPreview();
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,8 @@ SettingsCache::SettingsCache()
|
||||||
masterVolume = settings->value("sound/mastervolume", 100).toInt();
|
masterVolume = settings->value("sound/mastervolume", 100).toInt();
|
||||||
|
|
||||||
cardInfoViewMode = settings->value("cards/cardinfoviewmode", 0).toInt();
|
cardInfoViewMode = settings->value("cards/cardinfoviewmode", 0).toInt();
|
||||||
|
|
||||||
|
highlightWords = settings->value("personal/highlightWords", QStringList()).toStringList();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsCache::setCardInfoViewMode(const int _viewMode) {
|
void SettingsCache::setCardInfoViewMode(const int _viewMode) {
|
||||||
|
|
@ -89,6 +91,12 @@ void SettingsCache::setCardInfoViewMode(const int _viewMode) {
|
||||||
settings->setValue("cards/cardinfoviewmode", cardInfoViewMode);
|
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) {
|
void SettingsCache::setMasterVolume(int _masterVolume) {
|
||||||
masterVolume = _masterVolume;
|
masterVolume = _masterVolume;
|
||||||
settings->setValue("sound/mastervolume", masterVolume);
|
settings->setValue("sound/mastervolume", masterVolume);
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,7 @@ private:
|
||||||
bool leftJustified;
|
bool leftJustified;
|
||||||
int masterVolume;
|
int masterVolume;
|
||||||
int cardInfoViewMode;
|
int cardInfoViewMode;
|
||||||
|
QStringList highlightWords;
|
||||||
public:
|
public:
|
||||||
SettingsCache();
|
SettingsCache();
|
||||||
const QByteArray &getMainWindowGeometry() const { return mainWindowGeometry; }
|
const QByteArray &getMainWindowGeometry() const { return mainWindowGeometry; }
|
||||||
|
|
@ -143,6 +144,7 @@ public:
|
||||||
int getMasterVolume() const { return masterVolume; }
|
int getMasterVolume() const { return masterVolume; }
|
||||||
int getCardInfoViewMode() const { return cardInfoViewMode; }
|
int getCardInfoViewMode() const { return cardInfoViewMode; }
|
||||||
QStringList getCountries() const;
|
QStringList getCountries() const;
|
||||||
|
QStringList getHighlightWords() const { return highlightWords; }
|
||||||
public slots:
|
public slots:
|
||||||
void setMainWindowGeometry(const QByteArray &_mainWindowGeometry);
|
void setMainWindowGeometry(const QByteArray &_mainWindowGeometry);
|
||||||
void setLang(const QString &_lang);
|
void setLang(const QString &_lang);
|
||||||
|
|
@ -194,6 +196,7 @@ public slots:
|
||||||
void setLeftJustified( const int _leftJustified);
|
void setLeftJustified( const int _leftJustified);
|
||||||
void setMasterVolume(const int _masterVolume);
|
void setMasterVolume(const int _masterVolume);
|
||||||
void setCardInfoViewMode(const int _viewMode);
|
void setCardInfoViewMode(const int _viewMode);
|
||||||
|
void setHighlightWords(const QString _highlightWords);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern SettingsCache *settingsCache;
|
extern SettingsCache *settingsCache;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue