forked from easymodo/qimgv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp
584 lines (534 loc) · 19.6 KB
/
settings.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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include "settings.h"
Settings *settings = nullptr;
Settings::Settings(QObject *parent) : QObject(parent) {
#ifdef IS_PORTABLE
cacheDirectory = new QDir(QApplication::applicationDirPath() + "/cache");
cacheDirectory->mkpath(cacheDirectory->absolutePath());
thumbnailDirectory = new QDir(QApplication::applicationDirPath() + "/thumbnails");
thumbnailDirectory->mkpath(thumbnailDirectory->absolutePath());
s = new QSettings(QApplication::applicationDirPath() + "/qimgv.ini", QSettings::IniFormat);
#else
cacheDirectory = new QDir(QDir::homePath() + "/.cache/qimgv");
cacheDirectory->mkpath(cacheDirectory->absolutePath());
thumbnailDirectory = new QDir(QDir::homePath() + "/.cache/qimgv/thumbnails");
thumbnailDirectory->mkpath(thumbnailDirectory->absolutePath());
QSettings::setDefaultFormat(QSettings::NativeFormat);
s = new QSettings();
#endif
}
Settings::~Settings() {
delete thumbnailDirectory;
delete cacheDirectory;
delete s;
}
Settings *Settings::getInstance() {
if(!settings) {
settings = new Settings();
validate();
}
return settings;
}
void Settings::validate() {
if(settings) {
bool ok = true;
if(settings->s->value("lastDir") == "") {
settings->s->setValue("lastDir",
QDir::homePath());
}
if(!ok) {
qDebug() << "Settings: error reading thumbnail size (int conversion failed).";
qDebug() << "Settings: setting default size.";
ok = true;
settings->s->setValue("mainPanelSize", 160);
}
}
}
//------------------------------------------------------------------------------
QString Settings::cacheDir() {
return thumbnailDirectory->path() + "/";
}
//------------------------------------------------------------------------------
QString Settings::tempDir() {
return cacheDirectory->path() + "/";
}
//------------------------------------------------------------------------------
QString Settings::mpvBinary() {
QString mpvPath = settings->s->value("mpvBinary", "").toString();
if(!QFile::exists(mpvPath)) {
#ifdef _WIN32
mpvPath = QCoreApplication::applicationDirPath() + "/mpv.exe";
#elif defined __linux__
mpvPath = "/usr/bin/mpv";
#endif
if(!QFile::exists(mpvPath)) {
mpvPath = "";
}
}
return mpvPath;
}
void Settings::setMpvBinary(QString path) {
if(QFile::exists(path)) {
settings->s->setValue("mpvBinary", path);
}
}
//------------------------------------------------------------------------------
// returns list of regexps
QStringList Settings::supportedFormats() {
QStringList filters;
QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
if(this->playWebm()) {
supportedFormats << "webm";
}
if(this->playMp4()) {
supportedFormats << "mp4";
}
for(int i = 0; i < supportedFormats.count(); i++) {
filters << "*." + QString(supportedFormats.at(i));
}
return filters;
}
//------------------------------------------------------------------------------
// (for open/save dialogs)
// example: "Images (*.jpg, *.png)"
QString Settings::supportedFormatsString() {
QString filters;
QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
if(this->playWebm()) {
supportedFormats << "webm";
}
if(this->playMp4()) {
supportedFormats << "mp4";
}
filters.append("Images (");
for(int i = 0; i < supportedFormats.count(); i++) {
filters.append("*." + QString(supportedFormats.at(i)) + " ");
}
filters.append(")");
return filters;
}
//------------------------------------------------------------------------------
// returns list of mime types
QStringList Settings::supportedMimeTypes() {
QStringList filters;
QList<QByteArray> mimeTypes = QImageReader::supportedMimeTypes();
if(this->playWebm()) {
mimeTypes << "video/webm";
}
if(this->playMp4()) {
mimeTypes << "video/mp4";
}
for(int i = 0; i < mimeTypes.count(); i++) {
filters << QString(mimeTypes.at(i));
}
//qDebug() << filters;
return filters;
}
//------------------------------------------------------------------------------
bool Settings::playWebm() {
#ifdef USE_MPV
return settings->s->value("playWebm", true).toBool();
#else
return false;
#endif
}
void Settings::setPlayWebm(bool mode) {
settings->s->setValue("playWebm", mode);
}
//------------------------------------------------------------------------------
bool Settings::playMp4() {
#ifdef USE_MPV
return settings->s->value("playMp4", false).toBool();
#else
return false;
#endif
}
void Settings::setPlayMp4(bool mode) {
settings->s->setValue("playMp4", mode);
}
//------------------------------------------------------------------------------
QVersionNumber Settings::lastVersion() {
int vmajor = settings->s->value("lastVerMajor", 0).toInt();
int vminor = settings->s->value("lastVerMinor", 0).toInt();
int vmicro = settings->s->value("lastVerMicro", 0).toInt();
return QVersionNumber(vmajor, vminor, vmicro);
}
void Settings::setLastVersion(QVersionNumber &ver) {
settings->s->setValue("lastVerMajor", ver.majorVersion());
settings->s->setValue("lastVerMinor", ver.minorVersion());
settings->s->setValue("lastVerMicro", ver.microVersion());
}
//------------------------------------------------------------------------------
void Settings::setShowChangelogs(bool mode) {
settings->s->setValue("showChangelogs", mode);
}
bool Settings::showChangelogs() {
return settings->s->value("showChangelogs", true).toBool();
}
//------------------------------------------------------------------------------
qreal Settings::backgroundOpacity() {
bool ok = false;
qreal value = settings->s->value("backgroundOpacity", 1.0).toReal(&ok);
if(!ok)
return 0.0;
if(value > 1.0)
return 1.0;
if(value < 0.0)
return 0.0;
return value;
}
void Settings::setBackgroundOpacity(qreal value) {
if(value > 1.0)
value = 1.0;
else if(value < 0.0)
value = 0.0;
settings->s->setValue("backgroundOpacity", value);
}
//------------------------------------------------------------------------------
bool Settings::blurBackground() {
#ifndef USE_KDE_BLUR
return false;
#endif
return settings->s->value("blurBackground", true).toBool();
}
void Settings::setBlurBackground(bool mode) {
settings->s->setValue("blurBackground", mode);
}
//------------------------------------------------------------------------------
void Settings::setSortingMode(SortingMode mode) {
if(mode >= 6)
mode = SortingMode::NAME_ASC;
settings->s->setValue("sortingMode", mode);
}
SortingMode Settings::sortingMode() {
int mode = settings->s->value("sortingMode", 0).toInt();
if(mode < 0 || mode >= 6)
mode = 0;
return static_cast<SortingMode>(mode);
}
//------------------------------------------------------------------------------
bool Settings::playVideoSounds() {
return settings->s->value("playVideoSounds", false).toBool();
}
void Settings::setPlayVideoSounds(bool mode) {
settings->s->setValue("playVideoSounds", mode);
}
//------------------------------------------------------------------------------
bool Settings::useFastScale() {
return settings->s->value("useFastScale", "true").toBool();
}
void Settings::setUseFastScale(bool mode) {
settings->s->setValue("useFastScale", mode);
}
//------------------------------------------------------------------------------
QString Settings::lastDirectory() {
return settings->s->value("lastDir", QDir::homePath()).toString();
}
void Settings::setLastDirectory(QString path) {
settings->s->setValue("lastDir", path);
}
//------------------------------------------------------------------------------
unsigned int Settings::lastFilePosition() {
bool ok = true;
unsigned int pos = settings->s->value("lastFilePosition", "0").toUInt(&ok);
if(!ok) {
qDebug() << "Settings: Invalid lastFilePosition. Resetting to 0.";
pos = 0;
}
return pos;
}
void Settings::setLastFilePosition(unsigned int pos) {
settings->s->setValue("lastFilePosition", pos);
}
//------------------------------------------------------------------------------
unsigned int Settings::mainPanelSize() {
bool ok = true;
unsigned int size = settings->s->value("mainPanelSize", mainPanelSizeDefault).toUInt(&ok);
if(!ok) {
size = mainPanelSizeDefault;
}
return size;
}
void Settings::setMainPanelSize(unsigned int size) {
settings->s->setValue("mainPanelSize", size);
}
//------------------------------------------------------------------------------
bool Settings::usePreloader() {
return settings->s->value("usePreloader", true).toBool();
}
void Settings::setUsePreloader(bool mode) {
settings->s->setValue("usePreloader", mode);
}
//------------------------------------------------------------------------------
QColor Settings::backgroundColor() {
return settings->s->value("bgColor", QColor(27, 27, 28)).value<QColor>();
}
void Settings::setBackgroundColor(QColor color) {
settings->s->setValue("bgColor", color);
}
//------------------------------------------------------------------------------
QColor Settings::accentColor() {
return settings->s->value("accentColor", QColor(17, 121, 100)).value<QColor>();
}
void Settings::setAccentColor(QColor color) {
settings->s->setValue("accentColor", color);
}
//------------------------------------------------------------------------------
bool Settings::fullscreenMode() {
return settings->s->value("openInFullscreen", true).toBool();
}
void Settings::setFullscreenMode(bool mode) {
settings->s->setValue("openInFullscreen", mode);
}
//------------------------------------------------------------------------------
bool Settings::panelEnabled() {
return settings->s->value("panelEnabled", true).toBool();
}
void Settings::setPanelEnabled(bool mode) {
settings->s->setValue("panelEnabled", mode);
}
//------------------------------------------------------------------------------
bool Settings::panelFullscreenOnly() {
return settings->s->value("panelFullscreenOnly", false).toBool();
}
void Settings::setPanelFullscreenOnly(bool mode) {
settings->s->setValue("panelFullscreenOnly", mode);
}
//------------------------------------------------------------------------------
int Settings::lastDisplay() {
return settings->s->value("lastDisplay", 0).toInt();
}
void Settings::setLastDisplay(int display) {
settings->s->setValue("lastDisplay", display);
}
//------------------------------------------------------------------------------
PanelHPosition Settings::panelPosition() {
QString posString = settings->s->value("panelPosition", "top").toString();
if(posString == "top") {
return PanelHPosition::PANEL_TOP;
} else {
return PanelHPosition::PANEL_BOTTOM;
}
}
void Settings::setPanelPosition(PanelHPosition pos) {
QString posString;
switch(pos) {
case PANEL_TOP:
posString = "top";
break;
case PANEL_BOTTOM:
posString = "bottom";
break;
}
settings->s->setValue("panelPosition", posString);
}
//------------------------------------------------------------------------------
/*
* 0: fit window
* 1: fit width
* 2: orginal size
*/
ImageFitMode Settings::imageFitMode() {
int mode = settings->s->value("defaultFitMode", 0).toInt();
if(mode < 0 || mode > 2) {
qDebug() << "Settings: Invalid fit mode ( " + QString::number(mode) + " ). Resetting to default.";
mode = 0;
}
return static_cast<ImageFitMode>(mode);
}
void Settings::setImageFitMode(ImageFitMode mode) {
int modeInt = static_cast<ImageFitMode>(mode);
if(modeInt < 0 || modeInt > 2) {
qDebug() << "Settings: Invalid fit mode ( " + QString::number(modeInt) + " ). Resetting to default.";
modeInt = 0;
}
settings->s->setValue("defaultFitMode", modeInt);
}
//------------------------------------------------------------------------------
QRect Settings::windowGeometry() {
QRect savedRect = settings->s->value("windowGeometry").toRect();
if(savedRect.size().isEmpty())
savedRect.setRect(100, 100, 900, 600);
return savedRect;
}
void Settings::setWindowGeometry(QRect geometry) {
settings->s->setValue("windowGeometry", geometry);
}
//------------------------------------------------------------------------------
bool Settings::infiniteScrolling() {
return settings->s->value("infiniteScrolling", false).toBool();
}
void Settings::setInfiniteScrolling(bool mode) {
settings->s->setValue("infiniteScrolling", mode);
}
//------------------------------------------------------------------------------
void Settings::sendChangeNotification() {
emit settingsChanged();
}
//------------------------------------------------------------------------------
void Settings::readShortcuts(QMap<QString, QString> &shortcuts) {
settings->s->beginGroup("Controls");
QStringList in, pair;
in = settings->s->value("shortcuts").toStringList();
for(int i = 0; i < in.count(); i++) {
pair = in[i].split("=");
if(!pair[0].isEmpty() && !pair[1].isEmpty()) {
shortcuts.insert(pair[1], pair[0]);
}
}
settings->s->endGroup();
}
void Settings::saveShortcuts(const QMap<QString, QString> &shortcuts) {
settings->s->beginGroup("Controls");
QMapIterator<QString, QString> i(shortcuts);
QStringList out;
while(i.hasNext()) {
i.next();
out << i.value() + "=" + i.key();
}
settings->s->setValue("shortcuts", out);
settings->s->endGroup();
}
//------------------------------------------------------------------------------
void Settings::readScripts(QMap<QString, Script> &scripts) {
scripts.clear();
settings->s->beginGroup("Scripts");
int size = settings->s->beginReadArray("script");
for(int i=0; i < size; i++) {
settings->s->setArrayIndex(i);
QString name = settings->s->value("name").toString();
QVariant value = settings->s->value("value");
Script scr = value.value<Script>();
scripts.insert(name, scr);
}
settings->s->endArray();
settings->s->endGroup();
}
void Settings::saveScripts(const QMap<QString, Script> &scripts) {
settings->s->beginGroup("Scripts");
settings->s->beginWriteArray("script");
QMapIterator<QString, Script> i(scripts);
int counter = 0;
while(i.hasNext()) {
i.next();
settings->s->setArrayIndex(counter);
settings->s->setValue("name", i.key());
settings->s->setValue("value", QVariant::fromValue(i.value()));
counter++;
}
settings->s->endArray();
settings->s->endGroup();
}
//------------------------------------------------------------------------------
bool Settings::mouseWrapping() {
return settings->s->value("mouseWrapping", false).toBool();
}
void Settings::setMouseWrapping(bool mode) {
settings->s->setValue("mouseWrapping", mode);
}
//------------------------------------------------------------------------------
bool Settings::squareThumbnails() {
return settings->s->value("squareThumbnails", true).toBool();
}
void Settings::setSquareThumbnails(bool mode) {
settings->s->setValue("squareThumbnails", mode);
}
//------------------------------------------------------------------------------
bool Settings::transparencyGrid() {
return settings->s->value("drawTransparencyGrid", false).toBool();
}
void Settings::setTransparencyGrid(bool mode) {
settings->s->setValue("drawTransparencyGrid", mode);
}
//------------------------------------------------------------------------------
bool Settings::forceSmoothScroll() {
return settings->s->value("forceSmoothScroll", false).toBool();
}
void Settings::setForceSmoothScroll(bool mode) {
settings->s->setValue("forceSmoothScroll", mode);
}
//------------------------------------------------------------------------------
bool Settings::useThumbnailCache() {
return settings->s->value("thumbnailCache", true).toBool();
}
void Settings::setUseThumbnailCache(bool mode) {
settings->s->setValue("thumbnailCache", mode);
}
//------------------------------------------------------------------------------
QStringList Settings::savedPaths() {
return settings->s->value("savedPaths").toStringList();
}
void Settings::setSavedPaths(QStringList paths) {
settings->s->setValue("savedPaths", paths);
}
//------------------------------------------------------------------------------
int Settings::thumbnailerThreadCount() {
int count = settings->s->value("thumbnailerThreads", 2).toInt();
if(count < 1)
count = 1;
return count;
}
void Settings::setThumbnailerThreadCount(int count) {
settings->s->setValue("thumbnailerThreads", count);
}
//------------------------------------------------------------------------------
bool Settings::smoothUpscaling() {
return settings->s->value("smoothUpscaling", true).toBool();
}
void Settings::setSmoothUpscaling(bool mode) {
settings->s->setValue("smoothUpscaling", mode);
}
//------------------------------------------------------------------------------
int Settings::maxZoomedResolution() {
return settings->s->value("maximumZoomResolution", 75).toInt();
}
void Settings::setMaxZoomedResolution(int value) {
settings->s->setValue("maximumZoomResolution", value);
}
//------------------------------------------------------------------------------
int Settings::maximumZoom() {
return settings->s->value("maximumZoom", 8).toInt();
}
void Settings::setMaximumZoom(int value) {
settings->s->setValue("maximumZoom", value);
}
//------------------------------------------------------------------------------
bool Settings::expandImage() {
return settings->s->value("expandImage", false).toBool();
}
void Settings::setExpandImage(bool mode) {
settings->s->setValue("expandImage", mode);
}
//------------------------------------------------------------------------------
/* 0: nearest
* 1: bilinear
*/
int Settings::scalingFilter() {
int mode = settings->s->value("scalingFilter", 1).toInt();
if(mode < 0 || mode > 1)
mode = 1;
return mode;
}
void Settings::setScalingFilter(int mode) {
settings->s->setValue("scalingFilter", mode);
}
//------------------------------------------------------------------------------
bool Settings::smoothAnimatedImages() {
return settings->s->value("smoothAnimatedImages", true).toBool();
}
void Settings::setSmoothAnimatedImages(bool mode) {
settings->s->setValue("smoothAnimatedImages", mode);
}
//------------------------------------------------------------------------------
bool Settings::showInfoOverlay() {
return settings->s->value("showInfoOverlay", true).toBool();
}
void Settings::setShowInfoOverlay(bool mode) {
settings->s->setValue("showInfoOverlay", mode);
}
//------------------------------------------------------------------------------
bool Settings::firstRun() {
return settings->s->value("firstRun", true).toBool();
}
void Settings::setFirstRun(bool mode) {
settings->s->setValue("firstRun", mode);
}