Fix #632. Url and mentions work together

This commit is contained in:
Gavin Bises 2015-02-21 07:20:25 -05:00
parent 06e5327595
commit 5ff6693c9c

View file

@ -148,9 +148,25 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
messageFormat.setForeground(Qt::darkGreen); messageFormat.setForeground(Qt::darkGreen);
cursor.setCharFormat(messageFormat); cursor.setCharFormat(messageFormat);
int from = 0, index = 0; int from = 0, index = 0, bracket = 0, at = 0;
while ((index = message.indexOf('[', from)) != -1) { bool mentionEnabled = settingsCache->getChatMention();
cursor.insertText(message.left(index)); while (((message.indexOf('[', from)) != -1) || (mentionEnabled && (message.indexOf('@', from)) != -1)) {
bracket = message.indexOf('[', from);
if (!mentionEnabled) {
index = bracket;
} else {
at = message.indexOf('@', from);
if (bracket == -1)
index = at;
else if (at == -1)
index = bracket;
else
index = std::min(bracket, at);
}
cursor.insertText(message.left(index), defaultFormat);
message = message.mid(index); message = message.mid(index);
if (message.isEmpty()) if (message.isEmpty())
break; break;
@ -185,27 +201,15 @@ 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 } else if (mentionEnabled) {
from = 1;
}
if (settingsCache->getChatMention()) {
index = 0;
while((index = message.indexOf('@')) != -1) {
cursor.insertText(message.left(index), defaultFormat);
message = message.mid(index);
if (message.isEmpty())
break;
// you have been mentioned
if (message.toLower().startsWith(mention)) { if (message.toLower().startsWith(mention)) {
// 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("@" + userName, mentionFormat);
message = message.mid(mention.size()); message = message.mid(mention.size());
QApplication::alert(this); QApplication::alert(this);
} } else {
// another user has been mentioned
else {
int mentionEndIndex = message.indexOf(QRegExp("\\W"), 1);// from 1 as @ is non-char int mentionEndIndex = message.indexOf(QRegExp("\\W"), 1);// from 1 as @ is non-char
if (mentionEndIndex == -1) if (mentionEndIndex == -1)
mentionEndIndex = message.size(); // there is no text after the mention mentionEndIndex = message.size(); // there is no text after the mention
@ -221,8 +225,9 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
cursor.insertText("@" + userName, defaultFormat); cursor.insertText("@" + userName, defaultFormat);
message = message.mid(userName.size() + 1); message = message.mid(userName.size() + 1);
} }
cursor.setCharFormat(defaultFormat); // reset format after each itteration cursor.setCharFormat(defaultFormat); // reset format after each iteration
} } else
from = 1;
} }
if (!message.isEmpty()) if (!message.isEmpty())