OSDN Git Service

スレッド処理を整理
[gefu/Gefu.git] / preferencedialog.cpp
1 #include "global.h"
2 #include "preferences.h"
3 #include "preferencedialog.h"
4 #include "ui_preferencedialog.h"
5
6 #include <QColorDialog>
7 #include <QDebug>
8 #include <QDir>
9 #include <QFileDialog>
10 #include <QFontDialog>
11 #include <QStandardPaths>
12
13 PreferenceDialog::PreferenceDialog(QWidget *parent) :
14     QDialog(parent),
15     ui(new Ui::PreferenceDialog)
16 {
17     ui->setupUi(this);
18     ui->tabWidget->setCurrentIndex(0);
19     ui->lBoxSample->setText(QDir::homePath());
20
21     connect(ui->fvNormal, SIGNAL(clicked()), this, SLOT(chooseColor()));
22     connect(ui->fvNormalBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
23     connect(ui->fvMarked, SIGNAL(clicked()), this, SLOT(chooseColor()));
24     connect(ui->fvMarkedBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
25     connect(ui->fvSystem, SIGNAL(clicked()), this, SLOT(chooseColor()));
26     connect(ui->fvHidden, SIGNAL(clicked()), this, SLOT(chooseColor()));
27     connect(ui->fvReadOnly, SIGNAL(clicked()), this, SLOT(chooseColor()));
28     connect(ui->lBoxBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
29     connect(ui->lBoxFg, SIGNAL(clicked()), this, SLOT(chooseColor()));
30     connect(ui->sBoxNormal, SIGNAL(clicked()), this, SLOT(chooseColor()));
31     connect(ui->sBoxNormalBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
32     connect(ui->sBoxUnmatch, SIGNAL(clicked()), this, SLOT(chooseColor()));
33     connect(ui->sBoxUnmatchBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
34     connect(ui->tvBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
35     connect(ui->tvFg, SIGNAL(clicked()), this, SLOT(chooseColor()));
36     connect(ui->gvBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
37     connect(ui->allBg, SIGNAL(clicked()), this, SLOT(chooseColor()));
38     connect(ui->allFg, SIGNAL(clicked()), this, SLOT(chooseColor()));
39
40     connect(ui->fvFont, SIGNAL(clicked()), this, SLOT(chooseFont()));
41     connect(ui->lBoxFont, SIGNAL(clicked()), this, SLOT(chooseFont()));
42     connect(ui->sBoxFont, SIGNAL(clicked()), this, SLOT(chooseFont()));
43     connect(ui->tvFont, SIGNAL(clicked()), this, SLOT(chooseFont()));
44     connect(ui->allFont, SIGNAL(clicked()), this, SLOT(chooseFont()));
45
46     connect(ui->browseArchiver, SIGNAL(clicked()), this, SLOT(choosePath()));
47     connect(ui->browseEditor, SIGNAL(clicked()), this, SLOT(choosePath()));
48     connect(ui->browseTerminal, SIGNAL(clicked()), this, SLOT(choosePath()));
49
50     connect(ui->exportAppearance, SIGNAL(clicked()), this, SLOT(exportAppearance()));
51     connect(ui->importAppearance, SIGNAL(clicked()), this, SLOT(importAppearance()));
52
53     Preferences prefs(this);
54
55     // [全般]
56     ui->checkUpdate->setChecked(prefs.isCheckUpdate());
57     ui->confirmQuit->setChecked(prefs.isConfirmQuit());
58     ui->moveAfterCreation->setChecked(prefs.isMoveAfterCreation());
59     ui->openAfterCreation->setChecked(prefs.isOpenAfterCreation());
60     ui->confirmCopy->setChecked(prefs.isConfirmCopy());
61     ui->confirmMove->setChecked(prefs.isConfirmMove());
62     ui->confirmDelete->setChecked(prefs.isConfirmDelete());
63     ui->confirmRename->setChecked(prefs.isConfirmRename());
64     ui->autoCloseCopy->setChecked(prefs.isAutoCloseCopy());
65     ui->autoCloseMove->setChecked(prefs.isAutoCloseMove());
66     ui->autoCloseDelete->setChecked(prefs.isAutoCloseDelete());
67     ui->autoCloseRename->setChecked(prefs.isAutoCloseRename());
68     QRadioButton *radio = findChild<QRadioButton*>("rb" + prefs.getCopyBehavior());
69     if (radio)
70         radio->setChecked(true);
71     else
72         ui->rbOverWriteIfNew->setChecked(true);
73     ui->preferExtensions->setPlainText(prefs.getPreferExtensions());
74     ui->resetOnBoot->setChecked(prefs.isReset());
75
76     // [外観(色)]
77     loadAppearance(prefs);
78
79     // [外観(フォント)]
80     setFont(ui->fvFontSpec, prefs.getFolderViewFont());
81     setFont(ui->lBoxFontSpec, prefs.getLocationBoxFont());
82     setFont(ui->sBoxFontSpec, prefs.getSearchBoxFont());
83     setFont(ui->tvFontSpec, prefs.getTextViewFont());
84
85     ui->lineHeight->setValue(prefs.getLineHeight());
86
87     // [プログラムパス]
88     ui->editorPath->setText(prefs.getEditorPath());
89     ui->terminalPath->setText(prefs.getTerminalPath());
90     ui->archiverPath->setText(prefs.getArchiverPath());
91 }
92
93 PreferenceDialog::~PreferenceDialog()
94 {
95     delete ui;
96 }
97
98 void PreferenceDialog::loadAppearance(Preferences &prefs)
99 {
100     setPalette(ui->fvNormalSample, QPalette::Base, prefs.getFolderViewBgColor());
101     setPalette(ui->fvNormalSample, QPalette::Text, prefs.getFolderViewFgColor());
102
103     setPalette(ui->fvMarkedSample, QPalette::Base, prefs.getFolderViewMarkedBgColor());
104     setPalette(ui->fvMarkedSample, QPalette::Text, prefs.getFolderViewMarkedFgColor());
105
106     setPalette(ui->fvSystemSample, QPalette::Base, prefs.getFolderViewBgColor());
107     setPalette(ui->fvSystemSample, QPalette::Text, prefs.getFolderViewSystemColor());
108
109     setPalette(ui->fvHiddenSample, QPalette::Base, prefs.getFolderViewBgColor());
110     setPalette(ui->fvHiddenSample, QPalette::Text, prefs.getFolderViewHiddenColor());
111
112     setPalette(ui->fvReadOnlySample, QPalette::Base, prefs.getFolderViewBgColor());
113     setPalette(ui->fvReadOnlySample, QPalette::Text, prefs.getFolderViewReadOnlyColor());
114
115     setPalette(ui->lBoxSample, QPalette::Base, prefs.getLocationBoxBgColor());
116     setPalette(ui->lBoxSample, QPalette::Text, prefs.getLocationBoxFgColor());
117
118     setPalette(ui->sBoxNormalSample, QPalette::Base, prefs.getSearchBoxBgColor());
119     setPalette(ui->sBoxNormalSample, QPalette::Text, prefs.getSearchBoxFgColor());
120
121     setPalette(ui->sBoxUnmatchSample, QPalette::Base, prefs.getSearchBoxUnmatchBgColor());
122     setPalette(ui->sBoxUnmatchSample, QPalette::Text, prefs.getSearchBoxUnmatchFgColor());
123
124     setPalette(ui->tvSample, QPalette::Base, prefs.getTextViewBgColor());
125     setPalette(ui->tvSample, QPalette::Text, prefs.getTextViewFgColor());
126
127     setPalette(ui->gvSample, QPalette::Base, prefs.getImageViewBgColor());
128
129     ui->darkFactor->setValue(prefs.getDarkFacotr());
130 }
131
132 void PreferenceDialog::saveAppearance(Preferences &prefs)
133 {
134     prefs.setFolderViewBgColor(ui->fvNormalSample->palette().base().color());
135     prefs.setFolderViewFgColor(ui->fvNormalSample->palette().text().color());
136
137     prefs.setFolderViewMarkedBgColor(ui->fvMarkedSample->palette().base().color());
138     prefs.setFolderViewMarkedFgColor(ui->fvMarkedSample->palette().text().color());
139
140     prefs.setFolderViewHiddenColor(ui->fvHiddenSample->palette().text().color());
141     prefs.setFolderViewReadOnlyColor(ui->fvReadOnlySample->palette().text().color());
142     prefs.setFolderViewSystemColor(ui->fvSystemSample->palette().text().color());
143
144     prefs.setLocationBoxBgColor(ui->lBoxSample->palette().base().color());
145     prefs.setLocationBoxFgColor(ui->lBoxSample->palette().text().color());
146
147     prefs.setSearchBoxBgColor(ui->sBoxNormalSample->palette().base().color());
148     prefs.setSearchBoxFgColor(ui->sBoxNormalSample->palette().text().color());
149
150     prefs.setSearchBoxUnmatchBgColor(ui->sBoxUnmatchSample->palette().base().color());
151     prefs.setSearchBoxUnmatchFgColor(ui->sBoxUnmatchSample->palette().text().color());
152
153     prefs.setTextViewBgColor(ui->tvSample->palette().base().color());
154     prefs.setTextViewFgColor(ui->tvSample->palette().text().color());
155
156     prefs.setImageViewBgColor(ui->gvSample->palette().base().color());
157
158     prefs.setDarkFacotr(ui->darkFactor->value());
159 }
160
161 void PreferenceDialog::setFont(QLabel *label, const QFont &font)
162 {
163     label->setFont(font);
164     label->setText(QString("%1, %2pt").arg(font.family()).arg(font.pointSize()));
165 }
166
167 void PreferenceDialog::setPalette(QWidget *w, QPalette::ColorRole role, const QColor &color)
168 {
169     QPalette pal = w->palette();
170     pal.setColor(role, color);
171     w->setPalette(pal);
172 }
173
174 void PreferenceDialog::chooseColor()
175 {
176     QColor color;
177
178     if (sender() == ui->fvNormal) {
179         color = ui->fvNormalSample->palette().text().color();
180     }
181     else if (sender() == ui->fvNormalBg) {
182         color = ui->fvNormalSample->palette().base().color();
183     }
184     else if (sender() == ui->fvMarked) {
185         color = ui->fvMarkedSample->palette().text().color();
186     }
187     else if (sender() == ui->fvMarkedBg) {
188         color = ui->fvMarkedSample->palette().base().color();
189     }
190     else if (sender() == ui->fvSystem) {
191         color = ui->fvSystemSample->palette().text().color();
192     }
193     else if (sender() == ui->fvHidden) {
194         color = ui->fvHiddenSample->palette().text().color();
195     }
196     else if (sender() == ui->fvReadOnly) {
197         color = ui->fvReadOnlySample->palette().text().color();
198     }
199     else if (sender() == ui->lBoxBg) {
200         color = ui->lBoxSample->palette().base().color();
201     }
202     else if (sender() == ui->lBoxFg) {
203         color = ui->lBoxSample->palette().text().color();
204     }
205     else if (sender() == ui->sBoxNormal) {
206         color = ui->sBoxNormalSample->palette().text().color();
207     }
208     else if (sender() == ui->sBoxNormalBg) {
209         color = ui->sBoxNormalSample->palette().base().color();
210     }
211     else if (sender() == ui->sBoxUnmatch) {
212         color = ui->sBoxUnmatchSample->palette().text().color();
213     }
214     else if (sender() == ui->sBoxUnmatchBg) {
215         color = ui->sBoxUnmatchSample->palette().base().color();
216     }
217     else if (sender() == ui->tvBg) {
218         color = ui->tvSample->palette().base().color();
219     }
220     else if (sender() == ui->tvFg) {
221         color = ui->tvSample->palette().text().color();
222     }
223     else if (sender() == ui->gvBg) {
224         color = ui->gvSample->palette().base().color();
225     }
226     else if (sender() == ui->allBg) {
227         color = ui->fvNormalSample->palette().base().color();
228     }
229     else if (sender() == ui->allFg) {
230         color = ui->fvNormalSample->palette().text().color();
231     }
232
233     color = QColorDialog::getColor(color, this, tr("色を選択"));
234     if (!color.isValid()) {
235         return;
236     }
237
238     QPalette pal;
239     if (sender() == ui->fvNormal) {
240         setPalette(ui->fvNormalSample, QPalette::Text, color);
241     }
242     else if (sender() == ui->fvNormalBg) {
243         setPalette(ui->fvNormalSample, QPalette::Base, color);
244         setPalette(ui->fvHiddenSample, QPalette::Base, color);
245         setPalette(ui->fvReadOnlySample, QPalette::Base, color);
246         setPalette(ui->fvSystemSample, QPalette::Base, color);
247     }
248     else if (sender() == ui->fvMarked) {
249         setPalette(ui->fvMarkedSample, QPalette::Text, color);
250     }
251     else if (sender() == ui->fvMarkedBg) {
252         setPalette(ui->fvMarkedSample, QPalette::Base, color);
253     }
254     else if (sender() == ui->fvSystem) {
255         setPalette(ui->fvSystemSample, QPalette::Text, color);
256     }
257     else if (sender() == ui->fvHidden) {
258         setPalette(ui->fvHiddenSample, QPalette::Text, color);
259     }
260     else if (sender() == ui->fvReadOnly) {
261         setPalette(ui->fvReadOnlySample, QPalette::Text, color);
262     }
263     else if (sender() == ui->lBoxBg) {
264         setPalette(ui->lBoxSample, QPalette::Base, color);
265     }
266     else if (sender() == ui->lBoxFg) {
267         setPalette(ui->lBoxSample, QPalette::Text, color);
268     }
269     else if (sender() == ui->sBoxNormal) {
270         setPalette(ui->sBoxNormalSample, QPalette::Text, color);
271     }
272     else if (sender() == ui->sBoxNormalBg) {
273         setPalette(ui->sBoxNormalSample, QPalette::Base, color);
274     }
275     else if (sender() == ui->sBoxUnmatch) {
276         setPalette(ui->sBoxUnmatchSample, QPalette::Text, color);
277     }
278     else if (sender() == ui->sBoxUnmatchBg) {
279         setPalette(ui->sBoxUnmatchSample, QPalette::Base, color);
280     }
281     else if (sender() == ui->tvBg) {
282         setPalette(ui->tvSample, QPalette::Base, color);
283     }
284     else if (sender() == ui->tvFg) {
285         setPalette(ui->tvSample, QPalette::Text, color);
286     }
287     else if (sender() == ui->gvBg) {
288         setPalette(ui->gvSample, QPalette::Base, color);
289     }
290     else if (sender() == ui->allBg) {
291         setPalette(ui->fvNormalSample, QPalette::Base, color);
292         setPalette(ui->fvHiddenSample, QPalette::Base, color);
293         setPalette(ui->fvReadOnlySample, QPalette::Base, color);
294         setPalette(ui->fvSystemSample, QPalette::Base, color);
295         setPalette(ui->lBoxSample, QPalette::Base, color);
296         setPalette(ui->sBoxNormalSample, QPalette::Base, color);
297         setPalette(ui->tvSample, QPalette::Base, color);
298         setPalette(ui->gvSample, QPalette::Base, color);
299     }
300     else if (sender() == ui->allFg) {
301         setPalette(ui->fvNormalSample, QPalette::Text, color);
302         setPalette(ui->lBoxSample, QPalette::Text, color);
303         setPalette(ui->sBoxNormalSample, QPalette::Text, color);
304         setPalette(ui->tvSample, QPalette::Text, color);
305     }
306 }
307
308 void PreferenceDialog::chooseFont()
309 {
310     QFont font;
311     if (sender() == ui->fvFont) {
312         font = ui->fvFontSpec->font();
313     }
314     else if (sender() == ui->lBoxFont) {
315         font = ui->lBoxFontSpec->font();
316     }
317     else if (sender() == ui->sBoxFont) {
318         font = ui->sBoxFontSpec->font();
319     }
320     else if (sender() == ui->tvFont) {
321         font = ui->tvFontSpec->font();
322     }
323     else if (sender() == ui->allFont) {
324         font = ui->fvFontSpec->font();
325     }
326
327     bool ok;
328     font = QFontDialog::getFont(&ok, font, this, tr("フォントを選択"));
329
330     if (!ok) {
331         return;
332     }
333
334     if (sender() == ui->fvFont) {
335         setFont(ui->fvFontSpec, font);
336     }
337     else if (sender() == ui->lBoxFont) {
338         setFont(ui->lBoxFontSpec, font);
339     }
340     else if (sender() == ui->sBoxFont) {
341         setFont(ui->sBoxFontSpec, font);
342     }
343     else if (sender() == ui->tvFont) {
344         setFont(ui->tvFontSpec, font);
345     }
346     else if (sender() == ui->allFont) {
347         setFont(ui->fvFontSpec, font);
348         setFont(ui->lBoxFontSpec, font);
349         setFont(ui->sBoxFontSpec, font);
350         setFont(ui->tvFontSpec, font);
351     }
352 }
353
354 void PreferenceDialog::choosePath()
355 {
356     QStringList list = QStandardPaths::standardLocations(
357                 QStandardPaths::ApplicationsLocation);
358 #ifdef Q_OS_WIN
359     QString path = QFileDialog::getOpenFileName(
360                 this, tr("アプリケーションを選択"), getenv("PROGRAMFILES"),
361                 tr("実行ファイル (*.exe *.com *.bat *.pif);;すべてのファイル (*)"));
362 #elif defined(Q_OS_MAC)
363     QString path = QFileDialog::getOpenFileName(
364                 this, tr("アプリケーションを選択"), list.at(0),
365                 tr("実行ファイル (*.app);;すべてのファイル (*)"));
366 #else
367     QString path = QFileDialog::getOpenFileName(
368                 this, tr("アプリケーションを選択"), list.at(0),
369                 tr("すべてのファイル (*)"));
370 #endif
371     if (path.isEmpty()) {
372         return;
373     }
374
375     if (path.indexOf(" ") != -1) {
376         path = QQ(path);
377     }
378
379     if (sender() == ui->browseEditor) {
380         ui->editorPath->setText(path);
381     }
382     else if (sender() == ui->browseTerminal) {
383         ui->terminalPath->setText(path);
384     }
385     else if (sender() == ui->browseArchiver) {
386         ui->archiverPath->setText(path);
387     }
388 }
389
390 void PreferenceDialog::exportAppearance()
391 {
392     QStringList list = QStandardPaths::standardLocations(
393                 QStandardPaths::DocumentsLocation);
394     QString path = QFileDialog::getSaveFileName(
395                 this, tr("ファイルを選択"), list.at(0) + "/gefu_appearance.ini",
396                 tr("設定ファイル (*.ini);;すべてのファイル (*)"));
397     if (path.isEmpty()) {
398         return;
399     }
400
401     Preferences prefs(path, this);
402     saveAppearance(prefs);
403 }
404
405 void PreferenceDialog::importAppearance()
406 {
407     QStringList list = QStandardPaths::standardLocations(
408                 QStandardPaths::DocumentsLocation);
409     QString path = QFileDialog::getOpenFileName(
410                 this, tr("ファイルを選択"), list.at(0),
411                 tr("設定ファイル (*.ini);;すべてのファイル (*)"));
412     if (path.isEmpty()) {
413         return;
414     }
415
416     Preferences prefs(path, this);
417     loadAppearance(prefs);
418 }
419
420 void PreferenceDialog::accept()
421 {
422     Preferences prefs(this);
423
424     // [全般]
425     prefs.setCheckUpdate(ui->checkUpdate->isChecked());
426     prefs.setConfirmQuit(ui->confirmQuit->isChecked());
427     prefs.setMoveAfterCreation(ui->moveAfterCreation->isChecked());
428     prefs.setOpenAfterCreation(ui->openAfterCreation->isChecked());
429     prefs.setConfirmCopy(ui->confirmCopy->isChecked());
430     prefs.setConfirmMove(ui->confirmMove->isChecked());
431     prefs.setConfirmDelete(ui->confirmDelete->isChecked());
432     prefs.setConfirmRename(ui->confirmRename->isChecked());
433     prefs.setAutoCloseCopy(ui->autoCloseCopy->isChecked());
434     prefs.setAutoCloseMove(ui->autoCloseMove->isChecked());
435     prefs.setAutoCloseDelete(ui->autoCloseDelete->isChecked());
436     prefs.setAutoCloseRename(ui->autoCloseRename->isChecked());
437
438     QAbstractButton *checked = ui->copyBehavior->checkedButton();
439     prefs.setCopyBehavior(checked->objectName().mid(2));
440
441     prefs.setPreferExtensions(ui->preferExtensions->toPlainText());
442     prefs.setReset(ui->resetOnBoot->isChecked());
443
444     // [外観(色)]
445     saveAppearance(prefs);
446
447     // [外観(フォント)]
448     prefs.setFolderViewFont(ui->fvFontSpec->font());
449     prefs.setLocationBoxFont(ui->lBoxFontSpec->font());
450     prefs.setSearchBoxFont(ui->sBoxFontSpec->font());
451     prefs.setTextViewFont(ui->tvFontSpec->font());
452
453     prefs.setLineHeight(ui->lineHeight->value());
454
455     // [プログラムパス]
456     prefs.setEditorPath(ui->editorPath->text());
457     prefs.setTerminalPath(ui->terminalPath->text());
458     prefs.setArchiverPath(ui->archiverPath->text());
459
460     QDialog::accept();
461 }
462