Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
preferencemanager.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 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 
22 
23 PreferenceManager::PreferenceManager( QObject* parent )
24  : BaseManager( parent )
25 {
26 }
27 
28 PreferenceManager::~PreferenceManager()
29 {
30 }
31 
32 bool PreferenceManager::init()
33 {
34  loadPrefs();
35  return true;
36 }
37 
38 Status PreferenceManager::load( Object* )
39 {
40  return Status::OK;
41 }
42 
43 Status PreferenceManager::save( Object * )
44 {
45  return Status::OK;
46 }
47 
48 void PreferenceManager::loadPrefs()
49 {
50  QSettings settings( PENCIL2D, PENCIL2D );
51 
52  // Display
53  set( SETTING::GRID, settings.value( SETTING_SHOW_GRID, false ).toBool() );
54  set( SETTING::INVISIBLE_LINES, settings.value( SETTING_INVISIBLE_LINES, false ).toBool() );
55  set( SETTING::OUTLINES, settings.value( SETTING_OUTLINES, false ).toBool() );
56  set( SETTING::MIRROR_H, false ); // Always off by default
57  set( SETTING::MIRROR_V, false ); // Always off by default
58 
59  // Grid
60  set( SETTING::GRID_SIZE, settings.value( SETTING_GRID_SIZE, 50 ).toInt() );
61 
62  // General
63  //
64  set( SETTING::ANTIALIAS, settings.value( SETTING_ANTIALIAS, true ).toBool() );
65  set( SETTING::TOOL_CURSOR, settings.value( SETTING_TOOL_CURSOR, true ).toBool() );
66  set( SETTING::DOTTED_CURSOR, settings.value( SETTING_DOTTED_CURSOR, true ).toBool() );
67  set( SETTING::HIGH_RESOLUTION, settings.value( SETTING_HIGH_RESOLUTION, true ).toBool() );
68  set( SETTING::SHADOW, settings.value( SETTING_SHADOW, false ).toBool() );
69  set( SETTING::QUICK_SIZING, settings.value( SETTING_QUICK_SIZING, true ).toBool() );
70 
71  set( SETTING::WINDOW_OPACITY, settings.value( SETTING_WINDOW_OPACITY, 0 ).toInt() );
72  set( SETTING::CURVE_SMOOTHING, settings.value( SETTING_CURVE_SMOOTHING, 20 ).toInt() );
73 
74  set( SETTING::BACKGROUND_STYLE, settings.value( SETTING_BACKGROUND_STYLE, "white" ).toString() );
75 
76  set( SETTING::LAYOUT_LOCK, settings.value( SETTING_LAYOUT_LOCK, false ).toBool() );
77 
78  // Files
79  set( SETTING::AUTO_SAVE, settings.value( SETTING_AUTO_SAVE, true ).toBool() );
80  set( SETTING::AUTO_SAVE_NUMBER, settings.value( SETTING_AUTO_SAVE_NUMBER, 20 ).toInt() );
81 
82  // Timeline
83  //
84  set( SETTING::SHORT_SCRUB, settings.value( SETTING_SHORT_SCRUB, false ).toBool() );
85  set( SETTING::FRAME_SIZE, settings.value( SETTING_FRAME_SIZE, 12 ).toInt() );
86  set( SETTING::TIMELINE_SIZE, settings.value( SETTING_TIMELINE_SIZE, 240 ).toInt() );
87  set( SETTING::DRAW_LABEL, settings.value( SETTING_DRAW_LABEL, false ).toBool() );
88  set( SETTING::LABEL_FONT_SIZE, settings.value( SETTING_LABEL_FONT_SIZE, 12 ).toInt() );
89 
90  // Onion Skin
91  //
92  set( SETTING::PREV_ONION, settings.value( SETTING_PREV_ONION, false ).toBool() );
93  set( SETTING::NEXT_ONION, settings.value( SETTING_NEXT_ONION, false ).toBool() );
94  set( SETTING::MULTILAYER_ONION, settings.value( SETTING_MULTILAYER_ONION, false ).toBool() );
95  set( SETTING::ONION_BLUE, settings.value( SETTING_ONION_BLUE, false ).toBool() );
96  set( SETTING::ONION_RED, settings.value( SETTING_ONION_RED, false ).toBool() );
97 
98  set( SETTING::ONION_MAX_OPACITY, settings.value( SETTING_ONION_MAX_OPACITY, 50 ).toInt() );
99  set( SETTING::ONION_MIN_OPACITY, settings.value( SETTING_ONION_MIN_OPACITY, 20 ).toInt() );
100  set( SETTING::ONION_PREV_FRAMES_NUM, settings.value( SETTING_ONION_PREV_FRAMES_NUM, 5 ).toInt() );
101  set( SETTING::ONION_NEXT_FRAMES_NUM, settings.value( SETTING_ONION_NEXT_FRAMES_NUM, 5 ).toInt() );
102  set( SETTING::ONION_TYPE, settings.value( SETTING_ONION_TYPE, "relative" ).toString() );
103 
104  set( SETTING::LANGUAGE, settings.value( SETTING_LANGUAGE ).toString() );
105 
106  set( SETTING::AXIS, false );
107 //#define DRAW_AXIS
108 #ifdef DRAW_AXIS
109  set( SETTING::AXIS, true );
110 #endif
111 }
112 
113 void PreferenceManager::turnOn( SETTING option )
114 {
115  set( option, true );
116 }
117 
118 void PreferenceManager::turnOff( SETTING option )
119 {
120  set( option, false );
121 }
122 
123 bool PreferenceManager::isOn( SETTING option )
124 {
125  int optionId = static_cast< int >( option );
126  return mBooleanSet.value(optionId, false);
127 }
128 
129 int PreferenceManager::getInt( SETTING option )
130 {
131  int optionId = static_cast< int >( option );
132  return mIntegerSet.value(optionId, -1);
133 }
134 
135 QString PreferenceManager::getString( SETTING option )
136 {
137  int optionId = static_cast< int >( option );
138  if ( mIntegerSet.contains( optionId ) )
139  {
140  return QString::number( mIntegerSet.value( optionId, -1 ) );
141  }
142  else if ( mBooleanSet.contains( optionId ) )
143  {
144  if ( mBooleanSet.value( optionId, false ) )
145  {
146  return "true";
147  }
148  else
149  {
150  return "false";
151  }
152  }
153  return mStringSet.value(optionId);
154 }
155 
156 
157 // Set string value
158 //
159 void PreferenceManager::set(SETTING option, QString value)
160 {
161  QSettings settings( PENCIL2D, PENCIL2D );
162  switch ( option )
163  {
164  case SETTING::BACKGROUND_STYLE:
165  settings.setValue( SETTING_BACKGROUND_STYLE, value );
166  break;
167  case SETTING::ONION_TYPE:
168  settings.setValue( SETTING_ONION_TYPE, value );
169  break;
170  case SETTING::LANGUAGE:
171  settings.setValue( SETTING_LANGUAGE, value );
172  break;
173  default:
174  break;
175  }
176 
177  int optionId = static_cast< int >( option );
178  if ( mStringSet[ optionId ] != value )
179  {
180  mStringSet[ optionId ] = value;
181  emit optionChanged( option );
182  }
183 }
184 
185 // Set int value
186 //
187 void PreferenceManager::set( SETTING option, int value )
188 {
189  QSettings settings( PENCIL2D, PENCIL2D );
190  switch ( option )
191  {
192  case SETTING::WINDOW_OPACITY:
193  settings.setValue( SETTING_WINDOW_OPACITY, value );
194  break;
195  case SETTING::CURVE_SMOOTHING:
196  settings.setValue( SETTING_CURVE_SMOOTHING, value );
197  break;
198  case SETTING::AUTO_SAVE_NUMBER:
199  settings.setValue ( SETTING_AUTO_SAVE_NUMBER, value );
200  break;
201  case SETTING::FRAME_SIZE:
202  if (value < 4) { value = 4; }
203  else if (value > 20) { value = 20; }
204  settings.setValue ( SETTING_FRAME_SIZE, value );
205  break;
206  case SETTING::TIMELINE_SIZE:
207  if (value < 2) { value = 2; }
208  settings.setValue ( SETTING_TIMELINE_SIZE, value );
209  break;
210  case SETTING::LABEL_FONT_SIZE:
211  if (value < 12) { value = 12; }
212  settings.setValue ( SETTING_LABEL_FONT_SIZE, value );
213  break;
214  case SETTING::ONION_MAX_OPACITY:
215  settings.setValue ( SETTING_ONION_MAX_OPACITY, value );
216  break;
217  case SETTING::ONION_MIN_OPACITY:
218  settings.setValue ( SETTING_ONION_MIN_OPACITY, value );
219  break;
220  case SETTING::ONION_PREV_FRAMES_NUM:
221  settings.setValue ( SETTING_ONION_PREV_FRAMES_NUM, value );
222  break;
223  case SETTING::ONION_NEXT_FRAMES_NUM:
224  settings.setValue ( SETTING_ONION_NEXT_FRAMES_NUM, value );
225  break;
226  case SETTING::GRID_SIZE:
227  settings.setValue ( SETTING_GRID_SIZE, value );
228  break;
229  default:
230  Q_ASSERT( false );
231  break;
232  }
233 
234  int optionId = static_cast< int >( option );
235  if ( mIntegerSet[ optionId ] != value )
236  {
237  mIntegerSet[ optionId ] = value;
238  emit optionChanged( option );
239  }
240 }
241 
242 // Set bool value
243 //
244 void PreferenceManager::set( SETTING option, bool value )
245 {
246  QSettings settings( PENCIL2D, PENCIL2D );
247  switch ( option )
248  {
249  case SETTING::ANTIALIAS:
250  settings.setValue( SETTING_ANTIALIAS, value );
251  break;
252  case SETTING::GRID:
253  settings.setValue( SETTING_SHOW_GRID, value );
254  break;
255  case SETTING::SHADOW:
256  settings.setValue ( SETTING_SHADOW, value );
257  break;
258  case SETTING::PREV_ONION:
259  settings.setValue ( SETTING_PREV_ONION, value );
260  break;
261  case SETTING::NEXT_ONION:
262  settings.setValue ( SETTING_NEXT_ONION, value );
263  break;
264  case SETTING::MULTILAYER_ONION:
265  settings.setValue( SETTING_MULTILAYER_ONION, value);
266  break;
267  case SETTING::AXIS:
268  settings.setValue ( SETTING_AXIS, value );
269  break;
270  case SETTING::INVISIBLE_LINES:
271  settings.setValue ( SETTING_INVISIBLE_LINES, value );
272  break;
273  case SETTING::OUTLINES:
274  settings.setValue ( SETTING_OUTLINES, value );
275  break;
276  case SETTING::ONION_BLUE:
277  settings.setValue ( SETTING_ONION_BLUE, value );
278  break;
279  case SETTING::ONION_RED:
280  settings.setValue ( SETTING_ONION_RED, value );
281  break;
282  case SETTING::MIRROR_H:
283  settings.setValue ( SETTING_MIRROR_H, value );
284  break;
285  case SETTING::MIRROR_V:
286  settings.setValue ( SETTING_MIRROR_V, value );
287  break;
288  case SETTING::TOOL_CURSOR:
289  settings.setValue ( SETTING_TOOL_CURSOR, value );
290  break;
291  case SETTING::DOTTED_CURSOR:
292  settings.setValue ( SETTING_DOTTED_CURSOR, value );
293  break;
294  case SETTING::HIGH_RESOLUTION:
295  settings.setValue ( SETTING_HIGH_RESOLUTION, value );
296  break;
297  case SETTING::AUTO_SAVE:
298  settings.setValue ( SETTING_AUTO_SAVE, value );
299  break;
300  case SETTING::SHORT_SCRUB:
301  settings.setValue ( SETTING_SHORT_SCRUB, value );
302  break;
303  case SETTING::DRAW_LABEL:
304  settings.setValue ( SETTING_DRAW_LABEL, value );
305  break;
306  case SETTING::QUICK_SIZING:
307  settings.setValue ( SETTING_QUICK_SIZING, value );
308  break;
309  case SETTING::LAYOUT_LOCK:
310  settings.setValue( SETTING_LAYOUT_LOCK, value );
311  break;
312  default:
313  Q_ASSERT( false );
314  break;
315  }
316 
317  int optionId = static_cast< int >( option );
318  if ( mBooleanSet[ optionId ] != value )
319  {
320  mBooleanSet[ optionId ] = value;
321  emit optionChanged( option );
322  }
323 }
Definition: object.h:71