-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexdelegate.cpp
executable file
·182 lines (149 loc) · 4.57 KB
/
regexdelegate.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "regexdelegate.h"
#include <QPainter>
#include <QAbstractItemView>
RegExDelegate::RegExDelegate(QAbstractItemView *parent, Type type,\
const Qt::GlobalColor bgColor, const Qt::GlobalColor fgColor,\
const QRegExp ®Ex, const QString &newPhrase) :
QStyledItemDelegate(parent)
{
this->type = type;
this->regEx = regEx;
this->newPhrase = newPhrase;
this->bgColor = bgColor;
this->fgColor = fgColor;
}
RegExDelegate::~RegExDelegate()
{
}
// CORE part of this class
void RegExDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, \
const QModelIndex &index) const
{
if(type == Match){
paintMatch(painter, option, index);
}
else{ // if regEx!= Empty() && type == RegExDelegateType::Replace
paintReplace(painter, option, index);
}
}
void RegExDelegate::refresh()
{
qobject_cast<QAbstractItemView *>(parent())->viewport()->repaint();
}
void RegExDelegate::paintMatch(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// use parent's paint method
if(regEx.isEmpty() || (option.state & QStyle::State_Selected)){
QStyledItemDelegate::paint(painter, option, index);
return;
}
//rect is the area in which the text will be drawn
QRect rect = option.rect;
rect.adjust(PADDING_LEFT, 0, 0, 0);
QStringList l = splitString(regEx, index.data().toString());
// draw the first part
painter->drawText(rect, l[0]);
rect.adjust(QFontMetrics(option.font).width(l[0]),0,0,0);
// draw the second part
painter->save();
painter->setPen(QColor(fgColor));
painter->setBackgroundMode(Qt::OpaqueMode);
painter->setBackground(QBrush(bgColor));
painter->drawText(rect, l[1]);
rect.adjust(QFontMetrics(option.font).width(l[1]),0,0,0);
painter->restore();
// draw the rest
painter->drawText(rect, l[2]);
}
void RegExDelegate::paintReplace(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(regEx.isEmpty() || (option.state & QStyle::State_Selected)){
QStyledItemDelegate::paint(painter, option, index);
return;
}
QRect rect = option.rect;
rect.adjust(PADDING_LEFT, 0, 0, 0);
QStringList l = splitString(regEx, index.data().toString());
// draw the first part
painter->drawText(rect, l[0]);
rect.adjust(QFontMetrics(option.font).width(l[0]),0,0,0);
// draw the second part
painter->save();
painter->setPen(QColor(fgColor));
painter->setBackgroundMode(Qt::OpaqueMode);
painter->setBackground(QBrush(bgColor));
QString replacedStr = l[1].replace(regEx, newPhrase);
// QString.replace() replaces EVERY occurrence of the regular expression.
painter->drawText(rect, replacedStr);
rect.adjust(QFontMetrics(option.font).width(replacedStr),0,0,0);
painter->restore();
// draw the rest
painter->drawText(rect, l[2]);
}
/*
* Split string, eg. filename, with the given regular expression.
* The return value is a stringlist composed of 3 parts.
* The first part will be the prior section to the matching part.
* The second will be the matching part.
* The third part will be the remaining after the matching part.
*/
QStringList RegExDelegate::splitString(const QRegExp regEx, const QString &str)
{
QStringList result;
int pos = regEx.indexIn(str);
if(pos != -1){
result << str.left(pos)
<< str.mid(pos, regEx.matchedLength())
<< str.mid(pos + regEx.matchedLength());
} else {
// make sure that we always have 3 strings
result << str << "" << "";
}
return result;
}
Qt::GlobalColor RegExDelegate::getBgColor() const
{
return bgColor;
}
void RegExDelegate::setBgColor(const Qt::GlobalColor &color)
{
if(bgColor == color)
return;
bgColor = color;
refresh();
}
Qt::GlobalColor RegExDelegate::getFgColor() const
{
return fgColor;
}
void RegExDelegate::setFgColor(const Qt::GlobalColor &color)
{
if(fgColor == color)
return;
fgColor = color;
refresh();
}
QRegExp RegExDelegate::getRegEx() const
{
return regEx;
}
void RegExDelegate::setRegEx(const QRegExp ®Ex)
{
if(this->regEx == regEx)
return;
this->regEx = regEx;
refresh();
}
QString RegExDelegate::getAfter() const
{
return newPhrase;
}
void RegExDelegate::setAfter(const QString &str)
{
if(newPhrase == str)
return;
newPhrase = str;
if(type == RegExDelegate::Replace && !regEx.isEmpty()){
refresh();
}
}