-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.cpp
165 lines (146 loc) · 4.02 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
162
163
164
165
// Copyright 2022-present Contributors to the jobman project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/jobman
#include "preferences.h"
#include <QFileDialog>
#include <QPointer>
#include <QSettings>
#include <QStandardPaths>
#include <QDebug>
// generated files
#include "ui_preferences.h"
class PreferencesPrivate : public QObject
{
Q_OBJECT
public:
PreferencesPrivate();
void init();
bool eventFilter(QObject* object, QEvent* event);
void loadSettings();
void saveSettings();
public Q_SLOTS:
void selectionChanged();
void add();
void remove();
void close();
public:
QString searchpathfrom;
QStringList searchpaths;
QPointer<Preferences> dialog;
QScopedPointer<Ui_Preferences> ui;
};
PreferencesPrivate::PreferencesPrivate()
{
}
void
PreferencesPrivate::init()
{
// ui
ui.reset(new Ui_Preferences());
ui->setupUi(dialog);
// settings
loadSettings();
// event filter
dialog->installEventFilter(this);
// layout
for(QString searchpath : searchpaths) {
ui->searchpaths->addItem(searchpath);
}
// connect
connect(ui->searchpaths, &QListWidget::itemSelectionChanged, this, &PreferencesPrivate::selectionChanged);
connect(ui->add, &QPushButton::pressed, this, &PreferencesPrivate::add);
connect(ui->remove, &QPushButton::pressed, this, &PreferencesPrivate::remove);
connect(ui->close, &QPushButton::pressed, this, &PreferencesPrivate::close);
}
bool
PreferencesPrivate::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::Hide) {
saveSettings();
}
return false;
}
void
PreferencesPrivate::loadSettings()
{
QString documents = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
QSettings settings(MACOSX_BUNDLE_GUI_IDENTIFIER, "Jobman");
searchpathfrom = settings.value("searchpathFrom", documents).toString();
searchpaths = settings.value("searchpaths", documents).toStringList();
}
void
PreferencesPrivate::saveSettings()
{
QSettings settings(MACOSX_BUNDLE_GUI_IDENTIFIER, "Jobman");
settings.setValue("searchpathFrom", searchpathfrom);
searchpaths.clear();
for (int i = 0; i < ui->searchpaths->count(); ++i) {
QListWidgetItem* item = ui->searchpaths->item(i);
if (item) {
searchpaths.append(item->text());
}
}
settings.setValue("searchpaths", searchpaths);
}
void
PreferencesPrivate::selectionChanged()
{
QList<QListWidgetItem*> selectedItems = ui->searchpaths->selectedItems();
if (!selectedItems.isEmpty()) {
ui->remove->setEnabled(true);
} else {
ui->remove->setEnabled(false);
}
}
void
PreferencesPrivate::add()
{
QString dir = QFileDialog::getExistingDirectory(
dialog.data(),
tr("Add folder"),
searchpathfrom,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
);
if (!dir.isEmpty()) {
bool found = false;
for (int i = 0; i < ui->searchpaths->count(); ++i) {
QListWidgetItem *item = ui->searchpaths->item(i);
if (item->text() == dir) {
found = true;
break;
}
}
if (!found) {
ui->searchpaths->addItem(dir);
searchpathfrom = dir;
}
}
}
void
PreferencesPrivate::remove()
{
QListWidget* searchpath = ui->searchpaths;
QString dir = searchpath->currentItem()->text();
if (!dir.isEmpty()) {
QList<QListWidgetItem*> items = searchpath->findItems(dir, Qt::MatchExactly);
foreach (QListWidgetItem* item, items) {
delete searchpath->takeItem(ui->searchpaths->row(item));
}
}
}
void
PreferencesPrivate::close()
{
dialog->close();
}
#include "preferences.moc"
Preferences::Preferences(QWidget* parent)
: QDialog(parent)
, p(new PreferencesPrivate())
{
p->dialog = this;
p->init();
}
Preferences::~Preferences()
{
}