1 /*
2 * preferences.h
3 * Copyright 2009-2011, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4 * Copyright 2016, Mamed Ibrahimov <ibramlab@gmail.com>
5 *
6 * This file is part of Tiled.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #pragma once
23
24 #include <QColor>
25 #include <QDateTime>
26 #include <QObject>
27 #include <QSettings>
28
29 #include "filesystemwatcher.h"
30 #include "map.h"
31 #include "objecttypes.h"
32
33 namespace Tiled {
34
35 /**
36 * This class holds user preferences and provides a convenient interface to
37 * access them.
38 *
39 * Since it derives from QSettings, you can also store/retrieve arbitrary
40 * values. The naming style for groups and keys is CamelCase.
41 */
42 class Preferences : public QSettings
43 {
44 Q_OBJECT
45
46 public:
47 static Preferences *instance();
48 static void deleteInstance();
49
50 private:
51 Preferences();
52 Preferences(const QString &fileName);
53 ~Preferences() override;
54
55 void initialize();
56
57 public:
58 bool showGrid() const;
59 bool showTileObjectOutlines() const;
60 bool showTileAnimations() const;
61 bool showTileCollisionShapes() const;
62 bool showObjectReferences() const;
63 bool parallaxEnabled() const;
64 bool snapToGrid() const;
65 bool snapToFineGrid() const;
66 bool snapToPixels() const;
67 QColor gridColor() const;
68 QColor backgroundFadeColor() const;
69 int gridFine() const;
70 int gridMajor() const;
71 qreal objectLineWidth() const;
72
73 bool highlightCurrentLayer() const;
74 bool highlightHoveredObject() const;
75 bool showTilesetGrid() const;
76
77 enum ObjectLabelVisiblity {
78 NoObjectLabels,
79 SelectedObjectLabels,
80 AllObjectLabels
81 };
82
83 ObjectLabelVisiblity objectLabelVisibility() const;
84 void setObjectLabelVisibility(ObjectLabelVisiblity visiblity);
85
86 bool labelForHoveredObject() const;
87 void setLabelForHoveredObject(bool enabled);
88
89 enum ApplicationStyle {
90 SystemDefaultStyle,
91 FusionStyle,
92 TiledStyle
93 };
94
95 ApplicationStyle applicationStyle() const;
96 void setApplicationStyle(ApplicationStyle style);
97
98 QColor baseColor() const;
99 void setBaseColor(const QColor &color);
100
101 QColor selectionColor() const;
102 void setSelectionColor(const QColor &color);
103
104 Map::LayerDataFormat layerDataFormat() const;
105 void setLayerDataFormat(Map::LayerDataFormat layerDataFormat);
106
107 Map::RenderOrder mapRenderOrder() const;
108 void setMapRenderOrder(Map::RenderOrder mapRenderOrder);
109
110 bool safeSavingEnabled() const;
111 void setSafeSavingEnabled(bool enabled);
112
113 bool exportOnSave() const;
114 void setExportOnSave(bool enabled);
115
116 enum ExportOption {
117 EmbedTilesets = 0x1,
118 DetachTemplateInstances = 0x2,
119 ResolveObjectTypesAndProperties = 0x4,
120 ExportMinimized = 0x8,
121 };
122 Q_DECLARE_FLAGS(ExportOptions, ExportOption)
123
124 ExportOptions exportOptions() const;
125 void setExportOption(ExportOption option, bool value);
126 bool exportOption(ExportOption option) const;
127
128 QString language() const;
129 void setLanguage(const QString &language);
130
131 bool reloadTilesetsOnChange() const;
132 void setReloadTilesetsOnChanged(bool reloadOnChanged);
133
134 bool useOpenGL() const;
135 void setUseOpenGL(bool useOpenGL);
136
137 void setObjectTypes(const ObjectTypes &objectTypes);
138
139 QString objectTypesFile() const;
140 void setObjectTypesFile(const QString &filePath);
141 void setObjectTypesFileLastSaved(const QDateTime &time);
142
143 QDate firstRun() const;
144 int runCount() const;
145
146 bool isPatron() const;
147 void setPatron(bool isPatron);
148
149 bool shouldShowDonationReminder() const;
150 QDate donationReminderTime() const;
151 void setDonationReminder(const QDate &date);
152
153 enum { MaxRecentFiles = 12 };
154 void addRecentFile(const QString &fileName);
155
156 QStringList recentProjects() const;
157 QString recentProjectPath() const;
158 void addRecentProject(const QString &fileName);
159
160 QString startupSession() const;
161 void setLastSession(const QString &fileName);
162 bool restoreSessionOnStartup() const;
163
164 bool checkForUpdates() const;
165 void setCheckForUpdates(bool on);
166
167 bool displayNews() const;
168 void setDisplayNews(bool on);
169
170 bool wheelZoomsByDefault() const;
171
172 template <typename T>
173 T get(const char *key, const T &defaultValue = T()) const
174 { return value(QLatin1String(key), defaultValue).template value<T>(); }
175
176 static QString homeLocation();
177 QString dataLocation() const;
178 QString configLocation() const;
179
180 static QString startupProject();
181 static void setStartupProject(const QString &filePath);
182
183 public slots:
184 void setShowGrid(bool showGrid);
185 void setShowTileObjectOutlines(bool enabled);
186 void setShowTileAnimations(bool enabled);
187 void setShowTileCollisionShapes(bool enabled);
188 void setShowObjectReferences(bool enabled);
189 void setParallaxEnabled(bool enabled);
190 void setSnapToGrid(bool snapToGrid);
191 void setSnapToFineGrid(bool snapToFineGrid);
192 void setSnapToPixels(bool snapToPixels);
193 void setGridColor(QColor gridColor);
194 void setBackgroundFadeColor(QColor backgroundFadeColor);
195 void setGridFine(int gridFine);
196 void setGridMajor(int gridMajor);
197 void setObjectLineWidth(qreal lineWidth);
198 void setHighlightCurrentLayer(bool highlight);
199 void setHighlightHoveredObject(bool highlight);
200 void setShowTilesetGrid(bool showTilesetGrid);
201 void setRestoreSessionOnStartup(bool enabled);
202 void setPluginEnabled(const QString &fileName, bool enabled);
203 void setWheelZoomsByDefault(bool mode);
204
205 void clearRecentFiles();
206 void clearRecentProjects();
207
208 signals:
209 void showGridChanged(bool showGrid);
210 void showTileObjectOutlinesChanged(bool enabled);
211 void showTileAnimationsChanged(bool enabled);
212 void showTileCollisionShapesChanged(bool enabled);
213 void showObjectReferencesChanged(bool enabled);
214 void parallaxEnabledChanged(bool enabled);
215 void snapToGridChanged(bool snapToGrid);
216 void snapToFineGridChanged(bool snapToFineGrid);
217 void snapToPixelsChanged(bool snapToPixels);
218 void gridColorChanged(QColor gridColor);
219 void backgroundFadeColorChanged(QColor backgroundFadeColor);
220 void gridFineChanged(int gridFine);
221 void gridMajorChanged(int gridMajor);
222 void objectLineWidthChanged(qreal lineWidth);
223 void highlightCurrentLayerChanged(bool highlight);
224 void highlightHoveredObjectChanged(bool highlight);
225 void showTilesetGridChanged(bool showTilesetGrid);
226 void objectLabelVisibilityChanged(ObjectLabelVisiblity);
227 void labelForHoveredObjectChanged(bool enabled);
228
229 void applicationStyleChanged(ApplicationStyle);
230 void baseColorChanged(const QColor &baseColor);
231 void selectionColorChanged(const QColor &selectionColor);
232
233 void useOpenGLChanged(bool useOpenGL);
234
235 void languageChanged();
236
237 void objectTypesChanged();
238
239 void isPatronChanged();
240
241 void recentFilesChanged();
242 void recentProjectsChanged();
243
244 void checkForUpdatesChanged(bool on);
245 void displayNewsChanged(bool on);
246
247 void aboutToSwitchSession();
248
249 private:
250 void addToRecentFileList(const QString &fileName, QStringList &files);
251
252 void objectTypesFileChangedOnDisk();
253
254 FileSystemWatcher mWatcher;
255 bool mPortable = false;
256
257 QString mObjectTypesFile;
258 QDateTime mObjectTypesFileLastSaved;
259
260 static Preferences *mInstance;
261 static QString mStartupProject;
262 };
263
264
265 template<typename T>
266 class Preference
267 {
268 public:
269 Preference(const char * const key, T defaultValue = T())
mKey(key)270 : mKey(key)
271 , mDefault(defaultValue)
272 {}
273
274 inline T get() const;
275 inline void set(const T &value);
276
T()277 inline operator T() const { return get(); }
278 inline Preference &operator =(const T &value) { set(value); return *this; }
279
280 private:
281 const char * const mKey;
282 const T mDefault;
283 };
284
285 template<typename T>
get()286 T Preference<T>::get() const
287 {
288 return Preferences::instance()->get<T>(mKey, mDefault);
289 }
290
291 template<typename T>
set(const T & value)292 void Preference<T>::set(const T &value)
293 {
294 Preferences::instance()->setValue(QLatin1String(mKey), value);
295 }
296
297 } // namespace Tiled
298
299 Q_DECLARE_OPERATORS_FOR_FLAGS(Tiled::Preferences::ExportOptions)
300