-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathSettings.cpp
More file actions
665 lines (569 loc) · 21.1 KB
/
Settings.cpp
File metadata and controls
665 lines (569 loc) · 21.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "Settings.h"
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QSettings>
#include <QColor>
#include <QFontInfo>
#include <QLocale>
#include <QStandardPaths>
#include <QPalette>
QString Settings::userSettingsFile;
QSettings* Settings::settings;
std::unordered_map<std::string, QVariant> Settings::m_hCache;
int Settings::m_defaultFontSize;
static bool ends_with(const std::string& str, const std::string& with)
{
return str.rfind(with) == str.size() - with.size();
}
void Settings::setUserSettingsFile(const QString& userSettingsFileArg)
{
userSettingsFile = userSettingsFileArg;
}
bool Settings::isVaildSettingsFile(const QString& userSettingsFile)
{
/*
Variable that stores whether or not the settings file requested by the user is a normal settings file
If the file does not exist and is newly created, the if statement below is not executed, so the default value is set to true
*/
bool isNormalUserSettingsFile = true;
// Code that verifies that the settings file requested by the user is a normal settings file
if(userSettingsFile != nullptr)
{
QFile *file = new QFile;
file->setFileName(userSettingsFile);
if(file->open(QIODevice::ReadOnly | QIODevice::Text))
{
if(file->exists() &&
QString::compare(QString("[%General]\n"), file->readLine(), Qt::CaseInsensitive) != 0)
isNormalUserSettingsFile = false;
}
file->close();
}
return isNormalUserSettingsFile;
}
void Settings::setSettingsObject()
{
// If an object has already been created, it is terminated to reduce overhead
if(settings)
return;
const bool isNormalUserSettingsFile = isVaildSettingsFile(userSettingsFile);
if(userSettingsFile == nullptr)
{
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
} else {
if(isNormalUserSettingsFile)
{
settings = new QSettings(userSettingsFile, QSettings::IniFormat);
// Code to verify that the user does not have access to the requested settings file
if(settings->status() == QSettings::AccessError) {
qWarning() << qPrintable("The given settings file can NOT access. Please check the permission for the file.");
qWarning() << qPrintable("So, the -S/--settings option is ignored.");
// Since you do not have permission to the file, delete the existing assignment and assign the standard
delete settings;
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
}
} else {
qWarning() << qPrintable("The given settings file is not a normal settings file. Please check again.");
qWarning() << qPrintable("So, the -S/--settings option is ignored.");
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
}
}
}
QVariant Settings::getValue(const std::string& group, const std::string& name)
{
// Have a look in the cache first
auto cacheIndex = m_hCache.find(group + name);
if(cacheIndex != m_hCache.end())
{
return cacheIndex->second;
} else {
// Nothing found in the cache, so get the value from the settings file or get the default value if there is no value set yet
setSettingsObject();
QVariant value = settings->value(QString::fromStdString(group + "/" + name), getDefaultValue(group, name));
// Store this value in the cache for further usage and return it afterwards
m_hCache.insert({group + name, value});
return value;
}
}
void Settings::setValue(const std::string& group, const std::string& name, const QVariant& value, bool save_to_disk)
{
// Sometime the value has to be saved for the current session only but get discarded when the application exits.
// In order to achieve this this flag can be set which disables the save to disk mechanism and only leaves the save to cache part active.
if(save_to_disk)
{
setSettingsObject();
// Set the group and save the given value
settings->beginGroup(QString::fromStdString(group));
settings->setValue(QString::fromStdString(name), value);
settings->endGroup();
}
// Also change it in the cache
m_hCache[group + name] = value;
}
QVariant Settings::getDefaultValue(const std::string& group, const std::string& name)
{
// db/defaultencoding?
if(group == "db" && name == "defaultencoding")
return "UTF-8";
// db/savedefaultlocation?
if(group == "db" && name == "savedefaultlocation")
return 2;
// db/defaultlocation?
if(group == "db" && name == "defaultlocation")
return QDir::homePath();
// db/lastlocation?
if(group == "db" && name == "lastlocation")
return getValue("db", "defaultlocation");
// db/hideschemalinebreaks?
if(group == "db" && name == "hideschemalinebreaks")
return true;
// db/foreignkeys?
if(group == "db" && name == "foreignkeys")
return true;
// db/prefetchsize?
if(group == "db" && name == "prefetchsize")
return 50000U;
// db/defaultsqltext?
if(group == "db" && name == "defaultsqltext")
return QString();
// db/fontsize?
if(group == "db" && name == "fontsize")
return 10;
// db/watcher?
if(group == "db" && name == "watcher")
return false;
// exportcsv/firstrowheader?
if(group == "exportcsv" && name == "firstrowheader")
return true;
// exportcsv/separator?
if(group == "exportcsv" && name == "separator")
return ',';
// exportcsv/quotecharacter?
if(group == "exportcsv" && name == "quotecharacter")
return '"';
// importcsv group?
if(group == "importcsv")
{
if(name == "firstrowheader")
return false;
if(name == "trimfields")
return true;
if(name == "separatetables")
return false;
if(name == "separator")
return ',';
if(name == "quotecharacter")
return '"';
if(name == "encoding")
return "UTF-8";
if(name == "localconventions")
return false;
}
// exportsql group?
if(group == "exportsql")
{
if(name == "insertcolnames" || name == "insertmultiple" || name == "keeporiginal")
return false;
if(name == "oldschema")
return 0;
}
// newline character
if (group == "exportcsv" && name == "newlinecharacters")
#ifdef Q_OS_WIN
return "\r\n";
#else
return "\n";
#endif
// exportjson/prettyprint?
if(group == "exportjson" && name == "prettyprint")
return true;
// MainWindow/geometry?
if(group == "MainWindow" && name == "geometry")
return QString();
// MainWindow/windowState?
if(group == "MainWindow" && name == "windowState")
return QString();
// MainWindow/openTabs?
if(group == "MainWindow" && name == "openTabs")
return QString();
// SQLLogDock/Log?
if(group == "SQLLogDock" && name == "Log")
return "Application";
// General/recentFileList?
if(group == "General" && name == "recentFileList")
return QStringList();
// General/maxRecentFiles?
if(group == "General" && name == "maxRecentFiles")
return 5;
// General/language?
if(group == "General" && name == "language")
return QLocale::system().name();
// General/appStyle
if(group == "General" && name == "appStyle")
return static_cast<int>(FollowDesktopStyle);
// General/toolbarStyle
if(group == "General" && name == "toolbarStyle")
return static_cast<int>(Qt::ToolButtonTextBesideIcon);
// General/toolbarStyleStructure
if(group == "General" && name == "toolbarStyleStructure")
return static_cast<int>(Qt::ToolButtonTextBesideIcon);
// General/toolbarStyleBrowse
if(group == "General" && name == "toolbarStyleBrowse")
return static_cast<int>(Qt::ToolButtonIconOnly);
// General/toolbarStyleSql
if(group == "General" && name == "toolbarStyleSql")
return static_cast<int>(Qt::ToolButtonIconOnly);
// General/toolbarStyleEditCell
if(group == "General" && name == "toolbarStyleEditCell")
return static_cast<int>(Qt::ToolButtonIconOnly);
if(group == "General" && name == "DBFileExtensions")
return QObject::tr("SQLite database files (*.db *.sqlite *.sqlite3 *.db3)");
// General/fontsize
if(group == "General" && name == "fontsize")
return m_defaultFontSize;
// General/promptsqltabsinnewproject
if(group == "General" && name == "promptsqltabsinnewproject")
return true;
// checkversion group?
if(group == "checkversion")
{
if(name == "enabled")
return true;
if(name == "ignmajor")
return 999;
if(name == "ignminor" || name == "ignpatch")
return 0;
}
// Data Browser/NULL Fields
if(group == "databrowser")
{
if(name == "font") {
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
return QFontInfo(font).family();
}
if(name == "fontsize")
return 10;
if(name == "symbol_limit")
return 5000;
if (name == "rows_limit")
return 10'000'000;
if(name == "complete_threshold")
return 1000;
if(name == "image_preview")
return false;
if(name == "cell_word_wrap")
return true;
if(name == "indent_compact")
return false;
if (name == "sort_keys")
return true;
if(name == "auto_switch_mode")
return true;
if(name == "editor_word_wrap")
return true;
if(name == "null_text")
return "NULL";
if(name == "blob_text")
return "BLOB";
if(name == "filter_escape")
return "\\";
if(name == "filter_delay")
return 200;
if(ends_with(name, "colour"))
return getDefaultColorValue(group, name, FollowDesktopStyle);
}
// syntaxhighlighter?
if(group == "syntaxhighlighter")
{
// Bold? Only tables, functions and keywords are bold by default
if(ends_with(name, "bold"))
return name == "keyword_bold" || name == "table_bold" || name == "function_bold";
// Italic? Nothing by default
if(ends_with(name, "italic"))
return false;
// Underline? Nothing by default
if(ends_with(name, "underline"))
return false;
// Colour?
if(ends_with(name, "colour"))
return getDefaultColorValue(group, name, FollowDesktopStyle);
}
// editor/font?
if(group == "editor" && name == "font")
{
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
return QFontInfo(font).family();
}
// editor/fontsize or log/fontsize?
if((group == "editor" || group == "log") && name == "fontsize")
#ifdef Q_OS_MAC
// Use 12 pt size as the default on macOS
return 12;
#else
return 9;
#endif
if(group == "editor")
{
if(name == "tabsize")
return 4;
if(name == "indentation_use_tabs")
return true;
}
// editor/wrap_lines
if(group == "editor" && name == "wrap_lines")
return 0; // QsciScintilla::WrapNone
// editor/identifier_quotes
if(group == "editor" && name == "identifier_quotes")
return 0; // sqlb::DoubleQuotes
// editor/auto_completion?
if(group == "editor" && name == "auto_completion")
return true;
// editor/upper_keywords?
if(group == "editor" && name == "upper_keywords")
return true;
// editor/error_indicators?
if(group == "editor" && name == "error_indicators")
return true;
// editor/horizontal_tiling?
if(group == "editor" && name == "horizontal_tiling")
return false;
// editor/splitter1_sizes?
if(group == "editor" && name == "splitter1_sizes")
return QVariant();
// editor/splitter2_sizes?
if(group == "editor" && name == "splitter2_sizes")
return QVariant();
// editor/close_button_on_tabs?
if(group == "editor" && name == "close_button_on_tabs")
return true;
// extensions/list?
if(group == "extensions" && name == "list")
return QStringList();
// extensions/disableregex?
if(group == "extension" && name == "disableregex")
return false;
// extensions/enable_load_extension?
if(group == "extension" && name == "enable_load_extension")
return false;
// PlotDock/lineType or pointShape?
if(group == "PlotDock")
{
// QCPGraph::lsLine
if(name == "lineType")
return 1;
// QCPScatterStyle::ssDisk
if(name == "pointShape")
return 4;
}
// SchemaDock Drag & drop settings
if(group == "SchemaDock")
{
if(name == "dropSelectQuery")
return true;
if(name == "dropInsert")
return false;
if(name == "dropQualifiedNames")
return false;
if(name == "dropEnquotedNames")
return true;
}
// Proxy settings
if(group == "proxy")
{
// Use system settings by default
if(name == "type")
return "system";
}
// Unknown combination of group and name? Return an invalid QVariant!
return QVariant();
}
QColor Settings::getDefaultColorValue(const std::string& group, const std::string& name, AppStyle style)
{
// Data Browser/NULL & Binary Fields
if(group == "databrowser")
{
// The switch on style can be removed if the following issue is fixed:
// https://github.com/ColinDuquesnoy/QDarkStyleSheet/issues/171
switch (style) {
case FollowDesktopStyle :
if(name == "null_fg_colour")
return QColor(Qt::lightGray).name();
if(name == "null_bg_colour")
return QPalette().color(QPalette::Active, QPalette::Base).name();
if(name == "reg_fg_colour")
return QPalette().color(QPalette::Active, QPalette::Text).name();
if(name == "reg_bg_colour")
return QPalette().color(QPalette::Active, QPalette::Base).name();
if(name == "formatted_fg_colour")
return QPalette().color(QPalette::Active, QPalette::Text).name();
if(name == "formatted_bg_colour")
return QPalette().color(QPalette::Active, QPalette::AlternateBase).name();
if(name == "bin_fg_colour")
return QColor(Qt::lightGray).name();
if(name == "bin_bg_colour")
return QPalette().color(QPalette::Active, QPalette::Base).name();
break;
case DarkStyle :
if(name == "null_fg_colour")
return QColor(0x78, 0x78, 0x78);
if(name == "null_bg_colour")
return QColor(0x19, 0x23, 0x2D);
if(name == "reg_fg_colour")
return QColor(0xF0, 0xF0, 0xF0);
if(name == "reg_bg_colour")
return QColor(0x19, 0x23, 0x2D);
if(name == "formatted_fg_colour")
return QColor(0xF0, 0xF0, 0xF0);
if(name == "formatted_bg_colour")
return QColor(0x19, 0x23, 0x2D);
if(name == "bin_fg_colour")
return QColor(0x78, 0x78, 0x78);
if(name == "bin_bg_colour")
return QColor(0x19, 0x23, 0x2D);
break;
case LightStyle :
if(name == "null_fg_colour" || name == "bin_fg_colour")
return QColor(0xA5, 0xA9, 0xAC);
if(name == "null_bg_colour" || name == "bin_bg_colour")
return QColor(0xFA, 0xFA, 0xFA);
if(name == "reg_fg_colour")
return QColor(0x00, 0x00, 0x00);
if(name == "reg_bg_colour")
return QColor(0xFA, 0xFA, 0xFA);
if(name == "formatted_fg_colour")
return QColor(0x00, 0x00, 0x00);
if(name == "formatted_bg_colour")
return QColor(0xFA, 0xFA, 0xFA);
break;
}
}
// syntaxhighlighter?
if(group == "syntaxhighlighter")
{
// Colour?
if(ends_with(name, "colour"))
{
QColor backgroundColour;
QColor foregroundColour;
switch (style) {
case FollowDesktopStyle :
backgroundColour = QPalette().color(QPalette::Active, QPalette::Base);
foregroundColour = QPalette().color(QPalette::Active, QPalette::Text);
break;
case DarkStyle :
foregroundColour = QColor(0xF0, 0xF0, 0xF0);
backgroundColour = QColor(0x19, 0x23, 0x2D);
break;
case LightStyle :
foregroundColour = QColor(0x00, 0x00, 0x00);
backgroundColour = QColor(0xFA, 0xFA, 0xFA);
break;
}
if(name == "foreground_colour")
return foregroundColour;
else if(name == "background_colour")
return backgroundColour;
else if(name == "selected_fg_colour")
return QPalette().color(QPalette::Active, QPalette::HighlightedText);
else if(name == "selected_bg_colour")
return QPalette().color(QPalette::Active, QPalette::Highlight);
// Detect and provide sensible defaults for dark themes
if (backgroundColour.value() < foregroundColour.value()) {
if(name == "keyword_colour")
return QColor(82, 148, 226);
else if(name == "function_colour")
return QColor(Qt::yellow);
else if(name == "table_colour")
return QColor(Qt::cyan);
else if(name == "comment_colour")
return QColor(Qt::green);
else if(name == "identifier_colour")
return QColor(221, 160, 221);
else if(name == "string_colour")
return QColor(Qt::lightGray);
else if(name == "currentline_colour")
return backgroundColour.lighter(150);
else if(name == "highlight_colour")
return QColor(79, 148, 205);
} else {
if(name == "keyword_colour")
return QColor(Qt::darkBlue);
else if(name == "function_colour")
return QColor(Qt::blue);
else if(name == "table_colour")
return QColor(Qt::darkCyan);
else if(name == "comment_colour")
return QColor(Qt::darkGreen);
else if(name == "identifier_colour")
return QColor(Qt::darkMagenta);
else if(name == "string_colour")
return QColor(Qt::red);
else if(name == "currentline_colour")
return QColor(236, 236, 245);
else if(name == "highlight_colour")
return QColor(Qt::cyan);
}
}
}
// Unknown combination of group and name? Return an invalid QColor!
return QColor();
}
void Settings::clearValue(const std::string& group, const std::string& name)
{
setSettingsObject();
settings->beginGroup(QString::fromStdString(group));
settings->remove(QString::fromStdString(name));
settings->endGroup();
m_hCache.clear();
}
void Settings::restoreDefaults ()
{
setSettingsObject();
settings->clear();
m_hCache.clear();
}
void Settings::exportSettings(const QString& fileName)
{
QSettings exportSettings(fileName, QSettings::IniFormat);
const QStringList groups = settings->childGroups();
for(const QString& currentGroup : groups)
{
settings->beginGroup(currentGroup);
const QStringList keys = settings->childKeys();
for(const QString& currentKey : keys)
{
exportSettings.beginGroup(currentGroup);
exportSettings.setValue(currentKey, getValue((currentGroup.toStdString()), (currentKey.toStdString())));
exportSettings.endGroup();
}
settings->endGroup();
}
}
bool Settings::importSettings(const QString& fileName)
{
if(!isVaildSettingsFile(fileName))
return false;
QSettings importSettings(fileName, QSettings::IniFormat);
const QStringList groups = importSettings.childGroups();
for(const QString& currentGroup : groups)
{
importSettings.beginGroup(currentGroup);
const QStringList keys = importSettings.childKeys();
for(const QString& currentKey : keys)
{
settings->beginGroup(currentGroup);
settings->setValue(currentKey, importSettings.value(currentKey));
settings->endGroup();
}
importSettings.endGroup();
}
m_hCache.clear();
return true;
}
void Settings::sync()
{
settings->sync();
}