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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2010-05-01
* Description : an abstract rule class
*
* SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "rule.h"
// Qt includes
#include <QAction>
#include <QMenu>
#include <QPushButton>
#include <QRegularExpression>
#include <QString>
#include <QIcon>
#include <QApplication>
#include <QStyle>
namespace Digikam
{
class Q_DECL_HIDDEN Rule::Private
{
public:
Private() = default;
bool useTokenMenu = false;
QString description;
QString iconName;
QRegularExpression regExp;
TokenList tokens;
};
Rule::Rule(const QString& name)
: QObject(nullptr),
d (new Private)
{
setObjectName(name);
}
Rule::Rule(const QString& name, const QString& icon)
: QObject(nullptr),
d (new Private)
{
setObjectName(name);
setIcon(icon);
}
Rule::~Rule()
{
qDeleteAll(d->tokens);
d->tokens.clear();
delete d;
}
void Rule::setIcon(const QString& iconName)
{
d->iconName = iconName;
}
QPixmap Rule::icon(Rule::IconType type) const
{
QPixmap icon;
switch (type)
{
case Dialog:
{
icon = QIcon::fromTheme(d->iconName).pixmap(48);
break;
}
default:
{
icon = QIcon::fromTheme(d->iconName).pixmap(QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize));
break;
}
}
return icon;
}
void Rule::setDescription(const QString& desc)
{
d->description = desc;
}
QString Rule::description() const
{
return d->description;
}
QRegularExpression& Rule::regExp() const
{
return d->regExp;
}
void Rule::setRegExp(const QRegularExpression& regExp)
{
d->regExp = regExp;
}
QPushButton* Rule::createButton(const QString& name, const QIcon& icon)
{
const int maxHeight = 28;
QPushButton* const button = new QPushButton;
button->setText(name);
button->setIcon(icon);
button->setMinimumHeight(maxHeight);
button->setMaximumHeight(maxHeight);
return button;
}
QPushButton* Rule::registerButton(QWidget* parent)
{
QPushButton* const button = createButton(objectName(), icon());
QList<QAction*> actions;
if (d->tokens.count() > 1 && d->useTokenMenu)
{
QMenu* const menu = new QMenu(button);
for (Token* const token : std::as_const(d->tokens))<--- Variable 'token' can be declared as pointer to const
{
actions << token->action();
}
menu->addActions(actions);
button->setMenu(menu);
}
else if (!d->tokens.isEmpty())
{
Token* const token = d->tokens.first();
connect(button, SIGNAL(clicked()),
token, SLOT(slotTriggered()));
}
button->setParent(parent);
return button;
}
QAction* Rule::registerMenu(QMenu* parent)
{
QAction* action = nullptr;
if (d->tokens.count() > 1 && d->useTokenMenu)
{
QMenu* const menu = new QMenu(parent);
QList<QAction*> actions;
for (Token* const token : std::as_const(d->tokens))<--- Variable 'token' can be declared as pointer to const
{
actions << token->action();
}
menu->addActions(actions);
action = parent->addMenu(menu);
}
else if (!d->tokens.isEmpty())
{
action = d->tokens.first()->action();
parent->insertAction(nullptr, action);
}
if (action)
{
action->setText(objectName());
action->setIcon(icon());
}
return action;
}
bool Rule::addToken(const QString& id, const QString& description, const QString& actionName)
{
if (id.isEmpty() || description.isEmpty())
{
return false;
}
Token* const token = new Token(id, description);<--- Variable 'token' can be declared as pointer to const
if (!actionName.isEmpty())
{
token->action()->setText(actionName);
}
connect(token, SIGNAL(signalTokenTriggered(QString)),
this, SLOT(slotTokenTriggered(QString)));
d->tokens << token;
return true;
}
void Rule::setUseTokenMenu(bool value)
{
d->useTokenMenu = value;
}
bool Rule::useTokenMenu() const
{
return d->useTokenMenu;
}
TokenList& Rule::tokens() const
{
return d->tokens;
}
void Rule::slotTokenTriggered(const QString& token)
{
Q_EMIT signalTokenTriggered(token);
}
bool Rule::isValid() const
{
return (
!d->tokens.isEmpty() &&
!d->regExp.pattern().isEmpty() &&
d->regExp.isValid()
);
}
void Rule::reset()
{
}
QString Rule::escapeToken(const QString& token)
{
QString escaped = token;
// replace special characters for renaming options
escaped.replace(QLatin1Char('['), QLatin1String("\\["));
escaped.replace(QLatin1Char(']'), QLatin1String("\\]"));
// replace special characters for modifiers
escaped.replace(QLatin1Char('{'), QLatin1String("\\{"));
escaped.replace(QLatin1Char('}'), QLatin1String("\\}"));
return escaped;
}
ParseResults Rule::parse(ParseSettings& settings)
{
ParseResults parsedResults;
const QRegularExpression& reg = regExp();
const QString& parseString = settings.parseString;
QRegularExpressionMatch match;
int pos = 0;
while (pos > -1)
{
pos = parseString.indexOf(reg, pos, &match);
if (pos > -1)
{
QString result = parseOperation(settings, match);
ParseResults::ResultsKey k(pos, match.captured(0).count());
ParseResults::ResultsValue v(match.captured(0), result);
parsedResults.addEntry(k, v);
pos += match.capturedLength();
}
}
return parsedResults;
}
} // namespace Digikam
#include "moc_rule.cpp"
|