factored out duplicated code in [card] tag parsing

This commit is contained in:
Max-Wilhelm Bruker 2013-01-19 17:29:15 +01:00
parent a4ae1bcd39
commit f10543924d
2 changed files with 36 additions and 30 deletions

View file

@ -54,6 +54,35 @@ void ChatView::appendHtml(const QString &html)
verticalScrollBar()->setValue(verticalScrollBar()->maximum()); verticalScrollBar()->setValue(verticalScrollBar()->maximum());
} }
void ChatView::appendCardTag(QTextCursor &cursor, const QString &cardName)
{
QTextCharFormat oldFormat = cursor.charFormat();
QTextCharFormat anchorFormat = oldFormat;
anchorFormat.setForeground(Qt::blue);
anchorFormat.setAnchor(true);
anchorFormat.setAnchorHref("card://" + cardName);
cursor.setCharFormat(anchorFormat);
cursor.insertText(cardName);
cursor.setCharFormat(oldFormat);
}
void ChatView::appendUrlTag(QTextCursor &cursor, QString url)
{
if (!url.contains("://"))
url.prepend("http://");
QTextCharFormat oldFormat = cursor.charFormat();
QTextCharFormat anchorFormat = oldFormat;
anchorFormat.setForeground(Qt::blue);
anchorFormat.setAnchor(true);
anchorFormat.setAnchorHref(url);
cursor.setCharFormat(anchorFormat);
cursor.insertText(url);
cursor.setCharFormat(oldFormat);
}
void ChatView::appendMessage(QString message, QString sender, UserLevelFlags userLevel, bool playerBold) void ChatView::appendMessage(QString message, QString sender, UserLevelFlags userLevel, bool playerBold)
{ {
bool atBottom = verticalScrollBar()->value() >= verticalScrollBar()->maximum(); bool atBottom = verticalScrollBar()->value() >= verticalScrollBar()->maximum();
@ -112,15 +141,8 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
message.clear(); message.clear();
else else
message = message.mid(closeTagIndex + 7); message = message.mid(closeTagIndex + 7);
QTextCharFormat tempFormat = messageFormat; appendCardTag(cursor, cardName);
tempFormat.setForeground(Qt::blue);
tempFormat.setAnchor(true);
tempFormat.setAnchorHref("card://" + cardName);
cursor.setCharFormat(tempFormat);
cursor.insertText(cardName);
cursor.setCharFormat(messageFormat);
} else if (message.startsWith("[[")) { } else if (message.startsWith("[[")) {
message = message.mid(2); message = message.mid(2);
int closeTagIndex = message.indexOf("]]"); int closeTagIndex = message.indexOf("]]");
@ -129,16 +151,8 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
message.clear(); message.clear();
else else
message = message.mid(closeTagIndex + 2); message = message.mid(closeTagIndex + 2);
// TODO: Factor out this duplicated code (vs [card] parsing) appendCardTag(cursor, cardName);
QTextCharFormat tempFormat = messageFormat;
tempFormat.setForeground(Qt::blue);
tempFormat.setAnchor(true);
tempFormat.setAnchorHref("card://" + cardName);
cursor.setCharFormat(tempFormat);
cursor.insertText(cardName);
cursor.setCharFormat(messageFormat);
} else if (message.startsWith("[url]")) { } else if (message.startsWith("[url]")) {
message = message.mid(5); message = message.mid(5);
int closeTagIndex = message.indexOf("[/url]"); int closeTagIndex = message.indexOf("[/url]");
@ -147,18 +161,8 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use
message.clear(); message.clear();
else else
message = message.mid(closeTagIndex + 6); message = message.mid(closeTagIndex + 6);
if (!url.contains("://"))
url.prepend("http://");
QTextCharFormat tempFormat = messageFormat; appendUrlTag(cursor, url);
tempFormat.setForeground(Qt::blue);
tempFormat.setAnchor(true);
tempFormat.setAnchorHref(url);
cursor.setCharFormat(tempFormat);
cursor.insertText(url);
cursor.setCharFormat(messageFormat);
} else } else
from = 1; from = 1;
} }

View file

@ -28,6 +28,8 @@ private:
QString hoveredContent; QString hoveredContent;
QTextFragment getFragmentUnderMouse(const QPoint &pos) const; QTextFragment getFragmentUnderMouse(const QPoint &pos) const;
QTextCursor prepareBlock(bool same = false); QTextCursor prepareBlock(bool same = false);
void appendCardTag(QTextCursor &cursor, const QString &cardName);
void appendUrlTag(QTextCursor &cursor, QString url);
private slots: private slots:
void openLink(const QUrl &link); void openLink(const QUrl &link);
public: public: