Merge pull request #870 from ctrlaltca/mentions

Reworked a bit mentions/tags support in chat view; fix #791
This commit is contained in:
Zach 2015-04-01 09:34:42 -04:00
commit 580927a50b

View file

@ -28,7 +28,7 @@ ChatView::ChatView(const TabSupervisor *_tabSupervisor, TabGame *_game, bool _sh
if(tabSupervisor->getUserInfo()) if(tabSupervisor->getUserInfo())
{ {
userName = QString::fromStdString(tabSupervisor->getUserInfo()->name()); userName = QString::fromStdString(tabSupervisor->getUserInfo()->name());
mention = "@" + userName.toLower(); mention = "@" + userName;
} }
mentionFormat.setFontWeight(QFont::Bold); mentionFormat.setFontWeight(QFont::Bold);
@ -163,29 +163,44 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
} }
cursor.setCharFormat(messageFormat); cursor.setCharFormat(messageFormat);
int from = 0, index = 0, bracket = 0, at = 0; int index = -1, bracket = -1, at = -1;
bool mentionEnabled = settingsCache->getChatMention(); bool mentionEnabled = settingsCache->getChatMention();
while (((message.indexOf('[', from)) != -1) || (mentionEnabled && (message.indexOf('@', from)) != -1)) { while (message.size())
bracket = message.indexOf('[', from); {
if (!mentionEnabled) { // search for the first [ or @
bracket = message.indexOf('[');
at = mentionEnabled ? message.indexOf('@') : -1;
if(bracket == -1)
{
if(at == -1)
{
// quick way out
cursor.insertText(message);
break;
} else {
// mention
index = at;
}
} else {
if(at == -1)
{
// bracket
index = bracket; index = bracket;
} else { } else {
at = message.indexOf('@', from); // both, pick up the first one
if (bracket == -1)
index = at;
else if (at == -1)
index = bracket;
else
index = std::min(bracket, at); index = std::min(bracket, at);
} }
}
// insert the message text up to the [ / @
if(index > 0)
{
cursor.insertText(message.left(index), defaultFormat); cursor.insertText(message.left(index), defaultFormat);
message = message.mid(index); message = message.mid(index);
if (message.isEmpty()) }
break;
if(index == bracket)
{
if (message.startsWith("[card]")) { if (message.startsWith("[card]")) {
message = message.mid(6); message = message.mid(6);
int closeTagIndex = message.indexOf("[/card]"); int closeTagIndex = message.indexOf("[/card]");
@ -216,12 +231,17 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
message = message.mid(closeTagIndex + 6); message = message.mid(closeTagIndex + 6);
appendUrlTag(cursor, url); appendUrlTag(cursor, url);
} else if (mentionEnabled) { } else {
if (message.toLower().startsWith(mention)) { // not a recognized [tag]
cursor.insertText("[", defaultFormat);
message = message.mid(1);
}
} else {
if (message.startsWith(mention, Qt::CaseInsensitive)) {
// you have been mentioned // you have been mentioned
mentionFormat.setBackground(QBrush(getCustomMentionColor())); mentionFormat.setBackground(QBrush(getCustomMentionColor()));
mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white):QBrush(Qt::black)); mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white):QBrush(Qt::black));
cursor.insertText("@" + userName, mentionFormat); cursor.insertText(mention, mentionFormat);
message = message.mid(mention.size()); message = message.mid(mention.size());
QApplication::alert(this); QApplication::alert(this);
if (shouldShowSystemPopup()) { if (shouldShowSystemPopup()) {
@ -245,12 +265,8 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
message = message.mid(userName.size() + 1); message = message.mid(userName.size() + 1);
} }
cursor.setCharFormat(defaultFormat); // reset format after each iteration cursor.setCharFormat(defaultFormat); // reset format after each iteration
} else
from = 1;
} }
}
if (!message.isEmpty())
cursor.insertText(message);
if (atBottom) if (atBottom)
verticalScrollBar()->setValue(verticalScrollBar()->maximum()); verticalScrollBar()->setValue(verticalScrollBar()->maximum());