All Classes Namespaces Functions Variables Enumerations Properties Pages
preferencemanager.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #include "preferencemanager.h"
19 
20 #include <QSettings>
21 #include <QDebug>
22 
23 PreferenceManager::PreferenceManager(Editor* editor) : BaseManager(editor, __FUNCTION__)
24 {
25 }
26 
27 PreferenceManager::~PreferenceManager()
28 {
29 }
30 
31 bool PreferenceManager::init()
32 {
33  loadPrefs();
34  return true;
35 }
36 
37 Status PreferenceManager::load(Object*)
38 {
39  return Status::OK;
40 }
41 
42 Status PreferenceManager::save(Object*)
43 {
44  return Status::OK;
45 }
46 
47 void PreferenceManager::loadPrefs()
48 {
49  QSettings settings( PENCIL2D, PENCIL2D );
50 
51  // Display
52  set(SETTING::GRID, settings.value(SETTING_SHOW_GRID, false).toBool());
53  set(SETTING::INVISIBLE_LINES, settings.value(SETTING_INVISIBLE_LINES, false).toBool());
54  set(SETTING::OUTLINES, settings.value(SETTING_OUTLINES, false).toBool());
55  set(SETTING::OVERLAY_CENTER, settings.value(SETTING_OVERLAY_CENTER, false).toBool());
56  set(SETTING::OVERLAY_THIRDS, settings.value(SETTING_OVERLAY_THIRDS, false).toBool());
57  set(SETTING::OVERLAY_GOLDEN, settings.value(SETTING_OVERLAY_GOLDEN, false).toBool());
58  set(SETTING::OVERLAY_SAFE, settings.value(SETTING_OVERLAY_SAFE, false).toBool());
59  set(SETTING::OVERLAY_PERSPECTIVE1, settings.value(SETTING_OVERLAY_PERSPECTIVE1, false).toBool());
60  set(SETTING::OVERLAY_PERSPECTIVE2, settings.value(SETTING_OVERLAY_PERSPECTIVE2, false).toBool());
61  set(SETTING::OVERLAY_PERSPECTIVE3, settings.value(SETTING_OVERLAY_PERSPECTIVE3, false).toBool());
62  set(SETTING::OVERLAY_ANGLE, settings.value(SETTING_OVERLAY_ANGLE, 15).toInt());
63  set(SETTING::ACTION_SAFE, settings.value(SETTING_ACTION_SAFE, 5).toInt());
64  set(SETTING::ACTION_SAFE_ON, settings.value(SETTING_ACTION_SAFE_ON, true).toBool());
65  set(SETTING::OVERLAY_SAFE_HELPER_TEXT_ON, settings.value(SETTING_OVERLAY_SAFE_HELPER_TEXT_ON, true).toBool());
66  set(SETTING::TITLE_SAFE, settings.value(SETTING_TITLE_SAFE, 10).toInt());
67  set(SETTING::TITLE_SAFE_ON, settings.value(SETTING_TITLE_SAFE_ON, true).toBool());
68 
69  // Grid
70  set(SETTING::GRID_SIZE_W, settings.value(SETTING_GRID_SIZE_W, 100).toInt());
71  set(SETTING::GRID_SIZE_H, settings.value(SETTING_GRID_SIZE_H, 100).toInt());
72 
73  // General
74  set(SETTING::ANTIALIAS, settings.value(SETTING_ANTIALIAS, true).toBool());
75  set(SETTING::TOOL_CURSOR, settings.value(SETTING_TOOL_CURSOR, true).toBool());
76  set(SETTING::DOTTED_CURSOR, settings.value(SETTING_DOTTED_CURSOR, true).toBool());
77  set(SETTING::HIGH_RESOLUTION, settings.value(SETTING_HIGH_RESOLUTION, true).toBool());
78  set(SETTING::SHADOW, settings.value(SETTING_SHADOW, false).toBool());
79  set(SETTING::QUICK_SIZING, settings.value(SETTING_QUICK_SIZING, true).toBool());
80  set(SETTING::SHOW_SELECTION_INFO, settings.value(SETTING_SHOW_SELECTION_INFO, false).toBool());
81 
82  set(SETTING::ROTATION_INCREMENT, settings.value(SETTING_ROTATION_INCREMENT, 15).toInt());
83 
84  set(SETTING::WINDOW_OPACITY, settings.value(SETTING_WINDOW_OPACITY, 0).toInt());
85  set(SETTING::SHOW_STATUS_BAR, settings.value(SETTING_SHOW_STATUS_BAR, true).toBool());
86  set(SETTING::CURVE_SMOOTHING, settings.value(SETTING_CURVE_SMOOTHING, 20).toInt());
87 
88  set(SETTING::BACKGROUND_STYLE, settings.value(SETTING_BACKGROUND_STYLE, "white").toString());
89 
90  set(SETTING::LAYOUT_LOCK, settings.value(SETTING_LAYOUT_LOCK, false).toBool());
91  set(SETTING::FRAME_POOL_SIZE, settings.value(SETTING_FRAME_POOL_SIZE, 1024).toInt());
92 
93  set(SETTING::FPS, settings.value(SETTING_FPS, 12).toInt());
94  set(SETTING::FIELD_W, settings.value(SETTING_FIELD_W, 800).toInt());
95  set(SETTING::FIELD_H, settings.value(SETTING_FIELD_H, 600).toInt());
96  set(SETTING::TIMECODE_TEXT, settings.value(SETTING_TIMECODE_TEXT, 1).toInt());
97 
98  // Files
99  set(SETTING::AUTO_SAVE, settings.value(SETTING_AUTO_SAVE, false).toBool());
100  set(SETTING::AUTO_SAVE_NUMBER, settings.value(SETTING_AUTO_SAVE_NUMBER, 256).toInt());
101  set(SETTING::ASK_FOR_PRESET, settings.value(SETTING_ASK_FOR_PRESET, false).toBool());
102  set(SETTING::LOAD_MOST_RECENT, settings.value(SETTING_LOAD_MOST_RECENT, false).toBool());
103  set(SETTING::LOAD_DEFAULT_PRESET, settings.value(SETTING_LOAD_DEFAULT_PRESET, true).toBool());
104  set(SETTING::DEFAULT_PRESET, settings.value(SETTING_DEFAULT_PRESET, 0).toInt());
105 
106  // Timeline
107  set(SETTING::SHORT_SCRUB, settings.value(SETTING_SHORT_SCRUB, false ).toBool());
108  set(SETTING::FRAME_SIZE, settings.value(SETTING_FRAME_SIZE, 12).toInt());
109  set(SETTING::TIMELINE_SIZE, settings.value(SETTING_TIMELINE_SIZE, 240).toInt());
110  set(SETTING::DRAW_LABEL, settings.value(SETTING_DRAW_LABEL, false ).toBool());
111  set(SETTING::LABEL_FONT_SIZE, settings.value(SETTING_LABEL_FONT_SIZE, 12).toInt());
112 
113  set( SETTING::DRAW_ON_EMPTY_FRAME_ACTION, settings.value( SETTING_DRAW_ON_EMPTY_FRAME_ACTION,
114  KEEP_DRAWING_ON_PREVIOUS_KEY).toInt() );
115 
116  // Onion Skin
117  set(SETTING::PREV_ONION, settings.value(SETTING_PREV_ONION, false).toBool());
118  set(SETTING::NEXT_ONION, settings.value(SETTING_NEXT_ONION, false).toBool());
119  set(SETTING::MULTILAYER_ONION, settings.value(SETTING_MULTILAYER_ONION, false).toBool());
120  set(SETTING::ONION_BLUE, settings.value(SETTING_ONION_BLUE, false).toBool());
121  set(SETTING::ONION_RED, settings.value(SETTING_ONION_RED, false).toBool());
122 
123  set(SETTING::ONION_MAX_OPACITY, settings.value(SETTING_ONION_MAX_OPACITY, 50).toInt());
124  set(SETTING::ONION_MIN_OPACITY, settings.value(SETTING_ONION_MIN_OPACITY, 20).toInt());
125  set(SETTING::ONION_PREV_FRAMES_NUM, settings.value(SETTING_ONION_PREV_FRAMES_NUM, 5).toInt());
126  set(SETTING::ONION_NEXT_FRAMES_NUM, settings.value(SETTING_ONION_NEXT_FRAMES_NUM, 5).toInt());
127  set(SETTING::ONION_WHILE_PLAYBACK, settings.value(SETTING_ONION_WHILE_PLAYBACK, 0).toInt());
128  set(SETTING::ONION_TYPE, settings.value(SETTING_ONION_TYPE, "relative").toString());
129  set(SETTING::LAYER_VISIBILITY, settings.value(SETTING_LAYER_VISIBILITY, 2).toInt());
130  set(SETTING::LAYER_VISIBILITY_THRESHOLD, settings.value(SETTING_LAYER_VISIBILITY_THRESHOLD, 0.5f).toFloat());
131 
132  set(SETTING::FLIP_ROLL_MSEC, settings.value(SETTING_FLIP_ROLL_MSEC, 100).toInt());
133  set(SETTING::FLIP_ROLL_DRAWINGS, settings.value(SETTING_FLIP_ROLL_DRAWINGS, 5).toInt());
134  set(SETTING::FLIP_INBETWEEN_MSEC, settings.value(SETTING_FLIP_INBETWEEN_MSEC, 100).toInt());
135  set(SETTING::SOUND_SCRUB_ACTIVE, settings.value(SETTING_SOUND_SCRUB_ACTIVE, false).toBool());
136  set(SETTING::SOUND_SCRUB_MSEC, settings.value(SETTING_SOUND_SCRUB_MSEC, 100).toInt());
137 
138  set(SETTING::LANGUAGE, settings.value(SETTING_LANGUAGE).toString());
139 }
140 
141 void PreferenceManager::turnOn(SETTING option)
142 {
143  set(option, true);
144 }
145 
146 void PreferenceManager::turnOff(SETTING option)
147 {
148  set(option, false);
149 }
150 
151 bool PreferenceManager::isOn(SETTING option)
152 {
153  int optionId = static_cast<int>(option);
154  return mBooleanSet.value(optionId, false);
155 }
156 
157 int PreferenceManager::getInt(SETTING option)
158 {
159  int optionId = static_cast<int>(option);
160  return mIntegerSet.value(optionId, -1);
161 }
162 
163 float PreferenceManager::getFloat(SETTING option)
164 {
165  int optionId = static_cast<int>(option);
166  return mFloatingPointSet.value(optionId, -1);
167 }
168 
169 QString PreferenceManager::getString(SETTING option)
170 {
171  int optionId = static_cast<int>(option);
172  if (mIntegerSet.contains(optionId))
173  {
174  return QString::number(mIntegerSet.value(optionId, -1));
175  }
176  else if (mBooleanSet.contains(optionId))
177  {
178  if (mBooleanSet.value(optionId, false))
179  {
180  return "true";
181  }
182  else
183  {
184  return "false";
185  }
186  }
187  return mStringSet.value(optionId);
188 }
189 
190 void PreferenceManager::set(SETTING option, QString value)
191 {
192  QSettings settings(PENCIL2D, PENCIL2D);
193  switch (option)
194  {
195  case SETTING::BACKGROUND_STYLE:
196  settings.setValue(SETTING_BACKGROUND_STYLE, value);
197  break;
198  case SETTING::ONION_TYPE:
199  settings.setValue(SETTING_ONION_TYPE, value);
200  break;
201  case SETTING::LANGUAGE:
202  settings.setValue(SETTING_LANGUAGE, value);
203  break;
204  default:
205  break;
206  }
207 
208  int optionId = static_cast<int>(option);
209  if (mStringSet[optionId] != value)
210  {
211  mStringSet[optionId] = value;
212  emit optionChanged(option);
213  }
214 }
215 
216 void PreferenceManager::set(SETTING option, float value)
217 {
218  QSettings settings(PENCIL2D, PENCIL2D);
219  switch(option)
220  {
221  case SETTING::LAYER_VISIBILITY_THRESHOLD:
222  settings.setValue(SETTING_LAYER_VISIBILITY_THRESHOLD, value);
223  break;
224  default:
225  Q_ASSERT(false);
226  break;
227  }
228 
229 
230  int optionId = static_cast<int>(option);
231 
232  if (qFuzzyCompare(mFloatingPointSet[optionId], value) == false)
233  {
234  mFloatingPointSet[optionId] = value;
235  emit optionChanged(option);
236  }
237 }
238 
239 void PreferenceManager::set(SETTING option, int value)
240 {
241  QSettings settings(PENCIL2D, PENCIL2D);
242  switch (option)
243  {
244  case SETTING::WINDOW_OPACITY:
245  settings.setValue(SETTING_WINDOW_OPACITY, value);
246  break;
247  case SETTING::CURVE_SMOOTHING:
248  settings.setValue(SETTING_CURVE_SMOOTHING, value);
249  break;
250  case SETTING::AUTO_SAVE_NUMBER:
251  settings.setValue(SETTING_AUTO_SAVE_NUMBER, value);
252  break;
253  case SETTING::FRAME_SIZE:
254  if (value < 4) { value = 4; }
255  else if (value > 40) { value = 40; }
256  settings.setValue(SETTING_FRAME_SIZE, value);
257  break;
258  case SETTING::TIMELINE_SIZE:
259  if (value < 2) { value = 2; }
260  settings.setValue(SETTING_TIMELINE_SIZE, value);
261  break;
262  case SETTING::LABEL_FONT_SIZE:
263  if (value < 12) { value = 12; }
264  settings.setValue(SETTING_LABEL_FONT_SIZE, value);
265  break;
266  case SETTING::ONION_MAX_OPACITY:
267  settings.setValue(SETTING_ONION_MAX_OPACITY, value);
268  break;
269  case SETTING::ONION_MIN_OPACITY:
270  settings.setValue(SETTING_ONION_MIN_OPACITY, value);
271  break;
272  case SETTING::ONION_PREV_FRAMES_NUM:
273  settings.setValue(SETTING_ONION_PREV_FRAMES_NUM, value);
274  break;
275  case SETTING::ONION_NEXT_FRAMES_NUM:
276  settings.setValue(SETTING_ONION_NEXT_FRAMES_NUM, value);
277  break;
278  case SETTING::OVERLAY_ANGLE:
279  settings.setValue(SETTING_OVERLAY_ANGLE, value);
280  break;
281  case SETTING::FLIP_ROLL_MSEC :
282  settings.setValue(SETTING_FLIP_ROLL_MSEC, value);
283  break;
284  case SETTING::FLIP_ROLL_DRAWINGS :
285  settings.setValue(SETTING_FLIP_ROLL_DRAWINGS, value);
286  break;
287  case SETTING::FLIP_INBETWEEN_MSEC :
288  settings.setValue(SETTING_FLIP_INBETWEEN_MSEC, value);
289  break;
290  case SETTING::SOUND_SCRUB_MSEC :
291  settings.setValue(SETTING_SOUND_SCRUB_MSEC, value);
292  break;
293  case SETTING::GRID_SIZE_W:
294  settings.setValue(SETTING_GRID_SIZE_W, value);
295  break;
296  case SETTING::TIMECODE_TEXT:
297  settings.setValue(SETTING_TIMECODE_TEXT, value);
298  break;
299  case SETTING::GRID_SIZE_H:
300  settings.setValue(SETTING_GRID_SIZE_H, value);
301  break;
302  case SETTING::ACTION_SAFE:
303  settings.setValue(SETTING_ACTION_SAFE, value);
304  break;
305  case SETTING::TITLE_SAFE:
306  settings.setValue(SETTING_TITLE_SAFE, value);
307  break;
308  case SETTING::FRAME_POOL_SIZE:
309  settings.setValue(SETTING_FRAME_POOL_SIZE, value);
310  break;
311  case SETTING::DRAW_ON_EMPTY_FRAME_ACTION:
312  settings.setValue( SETTING_DRAW_ON_EMPTY_FRAME_ACTION, value);
313  break;
314  case SETTING::ONION_WHILE_PLAYBACK:
315  settings.setValue(SETTING_ONION_WHILE_PLAYBACK, value);
316  break;
317  case SETTING::ROTATION_INCREMENT:
318  settings.setValue(SETTING_ROTATION_INCREMENT, value);
319  break;
320  case SETTING::FPS:
321  settings.setValue(SETTING_FPS, value);
322  break;
323  case SETTING::FIELD_W:
324  settings.setValue(SETTING_FIELD_W, value);
325  break;
326  case SETTING::FIELD_H:
327  settings.setValue(SETTING_FIELD_H, value);
328  break;
329  case SETTING::LAYER_VISIBILITY:
330  settings.setValue(SETTING_LAYER_VISIBILITY, value);
331  break;
332  case SETTING::DEFAULT_PRESET:
333  settings.setValue(SETTING_DEFAULT_PRESET, value);
334  break;
335  default:
336  Q_ASSERT(false);
337  break;
338  }
339 
340  int optionId = static_cast<int>(option);
341  if (mIntegerSet[optionId] != value)
342  {
343  mIntegerSet[optionId] = value;
344  emit optionChanged(option);
345  }
346 }
347 
348 // Set bool value
349 //
350 void PreferenceManager::set(SETTING option, bool value)
351 {
352  QSettings settings(PENCIL2D, PENCIL2D);
353  switch (option)
354  {
355  case SETTING::SHOW_STATUS_BAR:
356  settings.setValue(SETTING_SHOW_STATUS_BAR, value);
357  break;
358  case SETTING::ANTIALIAS:
359  settings.setValue(SETTING_ANTIALIAS, value);
360  break;
361  case SETTING::GRID:
362  settings.setValue(SETTING_SHOW_GRID, value);
363  break;
364  case SETTING::OVERLAY_CENTER:
365  settings.setValue(SETTING_OVERLAY_CENTER, value);
366  break;
367  case SETTING::OVERLAY_THIRDS:
368  settings.setValue(SETTING_OVERLAY_THIRDS, value);
369  break;
370  case SETTING::OVERLAY_GOLDEN:
371  settings.setValue(SETTING_OVERLAY_GOLDEN, value);
372  break;
373  case SETTING::OVERLAY_SAFE:
374  settings.setValue(SETTING_OVERLAY_SAFE, value);
375  break;
376  case SETTING::OVERLAY_PERSPECTIVE1:
377  settings.setValue(SETTING_OVERLAY_PERSPECTIVE1, value);
378  break;
379  case SETTING::OVERLAY_PERSPECTIVE2:
380  settings.setValue(SETTING_OVERLAY_PERSPECTIVE2, value);
381  break;
382  case SETTING::OVERLAY_PERSPECTIVE3:
383  settings.setValue(SETTING_OVERLAY_PERSPECTIVE3, value);
384  break;
385  case SETTING::ACTION_SAFE_ON:
386  settings.setValue(SETTING_ACTION_SAFE_ON, value);
387  break;
388  case SETTING::TITLE_SAFE_ON:
389  settings.setValue(SETTING_TITLE_SAFE_ON, value);
390  break;
391  case SETTING::OVERLAY_SAFE_HELPER_TEXT_ON:
392  settings.setValue(SETTING_OVERLAY_SAFE_HELPER_TEXT_ON, value);
393  break;
394  case SETTING::SHADOW:
395  settings.setValue(SETTING_SHADOW, value);
396  break;
397  case SETTING::PREV_ONION:
398  settings.setValue(SETTING_PREV_ONION, value);
399  break;
400  case SETTING::NEXT_ONION:
401  settings.setValue(SETTING_NEXT_ONION, value);
402  break;
403  case SETTING::MULTILAYER_ONION:
404  settings.setValue(SETTING_MULTILAYER_ONION, value);
405  break;
406  case SETTING::INVISIBLE_LINES:
407  settings.setValue(SETTING_INVISIBLE_LINES, value);
408  break;
409  case SETTING::OUTLINES:
410  settings.setValue(SETTING_OUTLINES, value);
411  break;
412  case SETTING::ONION_BLUE:
413  settings.setValue(SETTING_ONION_BLUE, value);
414  break;
415  case SETTING::ONION_RED:
416  settings.setValue(SETTING_ONION_RED, value);
417  break;
418  case SETTING::TOOL_CURSOR:
419  settings.setValue(SETTING_TOOL_CURSOR, value);
420  break;
421  case SETTING::DOTTED_CURSOR:
422  settings.setValue(SETTING_DOTTED_CURSOR, value);
423  break;
424  case SETTING::HIGH_RESOLUTION:
425  settings.setValue(SETTING_HIGH_RESOLUTION, value);
426  break;
427  case SETTING::AUTO_SAVE:
428  settings.setValue(SETTING_AUTO_SAVE, value);
429  break;
430  case SETTING::SHORT_SCRUB:
431  settings.setValue(SETTING_SHORT_SCRUB, value);
432  break;
433  case SETTING::DRAW_LABEL:
434  settings.setValue(SETTING_DRAW_LABEL, value);
435  break;
436  case SETTING::QUICK_SIZING:
437  settings.setValue(SETTING_QUICK_SIZING, value);
438  break;
439  case SETTING::LAYOUT_LOCK:
440  settings.setValue(SETTING_LAYOUT_LOCK, value);
441  break;
442  case SETTING::SHOW_SELECTION_INFO:
443  settings.setValue(SETTING_SHOW_SELECTION_INFO, value);
444  break;
445  case SETTING::SOUND_SCRUB_ACTIVE:
446  settings.setValue(SETTING_SOUND_SCRUB_ACTIVE, value);
447  break;
448  case SETTING::ASK_FOR_PRESET:
449  settings.setValue(SETTING_ASK_FOR_PRESET, value);
450  break;
451  case SETTING::LOAD_MOST_RECENT:
452  settings.setValue(SETTING_LOAD_MOST_RECENT, value);
453  break;
454  case SETTING::LOAD_DEFAULT_PRESET:
455  settings.setValue(SETTING_LOAD_DEFAULT_PRESET, value);
456  break;
457  default:
458  Q_ASSERT(false);
459  break;
460  }
461 
462  int optionId = static_cast<int>(option);
463  if (mBooleanSet[optionId] != value)
464  {
465  mBooleanSet[optionId] = value;
466  emit optionChanged(option);
467  }
468 }
QString number(int n, int base)
const T value(const Key &key) const const
Definition: object.h:41
bool contains(const Key &key) const const
Definition: editor.h:55