OSDN Git Service

パネル自体の背景色を変更
[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 QString DefaultPreferExtensions()
10 {
11     QStringList list;
12
13     // 画像系
14     list << "ico" << "ai" << "psd" << "xcf" << "tif" << "tiff" << "wmf";
15     // 音・動画系
16     list << "wav" << "mp3" << "ogg" << "midi" << "mid" << "aif" << "aiff";
17     list << "mov" << "mpg" << "mpeg" << "wma" << "wmv" << "asf" << "avi";
18     list << "flac" << "mkv";
19     // 実行ファイル系
20     list << "exe" << "com" << "msi" << "scr";
21     // アーカイブ系
22     list << "lzh" << "zip" << "cab" << "tar" << "rar" << "gz" << "tgz";
23     list << "bz2" << "xz" << "jar" << "7z" << "dmg";
24     // ドキュメント系
25     list << "pdf" << "doc" << "docx" << "xls" << "xlsx" << "ppt" << "pptx";
26     // フォント
27     list << "ttf" << "ttc";
28
29     list.sort();
30
31     return list.join(",");
32 }
33
34 QFont DefaultFixedFont()
35 {
36     QFont font = qApp->font();
37 #ifdef Q_OS_WIN
38     font.setFamily("MS ゴシック");
39 #elif defined(Q_OS_MAC)
40     font.setFamily("Monaco");
41 #else
42 #endif
43     return font;
44 }
45
46 Preferences::Preferences(QObject *parent) :
47     QSettings(QSettings::IniFormat,
48               QSettings::UserScope,
49               qApp->organizationName(),
50               qApp->applicationName(),
51               parent)
52 {
53 }
54
55 Preferences::Preferences(const QString &path, QObject *parent) :
56     QSettings(path, QSettings::IniFormat, parent)
57 {
58 }
59
60 #define Key_Filter(side)        (side + "/Filter")
61 #define Key_NameFilter(side)    (side + "/NameFilter")
62 #define Key_Sort(side)          (side + "/Sort")
63 #define Key_Path(side)          (side + "/Path")
64 void Preferences::restoreModel(const QString &side, FolderModel *m)
65 {
66     qDebug() << "Preferences::restoreModel()" << side;
67
68     int filter = value(Key_Filter(side), 0).toInt();
69     filter |= QDir::NoDot | QDir::AllDirs | QDir::Files;
70     m->setFilter(QFlag(filter));
71
72     QString nameFilter = value(Key_NameFilter(side), "*").toString();
73     m->setNameFilters(nameFilter.split(" ", QString::SkipEmptyParts));
74
75     int sort = QDir::Name | QDir::DirsFirst | QDir::IgnoreCase;
76     sort = value(Key_Sort(side), sort).toInt();
77     m->setSorting(QFlag(sort));
78
79     QString path = QDir::homePath();
80     path = value(Key_Path(side), path).toString();
81     m->setRootPath(path);
82 }
83
84 void Preferences::saveModel(const QString &side, const FolderModel *m)
85 {
86     qDebug() << "Preferences::saveModel()" << side;
87
88     setValue(Key_Filter(side), static_cast<int>(m->filter()));
89     setValue(Key_Sort(side), static_cast<int>(m->sorting()));
90     setValue(Key_Path(side), m->rootPath());
91 }
92
93 #define Key_WindowGeometry  "WindowGeometry"
94 #define Key_WindowState     "WindowState"
95 void Preferences::restoreWindow(QMainWindow *w)
96 {
97     qDebug() << "Preferences::restoreWindow()";
98
99     QByteArray geometry = value(Key_WindowGeometry).toByteArray();
100     if (geometry.isNull()) {
101         QSize size(700, 400);
102         QPoint point;
103         point.setX((qApp->desktop()->width() - size.width()) / 2);
104         point.setY((qApp->desktop()->height() - size.height()) / 2);
105         w->setGeometry(QRect(point, size));
106     }
107     else {
108         w->restoreGeometry(geometry);
109     }
110
111     QByteArray state = value(Key_WindowState).toByteArray();
112     if (!state.isEmpty()) {
113         w->restoreState(state);
114     }
115 }
116
117 void Preferences::saveWindow(const QMainWindow *w)
118 {
119     setValue(Key_WindowGeometry, w->saveGeometry());
120     setValue(Key_WindowState, w->saveState());
121 }
122
123 ///////////////////////////////////////////////////////////////////////////////
124 #define Key_BookmarkEntry(i)    QString("Bookmark/Entry%1").arg(i)
125 #define Key_BookmarkPath(i)     QString("Bookmark/Path%1").arg(i)
126 void Preferences::addBookmark(const QString &name, const QString &path)
127 {
128     int n;
129     for (n = 1; !value(Key_BookmarkEntry(n)).isNull(); n++)
130         ;
131
132     setValue(Key_BookmarkEntry(n), name);
133     setValue(Key_BookmarkPath(n), path);
134 }
135
136 void Preferences::clearBookmark()
137 {
138     beginGroup("Bookmark");
139     remove("");
140     endGroup();
141 }
142
143 QString Preferences::getBookmarkEntry(int n) const
144 {
145     return value(Key_BookmarkEntry(n)).toString();
146 }
147
148 QString Preferences::getBookmarkPath(int n) const
149 {
150     return value(Key_BookmarkPath(n)).toString();
151 }
152
153 QColor Preferences::folderViewFgColor(bool active) const
154 {
155     int darkFactor = 100;
156     if (!active) {
157         darkFactor += getDarkFacotr() * 100;
158     }
159     return getFolderViewFgColor().darker(darkFactor);
160 }
161
162 QColor Preferences::folderViewBgColor(bool active) const
163 {
164     int darkFactor = 100;
165     if (!active) {
166         darkFactor += getDarkFacotr() * 100;
167     }
168     return getFolderViewBgColor().darker(darkFactor);
169 }
170
171 QColor Preferences::folderViewMarkedFgColor(bool active) const
172 {
173     int darkFactor = 100;
174     if (!active) {
175         darkFactor += getDarkFacotr() * 100;
176     }
177     return getFolderViewMarkedFgColor().darker(darkFactor);
178 }
179
180 QColor Preferences::folderViewMarkedBgColor(bool active) const
181 {
182     int darkFactor = 100;
183     if (!active) {
184         darkFactor += getDarkFacotr() * 100;
185     }
186     return getFolderViewMarkedBgColor().darker(darkFactor);
187 }
188
189 QColor Preferences::folderViewSystemColor(bool active) const
190 {
191     int darkFactor = 100;
192     if (!active) {
193         darkFactor += getDarkFacotr() * 100;
194     }
195     return getFolderViewSystemColor().darker(darkFactor);
196 }
197
198 QColor Preferences::folderViewHiddenColor(bool active) const
199 {
200     int darkFactor = 100;
201     if (!active) {
202         darkFactor += getDarkFacotr() * 100;
203     }
204     return getFolderViewHiddenColor().darker(darkFactor);
205 }
206
207 QColor Preferences::folderViewReadOnlyColor(bool active) const
208 {
209     int darkFactor = 100;
210     if (!active) {
211         darkFactor += getDarkFacotr() * 100;
212     }
213     return getFolderViewReadOnlyColor().darker(darkFactor);
214 }
215
216 QColor Preferences::locationBoxFgColor(bool active) const
217 {
218     int darkFactor = 100;
219     if (!active) {
220         darkFactor += getDarkFacotr() * 100;
221     }
222     return getLocationBoxFgColor().darker(darkFactor);
223 }
224
225 QColor Preferences::locationBoxBgColor(bool active) const
226 {
227     int darkFactor = 100;
228     if (!active) {
229         darkFactor += getDarkFacotr() * 100;
230     }
231     return getLocationBoxBgColor().darker(darkFactor);
232 }
233
234 #define IMPLEMENT_BOOL(key, defVal)                 \
235     bool Preferences::is##key() const {             \
236         return value(#key, def##key()).toBool();    \
237     }                                               \
238     bool Preferences::def##key() const {            \
239         return defVal;                              \
240     }                                               \
241     void Preferences::set##key(bool value) {        \
242         setValue(#key, value);                      \
243     }
244
245 IMPLEMENT_BOOL(Reset, false)
246 IMPLEMENT_BOOL(CheckUpdate, true)
247 IMPLEMENT_BOOL(ConfirmQuit, true)
248 IMPLEMENT_BOOL(ConfirmCopy, true)
249 IMPLEMENT_BOOL(AutoCloseCopy, false)
250 IMPLEMENT_BOOL(ConfirmMove, true)
251 IMPLEMENT_BOOL(AutoCloseMove, false)
252 IMPLEMENT_BOOL(ConfirmDelete, true)
253 IMPLEMENT_BOOL(AutoCloseDelete, false)
254 IMPLEMENT_BOOL(ConfirmRename, true)
255 IMPLEMENT_BOOL(AutoCloseRename, false)
256 IMPLEMENT_BOOL(OpenAfterCreation, false)
257 IMPLEMENT_BOOL(MoveAfterCreation, false)
258
259 #define IMPLEMENT_STRING(key, defVal)                   \
260     QString Preferences::get##key() const {             \
261         return value(#key, def##key()).toString();      \
262     }                                                   \
263     QString Preferences::def##key() const {             \
264         return defVal;                                  \
265     }                                                   \
266     void Preferences::set##key(const QString &value) {  \
267         setValue(#key, value);                          \
268     }
269
270 #ifdef Q_OS_LINUX
271
272 #elif defined(Q_OS_MAC)
273     IMPLEMENT_STRING(ArchiverPath, QQ("/System/Library/CoreServices/Archive Utility.app"))
274     IMPLEMENT_STRING(EditorPath, "/Applications/TextEdit.app")
275     IMPLEMENT_STRING(TerminalPath, "/Applications/Utilities/Terminal.app --args -c cd")
276 #else
277     IMPLEMENT_STRING(ArchiverPath, QQ("C:/Program Files/Lhaplus/Lhaplus.exe"))
278     IMPLEMENT_STRING(EditorPath, "notepad.exe")
279     IMPLEMENT_STRING(TerminalPath, "cmd.exe /k cd")
280 #endif
281
282 IMPLEMENT_STRING(CopyBehavior, "OverWriteIfNew")
283 IMPLEMENT_STRING(PreferExtensions, DefaultPreferExtensions())
284
285 #define IMPLEMENT_COLOR(key, defVal)                                    \
286     QColor Preferences::get##key() const {                              \
287         return value("Appearance/"#key, def##key()).value<QColor>();    \
288     }                                                                   \
289     QColor Preferences::def##key() const {                              \
290         return defVal;                                                  \
291     }                                                                   \
292     void Preferences::set##key(const QColor &value) {                   \
293         setValue("Appearance/"#key, value);                             \
294     }
295
296 IMPLEMENT_COLOR(FolderViewFgColor, QPalette().text().color())
297 IMPLEMENT_COLOR(FolderViewBgColor, QPalette().base().color())
298 IMPLEMENT_COLOR(FolderViewMarkedFgColor, QColor(128, 0, 0))
299 IMPLEMENT_COLOR(FolderViewMarkedBgColor, QColor(0, 192, 0))
300 IMPLEMENT_COLOR(FolderViewSystemColor, QColor(128, 0, 128))
301 IMPLEMENT_COLOR(FolderViewHiddenColor, QColor(128, 128, 128))
302 IMPLEMENT_COLOR(FolderViewReadOnlyColor, QColor(0, 128, 0))
303 IMPLEMENT_COLOR(LocationBoxFgColor, QPalette().text().color())
304 IMPLEMENT_COLOR(LocationBoxBgColor, QPalette().base().color())
305 IMPLEMENT_COLOR(SearchBoxFgColor, QPalette().text().color())
306 IMPLEMENT_COLOR(SearchBoxBgColor, QPalette().base().color())
307 IMPLEMENT_COLOR(SearchBoxUnmatchFgColor, QColor(255, 0, 0))
308 IMPLEMENT_COLOR(SearchBoxUnmatchBgColor, QPalette().base().color())
309 IMPLEMENT_COLOR(TextViewFgColor, QPalette().text().color())
310 IMPLEMENT_COLOR(TextViewBgColor, QPalette().base().color())
311 IMPLEMENT_COLOR(ImageViewBgColor, QPalette().base().color())
312
313 #define IMPLEMENT_DOUBLE(key, defVal)                           \
314     double Preferences::get##key() const {                      \
315         return value("Appearance/"#key, def##key()).toDouble(); \
316     }                                                           \
317     double Preferences::def##key() const {                      \
318         return defVal;                                          \
319     }                                                           \
320     void Preferences::set##key(double value) {                  \
321         setValue("Appearance/"#key, value);                     \
322     }
323
324 IMPLEMENT_DOUBLE(DarkFacotr, 0)
325 IMPLEMENT_DOUBLE(LineHeight, 1.5)
326
327 #define IMPLEMENT_FONT(key, defVal)                                 \
328     QFont Preferences::get##key() const {                           \
329         return value("Appearance/"#key, def##key()).value<QFont>(); \
330     }                                                               \
331     QFont Preferences::def##key() const {                           \
332         return defVal;                                              \
333     }                                                               \
334     void Preferences::set##key(const QFont &value) {                \
335         setValue("Appearance/"#key, value);                         \
336     }
337
338 IMPLEMENT_FONT(FolderViewFont, DefaultFixedFont())
339 IMPLEMENT_FONT(LocationBoxFont, DefaultFixedFont())
340 IMPLEMENT_FONT(SearchBoxFont, DefaultFixedFont())
341 IMPLEMENT_FONT(TextViewFont, DefaultFixedFont())