OSDN Git Service

Ver0.08pre
[gefu/Gefu.git] / preferencedialog.cpp
1 #include "colorsamplemodel.h"
2 #include"common.h"
3 #include "preferencedialog.h"
4 #include "ui_preferencedialog.h"
5
6 #include <QAbstractTableModel>
7 #include <QColorDialog>
8 #include <QSettings>
9 #include <QDebug>
10 #include <QDir>
11 #include <QFileDialog>
12 #include <QStandardPaths>
13
14 PreferenceDialog::PreferenceDialog(QWidget *parent) :
15     QDialog(parent),
16     ui(new Ui::PreferenceDialog),
17     m_model(),
18     m_colorMap()
19 {
20     m_model.setColorMap(&m_colorMap);
21
22     ui->setupUi(this);
23     ui->tabWidget->setCurrentIndex(0);
24     ui->sampleEdit->setText(QDir::homePath());
25     ui->sampleTable->setModel(&m_model);
26     ui->sampleTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
27     ui->sampleTable->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
28
29     connect(ui->bootSize, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
30     connect(ui->sizeAbsolute, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
31     connect(ui->sizeRelative, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
32     connect(ui->bootPos, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
33     connect(ui->posAbsolute, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
34     connect(ui->posRelative, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
35
36     connect(ui->boxClrBg, SIGNAL(clicked()), this, SLOT(selectBoxColor()));
37     connect(ui->boxClrFg, SIGNAL(clicked()), this, SLOT(selectBoxColor()));
38
39     connect(ui->clrBgMark, SIGNAL(clicked()), this, SLOT(selectViewColor()));
40     connect(ui->clrBgNormal, SIGNAL(clicked()), this, SLOT(selectViewColor()));
41     connect(ui->clrFgHidden, SIGNAL(clicked()), this, SLOT(selectViewColor()));
42     connect(ui->clrFgMark, SIGNAL(clicked()), this, SLOT(selectViewColor()));
43     connect(ui->clrFgNormal, SIGNAL(clicked()), this, SLOT(selectViewColor()));
44     connect(ui->clrFgReadonly, SIGNAL(clicked()), this, SLOT(selectViewColor()));
45     connect(ui->clrFgSystem, SIGNAL(clicked()), this, SLOT(selectViewColor()));
46
47     connect(ui->boxFont, SIGNAL(currentFontChanged(QFont)), this, SLOT(changeFont()));
48     connect(ui->boxFontBold, SIGNAL(clicked()), this, SLOT(changeFont()));
49     connect(ui->boxFontSize, SIGNAL(valueChanged(int)), this, SLOT(changeFont()));
50
51     connect(ui->viewFont, SIGNAL(currentFontChanged(QFont)), this, SLOT(changeFont()));
52     connect(ui->viewFontBold, SIGNAL(clicked()), this, SLOT(changeFont()));
53     connect(ui->viewFontSize, SIGNAL(valueChanged(int)), this, SLOT(changeFont()));
54
55     connect(ui->importAppearance, SIGNAL(clicked()), this, SLOT(importAppearance()));
56     connect(ui->exportAppearance, SIGNAL(clicked()), this, SLOT(exportAppearance()));
57     connect(ui->termBrowse, SIGNAL(clicked()), this, SLOT(browseApp()));
58     connect(ui->editorBrowse, SIGNAL(clicked()), this, SLOT(browseApp()));
59
60     QSettings settings;
61     QString strValue;
62     QSize size;
63     QPoint point;
64     QRadioButton *radioBtn;
65
66     //>>>>> 起動と終了
67     // 終了時の確認ダイアログ
68     ui->confirmExit->setChecked(settings.value(IniKey_ConfirmExit).toBool());
69     // 起動時のサイズ
70     strValue = settings.value(IniKey_BootSizeSpec).toString();
71     if (strValue.isEmpty()) {
72         ui->bootSize->setChecked(false);
73     }
74     else {
75         ui->bootSize->setChecked(true);
76         radioBtn = findChild<QRadioButton*>(strValue);
77         if (radioBtn == NULL) {
78             radioBtn = ui->sizeLast;
79         }
80         radioBtn->setChecked(true);
81     }
82     size = settings.value(IniKey_BootSizeAbs).toSize();
83     ui->absoluteWidth->setValue(size.width());
84     ui->absoluteHeight->setValue(size.height());
85     size = settings.value(IniKey_BootSizeRel).toSize();
86     ui->relativeWidth->setValue(size.width());
87     ui->relativeHeight->setValue(size.height());
88     // 起動時の位置
89     strValue = settings.value(IniKey_BootPosSpec).toString();
90     if (strValue.isEmpty()) {
91         ui->bootPos->setChecked(false);
92     }
93     else {
94         ui->bootPos->setChecked(true);
95         radioBtn = findChild<QRadioButton*>(strValue);
96         if (radioBtn == NULL) {
97             radioBtn = ui->posLast;
98         }
99         radioBtn->setChecked(true);
100     }
101     point = settings.value(IniKey_BootPosAbs).toPoint();
102     ui->absoluteLeft->setValue(point.x());
103     ui->absoluteTop->setValue(point.y());
104     point = settings.value(IniKey_BootPosRel).toPoint();
105     ui->relativeLeft->setValue(point.x());
106     ui->relativeTop->setValue(point.y());
107     // 起動時の設定削除
108     ui->resetOnBoot->setChecked(settings.value(IniKey_ResetOnBoot).toBool());
109
110     //>>>>> 色とフォント
111     loadAppearance(settings);
112
113     //>>>>> ファイル操作
114     // 確認ダイアログの表示
115     ui->confirmCopy->setChecked(settings.value(IniKey_ConfirmCopy).toBool());
116     ui->confirmDelete->setChecked(settings.value(IniKey_ConfirmDelete).toBool());
117     ui->confirmMove->setChecked(settings.value(IniKey_ConfirmMove).toBool());
118     ui->confirmRename->setChecked(settings.value(IniKey_ConfirmRename).toBool());
119     // 完了ダイアログの自動クローズ
120     ui->autoCloseCopy->setChecked(settings.value(IniKey_AutoCloseCopy).toBool());
121     ui->autoCloseDelete->setChecked(settings.value(IniKey_AutoCloseDelete).toBool());
122     ui->autoCloseMove->setChecked(settings.value(IniKey_AutoCloseMove).toBool());
123     ui->autoCloseRename->setChecked(settings.value(IniKey_AutoCloseRename).toBool());
124     // 上書き時の既定動作
125     strValue = settings.value(IniKey_DefaultOnCopy).toString();
126     if (strValue.isEmpty()) {
127         strValue = "owDefIfNew";
128     }
129     radioBtn = findChild<QRadioButton*>(strValue);
130     if (radioBtn == NULL) {
131         radioBtn = ui->owDefIfNew;
132     }
133     radioBtn->setChecked(true);
134     ui->moveAfterCreate->setChecked(settings.value(IniKey_MoveAfterCreateFolder).toBool());
135     ui->openAfterCreate->setChecked(settings.value(IniKey_OpenAfterCreateFile).toBool());
136
137     //>>>>> パス設定
138     // エディタ
139     ui->editorOpt->setText(settings.value(IniKey_EditorOption).toString());
140     ui->editorPath->setText(settings.value(IniKey_EditorPath).toString());
141     // ターミナル
142     ui->termOpt->setText(settings.value(IniKey_TerminalOption).toString());
143     ui->termPath->setText(settings.value(IniKey_TerminalPath).toString());
144 }
145
146 PreferenceDialog::~PreferenceDialog()
147 {
148     delete ui;
149 }
150
151 void PreferenceDialog::saveAppearance(QSettings &settings)
152 {
153     QFont font = ui->sampleEdit->font();
154     QPalette palette = ui->sampleEdit->palette();
155     settings.setValue(IniKey_BoxColorBg, palette.base().color());
156     settings.setValue(IniKey_BoxColorFg, palette.text().color());
157     settings.setValue(IniKey_BoxFont, font);
158
159     settings.setValue(IniKey_ViewColorBgMark, m_colorMap["clrBgMark"]);
160     settings.setValue(IniKey_ViewColorBgNormal, m_colorMap["clrBgNormal"]);
161     settings.setValue(IniKey_ViewColorFgHidden, m_colorMap["clrFgHidden"]);
162     settings.setValue(IniKey_ViewColorFgMark, m_colorMap["clrFgMark"]);
163     settings.setValue(IniKey_ViewColorFgNormal, m_colorMap["clrFgNormal"]);
164     settings.setValue(IniKey_ViewColorFgReadonly, m_colorMap["clrFgReadonly"]);
165     settings.setValue(IniKey_ViewColorFgSystem, m_colorMap["clrFgSystem"]);
166     settings.setValue(IniKey_ViewFont, m_model.font());
167 }
168
169 void PreferenceDialog::loadAppearance(QSettings &settings)
170 {
171     QPalette palette;
172     QColor color;
173     QFont font;
174
175     //>>>> アドレスボックス
176     palette = QPalette();
177     // 背景色
178     color = settings.value(IniKey_BoxColorBg).value<QColor>();
179     palette.setColor(QPalette::Base, color);
180     // 文字色
181     color = settings.value(IniKey_BoxColorFg).value<QColor>();
182     palette.setColor(QPalette::Text, color);
183     // フォント
184     font = settings.value(IniKey_BoxFont).value<QFont>();
185     ui->boxFont->setCurrentText(font.family());
186     ui->boxFontBold->setChecked(font.bold());
187     ui->boxFontSize->setValue(font.pointSize());
188     // サンプル表示
189     ui->sampleEdit->setPalette(palette);
190     ui->sampleEdit->setFont(font);
191     //>>>> ファイルビュー
192     // 背景色
193     color = settings.value(IniKey_ViewColorBgMark).value<QColor>();
194     m_colorMap["clrBgMark"] = color;
195     color = settings.value(IniKey_ViewColorBgNormal).value<QColor>();
196     m_colorMap["clrBgNormal"] = color;
197     // 文字色
198     color = settings.value(IniKey_ViewColorFgHidden).value<QColor>();
199     m_colorMap["clrFgHidden"] = color;
200     color = settings.value(IniKey_ViewColorFgMark).value<QColor>();
201     m_colorMap["clrFgMark"] = color;
202     color = settings.value(IniKey_ViewColorFgNormal).value<QColor>();
203     m_colorMap["clrFgNormal"] = color;
204     color = settings.value(IniKey_ViewColorFgReadonly).value<QColor>();
205     m_colorMap["clrFgReadonly"] = color;
206     color = settings.value(IniKey_ViewColorFgSystem).value<QColor>();
207     m_colorMap["clrFgSystem"] = color;
208     // フォント
209     font = settings.value(IniKey_ViewFont).value<QFont>();
210     ui->viewFont->setCurrentText(font.family());
211     ui->viewFontBold->setChecked(font.bold());
212     ui->viewFontSize->setValue(font.pointSize());
213     // サンプル表示
214     m_model.setFont(font);
215     m_model.update();
216
217 }
218
219 void PreferenceDialog::changeFont()
220 {
221     QFont font;
222
223     if (sender() == ui->boxFont ||
224         sender() == ui->boxFontBold ||
225         sender() == ui->boxFontSize)
226     {
227         font.setBold(ui->boxFontBold->isChecked());
228         font.setPointSize(ui->boxFontSize->value());
229         font.setFamily(ui->boxFont->currentText());
230         ui->sampleEdit->setFont(font);
231     }
232     else {
233         font.setBold(ui->viewFontBold->isChecked());
234         font.setPointSize(ui->viewFontSize->value());
235         font.setFamily(ui->viewFont->currentText());
236         m_model.setFont(font);
237         m_model.update();
238     }
239 }
240
241 void PreferenceDialog::setControlsEnabled(bool enabled)
242 {
243     if (sender() == ui->bootSize) {
244         ui->sizeAbsolute->setEnabled(enabled);
245         ui->sizeLast->setEnabled(enabled);
246         ui->sizeRelative->setEnabled(enabled);
247         if (enabled) {
248             emit ui->sizeAbsolute->toggled(ui->sizeAbsolute->isChecked());
249             emit ui->sizeRelative->toggled(ui->sizeRelative->isChecked());
250         }
251         else {
252             emit ui->sizeAbsolute->toggled(false);
253             emit ui->sizeRelative->toggled(false);
254         }
255     }
256     else if (sender() == ui->sizeAbsolute) {
257         ui->absoluteHeight->setEnabled(enabled);
258         ui->absoluteWidth->setEnabled(enabled);
259     }
260     else if (sender() == ui->sizeRelative) {
261         ui->relativeHeight->setEnabled(enabled);
262         ui->relativeWidth->setEnabled(enabled);
263     }
264     else if (sender() == ui->bootPos) {
265         ui->posAbsolute->setEnabled(enabled);
266         ui->posCenter->setEnabled(enabled);
267         ui->posLast->setEnabled(enabled);
268         ui->posRelative->setEnabled(enabled);
269         if (enabled) {
270             emit ui->posAbsolute->toggled(ui->posAbsolute->isChecked());
271             emit ui->posRelative->toggled(ui->posRelative->isChecked());
272         }
273         else {
274             emit ui->posAbsolute->toggled(false);
275             emit ui->posRelative->toggled(false);
276         }
277     }
278     else if (sender() == ui->posAbsolute) {
279         ui->absoluteLeft->setEnabled(enabled);
280         ui->absoluteTop->setEnabled(enabled);
281     }
282     else if (sender() == ui->posRelative) {
283         ui->relativeLeft->setEnabled(enabled);
284         ui->relativeTop->setEnabled(enabled);
285     }
286 }
287
288 void PreferenceDialog::selectBoxColor()
289 {
290     QColor color;
291     QPalette palette = ui->sampleEdit->palette();
292     if (sender() == ui->boxClrBg) {
293         color = palette.background().color();
294     }
295     else if (sender() == ui->boxClrFg) {
296         color = palette.text().color();
297     }
298
299     color = QColorDialog::getColor(color, this, tr("色選択"));
300     if (!color.isValid()) {
301         return;
302     }
303
304     if (sender() == ui->boxClrBg) {
305         palette.setColor(QPalette::Base, color);
306         ui->sampleEdit->setPalette(palette);
307     }
308     else if (sender() == ui->boxClrFg) {
309         palette.setColor(QPalette::Text, color);
310         ui->sampleEdit->setPalette(palette);
311     }
312 }
313
314 void PreferenceDialog::selectViewColor()
315 {
316     const QString objName = sender()->objectName();
317     QColor color = m_colorMap[objName];
318
319     color = QColorDialog::getColor(color, this, tr("色選択"));
320     if (!color.isValid()) {
321         return;
322     }
323
324     m_colorMap[objName] = color;
325     m_model.update();
326 }
327
328 void PreferenceDialog::browseApp()
329 {
330     QStringList list = QStandardPaths::standardLocations(
331                 QStandardPaths::ApplicationsLocation);
332 #ifdef Q_OS_WIN
333     QString path = QFileDialog::getOpenFileName(
334                 this, tr("アプリケーションを選択"), list.at(0),
335                 tr("実行ファイル (*.exe *.com *.bat *.pif);;すべてのファイル (*)"));
336 #elif defined(Q_OS_MAC)
337     QString path = QFileDialog::getOpenFileName(
338                 this, tr("アプリケーションを選択"), list.at(0),
339                 tr("実行ファイル (*.app);;すべてのファイル (*)"));
340 #else
341     QString path = QFileDialog::getOpenFileName(
342                 this, tr("アプリケーションを選択"), list.at(0),
343                 tr("すべてのファイル (*)"));
344 #endif
345     if (!path.isEmpty()) {
346         if (sender() == ui->editorBrowse) {
347             ui->editorPath->setText(path);
348         }
349         else if (sender() == ui->termBrowse) {
350             ui->termPath->setText(path);
351         }
352     }
353 }
354
355 void PreferenceDialog::importAppearance()
356 {
357     QStringList list = QStandardPaths::standardLocations(
358                 QStandardPaths::DocumentsLocation);
359     QString path = QFileDialog::getOpenFileName(
360                 this, tr("ファイルを選択"), list.at(0),
361                 tr("設定ファイル (*.ini);;すべてのファイル (*)"));
362     if (path.isEmpty()) {
363         return;
364     }
365
366     QSettings settings(path, QSettings::IniFormat);
367     loadAppearance(settings);
368 }
369
370 void PreferenceDialog::exportAppearance()
371 {
372     QStringList list = QStandardPaths::standardLocations(
373                 QStandardPaths::DocumentsLocation);
374     QString path = QFileDialog::getSaveFileName(
375                 this, tr("ファイルを選択"), list.at(0) + "/gefu_appearance.ini",
376                 tr("設定ファイル (*.ini);;すべてのファイル (*)"));
377     if (path.isEmpty()) {
378         return;
379     }
380
381     QSettings settings(path, QSettings::IniFormat);
382     saveAppearance(settings);
383 }
384
385 void PreferenceDialog::accept()
386 {
387     QSettings settings;
388     QAbstractButton *selected;
389
390     //>>>>> 起動と終了
391     // 終了時の確認ダイアログ
392     settings.setValue(IniKey_ConfirmExit, ui->confirmExit->isChecked());
393     // 起動時のサイズ
394     if (!ui->bootSize->isChecked()) {
395         settings.setValue(IniKey_BootSizeSpec, "");
396     }
397     else {
398         selected = ui->sizeOptions->checkedButton();
399         settings.setValue(IniKey_BootSizeSpec, selected->objectName());
400         QSize size;
401         // 絶対指定
402         size = QSize(ui->absoluteWidth->value(), ui->absoluteHeight->value());
403         settings.setValue(IniKey_BootSizeAbs, size);
404         // 相対指定
405         size = QSize(ui->relativeWidth->value(), ui->relativeHeight->value());
406         settings.setValue(IniKey_BootSizeRel, size);
407     }
408     // 起動時の位置
409     if (!ui->bootPos->isChecked()) {
410         settings.setValue(IniKey_BootPosSpec, "");
411     }
412     else {
413         selected = ui->posOptions->checkedButton();
414         settings.setValue(IniKey_BootPosSpec, selected->objectName());
415         // 絶対指定
416         QPoint pos;
417         pos = QPoint(ui->absoluteLeft->value(), ui->absoluteTop->value());
418         settings.setValue(IniKey_BootPosAbs, pos);
419         // 相対指定
420         pos = QPoint(ui->relativeLeft->value(), ui->relativeTop->value());
421         settings.setValue(IniKey_BootPosRel, pos);
422     }
423     // 起動時の設定削除
424     settings.setValue(IniKey_ResetOnBoot, ui->resetOnBoot->isChecked());
425
426     //>>>>> 色とフォント
427     saveAppearance(settings);
428
429     //>>>>> ファイル操作
430     settings.setValue(IniKey_ConfirmCopy, ui->confirmCopy->isChecked());
431     settings.setValue(IniKey_ConfirmDelete, ui->confirmDelete->isChecked());
432     settings.setValue(IniKey_ConfirmMove, ui->confirmMove->isChecked());
433     settings.setValue(IniKey_ConfirmRename, ui->confirmRename->isChecked());
434
435     settings.setValue(IniKey_AutoCloseCopy, ui->autoCloseCopy->isChecked());
436     settings.setValue(IniKey_AutoCloseDelete, ui->autoCloseDelete->isChecked());
437     settings.setValue(IniKey_AutoCloseMove, ui->autoCloseMove->isChecked());
438     settings.setValue(IniKey_AutoCloseRename, ui->autoCloseRename->isChecked());
439
440     selected = ui->overwriteOptions->checkedButton();
441     settings.setValue(IniKey_DefaultOnCopy, selected->objectName());
442
443     settings.setValue(IniKey_MoveAfterCreateFolder, ui->moveAfterCreate->isChecked());
444     settings.setValue(IniKey_OpenAfterCreateFile, ui->openAfterCreate->isChecked());
445
446     //>>>>> パス設定
447     settings.setValue(IniKey_EditorOption, ui->editorOpt->text().trimmed());
448     settings.setValue(IniKey_EditorPath, ui->editorPath->text().trimmed());
449
450     settings.setValue(IniKey_TerminalOption, ui->termOpt->text().trimmed());
451     settings.setValue(IniKey_TerminalPath, ui->termPath->text().trimmed());
452
453     QDialog::accept();
454 }