OSDN Git Service

ドロップ処理のテスト
[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
12
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->tabWidget->setTabText(0, "起動と終了");
25     ui->tabWidget->setTabText(1, "色とフォント");
26     ui->sampleEdit->setText(QDir::homePath());
27     ui->sampleTable->setModel(&m_model);
28     ui->sampleTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
29     ui->sampleTable->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
30
31     connect(ui->bootSize, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
32     connect(ui->sizeAbsolute, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
33     connect(ui->sizeRelative, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
34     connect(ui->bootPos, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
35     connect(ui->posAbsolute, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
36     connect(ui->posRelative, SIGNAL(toggled(bool)), this, SLOT(setControlsEnabled(bool)));
37
38     connect(ui->boxClrBg, SIGNAL(clicked()), this, SLOT(selectBoxColor()));
39     connect(ui->boxClrFg, SIGNAL(clicked()), this, SLOT(selectBoxColor()));
40
41     connect(ui->clrBgMark, SIGNAL(clicked()), this, SLOT(selectViewColor()));
42     connect(ui->clrBgNormal, SIGNAL(clicked()), this, SLOT(selectViewColor()));
43     connect(ui->clrFgHidden, SIGNAL(clicked()), this, SLOT(selectViewColor()));
44     connect(ui->clrFgMark, SIGNAL(clicked()), this, SLOT(selectViewColor()));
45     connect(ui->clrFgNormal, SIGNAL(clicked()), this, SLOT(selectViewColor()));
46     connect(ui->clrFgReadonly, SIGNAL(clicked()), this, SLOT(selectViewColor()));
47     connect(ui->clrFgSystem, SIGNAL(clicked()), this, SLOT(selectViewColor()));
48
49     connect(ui->boxFont, SIGNAL(currentFontChanged(QFont)), this, SLOT(changeFont()));
50     connect(ui->boxFontBold, SIGNAL(clicked()), this, SLOT(changeFont()));
51     connect(ui->boxFontSize, SIGNAL(valueChanged(int)), this, SLOT(changeFont()));
52
53     connect(ui->viewFont, SIGNAL(currentFontChanged(QFont)), this, SLOT(changeFont()));
54     connect(ui->viewFontBold, SIGNAL(clicked()), this, SLOT(changeFont()));
55     connect(ui->viewFontSize, SIGNAL(valueChanged(int)), this, SLOT(changeFont()));
56
57     QSettings settings;
58     QString strValue;
59     QSize size;
60     QPoint point;
61     QColor color;
62     QPalette palette;
63     QFont font;
64     QRadioButton *radioBtn;
65
66     //>>>>> 起動と終了
67     // 終了時の確認ダイアログ
68     ui->confirmExit->setChecked(settings.value(IniKey_ConfirmExit, true).toBool());
69     // 起動時のサイズ
70     strValue = settings.value(IniKey_BootSizeSpec, "sizeLast").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, QSize(800, 600)).toSize();
83     ui->absoluteWidth->setValue(size.width());
84     ui->absoluteHeight->setValue(size.height());
85     size = settings.value(IniKey_BootSizeRel, QSize(50, 50)).toSize();
86     ui->relativeWidth->setValue(size.width());
87     ui->relativeHeight->setValue(size.height());
88     // 起動時の位置
89     strValue = settings.value(IniKey_BootPosSpec, "posLast").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, QPoint(0, 0)).toPoint();
102     ui->absoluteLeft->setValue(point.x());
103     ui->absoluteTop->setValue(point.y());
104     point = settings.value(IniKey_BootPosRel, QPoint(0, 0)).toPoint();
105     ui->relativeLeft->setValue(point.x());
106     ui->relativeTop->setValue(point.y());
107     // 起動時の設定削除
108     ui->resetOnBoot->setChecked(settings.value(IniKey_ResetOnBoot, false).toBool());
109
110     //>>>>> 色とフォント
111     //>>>> アドレスボックス
112     palette = QPalette();
113     // 背景色
114     color = settings.value(IniKey_BoxColorBg, QPalette().base().color()).value<QColor>();
115     palette.setColor(QPalette::Base, color);
116     // 文字色
117     color = settings.value(IniKey_BoxColorFg, QPalette().text().color()).value<QColor>();
118     palette.setColor(QPalette::Text, color);
119     // フォント
120     QFont defaultFont = ui->sampleEdit->font();
121     font = settings.value(IniKey_BoxFont, defaultFont).value<QFont>();
122     ui->boxFont->setCurrentText(font.family());
123     ui->boxFontBold->setChecked(font.bold());
124     ui->boxFontSize->setValue(font.pointSize());
125     // サンプル表示
126     ui->sampleEdit->setPalette(palette);
127     ui->sampleEdit->setFont(font);
128     //>>>> ファイルビュー
129     // 背景色
130     color = settings.value(IniKey_ViewColorBgMark, DefaultMarkBgColor).value<QColor>();
131     m_colorMap["clrBgMark"] = color;
132     color = settings.value(IniKey_ViewColorBgNormal, QPalette().base().color()).value<QColor>();
133     m_colorMap["clrBgNormal"] = color;
134     // 文字色
135     color = settings.value(IniKey_ViewColorFgHidden, DefaultHiddenColor).value<QColor>();
136     m_colorMap["clrFgHidden"] = color;
137     color = settings.value(IniKey_ViewColorFgMark, DefaultMarkFgColor).value<QColor>();
138     m_colorMap["clrFgMark"] = color;
139     color = settings.value(IniKey_ViewColorFgNormal, QPalette().text().color()).value<QColor>();
140     m_colorMap["clrFgNormal"] = color;
141     color = settings.value(IniKey_ViewColorFgReadonly, DefaultReadonlyColor).value<QColor>();
142     m_colorMap["clrFgReadonly"] = color;
143     color = settings.value(IniKey_ViewColorFgSystem, DefaultSystemColor).value<QColor>();
144     m_colorMap["clrFgSystem"] = color;
145     // フォント
146     defaultFont = ui->sampleTable->font();
147     font = settings.value(IniKey_ViewFont, defaultFont).value<QFont>();
148     ui->viewFont->setCurrentText(font.family());
149     ui->viewFontBold->setChecked(font.bold());
150     ui->viewFontSize->setValue(font.pointSize());
151     // サンプル表示
152     m_model.setFont(font);
153     m_model.update();
154 }
155
156 PreferenceDialog::~PreferenceDialog()
157 {
158     delete ui;
159 }
160
161 void PreferenceDialog::changeFont()
162 {
163     QFont font;
164
165     if (sender() == ui->boxFont ||
166         sender() == ui->boxFontBold ||
167         sender() == ui->boxFontSize)
168     {
169         font.setBold(ui->boxFontBold->isChecked());
170         font.setPointSize(ui->boxFontSize->value());
171         font.setFamily(ui->boxFont->currentText());
172         ui->sampleEdit->setFont(font);
173     }
174     else {
175         font.setBold(ui->viewFontBold->isChecked());
176         font.setPointSize(ui->viewFontSize->value());
177         font.setFamily(ui->viewFont->currentText());
178         m_model.setFont(font);
179         m_model.update();
180     }
181 }
182
183 void PreferenceDialog::setControlsEnabled(bool enabled)
184 {
185     if (sender() == ui->bootSize) {
186         ui->sizeAbsolute->setEnabled(enabled);
187         ui->sizeLast->setEnabled(enabled);
188         ui->sizeRelative->setEnabled(enabled);
189         if (enabled) {
190             emit ui->sizeAbsolute->toggled(ui->sizeAbsolute->isChecked());
191             emit ui->sizeRelative->toggled(ui->sizeRelative->isChecked());
192         }
193         else {
194             emit ui->sizeAbsolute->toggled(false);
195             emit ui->sizeRelative->toggled(false);
196         }
197     }
198     else if (sender() == ui->sizeAbsolute) {
199         ui->absoluteHeight->setEnabled(enabled);
200         ui->absoluteWidth->setEnabled(enabled);
201     }
202     else if (sender() == ui->sizeRelative) {
203         ui->relativeHeight->setEnabled(enabled);
204         ui->relativeWidth->setEnabled(enabled);
205     }
206     else if (sender() == ui->bootPos) {
207         ui->posAbsolute->setEnabled(enabled);
208         ui->posCenter->setEnabled(enabled);
209         ui->posLast->setEnabled(enabled);
210         ui->posRelative->setEnabled(enabled);
211         if (enabled) {
212             emit ui->posAbsolute->toggled(ui->posAbsolute->isChecked());
213             emit ui->posRelative->toggled(ui->posRelative->isChecked());
214         }
215         else {
216             emit ui->posAbsolute->toggled(false);
217             emit ui->posRelative->toggled(false);
218         }
219     }
220     else if (sender() == ui->posAbsolute) {
221         ui->absoluteLeft->setEnabled(enabled);
222         ui->absoluteTop->setEnabled(enabled);
223     }
224     else if (sender() == ui->posRelative) {
225         ui->relativeLeft->setEnabled(enabled);
226         ui->relativeTop->setEnabled(enabled);
227     }
228 }
229
230 void PreferenceDialog::selectBoxColor()
231 {
232     QColor color;
233     QPalette palette = ui->sampleEdit->palette();
234     if (sender() == ui->boxClrBg) {
235         color = palette.background().color();
236     }
237     else if (sender() == ui->boxClrFg) {
238         color = palette.text().color();
239     }
240
241     color = QColorDialog::getColor(color, this, tr("色選択"));
242     if (!color.isValid()) {
243         return;
244     }
245
246     if (sender() == ui->boxClrBg) {
247         palette.setColor(QPalette::Base, color);
248         ui->sampleEdit->setPalette(palette);
249     }
250     else if (sender() == ui->boxClrFg) {
251         palette.setColor(QPalette::Text, color);
252         ui->sampleEdit->setPalette(palette);
253     }
254 }
255
256 void PreferenceDialog::selectViewColor()
257 {
258     const QString objName = sender()->objectName();
259     QColor color = m_colorMap[objName];
260
261     color = QColorDialog::getColor(color, this, tr("色選択"));
262     if (!color.isValid()) {
263         return;
264     }
265
266     m_colorMap[objName] = color;
267     m_model.update();
268 }
269
270 void PreferenceDialog::accept()
271 {
272     QSettings settings;
273     QAbstractButton *selected;
274
275     //>>>>> 起動と終了
276     // 終了時の確認ダイアログ
277     settings.setValue(IniKey_ConfirmExit, ui->confirmExit->isChecked());
278     // 起動時のサイズ
279     if (!ui->bootSize->isChecked()) {
280         settings.setValue(IniKey_BootSizeSpec, "");
281     }
282     else {
283         selected = ui->sizeOptions->checkedButton();
284         settings.setValue(IniKey_BootSizeSpec, selected->objectName());
285         QSize size;
286         // 絶対指定
287         size = QSize(ui->absoluteWidth->value(), ui->absoluteHeight->value());
288         settings.setValue(IniKey_BootSizeAbs, size);
289         // 相対指定
290         size = QSize(ui->relativeWidth->value(), ui->relativeHeight->value());
291         settings.setValue(IniKey_BootSizeRel, size);
292     }
293     // 起動時の位置
294     if (!ui->bootPos->isChecked()) {
295         settings.setValue(IniKey_BootPosSpec, "");
296     }
297     else {
298         selected = ui->posOptions->checkedButton();
299         settings.setValue(IniKey_BootPosSpec, selected->objectName());
300         // 絶対指定
301         QPoint pos;
302         pos = QPoint(ui->absoluteLeft->value(), ui->absoluteTop->value());
303         settings.setValue(IniKey_BootPosAbs, pos);
304         // 相対指定
305         pos = QPoint(ui->relativeLeft->value(), ui->relativeTop->value());
306         settings.setValue(IniKey_BootPosRel, pos);
307     }
308     // 起動時の設定削除
309     settings.setValue(IniKey_ResetOnBoot, ui->resetOnBoot->isChecked());
310
311     //>>>>> 色とフォント
312     QFont font = ui->sampleEdit->font();
313     QPalette palette = ui->sampleEdit->palette();
314     settings.setValue(IniKey_BoxColorBg, palette.base().color());
315     settings.setValue(IniKey_BoxColorFg, palette.text().color());
316     settings.setValue(IniKey_BoxFont, font);
317
318     settings.setValue(IniKey_ViewColorBgMark, m_colorMap["clrBgMark"]);
319     settings.setValue(IniKey_ViewColorBgNormal, m_colorMap["clrBgNormal"]);
320     settings.setValue(IniKey_ViewColorFgHidden, m_colorMap["clrFgHidden"]);
321     settings.setValue(IniKey_ViewColorFgMark, m_colorMap["clrFgMark"]);
322     settings.setValue(IniKey_ViewColorFgNormal, m_colorMap["clrFgNormal"]);
323     settings.setValue(IniKey_ViewColorFgReadonly, m_colorMap["clrFgReadonly"]);
324     settings.setValue(IniKey_ViewColorFgSystem, m_colorMap["clrFgSystem"]);
325     settings.setValue(IniKey_ViewFont, m_model.font());
326
327     QDialog::accept();
328 }