-
Notifications
You must be signed in to change notification settings - Fork 48
/
config.cpp
377 lines (316 loc) · 13.3 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/****************************************************************************
* EZ Viewer
* Copyright (C) 2013 huangezhao. CHINA.
* Contact: huangezhao ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
#include "config.h"
#include "osrelated.h"
#include "actionmanager.h"
#include "qxmlputget.h"
#include <QFileSystemWatcher>
#include <QImageReader>
#define SPLIT_FUNCTION(x) &x, #x
#define SPLIT_PARAM(x) #x, x
const qreal Config::ZoomInRatioNomal = 0.1;
const qreal Config::ZoomInRatioSlow = 0.05;
const qreal Config::ZoomInRatioFast = 0.2;
const qreal Config::ZoomOutRatioNomal = -0.1;
const qreal Config::ZoomOutRatioSlow = -0.05;
const qreal Config::ZoomOutRatioFast = -0.2;
const int Config::DefaultScrollContentSpeed = 15;
const qreal Config::ScaleMaxLimit = 20.0;
const qreal Config::ScaleMinLimit = 0.1;
const QPointF Config::OriginPoint(0.0, 0.0);
const QSize Config::SizeAdjusted(0, 1);
const QSize Config::WindowMinSize(280, 200);
const QSize Config::WindowFitSize(800, 500);
const QString Config::DefaultBgColor = "#C7EDCC";
const QDir::SortFlags Config::DefaultDirSortFlag = QDir::LocaleAware;//QDir::Name | QDir::IgnoreCase;
const int Config::ClickInterval = 1500;
const int Config::ClickThreshold = 30;
const int Config::AutoScrollInterval = 20;
const int Config::FileSizePrecision = 2;
const bool DefaultShowDialog = true;
const bool DefaultEnableBgColor = true;
const bool DefaultAutoRotateImage = true;
const int DefaultTimerInterval = 4;
const int TimerIntervalMinLimit = 1;
const int TimerIntervalMaxLimit = 1000;
const int InvalidCacheNum = -1;
const int CacheNumMinLimit = 0;
const int CacheNumMaxLimit = 5;
Config *Config::sInstance = NULL;
const QString ConfigFileName = "EzViewer.ini";
const QString ShortcutFileName = "shortcut.xml";
const QString GeometryKey = "geometry";
const QString StartupGroup = "Startup";
const QString SizeModeKey = StartupGroup + "/SizeMode";
const QString DialogKey = StartupGroup + "/ShowDialog";
const QString EffectGroup = "Effect";
const QString ScaleModeKey = EffectGroup + "/ScaleMode";
const QString AlignModeKey = EffectGroup + "/AlignMode";
const QString AntialiasModeKey = EffectGroup + "/Antialiasing";
const QString EnableBgColorKey = EffectGroup + "/EnableBgColor";
const QString BgColorKey = EffectGroup + "/BgColor";
const QString AutoPlayGroup = "AutoPlay";
const QString TimerIntervalKey = AutoPlayGroup + "/TimerInterval";
const QString AdvancedGroup = "Advanced";
const QString AutoRotateKey = AdvancedGroup + "/AutoRotate";
const QString EnablePreReadingKey = AdvancedGroup + "/PreReading";
const QString CacheNumKey = AdvancedGroup + "/CacheValue";
const QString FormGroup = "Form";
const QString UseTitleBarKey = FormGroup + "/UseTitleBar";
const QString RootKey = "ezviewer";
const QString ShortcutKey = "shortcut";
const QString KeySequenceKey = "keys";
const QString ActionKey = "action";
Config::Config()
: QObject(qApp), cfgWatcher(new QFileSystemWatcher(this))
{
initConfigValue();
loadAllShortcut();
ActionManager::registerFunction(tr("Setting: Enable/Disable Custom Background Color"),
this, SPLIT_FUNCTION(Config::changeBgColorMode));
ActionManager::registerFunction(tr("Setting: Enable/Disable Pre-reading"),
this, SPLIT_FUNCTION(Config::changePreReadingMode));
ActionManager::registerFunction(tr("Setting Scale Mode: ") + tr("Scale Large Image to Fit Window"),
this, &Config::changeScaleMode, SPLIT_PARAM(ScaleLargeImageToFitWidget));
ActionManager::registerFunction(tr("Setting Scale Mode: ") + tr("Keep Image Size"),
this, &Config::changeScaleMode, SPLIT_PARAM(KeepImageSize));
ActionManager::registerFunction(tr("Setting Scale Mode: ") + tr("Fit Window Width"),
this, &Config::changeScaleMode, SPLIT_PARAM(FitWidgetWidth));
ActionManager::registerFunction(tr("Setting Scale Mode: ") + tr("Fit Window Height"),
this, &Config::changeScaleMode, SPLIT_PARAM(FitWidgetHeight));
ActionManager::registerFunction(tr("Setting Scale Mode: ") + tr("Scale to Fit Window"),
this, &Config::changeScaleMode, SPLIT_PARAM(ScaleToFitWidget));
ActionManager::registerFunction(tr("Setting Scale Mode: ") + tr("Scale to Expand Window"),
this, &Config::changeScaleMode, SPLIT_PARAM(ScaleToExpandWidget));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Left Top"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignLeftTop));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Left Center"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignLeftCenter));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Left Bottom"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignLeftBottom));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Center Top"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignCenterTop));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Center"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignCenterCenter));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Center Bottom"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignCenterBottom));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Right Top"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignRightTop));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Right Center"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignRightCenter));
ActionManager::registerFunction(tr("Setting Align Mode: ") + tr("Align Right Bottom"),
this, &Config::changeAlignMode, SPLIT_PARAM(AlignRightBottom));
ActionManager::registerFunction(tr("Setting Antialias Mode: ") + tr("Using When Pictures Zoom In"),
this, &Config::changeAntialiasMode, SPLIT_PARAM(AntialiasWhenZoomIn));
ActionManager::registerFunction(tr("Setting Antialias Mode: ") + tr("Always Using"),
this, &Config::changeAntialiasMode, SPLIT_PARAM(AlwaysAntialias));
ActionManager::registerFunction(tr("Setting Antialias Mode: ") + tr("Never Using"),
this, &Config::changeAntialiasMode, SPLIT_PARAM(NoAntialias));
QList<QByteArray> list = QImageReader::supportedImageFormats();
for(int i=0; i < list.size(); ++i)
mFormatsList.append(list.at(i));
mFormats = mFormatsList.join(" *.");
mFormats.prepend("*.");
watchConfigFile(ConfigFilePath());
watchConfigFile(ShortcutFilePath());
}
QString Config::ConfigFilePath()
{
return qApp->applicationDirPath() + "/" + ConfigFileName;
}
QString Config::ShortcutFilePath()
{
return qApp->applicationDirPath() + "/" + ShortcutFileName;
}
void Config::watchConfigFile(const QString &filePath)
{
if(!QFile::exists(filePath))
QFile(filePath).open(QIODevice::WriteOnly); // create config file
cfgWatcher->addPath(filePath);
connect(cfgWatcher, SIGNAL(fileChanged(QString)), SLOT(fileChanged(QString)));
}
void Config::fileChanged(const QString &filePath)
{
if (filePath == ConfigFilePath())
initConfigValue();
else if (filePath == ShortcutFilePath())
loadAllShortcut();
}
void Config::initConfigValue()
{
qDebug("initConfigValue");
QSettings settings(ConfigFilePath(), QSettings::IniFormat);
mShowDialog = settings.value(DialogKey, DefaultShowDialog).toBool();
mScaleMode = (ScaleMode)settings.value(ScaleModeKey, DefaultScaleMode).toInt();
mAlignMode = (AlignMode)settings.value(AlignModeKey, DefaultAlignMode).toInt();
mAntialiasMode = (AntialiasMode)settings.value(AntialiasModeKey, DefaultAntialiasMode).toInt();
mEnableBgColor = settings.value(EnableBgColorKey, DefaultEnableBgColor).toBool();
QString colorStr = settings.value(BgColorKey, DefaultBgColor).toString();
mTimerInterval = settings.value(TimerIntervalKey, DefaultTimerInterval).toInt();
mAutoRotateImage = settings.value(AutoRotateKey, DefaultAutoRotateImage).toBool();
mEnablePreReading = settings.value(
EnablePreReadingKey, OSRelated::preReadingSuggested()).toBool();
mCacheNum = settings.value(CacheNumKey, InvalidCacheNum).toInt();
mLastGeometry = settings.value(GeometryKey).toByteArray();
if (mScaleMode < ScaleModeBegin || mScaleMode > ScaleModeEnd)
mScaleMode = DefaultScaleMode;
if (mAlignMode < AlignModeBegin || mAlignMode > AlignModeEnd)
mAlignMode = DefaultAlignMode;
if(mAntialiasMode < AntialiasModeBegin || mAntialiasMode > AntialiasModeEnd)
mAntialiasMode = DefaultAntialiasMode;
mBgColor.setNamedColor(colorStr);
if(!mBgColor.isValid())
mBgColor.setNamedColor(DefaultBgColor);
if(mTimerInterval < TimerIntervalMinLimit || mTimerInterval > TimerIntervalMaxLimit)
mTimerInterval = DefaultTimerInterval;
if(mCacheNum < CacheNumMinLimit || mCacheNum > CacheNumMaxLimit)
mCacheNum = OSRelated::cacheSizeSuggested();
emit configChanged();
}
void Config::restoreDefaultsConfig()
{
QSettings(ConfigFilePath(), QSettings::IniFormat).clear();
}
void Config::insertConfigWatcher(const QObject *receiver, const char *method)
{
QObject::connect(instance(), SIGNAL(configChanged()), receiver, method);
}
void Config::cancelConfigWatcher(const QObject *receiver)
{
QObject::disconnect(instance(), 0, receiver, 0);
}
void Config::setShowDialog(bool enabled)
{
setValue(DialogKey, enabled);
}
void Config::setScaleMode(ScaleMode mode)
{
setValue(ScaleModeKey, mode);
}
void Config::setAlignMode(AlignMode mode)
{
setValue(AlignModeKey, mode);
}
void Config::setAntialiasMode(AntialiasMode mode)
{
setValue(AntialiasModeKey, mode);
}
void Config::setEnableBgColor(bool enabled)
{
setValue(EnableBgColorKey, enabled);
}
void Config::setBgColor(const QColor &color)
{
setValue(BgColorKey, color.name());
}
void Config::setTimerInterval(int interval)
{
setValue(TimerIntervalKey, interval);
}
void Config::setAutoRotateImage(bool enabled)
{
setValue(AutoRotateKey, enabled);
}
void Config::setEnablePreReading(bool enabled)
{
setValue(EnablePreReadingKey, enabled);
}
void Config::setCacheValue(int value)
{
setValue(CacheNumKey, value);
}
void Config::setLastGeometry(const QByteArray &geometry)
{
setValue(GeometryKey, geometry);
}
void Config::changeScaleMode(ScaleMode mode)
{
if (scaleMode() == mode)
setValue(ScaleModeKey, DefaultScaleMode);
else
setValue(ScaleModeKey, mode);
}
void Config::changeAlignMode(AlignMode mode)
{
if (alignMode() == mode)
setValue(AlignModeKey, DefaultAlignMode);
else
setValue(AlignModeKey, mode);
}
void Config::changeAntialiasMode(AntialiasMode mode)
{
if (antialiasMode() == mode)
setValue(AntialiasModeKey, DefaultAntialiasMode);
else
setValue(AntialiasModeKey, mode);
}
void Config::addShortcut(const QString &keySequence, const QString &actionScript)
{
if (ActionManager::bindShortcut(keySequence, actionScript))
saveAllShortcut();
}
void Config::addShortcut(const QStringList &keySequences, const QString &actionScript)
{
ActionManager::bindShortcut(keySequences, actionScript);
saveAllShortcut();
}
void Config::removeShortcut(const QString &keySequence)
{
if (ActionManager::unbindShortcut(keySequence))
saveAllShortcut();
}
void Config::removeShortcut(const QStringList &keySequences)
{
ActionManager::unbindShortcut(keySequences);
saveAllShortcut();
}
void Config::saveAllShortcut()
{
QXmlPut xmlPut(RootKey);
QMap<QString, QString> shortcuts = ActionManager::getAllShortcut();
QMap<QString, QString>::const_iterator it = shortcuts.constBegin();
while (it != shortcuts.constEnd()) {
xmlPut.descend(ShortcutKey);
xmlPut.putString(KeySequenceKey, it.key());
xmlPut.putString(ActionKey, it.value());
xmlPut.rise();
++it;
}
xmlPut.save(ShortcutFilePath());
}
void Config::loadAllShortcut()
{
ActionManager::unbindAllShortcut(); ///
QXmlGet xmlGet;
xmlGet.load(ShortcutFilePath());
QString key, action;
while (xmlGet.findNext(ShortcutKey)) {
xmlGet.descend();
if (xmlGet.find(KeySequenceKey))
key = xmlGet.getString();
if (xmlGet.find(ActionKey))
action = xmlGet.getString();
xmlGet.rise();
if (!key.isEmpty() && !action.isEmpty())
ActionManager::bindShortcut(key, action);
key.clear();
action.clear();
}
}