forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferencesDialog.cpp
234 lines (196 loc) · 8.61 KB
/
PreferencesDialog.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
#include "PreferencesDialog.h"
#include "ui_PreferencesDialog.h"
#include "sqlitedb.h"
#include <QDir>
#include <QSettings>
#include <QFileDialog>
#include <QColorDialog>
QHash<QString, QVariant> PreferencesDialog::m_hCache;
PreferencesDialog::PreferencesDialog(QWidget* parent)
: QDialog(parent),
ui(new Ui::PreferencesDialog)
{
ui->setupUi(this);
ui->treeSyntaxHighlighting->setColumnHidden(0, true);
loadSettings();
}
/*
* Destroys the object and frees any allocated resources
*/
PreferencesDialog::~PreferencesDialog()
{
delete ui;
}
void PreferencesDialog::chooseLocation()
{
QString s = QFileDialog::getExistingDirectory(
this,
tr("Choose a directory"),
getSettingsValue("db", "defaultlocation").toString(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if(!s.isEmpty())
ui->locationEdit->setText(s);
}
void PreferencesDialog::loadSettings()
{
ui->encodingComboBox->setCurrentIndex(ui->encodingComboBox->findText(getSettingsValue("db", "defaultencoding").toString(), Qt::MatchFixedString));
ui->locationEdit->setText(getSettingsValue("db", "defaultlocation").toString());
ui->foreignKeysCheckBox->setChecked(getSettingsValue("db", "foreignkeys").toBool());
ui->spinPrefetchSize->setValue(getSettingsValue("db", "prefetchsize").toInt());
for(int i=0; i < ui->treeSyntaxHighlighting->topLevelItemCount(); ++i)
{
QString name = ui->treeSyntaxHighlighting->topLevelItem(i)->text(0);
QString colorname = getSettingsValue("syntaxhighlighter", name + "_colour").toString();
QColor color = QColor(colorname);
ui->treeSyntaxHighlighting->topLevelItem(i)->setTextColor(2, color);
ui->treeSyntaxHighlighting->topLevelItem(i)->setBackgroundColor(2, color);
ui->treeSyntaxHighlighting->topLevelItem(i)->setText(2, colorname);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(3, getSettingsValue("syntaxhighlighter", name + "_bold").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(4, getSettingsValue("syntaxhighlighter", name + "_italic").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(5, getSettingsValue("syntaxhighlighter", name + "_underline").toBool() ? Qt::Checked : Qt::Unchecked);
}
ui->spinEditorFontSize->setValue(getSettingsValue("editor", "fontsize").toInt());
ui->spinLogFontSize->setValue(getSettingsValue("log", "fontsize").toInt());
ui->listExtensions->addItems(getSettingsValue("extensions", "list").toStringList());
}
void PreferencesDialog::saveSettings()
{
setSettingsValue("db", "defaultencoding", ui->encodingComboBox->currentText());
setSettingsValue("db", "defaultlocation", ui->locationEdit->text());
setSettingsValue("db", "foreignkeys", ui->foreignKeysCheckBox->isChecked());
setSettingsValue("db", "prefetchsize", ui->spinPrefetchSize->value());
for(int i=0; i < ui->treeSyntaxHighlighting->topLevelItemCount(); ++i)
{
QString name = ui->treeSyntaxHighlighting->topLevelItem(i)->text(0);
setSettingsValue("syntaxhighlighter", name + "_colour", ui->treeSyntaxHighlighting->topLevelItem(i)->text(2));
setSettingsValue("syntaxhighlighter", name + "_bold", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(3) == Qt::Checked);
setSettingsValue("syntaxhighlighter", name + "_italic", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(4) == Qt::Checked);
setSettingsValue("syntaxhighlighter", name + "_underline", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(5) == Qt::Checked);
}
setSettingsValue("editor", "fontsize", ui->spinEditorFontSize->value());
setSettingsValue("log", "fontsize", ui->spinLogFontSize->value());
QStringList extList;
foreach(QListWidgetItem* item, ui->listExtensions->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard))
extList.append(item->text());
setSettingsValue("extensions", "list", extList);
accept();
}
QVariant PreferencesDialog::getSettingsValue(const QString& group, const QString& name)
{
// Have a look in the cache first
QHash<QString, QVariant>::iterator cacheIndex = m_hCache.find(group + name);
if(cacheIndex != m_hCache.end())
{
return cacheIndex.value();
} 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
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
QVariant value = settings.value(group + "/" + name, getSettingsDefaultValue(group, name));
// Store this value in the cache for further usage and return it afterwards
m_hCache.insert(group + name, value);
return value;
}
}
void PreferencesDialog::setSettingsValue(const QString& group, const QString& name, const QVariant& value)
{
// Set the group and save the given value
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
settings.beginGroup(group);
settings.setValue(name, value);
settings.endGroup();
// Also change it in the cache
m_hCache[group + name] = value;
}
QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const QString& name)
{
// db/defaultencoding?
if(group == "db" && name == "defaultencoding")
return "UTF-8";
// db/defaultlocation?
if(group == "db" && name == "defaultlocation")
return QDir::homePath();
// db/foreignkeys?
if(group == "db" && name == "foreignkeys")
return true;
// db/prefetchsize?
if(group == "db" && name == "prefetchsize")
return 50000;
// MainWindow/geometry?
if(group == "MainWindow" && name == "geometry")
return "";
// MainWindow/windowState?
if(group == "MainWindow" && name == "windowState")
return "";
// SQLLogDock/Log?
if(group == "SQLLogDock" && name == "Log")
return "Application";
// General/recentFileList?
if(group == "General" && name == "recentFileList")
return QStringList();
// syntaxhighlighter?
if(group == "syntaxhighlighter")
{
// Bold? Only tables, functions and keywords are bold by default
if(name.right(4) == "bold")
return name == "keyword_bold" || name == "table_bold" || name == "function_bold";
// Italic? Nothing by default
if(name.right(6) == "italic")
return false;
// Underline? Nothing by default
if(name.right(9) == "underline")
return false;
// Colour?
if(name.right(6) == "colour")
{
if(name == "keyword_colour")
return QColor(Qt::darkBlue).name();
else if(name == "function_colour")
return QColor(Qt::blue).name();
else if(name == "table_colour")
return QColor(Qt::darkCyan).name();
else if(name == "comment_colour")
return QColor(Qt::darkGreen).name();
else if(name == "identifier_colour")
return QColor(Qt::darkMagenta).name();
else if(name == "string_colour")
return QColor(Qt::red).name();
else if(name == "currentline_colour")
return QColor(236, 236, 245).name();
}
}
// editor/fontsize or log/fontsize?
if((group == "editor" || group == "log") && name == "fontsize")
return 9;
// extensions/list?
if(group == "extensions" && name == "list")
return QStringList();
// Unknown combination of group and name? Return an invalid QVariant!
return QVariant();
}
void PreferencesDialog::showColourDialog(QTreeWidgetItem* item, int column)
{
if(item->text(column).left(1) != "#")
return;
QColor colour = QColorDialog::getColor(QColor(item->text(column)), this);
if(colour.isValid())
{
item->setTextColor(column, colour);
item->setBackgroundColor(column, colour);
item->setText(column, colour.name());
}
}
void PreferencesDialog::addExtension()
{
QString file = QFileDialog::getOpenFileName(
this,
tr("Select extension file"),
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
tr("Extensions(*.so *.dll);;All files(*)"));
if(QFile::exists(file))
ui->listExtensions->addItem(file);
}
void PreferencesDialog::removeExtension()
{
if(ui->listExtensions->currentIndex().isValid())
ui->listExtensions->takeItem(ui->listExtensions->currentIndex().row());
}