-
Notifications
You must be signed in to change notification settings - Fork 2
/
TweetTextHighlighter.cpp
89 lines (74 loc) · 2.31 KB
/
TweetTextHighlighter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
(c) 2009,2010,2011 Ian Clark
This file is part of Yasst.
Yasst is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Yasst is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Yasst. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* TweetTextHighlighter.cpp
*
* Created on: 22-Feb-2009
* Author: ian
*/
#include "TweetTextHighlighter.h"
#include <QTextCharFormat>
#include <QUrl>
TweetTextHighlighter::TweetTextHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) {
dmcount=0;
}
TweetTextHighlighter::~TweetTextHighlighter() {
}
void TweetTextHighlighter::highlightBlock(const QString &text) {
int size = previousBlockState();
int prev = size;
TweetTextHighlighter::urlData *url = 0;
QRegExp expression("https*://[^ ]+[^ .]");
if (size<0) {
size=text.size();
prev=0;
} else
size+=text.size();
if (size>140+dmcount) {
QTextCharFormat sizeFormatter;
sizeFormatter.setBackground(QColor(255,150,150));
sizeFormatter.setForeground(QBrush(QColor(0,0,0)));
setFormat(0,140+dmcount-prev,sizeFormatter);
}
setCurrentBlockState(size+1);
QTextCharFormat urlFormatter;
urlFormatter.setBackground(Qt::yellow);
QTextCharFormat shortUrlFormatter;
shortUrlFormatter.setBackground(Qt::green);
int index = text.indexOf(expression);
while (index >=0 ) {
int length = expression.matchedLength();
QString urlText = expression.cap(0);
QString urlHost=QUrl(urlText).host();
bool ignore=false;
if (urlHost=="bit.ly"||urlHost=="twitpic.com"||urlText.length()<25)
ignore=true;
if (!ignore) {
if (!url) {
url = new TweetTextHighlighter::urlData();
}
url->urls.append(urlText);
setFormat(index,length,urlFormatter);
} else
setFormat(index,length,shortUrlFormatter);
index=text.indexOf(expression,index+length);
}
// delete currentBlockUserData();
// if (url)
setCurrentBlockUserData(url);
}
void TweetTextHighlighter::setDirectMessagePrefix(int i) {
dmcount=i;
}