-
Notifications
You must be signed in to change notification settings - Fork 28
/
Preferences.cpp
161 lines (136 loc) · 4.95 KB
/
Preferences.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
/************************************************************************
**
** Copyright (C) 2016-2024 Kevin B. Hendricks, Stratford, ON
** Copyright (C) 2016-2024 Doug Massay
** Copyright (C) 2011 John Schember <john@nachtimwald.com>
**
** This file is part of PageEdit.
**
** PageEdit 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.
**
** PageEdit 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 PageEdit. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include <QDir>
#include <QUrl>
#include <QDesktopServices>
#include <QMessageBox>
#include <QScrollArea>
#include <QWidget>
#include "Preferences.h"
#include "SettingsStore.h"
#include "Utility.h"
#include "PreferencesWidget.h"
#include "AppearanceWidget.h"
#include "GeneralSettings.h"
static const QString SETTINGS_GROUP = "preferences_dialog";
Preferences::Preferences(QWidget *parent) :
QDialog(parent),
m_restartPageEdit(false),
m_reloadPreview(false)
{
ui.setupUi(this);
extendUI();
// Create and load all of our preference widgets.;
appendPreferenceWidget(new AppearanceWidget);
appendPreferenceWidget(new GeneralSettings);
connectSignalsSlots();
QApplication::setOverrideCursor(Qt::WaitCursor);
readSettings();
QApplication::restoreOverrideCursor();
}
void Preferences::selectPWidget(QListWidgetItem *current, QListWidgetItem *previous)
{
Q_UNUSED(previous)
int index = ui.availableWidgets->row(current);
ui.pWidget->setCurrentIndex(index);
}
void Preferences::saveSettings()
{
PreferencesWidget::ResultActions widgetResult;
SettingsStore settings;
settings.beginGroup(SETTINGS_GROUP);
QApplication::setOverrideCursor(Qt::WaitCursor);
settings.setValue("geometry", saveGeometry());
settings.setValue("lastpreference", ui.availableWidgets->currentRow());
for (int i = 0; i < ui.pWidget->count(); ++i) {
PreferencesWidget *pw = qobject_cast<PreferencesWidget *>(ui.pWidget->widget(i));
if (pw != 0) {
widgetResult = pw->saveSettings();
if (widgetResult & PreferencesWidget::ResultAction_RestartPageEdit) {
m_restartPageEdit = true;
}
if (widgetResult & PreferencesWidget::ResultAction_ReloadPreview) {
m_reloadPreview = true;
}
}
}
QApplication::restoreOverrideCursor();
settings.endGroup();
if (m_restartPageEdit) {
Utility::warning(this, tr("PageEdit"), tr("Changes will take effect when you restart PageEdit."));
}
}
void Preferences::makeActive(int index)
{
if ((index > -1) || (index < ui.availableWidgets->count())) {
ui.availableWidgets->setCurrentRow(index);
}
}
void Preferences::readSettings()
{
SettingsStore settings;
settings.beginGroup(SETTINGS_GROUP);
QByteArray geometry = settings.value("geometry").toByteArray();
if (!geometry.isNull()) {
restoreGeometry(geometry);
}
// Ensure the previous item selected in the available preferences widgets list
// is highlighted.
int last_preference_index = settings.value("lastpreference", 0).toInt();
if (last_preference_index > ui.availableWidgets->count() - 1) {
last_preference_index = 0;
}
ui.availableWidgets->setCurrentRow(last_preference_index);
settings.endGroup();
}
void Preferences::appendPreferenceWidget(PreferencesWidget *widget)
{
// Add the PreferencesWidget to the stack view area.
ui.pWidget->addWidget(widget);
// Add an entry to the list of available preference widgets.
ui.availableWidgets->addItem(widget->windowTitle());
}
bool Preferences::isRestartRequired()
{
return m_restartPageEdit;
}
bool Preferences::isReloadPreviewRequired()
{
return m_reloadPreview;
}
void Preferences::openPreferencesLocation()
{
QUrl location = QUrl::fromLocalFile(Utility::DefinePrefsDir());
QDesktopServices::openUrl(location);
}
void Preferences::extendUI()
{
QPushButton *open_button = ui.buttonBox->addButton(tr("Open Preferences Location"), QDialogButtonBox::ResetRole);
open_button->setToolTip(QDir::toNativeSeparators(Utility::DefinePrefsDir()));
connect(open_button, SIGNAL(clicked()), this, SLOT(openPreferencesLocation()));
}
void Preferences::connectSignalsSlots()
{
connect(ui.availableWidgets, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(selectPWidget(QListWidgetItem *, QListWidgetItem *)));
connect(this, SIGNAL(finished(int)), this, SLOT(saveSettings()));
}