-
-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathPreferencesDialog.cpp
More file actions
207 lines (170 loc) · 9.88 KB
/
PreferencesDialog.cpp
File metadata and controls
207 lines (170 loc) · 9.88 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
/*
* This file is part of Notepad Next.
* Copyright 2019 Justin Dailey
*
* Notepad Next 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.
*
* Notepad Next 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 Notepad Next. If not, see <https://www.gnu.org/licenses/>.
*/
#include "PreferencesDialog.h"
#include "NotepadNextApplication.h"
#include "TranslationManager.h"
#include "ui_PreferencesDialog.h"
#include "ScintillaNext.h"
#include <QButtonGroup>
#include <QFileDialog>
#include <QMessageBox>
PreferencesDialog::PreferencesDialog(ApplicationSettings *settings, QWidget *parent) :
QDialog(parent, Qt::Tool),
ui(new Ui::PreferencesDialog),
settings(settings)
{
ui->setupUi(this);
QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxInformation);
QPixmap pixmap = icon.pixmap(QSize(16, 16));
ui->labelAppRestartIcon->setPixmap(pixmap);
ui->labelAppRestartIcon->hide();
ui->labelAppRestart->hide();
MapSettingToCheckBox(ui->checkBoxMenuBar, &ApplicationSettings::showMenuBar, &ApplicationSettings::setShowMenuBar, &ApplicationSettings::showMenuBarChanged);
MapSettingToCheckBox(ui->checkBoxToolBar, &ApplicationSettings::showToolBar, &ApplicationSettings::setShowToolBar, &ApplicationSettings::showToolBarChanged);
MapSettingToCheckBox(ui->checkBoxStatusBar, &ApplicationSettings::showStatusBar, &ApplicationSettings::setShowStatusBar, &ApplicationSettings::showStatusBarChanged);
ui->comboBoxTheme->setCurrentIndex(static_cast<int>(settings->theme()));
connect(ui->comboBoxTheme, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
settings->setTheme(static_cast<ApplicationSettings::ThemeMode>(index));
});
connect(settings, &ApplicationSettings::themeChanged, this, [=](ApplicationSettings::ThemeMode mode) {
ui->comboBoxTheme->setCurrentIndex(static_cast<int>(mode));
});
MapSettingToCheckBox(ui->checkBoxRecenterSearchDialog, &ApplicationSettings::centerSearchDialog, &ApplicationSettings::setCenterSearchDialog, &ApplicationSettings::centerSearchDialogChanged);
MapSettingToGroupBox(ui->gbxRestorePreviousSession, &ApplicationSettings::restorePreviousSession, &ApplicationSettings::setRestorePreviousSession, &ApplicationSettings::restorePreviousSessionChanged);
connect(ui->gbxRestorePreviousSession, &QGroupBox::toggled, this, [=](bool checked) {
if (!checked) {
ui->checkBoxUnsavedFiles->setChecked(false);
ui->checkBoxRestoreTempFiles->setChecked(false);
}
else {
QMessageBox::warning(this, tr("Warning"), tr("This feature is experimental and it should not be considered safe for critically important work. It may lead to possible data loss. Use at your own risk."));
}
});
MapSettingToCheckBox(ui->checkBoxUnsavedFiles, &ApplicationSettings::restoreUnsavedFiles, &ApplicationSettings::setRestoreUnsavedFiles, &ApplicationSettings::restoreUnsavedFilesChanged);
MapSettingToCheckBox(ui->checkBoxRestoreTempFiles, &ApplicationSettings::restoreTempFiles, &ApplicationSettings::setRestoreTempFiles, &ApplicationSettings::restoreTempFilesChanged);
MapSettingToCheckBox(ui->checkBoxCombineSearchResults, &ApplicationSettings::combineSearchResults, &ApplicationSettings::setCombineSearchResults, &ApplicationSettings::combineSearchResultsChanged);
populateTranslationComboBox();
connect(ui->comboBoxTranslation, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
settings->setTranslation(ui->comboBoxTranslation->itemData(index).toString());
showApplicationRestartRequired();
});
MapSettingToCheckBox(ui->checkBoxExitOnLastTabClosed, &ApplicationSettings::exitOnLastTabClosed, &ApplicationSettings::setExitOnLastTabClosed, &ApplicationSettings::exitOnLastTabClosedChanged);
ui->fcbDefaultFont->setCurrentFont(QFont(settings->fontName()));
connect(ui->fcbDefaultFont, &QFontComboBox::currentFontChanged, this, [=](const QFont &f) {
settings->setFontName(f.family());
});
connect(settings, &ApplicationSettings::fontNameChanged, this, [=](QString fontName){
ui->fcbDefaultFont->setCurrentFont(QFont(fontName));
});
ui->spbDefaultFontSize->setValue(settings->fontSize());
connect(ui->spbDefaultFontSize, QOverload<int>::of(&QSpinBox::valueChanged), settings, &ApplicationSettings::setFontSize);
connect(settings, &ApplicationSettings::fontSizeChanged, ui->spbDefaultFontSize, &QSpinBox::setValue);
ui->comboBoxLineEndings->addItem(tr("System Default"), QString(""));
ui->comboBoxLineEndings->addItem(tr("Windows (CR LF)"), ScintillaNext::eolModeToString(SC_EOL_CRLF));
ui->comboBoxLineEndings->addItem(tr("Linux (LF)"), ScintillaNext::eolModeToString(SC_EOL_LF));
ui->comboBoxLineEndings->addItem(tr("Macintosh (CR)"), ScintillaNext::eolModeToString(SC_EOL_CR));
// Select the current one
int index = ui->comboBoxLineEndings->findData(settings->defaultEOLMode());
ui->comboBoxLineEndings->setCurrentIndex(index == -1 ? 0 : index);
connect(ui->comboBoxLineEndings, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
settings->setDefaultEOLMode(ui->comboBoxLineEndings->itemData(index).toString());
});
connect(settings, &ApplicationSettings::defaultEOLModeChanged, this, [=](QString defaultEOLMode) {
int index = ui->comboBoxLineEndings->findData(defaultEOLMode);
ui->comboBoxLineEndings->setCurrentIndex(index == -1 ? 0 : index);
});
MapSettingToCheckBox(ui->checkBoxHighlightURLs, &ApplicationSettings::urlHighlighting, &ApplicationSettings::setURLHighlighting, &ApplicationSettings::urlHighlightingChanged);
MapSettingToCheckBox(ui->checkBoxShowLineNumbers, &ApplicationSettings::showLineNumbers, &ApplicationSettings::setShowLineNumbers, &ApplicationSettings::showLineNumbersChanged);
QButtonGroup *buttonGroup = new QButtonGroup(this);
buttonGroup->addButton(ui->radioFollowCurrentDirectory, ApplicationSettings::FollowCurrentDocument);
buttonGroup->addButton(ui->radioLastUsedDirectory, ApplicationSettings::RememberLastUsed);
buttonGroup->addButton(ui->radioHardCoded, ApplicationSettings::HardCoded);
connect(buttonGroup, &QButtonGroup::idClicked, this, [=](int id) {
ApplicationSettings::DefaultDirectoryBehaviorEnum e = static_cast<ApplicationSettings::DefaultDirectoryBehaviorEnum>(id);
settings->setDefaultDirectoryBehavior(e);
});
connect(ui->radioHardCoded, &QRadioButton::toggled, this, [=](bool checked){
ui->btnSelectHardCodedPath->setEnabled(checked);
ui->txtHardCodedPath->setEnabled(checked);
});
connect(ui->btnSelectHardCodedPath, &QToolButton::clicked, this, [=]() {
QString dir = QFileDialog::getExistingDirectory(this, tr("Default Directory"));
if (dir.isEmpty()) return; // user cancelled
settings->setDefaultDirectory(QDir::fromNativeSeparators(dir));
ui->txtHardCodedPath->setText(QDir::toNativeSeparators(dir));
});
connect(ui->txtHardCodedPath, &QLineEdit::editingFinished, this, [=]() {
QString dir = ui->txtHardCodedPath->text();
settings->setDefaultDirectory(QDir::fromNativeSeparators(dir));
ui->txtHardCodedPath->setText(QDir::toNativeSeparators(dir));
});
if (auto b = buttonGroup->button(settings->defaultDirectoryBehavior())) {
b->setChecked(true);
}
if (settings->defaultDirectoryBehavior() == ApplicationSettings::HardCoded) {
ui->txtHardCodedPath->setText((QDir::toNativeSeparators(settings->defaultDirectory())));
}
else {
ui->txtHardCodedPath->setText(QString());
}
}
PreferencesDialog::~PreferencesDialog()
{
delete ui;
}
void PreferencesDialog::showApplicationRestartRequired() const
{
ui->labelAppRestartIcon->show();
ui->labelAppRestart->show();
}
template<typename Func1, typename Func2, typename Func3>
void PreferencesDialog::MapSettingToCheckBox(QCheckBox *checkBox, Func1 getter, Func2 setter, Func3 notifier) const
{
// Get the value and set the checkbox state
checkBox->setChecked(std::bind(getter, settings)());
// Set up two way connection
connect(settings, notifier, checkBox, &QCheckBox::setChecked);
connect(checkBox, &QCheckBox::toggled, settings, setter);
}
template<typename Func1, typename Func2, typename Func3>
void PreferencesDialog::MapSettingToGroupBox(QGroupBox *groupBox, Func1 getter, Func2 setter, Func3 notifier) const
{
// Get the value and set the checkbox state
groupBox->setChecked(std::bind(getter, settings)());
// Set up two way connection
connect(settings, notifier, groupBox, &QGroupBox::setChecked);
connect(groupBox, &QGroupBox::toggled, settings, setter);
}
void PreferencesDialog::populateTranslationComboBox()
{
NotepadNextApplication *app = qobject_cast<NotepadNextApplication *>(qApp);
// Add the system default at the top
ui->comboBoxTranslation->addItem(tr("<System Default>"), QStringLiteral(""));
// TODO: sort this list and keep the system default at the top
for (const auto &localeName : app->getTranslationManager()->availableTranslations())
{
QLocale locale(localeName);
const QString localeDisplay = TranslationManager::FormatLocaleTerritoryAndLanguage(locale);
ui->comboBoxTranslation->addItem(localeDisplay, localeName);
}
// Select the current one
int index = ui->comboBoxTranslation->findData(settings->translation());
if (index != -1) {
ui->comboBoxTranslation->setCurrentIndex(index);
}
}