OSDN Git Service

Ver0.26
[gefu/Gefu.git] / preferences.cpp
1 #include "foldermodel.h"
2 #include "global.h"
3 #include "preferences.h"
4
5 #include <QApplication>
6 #include <QDebug>
7 #include <QDesktopWidget>
8
9 ///////////////////////////////////////////////////////////////////////////////
10 /// \brief DefaultPreferExtensions
11 /// \return 外部アプリを優先する拡張子のデフォルトを返します。
12 ///
13 QString DefaultPreferExtensions()
14 {
15     QStringList list;
16
17     // 画像系
18     list << "ico" << "ai" << "psd" << "xcf" << "tif" << "tiff" << "wmf";
19     // 音・動画系
20     list << "wav" << "mp3" << "ogg" << "midi" << "mid" << "aif" << "aiff";
21     list << "mov" << "mpg" << "mpeg" << "wma" << "wmv" << "asf" << "avi";
22     list << "flac" << "mkv";
23     // 実行ファイル系
24     list << "exe" << "com" << "msi" << "scr";
25     // アーカイブ系
26     list << "lzh" << "zip" << "cab" << "tar" << "rar" << "gz" << "tgz";
27     list << "bz2" << "xz" << "jar" << "7z" << "dmg";
28     // ドキュメント系
29     list << "pdf" << "doc" << "docx" << "xls" << "xlsx" << "ppt" << "pptx";
30     // フォント
31     list << "ttf" << "ttc";
32
33     list.sort();
34
35     return list.join(",");
36 }
37
38 ///////////////////////////////////////////////////////////////////////////////
39 /// \brief DefaultFixedFont
40 /// \return デフォルトの固定幅フォントを返します。
41 ///
42 QFont DefaultFixedFont()
43 {
44     QFont font = qApp->font();
45 #ifdef Q_OS_WIN
46     font.setFamily("MS ゴシック");
47 #elif defined(Q_OS_MAC)
48     font.setFamily("Monaco");
49 #else
50 #endif
51     return font;
52 }
53
54 ///////////////////////////////////////////////////////////////////////////////
55 /// \brief Preferences::Preferences
56 /// \param parent   親オブジェクト
57 ///
58 /// コンストラクタ
59 ///
60 Preferences::Preferences(QObject *parent) :
61     QSettings(QSettings::IniFormat,
62               QSettings::UserScope,
63               qApp->organizationName(),
64               qApp->applicationName(),
65               parent)
66 {
67 }
68
69 ///////////////////////////////////////////////////////////////////////////////
70 /// \brief Preferences::Preferences
71 /// \param path     設定ファイルのパス
72 /// \param parent   親オブジェクト
73 ///
74 /// コンストラクタ
75 ///
76 Preferences::Preferences(const QString &path, QObject *parent) :
77     QSettings(path, QSettings::IniFormat, parent)
78 {
79 }
80
81 #define Key_Filter(side)        (side + "/Filter")
82 #define Key_NameFilter(side)    (side + "/NameFilter")
83 #define Key_Sort(side)          (side + "/Sort")
84 #define Key_Path(side)          (side + "/Path")
85 ///////////////////////////////////////////////////////////////////////////////
86 /// \brief Preferences::restoreModel
87 /// \param side "Left" or "Right"
88 /// \param m    フォルダモデルオブジェクト
89 ///
90 /// モデルの状態を復元します。
91 ///
92 void Preferences::restoreModel(const QString &side, FolderModel *m)
93 {
94     qDebug() << "Preferences::restoreModel()" << side;
95
96     int filter = value(Key_Filter(side), 0).toInt();
97     filter |= QDir::NoDot | QDir::AllDirs | QDir::Files;
98     m->setFilter(QFlag(filter));
99
100     QString nameFilter = value(Key_NameFilter(side), "*").toString();
101     m->setNameFilters(nameFilter.split(" ", QString::SkipEmptyParts));
102
103     int sort = QDir::Name | QDir::DirsFirst | QDir::IgnoreCase;
104     sort = value(Key_Sort(side), sort).toInt();
105     m->setSorting(QFlag(sort));
106
107     QString path = QDir::homePath();
108     path = value(Key_Path(side), path).toString();
109     m->setRootPath(path);
110 }
111
112 ///////////////////////////////////////////////////////////////////////////////
113 /// \brief Preferences::saveModel
114 /// \param side "Left" or "Right"
115 /// \param m    フォルダモデルオブジェクト
116 ///
117 /// モデルの状態を保存します。
118 ///
119 void Preferences::saveModel(const QString &side, const FolderModel *m)
120 {
121     qDebug() << "Preferences::saveModel()" << side;
122
123     setValue(Key_Filter(side), static_cast<int>(m->filter()));
124     setValue(Key_Sort(side), static_cast<int>(m->sorting()));
125     setValue(Key_Path(side), m->rootPath());
126 }
127
128 #define Key_WindowGeometry  "WindowGeometry"
129 #define Key_WindowState     "WindowState"
130 ///////////////////////////////////////////////////////////////////////////////
131 /// \brief Preferences::restoreWindow
132 /// \param w    メインウィンドウオブジェクト
133 ///
134 /// メインウィンドウの状態を復元します。
135 ///
136 void Preferences::restoreWindow(QMainWindow *w)
137 {
138     qDebug() << "Preferences::restoreWindow()";
139
140     QByteArray geometry = value(Key_WindowGeometry).toByteArray();
141     if (geometry.isNull()) {
142         QSize size(700, 400);
143         QPoint point;
144         point.setX((qApp->desktop()->width() - size.width()) / 2);
145         point.setY((qApp->desktop()->height() - size.height()) / 2);
146         w->setGeometry(QRect(point, size));
147     }
148     else {
149         w->restoreGeometry(geometry);
150     }
151
152     QByteArray state = value(Key_WindowState).toByteArray();
153     if (!state.isEmpty()) {
154         w->restoreState(state);
155     }
156 }
157
158 ///////////////////////////////////////////////////////////////////////////////
159 /// \brief Preferences::saveWindow
160 /// \param w    メインウィンドウオブジェクト
161 ///
162 /// メインウィンドウの状態を保存します。
163 ///
164 void Preferences::saveWindow(const QMainWindow *w)
165 {
166     setValue(Key_WindowGeometry, w->saveGeometry());
167     setValue(Key_WindowState, w->saveState());
168 }
169
170 #define Key_BookmarkEntry(i)    QString("Bookmark/Entry%1").arg(i)
171 #define Key_BookmarkPath(i)     QString("Bookmark/Path%1").arg(i)
172 ///////////////////////////////////////////////////////////////////////////////
173 /// \brief Preferences::addBookmark
174 /// \param name 名前
175 /// \param path パス
176 ///
177 /// ブックマークを追加します。
178 ///
179 void Preferences::addBookmark(const QString &name, const QString &path)
180 {
181     int n;
182     for (n = 1; !value(Key_BookmarkEntry(n)).isNull(); n++)
183         ;
184
185     setValue(Key_BookmarkEntry(n), name);
186     setValue(Key_BookmarkPath(n), path);
187 }
188
189 ///////////////////////////////////////////////////////////////////////////////
190 /// \brief Preferences::clearBookmark
191 ///
192 /// ブックマークをクリアします。
193 ///
194 void Preferences::clearBookmark()
195 {
196     beginGroup("Bookmark");
197     remove("");
198     endGroup();
199 }
200
201 ///////////////////////////////////////////////////////////////////////////////
202 /// \brief Preferences::getBookmarkEntry
203 /// \param n    番号
204 /// \return n番目のブックマーク名を取得します。
205 ///
206 QString Preferences::getBookmarkEntry(int n) const
207 {
208     return value(Key_BookmarkEntry(n)).toString();
209 }
210
211 ///////////////////////////////////////////////////////////////////////////////
212 /// \brief Preferences::getBookmarkPath
213 /// \param n    番号
214 /// \return n番目のブックマークパスを取得します。
215 ///
216 QString Preferences::getBookmarkPath(int n) const
217 {
218     return value(Key_BookmarkPath(n)).toString();
219 }
220
221 ///////////////////////////////////////////////////////////////////////////////
222 /// \brief Preferences::folderViewFgColor
223 /// \param active   アクティブならtrue
224 /// \return フォルダービューの文字色を返します。
225 ///
226 QColor Preferences::folderViewFgColor(bool active) const
227 {
228     int darkFactor = 100;
229     if (!active) {
230         darkFactor += getDarkFacotr() * 100;
231     }
232     return getFolderViewFgColor().darker(darkFactor);
233 }
234
235 ///////////////////////////////////////////////////////////////////////////////
236 /// \brief Preferences::folderViewBgColor
237 /// \param active   アクティブならtrue
238 /// \return フォルダービューの背景色を返します。
239 ///
240 QColor Preferences::folderViewBgColor(bool active) const
241 {
242     int darkFactor = 100;
243     if (!active) {
244         darkFactor += getDarkFacotr() * 100;
245     }
246     return getFolderViewBgColor().darker(darkFactor);
247 }
248
249 ///////////////////////////////////////////////////////////////////////////////
250 /// \brief Preferences::folderViewMarkedFgColor
251 /// \param active   アクティブならtrue
252 /// \return フォルダービューのマーク文字色を返します。
253 ///
254 QColor Preferences::folderViewMarkedFgColor(bool active) const
255 {
256     int darkFactor = 100;
257     if (!active) {
258         darkFactor += getDarkFacotr() * 100;
259     }
260     return getFolderViewMarkedFgColor().darker(darkFactor);
261 }
262
263 ///////////////////////////////////////////////////////////////////////////////
264 /// \brief Preferences::folderViewMarkedBgColor
265 /// \param active   アクティブならtrue
266 /// \return フォルダービューのマーク背景色を返します。
267 ///
268 QColor Preferences::folderViewMarkedBgColor(bool active) const
269 {
270     int darkFactor = 100;
271     if (!active) {
272         darkFactor += getDarkFacotr() * 100;
273     }
274     return getFolderViewMarkedBgColor().darker(darkFactor);
275 }
276
277 ///////////////////////////////////////////////////////////////////////////////
278 /// \brief Preferences::folderViewSystemColor
279 /// \param active   アクティブならtrue
280 /// \return システム属性の文字色を返します。
281 ///
282 QColor Preferences::folderViewSystemColor(bool active) const
283 {
284     int darkFactor = 100;
285     if (!active) {
286         darkFactor += getDarkFacotr() * 100;
287     }
288     return getFolderViewSystemColor().darker(darkFactor);
289 }
290
291 ///////////////////////////////////////////////////////////////////////////////
292 /// \brief Preferences::folderViewHiddenColor
293 /// \param active   アクティブならtrue
294 /// \return 隠し属性の文字色を返します。
295 ///
296 QColor Preferences::folderViewHiddenColor(bool active) const
297 {
298     int darkFactor = 100;
299     if (!active) {
300         darkFactor += getDarkFacotr() * 100;
301     }
302     return getFolderViewHiddenColor().darker(darkFactor);
303 }
304
305 ///////////////////////////////////////////////////////////////////////////////
306 /// \brief Preferences::folderViewReadOnlyColor
307 /// \param active   アクティブならtrue
308 /// \return 読取専用属性の文字色を返します。
309 ///
310 QColor Preferences::folderViewReadOnlyColor(bool active) const
311 {
312     int darkFactor = 100;
313     if (!active) {
314         darkFactor += getDarkFacotr() * 100;
315     }
316     return getFolderViewReadOnlyColor().darker(darkFactor);
317 }
318
319 ///////////////////////////////////////////////////////////////////////////////
320 /// \brief Preferences::locationBoxFgColor
321 /// \param active   アクティブならtrue
322 /// \return ロケーションボックスの文字色を返します。
323 ///
324 QColor Preferences::locationBoxFgColor(bool active) const
325 {
326     int darkFactor = 100;
327     if (!active) {
328         darkFactor += getDarkFacotr() * 100;
329     }
330     return getLocationBoxFgColor().darker(darkFactor);
331 }
332
333 ///////////////////////////////////////////////////////////////////////////////
334 /// \brief Preferences::locationBoxBgColor
335 /// \param active   アクティブならtrue
336 /// \return ロケーションボックスの背景色を返します。
337 ///
338 QColor Preferences::locationBoxBgColor(bool active) const
339 {
340     int darkFactor = 100;
341     if (!active) {
342         darkFactor += getDarkFacotr() * 100;
343     }
344     return getLocationBoxBgColor().darker(darkFactor);
345 }
346
347 #define IMPLEMENT_BOOL(key, defVal)                 \
348     bool Preferences::is##key() const {             \
349         return value(#key, def##key()).toBool();    \
350     }                                               \
351     bool Preferences::def##key() const {            \
352         return defVal;                              \
353     }                                               \
354     void Preferences::set##key(bool value) {        \
355         setValue(#key, value);                      \
356     }
357
358 IMPLEMENT_BOOL(Reset, false)
359 IMPLEMENT_BOOL(CheckUpdate, true)
360 IMPLEMENT_BOOL(ConfirmQuit, true)
361 IMPLEMENT_BOOL(ConfirmCopy, true)
362 IMPLEMENT_BOOL(AutoCloseCopy, false)
363 IMPLEMENT_BOOL(ConfirmMove, true)
364 IMPLEMENT_BOOL(AutoCloseMove, false)
365 IMPLEMENT_BOOL(ConfirmDelete, true)
366 IMPLEMENT_BOOL(AutoCloseDelete, false)
367 IMPLEMENT_BOOL(ConfirmRename, true)
368 IMPLEMENT_BOOL(AutoCloseRename, false)
369 IMPLEMENT_BOOL(OpenAfterCreation, false)
370 IMPLEMENT_BOOL(MoveAfterCreation, false)
371
372 #define IMPLEMENT_STRING(key, defVal)                   \
373     QString Preferences::get##key() const {             \
374         return value(#key, def##key()).toString();      \
375     }                                                   \
376     QString Preferences::def##key() const {             \
377         return defVal;                                  \
378     }                                                   \
379     void Preferences::set##key(const QString &value) {  \
380         setValue(#key, value);                          \
381     }
382
383 #ifdef Q_OS_LINUX
384
385 #elif defined(Q_OS_MAC)
386     IMPLEMENT_STRING(ArchiverPath, QQ("/System/Library/CoreServices/Archive Utility.app"))
387     IMPLEMENT_STRING(EditorPath, "/Applications/TextEdit.app")
388     IMPLEMENT_STRING(TerminalPath, "/Applications/Utilities/Terminal.app --args -c cd")
389 #else
390     IMPLEMENT_STRING(ArchiverPath, QQ("C:/Program Files/Lhaplus/Lhaplus.exe"))
391     IMPLEMENT_STRING(EditorPath, "notepad.exe")
392     IMPLEMENT_STRING(TerminalPath, "cmd.exe /k cd")
393 #endif
394
395 IMPLEMENT_STRING(CopyBehavior, "OverWriteIfNew")
396 IMPLEMENT_STRING(PreferExtensions, DefaultPreferExtensions())
397
398 #define IMPLEMENT_COLOR(key, defVal)                                    \
399     QColor Preferences::get##key() const {                              \
400         return value("Appearance/"#key, def##key()).value<QColor>();    \
401     }                                                                   \
402     QColor Preferences::def##key() const {                              \
403         return defVal;                                                  \
404     }                                                                   \
405     void Preferences::set##key(const QColor &value) {                   \
406         setValue("Appearance/"#key, value);                             \
407     }
408
409 IMPLEMENT_COLOR(FolderViewFgColor, QPalette().text().color())
410 IMPLEMENT_COLOR(FolderViewBgColor, QPalette().base().color())
411 IMPLEMENT_COLOR(FolderViewMarkedFgColor, QColor(128, 0, 0))
412 IMPLEMENT_COLOR(FolderViewMarkedBgColor, QColor(0, 192, 0))
413 IMPLEMENT_COLOR(FolderViewSystemColor, QColor(128, 0, 128))
414 IMPLEMENT_COLOR(FolderViewHiddenColor, QColor(128, 128, 128))
415 IMPLEMENT_COLOR(FolderViewReadOnlyColor, QColor(0, 128, 0))
416 IMPLEMENT_COLOR(LocationBoxFgColor, QPalette().text().color())
417 IMPLEMENT_COLOR(LocationBoxBgColor, QPalette().base().color())
418 IMPLEMENT_COLOR(SearchBoxFgColor, QPalette().text().color())
419 IMPLEMENT_COLOR(SearchBoxBgColor, QPalette().base().color())
420 IMPLEMENT_COLOR(SearchBoxUnmatchFgColor, QColor(255, 0, 0))
421 IMPLEMENT_COLOR(SearchBoxUnmatchBgColor, QPalette().base().color())
422 IMPLEMENT_COLOR(TextViewFgColor, QPalette().text().color())
423 IMPLEMENT_COLOR(TextViewCtrlColor, Qt::cyan)
424 IMPLEMENT_COLOR(TextViewBgColor, QPalette().base().color())
425 IMPLEMENT_COLOR(HexViewFgColor, getTextViewFgColor())
426 IMPLEMENT_COLOR(HexViewBgColor, getTextViewBgColor())
427 IMPLEMENT_COLOR(ImageViewBgColor, QPalette().base().color())
428
429 #define IMPLEMENT_DOUBLE(key, defVal)                           \
430     double Preferences::get##key() const {                      \
431         return value("Appearance/"#key, def##key()).toDouble(); \
432     }                                                           \
433     double Preferences::def##key() const {                      \
434         return defVal;                                          \
435     }                                                           \
436     void Preferences::set##key(double value) {                  \
437         setValue("Appearance/"#key, value);                     \
438     }
439
440 IMPLEMENT_DOUBLE(DarkFacotr, 0)
441 IMPLEMENT_DOUBLE(LineHeight, 1.5)
442 IMPLEMENT_DOUBLE(TextViewLineHeight, 1.0)
443
444 #define IMPLEMENT_FONT(key, defVal)                                 \
445     QFont Preferences::get##key() const {                           \
446         return value("Appearance/"#key, def##key()).value<QFont>(); \
447     }                                                               \
448     QFont Preferences::def##key() const {                           \
449         return defVal;                                              \
450     }                                                               \
451     void Preferences::set##key(const QFont &value) {                \
452         setValue("Appearance/"#key, value);                         \
453     }
454
455 IMPLEMENT_FONT(FolderViewFont, DefaultFixedFont())
456 IMPLEMENT_FONT(LocationBoxFont, DefaultFixedFont())
457 IMPLEMENT_FONT(SearchBoxFont, DefaultFixedFont())
458 IMPLEMENT_FONT(TextViewFont, DefaultFixedFont())
459 IMPLEMENT_FONT(HexViewFont, getTextViewFont())