forked from flameshot-org/flameshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvaluehandler.cpp
More file actions
569 lines (471 loc) · 12.1 KB
/
valuehandler.cpp
File metadata and controls
569 lines (471 loc) · 12.1 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "valuehandler.h"
#include "capturetool.h"
#include "colorpickerwidget.h"
#include "confighandler.h"
#include "screengrabber.h"
#include <QColor>
#include <QFileInfo>
#include <QImageWriter>
#include <QKeySequence>
#include <QStandardPaths>
#include <QVariant>
// VALUE HANDLER
QVariant ValueHandler::value(const QVariant& val)
{
if (!val.isValid() || !check(val)) {
return fallback();
} else {
return process(val);
}
}
QVariant ValueHandler::fallback()
{
return {};
}
QVariant ValueHandler::representation(const QVariant& val)
{
return val.toString();
}
QString ValueHandler::expected()
{
return {};
}
QVariant ValueHandler::process(const QVariant& val)
{
return val;
}
// BOOL
Bool::Bool(bool def)
: m_def(def)
{}
bool Bool::check(const QVariant& val)
{
QString str = val.toString();
if (str != "true" && str != "false") {
return false;
}
return true;
}
QVariant Bool::fallback()
{
return m_def;
}
QString Bool::expected()
{
return QStringLiteral("true or false");
}
// STRING
String::String(QString def)
: m_def(std::move(def))
{}
bool String::check(const QVariant&)
{
return true;
}
QVariant String::fallback()
{
return m_def;
}
QString String::expected()
{
return QStringLiteral("string");
}
// COLOR
Color::Color(QColor def)
: m_def(std::move(def))
{}
bool Color::check(const QVariant& val)
{
QString str = val.toString();
// Disable #RGB, #RRRGGGBBB and #RRRRGGGGBBBB formats that QColor supports
return QColor::isValidColor(str) &&
(str[0] != '#' ||
(str.length() != 4 && str.length() != 10 && str.length() != 13));
}
QVariant Color::process(const QVariant& val)
{
QString str = val.toString();
QColor color(str);
if (str.length() == 9 && str[0] == '#') {
// Convert #RRGGBBAA (flameshot) to #AARRGGBB (QColor)
int blue = color.blue();
color.setBlue(color.green());
color.setGreen(color.red());
color.setRed(color.alpha());
color.setAlpha(blue);
}
return color;
}
QVariant Color::fallback()
{
return m_def;
}
QVariant Color::representation(const QVariant& val)
{
QString str = val.toString();
QColor color(str);
if (str.length() == 9 && str[0] == '#') {
// Convert #AARRGGBB (QColor) to #RRGGBBAA (flameshot)
int alpha = color.alpha();
color.setAlpha(color.red());
color.setRed(color.green());
color.setGreen(color.blue());
color.setBlue(alpha);
}
return color.name();
}
QString Color::expected()
{
return QStringLiteral("color name or hex value");
}
// BOUNDED INT
BoundedInt::BoundedInt(int min, int max, int def)
: m_min(min)
, m_max(max)
, m_def(def)
{}
bool BoundedInt::check(const QVariant& val)
{
QString str = val.toString();
bool conversionOk;
int num = str.toInt(&conversionOk);
return conversionOk && m_min <= num && num <= m_max;
}
QVariant BoundedInt::fallback()
{
return m_def;
}
QString BoundedInt::expected()
{
return QStringLiteral("number between %1 and %2").arg(m_min).arg(m_max);
}
// LOWER BOUNDED INT
LowerBoundedInt::LowerBoundedInt(int min, int def)
: m_min(min)
, m_def(def)
{}
bool LowerBoundedInt::check(const QVariant& val)
{
QString str = val.toString();
bool conversionOk;
int num = str.toInt(&conversionOk);
return conversionOk && num >= m_min;
}
QVariant LowerBoundedInt::fallback()
{
return m_def;
}
QString LowerBoundedInt::expected()
{
return QStringLiteral("number >= %1").arg(m_min);
}
// KEY SEQUENCE
KeySequence::KeySequence(const QKeySequence& fallback)
: m_fallback(fallback)
{}
bool KeySequence::check(const QVariant& val)
{
QString str = val.toString();
if (!str.isEmpty() && QKeySequence(str).toString().isEmpty()) {
return false;
}
return true;
}
QVariant KeySequence::fallback()
{
return process(m_fallback);
}
QString KeySequence::expected()
{
return QStringLiteral("keyboard shortcut");
}
QVariant KeySequence::representation(const QVariant& val)
{
QString str(val.toString());
if (QKeySequence(str) == QKeySequence(Qt::Key_Return)) {
return QStringLiteral("Enter");
}
return str;
}
QVariant KeySequence::process(const QVariant& val)
{
QString str(val.toString());
if (str == "Enter") {
return QKeySequence(Qt::Key_Return).toString();
}
if (str.length() > 0) {
// Make the "main" key in sequence (last one) lower-case.
const QCharRef& lastChar = str[str.length() - 1];
str.replace(str.length() - 1, 1, lastChar.toLower());
}
return str;
}
// EXISTING DIR
bool ExistingDir::check(const QVariant& val)
{
if (!val.canConvert(QVariant::String) || val.toString().isEmpty()) {
return false;
}
QFileInfo info(val.toString());
return info.isDir() && info.exists();
}
QVariant ExistingDir::fallback()
{
using SP = QStandardPaths;
for (auto location :
{ SP::PicturesLocation, SP::HomeLocation, SP::TempLocation }) {
QString path = SP::writableLocation(location);
if (QFileInfo(path).isDir()) {
return path;
}
}
return {};
}
QString ExistingDir::expected()
{
return QStringLiteral("existing directory");
}
// FILENAME PATTERN
bool FilenamePattern::check(const QVariant&)
{
return true;
}
QVariant FilenamePattern::fallback()
{
return ConfigHandler().filenamePatternDefault();
}
QVariant FilenamePattern::process(const QVariant& val)
{
QString str = val.toString();
return !str.isEmpty() ? val : fallback();
}
QString FilenamePattern::expected()
{
return QStringLiteral("please edit using the GUI");
}
// BUTTON LIST
using BType = CaptureTool::Type;
using BList = QList<CaptureTool::Type>;
bool ButtonList::check(const QVariant& val)
{
// TODO stop using CTB
using CTB = CaptureToolButton;
auto allButtons = CTB::getIterableButtonTypes();
for (int btn : val.value<QList<int>>()) {
if (!allButtons.contains(static_cast<BType>(btn))) {
return false;
}
}
return true;
}
// Helper
void sortButtons(BList& buttons)
{
std::sort(buttons.begin(), buttons.end(), [](BType a, BType b) {
return CaptureToolButton::getPriorityByButton(a) <
CaptureToolButton::getPriorityByButton(b);
});
}
QVariant ButtonList::process(const QVariant& val)
{
auto intButtons = val.value<QList<int>>();
auto buttons = ButtonList::fromIntList(intButtons);
sortButtons(buttons);
return QVariant::fromValue(buttons);
}
QVariant ButtonList::fallback()
{
auto buttons = CaptureToolButton::getIterableButtonTypes();
buttons.removeOne(CaptureTool::TYPE_SIZEDECREASE);
buttons.removeOne(CaptureTool::TYPE_SIZEINCREASE);
sortButtons(buttons);
return QVariant::fromValue(buttons);
}
QVariant ButtonList::representation(const QVariant& val)
{
auto intList = toIntList(val.value<BList>());
normalizeButtons(intList);
return QVariant::fromValue(intList);
}
QString ButtonList::expected()
{
return QStringLiteral("please don't edit by hand");
}
QList<CaptureTool::Type> ButtonList::fromIntList(const QList<int>& l)
{
QList<CaptureTool::Type> buttons;
buttons.reserve(l.size());
for (auto const i : l) {
buttons << static_cast<CaptureTool::Type>(i);
}
return buttons;
}
QList<int> ButtonList::toIntList(const QList<CaptureTool::Type>& l)
{
QList<int> buttons;
buttons.reserve(l.size());
for (auto const i : l) {
buttons << static_cast<int>(i);
}
return buttons;
}
bool ButtonList::normalizeButtons(QList<int>& buttons)
{
QList<int> listTypesInt =
toIntList(CaptureToolButton::getIterableButtonTypes());
bool hasChanged = false;
for (int i = 0; i < buttons.size(); i++) {
if (!listTypesInt.contains(buttons.at(i))) {
buttons.removeAt(i);
hasChanged = true;
}
}
return hasChanged;
}
// USER COLORS
UserColors::UserColors(int min, int max)
: m_min(min)
, m_max(max)
{}
bool UserColors::check(const QVariant& val)
{
if (!val.isValid()) {
return false;
}
if (!val.canConvert(QVariant::StringList)) {
return false;
}
for (const QString& str : val.toStringList()) {
if (!QColor::isValidColor(str) && str != "picker") {
return false;
}
}
int sz = val.toStringList().size();
return sz >= m_min && sz <= m_max;
}
QVariant UserColors::process(const QVariant& val)
{
QStringList strColors = val.toStringList();
if (strColors.isEmpty()) {
return fallback();
}
QVector<QColor> colors;
colors.reserve(strColors.size());
for (const QString& str : strColors) {
if (str != "picker") {
colors.append(QColor(str));
} else {
colors.append(QColor());
}
}
return QVariant::fromValue(colors);
}
QVariant UserColors::fallback()
{
if (ConfigHandler().predefinedColorPaletteLarge()) {
return QVariant::fromValue(
ColorPickerWidget::getDefaultLargeColorPalette());
} else {
return QVariant::fromValue(
ColorPickerWidget::getDefaultSmallColorPalette());
}
}
QString UserColors::expected()
{
return QStringLiteral(
"list of colors(min %1 and max %2) separated by comma")
.arg(m_min - 1)
.arg(m_max - 1);
}
QVariant UserColors::representation(const QVariant& val)
{
auto colors = val.value<QVector<QColor>>();
QStringList strColors;
for (const auto& col : colors) {
if (col.isValid()) {
strColors.append(col.name(QColor::HexRgb));
} else {
strColors.append(QStringLiteral("picker"));
}
}
return QVariant::fromValue(strColors);
}
// SET SAVE FILE AS EXTENSION
bool SaveFileExtension::check(const QVariant& val)
{
if (!val.canConvert(QVariant::String) || val.toString().isEmpty()) {
return false;
}
QString extension = val.toString();
if (extension.startsWith(".")) {
extension.remove(0, 1);
}
QStringList imageFormatList;
foreach (auto imageFormat, QImageWriter::supportedImageFormats())
imageFormatList.append(imageFormat);
if (!imageFormatList.contains(extension)) {
return false;
}
return true;
}
QVariant SaveFileExtension::process(const QVariant& val)
{
QString extension = val.toString();
if (extension.startsWith(".")) {
extension.remove(0, 1);
}
return QVariant::fromValue(extension);
}
QString SaveFileExtension::expected()
{
return QStringLiteral("supported image extension");
}
// REGION
bool Region::check(const QVariant& val)
{
QVariant region = process(val);
return region.isValid();
}
#include <QApplication> // TODO remove after FIXME (see below)
#include <utility>
QVariant Region::process(const QVariant& val)
{
// FIXME: This is temporary, just before D-Bus is removed
char** argv = new char*[1];
int* argc = new int{ 0 };
if (QGuiApplication::screens().empty()) {
new QApplication(*argc, argv);
}
QString str = val.toString();
if (str == "all") {
return ScreenGrabber().desktopGeometry();
} else if (str.startsWith("screen")) {
bool ok;
int number = str.midRef(6).toInt(&ok);
if (!ok || number < 0) {
return {};
}
return ScreenGrabber().screenGeometry(qApp->screens()[number]);
}
QRegExp regex("(-{,1}\\d+)" // number (any sign)
"[x,\\.\\s]" // separator ('x', ',', '.', or whitespace)
"(-{,1}\\d+)" // number (any sign)
"[\\+,\\.\\s]*" // separator ('+',',', '.', or whitespace)
"(-{,1}\\d+)" // number (non-negative)
"[\\+,\\.\\s]*" // separator ('+', ',', '.', or whitespace)
"(-{,1}\\d+)" // number (non-negative)
);
if (!regex.exactMatch(str)) {
return {};
}
int w, h, x, y;
bool w_ok, h_ok, x_ok, y_ok;
w = regex.cap(1).toInt(&w_ok);
h = regex.cap(2).toInt(&h_ok);
x = regex.cap(3).toInt(&x_ok);
y = regex.cap(4).toInt(&y_ok);
if (!(w_ok && h_ok && x_ok && y_ok)) {
return {};
}
return QRect(x, y, w, h).normalized();
}