-
Notifications
You must be signed in to change notification settings - Fork 1
/
optionsdialog.cpp
434 lines (380 loc) · 16 KB
/
optionsdialog.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
/****************************************************************************
**
** Copyright (C) 2011 Alexander Vos <[email protected]>,
** Till Althaus <[email protected]>
**
** This file is part of Qt Slideshow Manager (QSM).
**
** QSM is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** QSM is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with QSM. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "optionsdialog.h"
#include "ui_optionsdialog.h"
#include <QDir>
#include <QFontDialog>
#include <QFileDialog>
#include <QKeyEvent>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
const QLocale::Language OptionsDialog::DEFAULT_LANGUAGE = QLocale::C;
const QColor OptionsDialog::DEFAULT_BACKGROUND_COLOR = Qt::black;
const QColor OptionsDialog::DEFAULT_TEXT_BACKGROUND_COLOR = QColor(0, 0, 0, 127);
const QFont OptionsDialog::DEFAULT_TEXT_FONT;
const QColor OptionsDialog::DEFAULT_TEXT_COLOR = Qt::white;
const QString OptionsDialog::DEFAULT_APPLICATION_DIRECTORY = QDir::homePath() + "/.qsm";
const QString OptionsDialog::DEFAULT_SLIDESHOWS_DIRECTORY = OptionsDialog::DEFAULT_APPLICATION_DIRECTORY + "/slideshows";
const QString OptionsDialog::DEFAULT_IMAGES_DIRECTORY = OptionsDialog::DEFAULT_APPLICATION_DIRECTORY + "/images";
QHash<QAction*, QKeySequence> OptionsDialog::DefaultShortcuts;
void OptionsDialog::loadAllShortcuts(const QSettings &settings)
{
foreach (QAction *action, OptionsDialog::DefaultShortcuts.keys()) {
action->setShortcut(settings.value("shortcuts/" + action->objectName(),
OptionsDialog::DefaultShortcuts.value(action)).value<QKeySequence>());
}
}
bool OptionsDialog::checkDirectory(const QString &directory, bool create, bool warningOnFailure)
{
QDir dir(directory);
if (dir.exists())
return true;
else if (create) {
bool ok = dir.mkpath(directory);
if (!ok && warningOnFailure)
QMessageBox::warning(NULL, tr("Failed to create directory"),
tr("Could not create directory \"%1\".").arg(directory));
return ok;
} else
return false;
}
OptionsDialog::OptionsDialog(QList<QPair<QString, QList<QAction*> > > *actions, QWidget *parent) :
QDialog(parent),
ui(new Ui::OptionsDialog),
m_actions(actions)
{
Q_ASSERT(actions);
ui->setupUi(this);
connect(ui->slideshowsDirectoryLineEdit, SIGNAL(textChanged(QString)), SLOT(directoryLineEdit_textChanged(QString)));
connect(ui->imagesDirectoryLineEdit, SIGNAL(textChanged(QString)), SLOT(directoryLineEdit_textChanged(QString)));
loadSettings();
// set shortcuts
QList<QTreeWidgetItem*> items;
static const QString windowContext(tr("Window Context"));
static const QString widgetContext(tr("Widget Context"));
for (int i = 0; i < actions->count(); ++i) {
const QPair<QString, QList<QAction*> > actionGroup = actions->at(i);
QTreeWidgetItem *itemGroup = new QTreeWidgetItem(ui->shortcutsTreeWidget, QStringList(actionGroup.first));
QFont font = itemGroup->font(0);
font.setBold(true);
itemGroup->setFont(0, font);
items.append(itemGroup);
for (int j = 0; j < actionGroup.second.count(); ++j) {
const QAction *action = actionGroup.second.at(j);
QStringList itemText(action->text().replace('&', ""));
itemText.append(action->toolTip());
itemText.append(action->shortcut().toString(QKeySequence::NativeText));
QTreeWidgetItem *item = new QTreeWidgetItem(itemGroup, itemText);
item->setIcon(0, action->icon());
item->setToolTip(2, action->shortcutContext() == Qt::WindowShortcut ? windowContext : widgetContext);
items.append(item);
}
}
ui->shortcutsTreeWidget->addTopLevelItems(items);
ui->shortcutsTreeWidget->expandAll();
checkShortcuts();
ui->keyLineEdit->installEventFilter(this);
}
void OptionsDialog::loadSettings()
{
QSettings settings;
QLocale::Language language = (QLocale::Language)settings.value("language", (int)OptionsDialog::DEFAULT_LANGUAGE).toInt();
switch (language) {
case QLocale::English:
ui->languageComboBox->setCurrentIndex(1);
break;
case QLocale::German:
ui->languageComboBox->setCurrentIndex(2);
break;
default:
ui->languageComboBox->setCurrentIndex(0);
}
QColor backgroundColor = settings.value("backgroundColor", OptionsDialog::DEFAULT_BACKGROUND_COLOR).value<QColor>();
QPalette backgroundColorPalette = ui->backgroundColorPushButton->palette();
backgroundColorPalette.setColor(QPalette::Button, backgroundColor);
ui->backgroundColorPushButton->setPalette(backgroundColorPalette);
QColor textBackgroundColor = settings.value("textBackgroundColor", OptionsDialog::DEFAULT_TEXT_BACKGROUND_COLOR).value<QColor>();
QPalette textBackgroundColorPalette = ui->textBackgroundColorPushButton->palette();
textBackgroundColorPalette.setColor(QPalette::Button, textBackgroundColor);
ui->textBackgroundColorPushButton->setPalette(textBackgroundColorPalette);
QFont textFont = settings.value("textFont", OptionsDialog::DEFAULT_TEXT_FONT).value<QFont>();
ui->textFontPushButton->setFont(textFont);
QColor textColor = settings.value("textColor", OptionsDialog::DEFAULT_TEXT_COLOR).value<QColor>();
QPalette textColorPalette = ui->textColorPushButton->palette();
textColorPalette.setColor(QPalette::Button, textColor);
ui->textColorPushButton->setPalette(textColorPalette);
QString slideshowsDirectory = settings.value("slideshowsDirectory", OptionsDialog::DEFAULT_SLIDESHOWS_DIRECTORY).toString();
ui->slideshowsDirectoryLineEdit->setText(QDir::toNativeSeparators(slideshowsDirectory));
QString imagesDirectory = settings.value("imagesDirectory", OptionsDialog::DEFAULT_IMAGES_DIRECTORY).toString();
ui->imagesDirectoryLineEdit->setText(QDir::toNativeSeparators(imagesDirectory));
}
void OptionsDialog::saveSettings()
{
QSettings settings;
QLocale::Language lang = language();
if (lang != (QLocale::Language)settings.value("language", (int)OptionsDialog::DEFAULT_LANGUAGE).toInt()) {
if (lang == OptionsDialog::DEFAULT_LANGUAGE)
settings.remove("language");
else
settings.setValue("language", (int)lang);
QMessageBox::information(this, tr("Restart required"),
tr("The language change will take effect after a restart of Qt Slideshow Manager."));
}
settings.setValue("backgroundColor", backgroundColor());
settings.setValue("textBackgroundColor", textBackgroundColor());
settings.setValue("textFont", textFont());
settings.setValue("textColor", textColor());
settings.setValue("slideshowsDirectory", slideshowsDirectory());
settings.setValue("imagesDirectory", imagesDirectory());
settings.beginGroup("shortcuts");
for (int i = 0; i < m_actions->count(); ++i) {
const QTreeWidgetItem *shortcutGroup = ui->shortcutsTreeWidget->topLevelItem(i);
QPair<QString, QList<QAction*> > actionGroup = m_actions->at(i);
for (int j = 0; j < actionGroup.second.count(); ++j) {
const QTreeWidgetItem *shortcutItem = shortcutGroup->child(j);
QAction *action = actionGroup.second.at(j);
action->setShortcut(shortcutItem->text(2));
settings.setValue(action->objectName(), action->shortcut());
}
}
settings.endGroup();
}
OptionsDialog::~OptionsDialog()
{
delete ui;
}
void OptionsDialog::selectColor(QPushButton *pushButton, QColorDialog::ColorDialogOptions options)
{
Q_ASSERT(pushButton);
QPalette palette = pushButton->palette();
QColor color = QColorDialog::getColor(palette.button().color(), this, QString(), options);
if (color.isValid()) {
palette.setColor(QPalette::Button, color);
pushButton->setPalette(palette);
}
}
void OptionsDialog::on_backgroundColorPushButton_clicked()
{
selectColor(ui->backgroundColorPushButton);
}
void OptionsDialog::on_backgroundColorResetPushButton_clicked()
{
QPalette palette = ui->backgroundColorPushButton->palette();
palette.setColor(QPalette::Button, OptionsDialog::DEFAULT_BACKGROUND_COLOR);
ui->backgroundColorPushButton->setPalette(palette);
}
void OptionsDialog::on_textBackgroundColorPushButton_clicked()
{
selectColor(ui->textBackgroundColorPushButton, QColorDialog::ShowAlphaChannel);
}
void OptionsDialog::on_textBackgroundColorResetPushButton_clicked()
{
QPalette palette = ui->textBackgroundColorPushButton->palette();
palette.setColor(QPalette::Button, OptionsDialog::DEFAULT_TEXT_BACKGROUND_COLOR);
ui->textBackgroundColorPushButton->setPalette(palette);
}
void OptionsDialog::on_textFontPushButton_clicked()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, ui->textFontPushButton->font(), this);
if (ok)
ui->textFontPushButton->setFont(font);
}
void OptionsDialog::on_textFontResetPushButton_clicked()
{
ui->textFontPushButton->setFont(OptionsDialog::DEFAULT_TEXT_FONT);
}
void OptionsDialog::on_textColorPushButton_clicked()
{
selectColor(ui->textColorPushButton);
}
void OptionsDialog::on_textColorResetPushButton_clicked()
{
QPalette palette = ui->textColorPushButton->palette();
palette.setColor(QPalette::Button, OptionsDialog::DEFAULT_TEXT_COLOR);
ui->textColorPushButton->setPalette(palette);
}
void OptionsDialog::directoryLineEdit_textChanged(const QString &text)
{
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender());
if (!lineEdit) return;
bool dirExists = QDir(text).exists();
QPalette palette = lineEdit->palette();
palette.setColor(QPalette::Text, dirExists ? Qt::black : Qt::red);
lineEdit->setPalette(palette);
}
void OptionsDialog::chooseDirectory(QLineEdit *lineEdit, const QString &defaultDirectory)
{
Q_ASSERT(lineEdit);
QDir currentDir(lineEdit->text());
if (!currentDir.exists())
currentDir = defaultDirectory;
QString dir = QFileDialog::getExistingDirectory(this, QString(), currentDir.path());
if (!dir.isEmpty())
lineEdit->setText(dir);
}
void OptionsDialog::on_openSlideshowsDirectoryPushButton_clicked()
{
QDesktopServices::openUrl(QUrl("file:///" + slideshowsDirectory(), QUrl::TolerantMode));
}
void OptionsDialog::on_slideshowsDirectoryPushButton_clicked()
{
chooseDirectory(ui->slideshowsDirectoryLineEdit, OptionsDialog::DEFAULT_SLIDESHOWS_DIRECTORY);
}
void OptionsDialog::on_openImagesDirectoryPushButton_clicked()
{
QDesktopServices::openUrl(QUrl("file:///" + imagesDirectory(), QUrl::TolerantMode));
}
void OptionsDialog::on_imagesDirectoryPushButton_clicked()
{
chooseDirectory(ui->imagesDirectoryLineEdit, OptionsDialog::DEFAULT_IMAGES_DIRECTORY);
}
void OptionsDialog::on_shortcutsTreeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
{
ui->shortcutGroupBox->setEnabled(current->parent());
ui->keyLineEdit->setText(current->text(2));
}
void OptionsDialog::on_resetAllPushButton_clicked()
{
for (int i = 0; i < ui->shortcutsTreeWidget->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = ui->shortcutsTreeWidget->topLevelItem(i);
for (int j = 0; j < item->childCount(); ++j) {
QAction *action = m_actions->at(i).second.at(j);
QString keySequence = OptionsDialog::DefaultShortcuts.value(action).toString(QKeySequence::NativeText);
item->child(j)->setText(2, keySequence);
}
}
QTreeWidgetItem *item = ui->shortcutsTreeWidget->currentItem();
if (item && item->parent())
ui->keyLineEdit->setText(item->text(2));
checkShortcuts();
}
void OptionsDialog::on_resetPushButton_clicked()
{
QTreeWidgetItem *item = ui->shortcutsTreeWidget->currentItem();
if (item && item->parent()) {
QModelIndex index = ui->shortcutsTreeWidget->currentIndex();
QAction *action = m_actions->at(index.parent().row()).second.at(index.row());
QString keySequence = OptionsDialog::DefaultShortcuts.value(action).toString(QKeySequence::NativeText);
item->setText(2, keySequence);
ui->keyLineEdit->setText(keySequence);
checkShortcuts();
}
}
void OptionsDialog::on_removePushButton_clicked()
{
QTreeWidgetItem *item = ui->shortcutsTreeWidget->currentItem();
if (item && item->parent()) {
item->setText(2, "");
ui->keyLineEdit->clear();
checkShortcuts();
}
}
bool OptionsDialog::checkShortcuts()
{
bool multipleUsage = false;
for (int i = 0; i < ui->shortcutsTreeWidget->topLevelItemCount(); ++i) {
QTreeWidgetItem *shortcutGroup = ui->shortcutsTreeWidget->topLevelItem(i);
for (int j = 0; j < shortcutGroup->childCount(); ++j) {
QTreeWidgetItem *shortcutItem = shortcutGroup->child(j);
shortcutItem->setForeground(2, Qt::black); // reset color
for (int k = 0; k < ui->shortcutsTreeWidget->topLevelItemCount(); ++k) {
QTreeWidgetItem *shortcutGroup2 = ui->shortcutsTreeWidget->topLevelItem(k);
for (int l = 0; l < shortcutGroup2->childCount(); ++l) {
QTreeWidgetItem *shortcutItem2 = shortcutGroup2->child(l);
if (shortcutItem != shortcutItem2 && shortcutItem->text(2) == shortcutItem2->text(2)) {
shortcutItem->setForeground(2, Qt::red);
multipleUsage = true;
}
}
}
}
}
return multipleUsage;
}
bool OptionsDialog::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->keyLineEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
QModelIndex index = ui->shortcutsTreeWidget->currentIndex();
if (index.parent().isValid()) {
int key = keyEvent->key();
if (key < Qt::Key_Shift || key > Qt::Key_ScrollLock) {
QKeySequence keySequence(key + keyEvent->modifiers());
QString keySequenceString = keySequence.toString(QKeySequence::NativeText);
ui->shortcutsTreeWidget->topLevelItem(index.parent().row())->child(index.row())->setText(2, keySequenceString);
ui->keyLineEdit->setText(keySequenceString);
checkShortcuts();
}
}
return true;
} else
return false;
} else
return QDialog::eventFilter(obj, event);
}
QLocale::Language OptionsDialog::language() const
{
QLocale::Language language = OptionsDialog::DEFAULT_LANGUAGE;
switch (ui->languageComboBox->currentIndex()) {
case 1:
language = QLocale::English;
break;
case 2:
language = QLocale::German;
break;
}
return language;
}
QColor OptionsDialog::backgroundColor() const
{
return ui->backgroundColorPushButton->palette().button().color();
}
QColor OptionsDialog::textBackgroundColor() const
{
return ui->textBackgroundColorPushButton->palette().button().color();
}
QFont OptionsDialog::textFont() const
{
return ui->textFontPushButton->font();
}
QColor OptionsDialog::textColor() const
{
return ui->textColorPushButton->palette().button().color();
}
QString OptionsDialog::slideshowsDirectory() const
{
return ui->slideshowsDirectoryLineEdit->text();
}
QString OptionsDialog::imagesDirectory() const
{
return ui->imagesDirectoryLineEdit->text();
}
void OptionsDialog::on_buttonBox_accepted()
{
if (OptionsDialog::checkDirectory(slideshowsDirectory()) && OptionsDialog::checkDirectory(imagesDirectory())) {
saveSettings();
accept();
}
}