Salome HOME
PAL9883 - invalid values in saved user preferences file
[modules/gui.git] / src / Qtx / QtxResourceEdit.cxx
1 // File:      QtxResourceEdit.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxResourceEdit.h"
5
6 #include "QtxResourceMgr.h"
7
8 /*
9   Class: QtxResourceEdit
10   Descr: Class for managing preferences items
11 */
12
13 QtxResourceEdit::QtxResourceEdit( QtxResourceMgr* mgr )
14 : myResMgr( mgr )
15 {
16 }
17
18 QtxResourceEdit::~QtxResourceEdit()
19 {
20   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
21     delete it.data();
22 }
23
24 QtxResourceMgr* QtxResourceEdit::resourceMgr() const
25 {
26   return myResMgr;
27 }
28
29 int QtxResourceEdit::addItem( const QString& label, const int pId, const int type,
30                               const QString& section, const QString& param )
31 {
32   Item* i = createItem( label, type, pId );
33   if ( !i )
34     return -1;
35
36   if ( !myItems.contains( i->id() ) )
37   {
38     myItems.insert( i->id(), i );
39
40     i->setTitle( label );
41     i->setResource( section, param );
42
43     if ( !i->parentItem() && !myChildren.contains( i ) )
44       myChildren.append( i );
45
46     itemAdded( i );
47   }
48
49   return i->id();
50 }
51
52 QVariant QtxResourceEdit::itemProperty( const int id, const QString& propName ) const
53 {
54   QVariant propValue;
55   Item* i = item( id );
56   if ( i )
57     propValue = i->property( propName );
58   return propValue;
59 }
60
61 void QtxResourceEdit::setItemProperty( const int id, const QString& propName, const QVariant& propValue )
62 {
63   Item* i = item( id );
64   if ( i )
65     i->setProperty( propName, propValue );
66 }
67
68 void QtxResourceEdit::resource( const int id, QString& sec, QString& param ) const
69 {
70   Item* i = item( id );
71   if ( i )
72     i->resource( sec, param );
73 }
74
75 void QtxResourceEdit::store()
76 {
77   QMap<Item*, QString> before;
78   resourceValues( before );
79
80   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
81     it.data()->store();
82
83   QMap<Item*, QString> after;
84   resourceValues( after );
85
86   QMap<Item*, QString> changed;
87   differentValues( before, after, changed );
88
89   changedResources( changed );
90 }
91
92 void QtxResourceEdit::retrieve()
93 {
94   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
95     it.data()->retrieve();
96 }
97
98 void QtxResourceEdit::toBackup()
99 {
100   myBackup.clear();
101   resourceValues( myBackup );
102 }
103
104 void QtxResourceEdit::fromBackup()
105 {
106   QMap<Item*, QString> before;
107   resourceValues( before );
108
109   setResourceValues( myBackup );
110
111   QMap<Item*, QString> after;
112   resourceValues( after );
113
114   QMap<Item*, QString> changed;
115   differentValues( before, after, changed );
116
117   changedResources( changed );
118 }
119
120 void QtxResourceEdit::update()
121 {
122 }
123
124 QtxResourceEdit::Item* QtxResourceEdit::item( const int id ) const
125 {
126   Item* i = 0;
127   if ( myItems.contains( id ) )
128     i = myItems[id];
129   return i;
130 }
131
132 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title ) const
133 {
134   Item* i = 0;
135   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
136   {
137     if ( it.data()->title() == title )
138       i = it.data();
139   }
140   return i;
141 }
142
143 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title, const int pId ) const
144 {
145   Item* i = 0;
146   Item* pItem = item( pId );
147   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
148   {
149     if ( it.data()->parentItem() == pItem && it.data()->title() == title )
150       i = it.data();
151   }
152   return i;
153 }
154
155 QtxResourceEdit::Item* QtxResourceEdit::createItem( const QString& label, const int type, const int pId )
156 {
157   Item* i = 0;
158   if ( pId < 0 )
159     i = createItem( label, type );
160   else
161   {
162     Item* pItem = item( pId );
163     if ( pItem )
164     {
165       i = pItem->createItem( label, type );
166       pItem->insertChild( i );
167     }
168   }
169
170   return i;
171 }
172
173 void QtxResourceEdit::removeItem( Item* item )
174 {
175   if ( !item )
176     return;
177
178   myChildren.remove( item );
179   myItems.remove( item->id() );
180
181   itemRemoved( item );
182 }
183
184 void QtxResourceEdit::childItems( QPtrList<Item>& lst ) const
185 {
186   lst.clear();
187   for ( QPtrListIterator<Item> it( myChildren ); it.current(); ++it )
188     lst.append( it.current() );
189 }
190
191 void QtxResourceEdit::resourceValues( QMap<int, QString>& map ) const
192 {
193   QString sect, name;
194   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
195   {
196     it.data()->resource( sect, name );
197     if( myResMgr->hasValue( sect, name ) )
198       map.insert( it.key(), it.data()->resourceValue() );
199   }
200 }
201
202 void QtxResourceEdit::resourceValues( QMap<Item*, QString>& map ) const
203 {
204   QString sect, name;
205   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
206   {
207     it.data()->resource( sect, name );
208     if( myResMgr->hasValue( sect, name ) )
209       map.insert( it.data(), it.data()->resourceValue() );
210   }
211 }
212
213 void QtxResourceEdit::setResourceValues( QMap<int, QString>& map ) const
214 {
215   for ( QMap<int, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
216   {
217     Item* i = item( it.key() );
218     if ( i )
219       i->setResourceValue( it.data() );
220   }
221 }
222
223 void QtxResourceEdit::setResourceValues( QMap<Item*, QString>& map ) const
224 {
225   for ( QMap<Item*, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
226     it.key()->setResourceValue( it.data() );
227 }
228
229
230 void QtxResourceEdit::differentValues( const QMap<int, QString>& map1, const QMap<int, QString>& map2,
231                                        QMap<int, QString>& resMap, const bool fromFirst ) const
232 {
233   resMap.clear();
234   const QMap<int, QString>& later = fromFirst ? map1 : map2;
235   const QMap<int, QString>& early = fromFirst ? map2 : map1;
236
237   for ( QMap<int, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
238   {
239     if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
240       resMap.insert( it.key(), it.data() );
241   }
242 }
243
244 void QtxResourceEdit::differentValues( const QMap<Item*, QString>& map1, const QMap<Item*, QString>& map2,
245                                        QMap<Item*, QString>& resMap, const bool fromFirst ) const
246 {
247   resMap.clear();
248   const QMap<Item*, QString>& later = fromFirst ? map1 : map2;
249   const QMap<Item*, QString>& early = fromFirst ? map2 : map1;
250
251   for ( QMap<Item*, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
252   {
253     if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
254       resMap.insert( it.key(), it.data() );
255   }
256 }
257
258 void QtxResourceEdit::changedResources( const QMap<Item*, QString>& )
259 {
260 }
261
262 void QtxResourceEdit::itemAdded( Item* )
263 {
264 }
265
266 void QtxResourceEdit::itemRemoved( Item* )
267 {
268 }
269
270 /*
271   Class: QtxResourceEdit::Item
272   Descr: Class for incapsulation of one preference item
273 */
274
275 QtxResourceEdit::Item::Item( QtxResourceEdit* edit, Item* parent )
276 : myEdit( edit ),
277 myParent( 0 )
278 {
279   myId = generateId();
280
281   if ( parent )
282     parent->insertChild( this );
283 }
284
285 QtxResourceEdit::Item::~Item()
286 {
287   if ( resourceEdit() )
288     resourceEdit()->removeItem( this );
289 }
290
291 int QtxResourceEdit::Item::id() const
292 {
293   return myId;
294 }
295
296 QtxResourceEdit::Item* QtxResourceEdit::Item::parentItem() const
297 {
298   return myParent;
299 }
300
301 void QtxResourceEdit::Item::insertChild( Item* item )
302 {
303   if ( !item || myChildren.contains( item ) )
304     return;
305
306   if ( item->parentItem() && item->parentItem() != this )
307     item->parentItem()->removeChild( item );
308
309   item->myParent = this;
310   myChildren.append( item );
311 }
312
313 void QtxResourceEdit::Item::removeChild( Item* item )
314 {
315   if ( !item || !myChildren.contains( item ) )
316     return;
317
318   myChildren.remove( item );
319   item->myParent = 0;
320 }
321
322 void QtxResourceEdit::Item::childItems( QPtrList<Item>& lst ) const
323 {
324   for ( ItemListIterator it( myChildren ); it.current(); ++it )
325     lst.append( it.current() );
326 }
327
328 bool QtxResourceEdit::Item::isEmpty() const
329 {
330   return myChildren.isEmpty();
331 }
332
333 QString QtxResourceEdit::Item::title() const
334 {
335   return myTitle;
336 }
337
338 void QtxResourceEdit::Item::resource( QString& sec, QString& param ) const
339 {
340   sec = myResSection;
341   param = myResParameter;
342 }
343
344 void QtxResourceEdit::Item::setTitle( const QString& title )
345 {
346   myTitle = title;
347 }
348
349 void QtxResourceEdit::Item::setResource( const QString& sec, const QString& param )
350 {
351   myResSection = sec;
352   myResParameter = param;
353 }
354
355 void QtxResourceEdit::Item::update()
356 {
357 }
358
359 QVariant QtxResourceEdit::Item::property( const QString& ) const
360 {
361   return QVariant();
362 }
363
364 void QtxResourceEdit::Item::setProperty( const QString&, const QVariant& )
365 {
366 }
367
368 QString QtxResourceEdit::Item::resourceValue() const
369 {
370   return getString();
371 }
372
373 void QtxResourceEdit::Item::setResourceValue( const QString& val )
374 {
375   setString( val );
376 }
377
378 QtxResourceMgr* QtxResourceEdit::Item::resourceMgr() const
379 {
380   QtxResourceMgr* resMgr = 0;
381   if ( resourceEdit() )
382     resMgr = resourceEdit()->resourceMgr();
383   return resMgr;
384 }
385
386 QtxResourceEdit* QtxResourceEdit::Item::resourceEdit() const
387 {
388   return myEdit;
389 }
390
391 int QtxResourceEdit::Item::getInteger( const int val ) const
392 {
393   QtxResourceMgr* resMgr = resourceMgr();
394   return resMgr ? resMgr->integerValue( myResSection, myResParameter, val ) : val;
395 }
396
397 double QtxResourceEdit::Item::getDouble( const double val ) const
398 {
399   QtxResourceMgr* resMgr = resourceMgr();
400   return resMgr ? resMgr->doubleValue( myResSection, myResParameter, val ) : val;
401 }
402
403 bool QtxResourceEdit::Item::getBoolean( const bool val ) const
404 {
405   QtxResourceMgr* resMgr = resourceMgr();
406   return resMgr ? resMgr->booleanValue( myResSection, myResParameter, val ) : val;
407 }
408
409 QString QtxResourceEdit::Item::getString( const QString& val ) const
410 {
411   QtxResourceMgr* resMgr = resourceMgr();
412   return resMgr ? resMgr->stringValue( myResSection, myResParameter, val ) : val;
413 }
414
415 QColor QtxResourceEdit::Item::getColor( const QColor& val ) const
416 {
417   QtxResourceMgr* resMgr = resourceMgr();
418   return resMgr ? resMgr->colorValue( myResSection, myResParameter, val ) : val;
419 }
420
421 QFont QtxResourceEdit::Item::getFont( const QFont& val ) const
422 {
423   QtxResourceMgr* resMgr = resourceMgr();
424   return resMgr ? resMgr->fontValue( myResSection, myResParameter, val ) : val;
425 }
426
427 void QtxResourceEdit::Item::setInteger( const int val )
428 {
429   QtxResourceMgr* resMgr = resourceMgr();
430   if ( resMgr )
431     resMgr->setValue( myResSection, myResParameter, val );
432 }
433
434 void QtxResourceEdit::Item::setDouble( const double val )
435 {
436   QtxResourceMgr* resMgr = resourceMgr();
437   if ( resMgr )
438     resMgr->setValue( myResSection, myResParameter, val );
439 }
440
441 void QtxResourceEdit::Item::setBoolean( const bool val )
442 {
443   QtxResourceMgr* resMgr = resourceMgr();
444   if ( resMgr )
445     resMgr->setValue( myResSection, myResParameter, val );
446 }
447
448 void QtxResourceEdit::Item::setString( const QString& val )
449 {
450   QtxResourceMgr* resMgr = resourceMgr();
451   if ( resMgr )
452     resMgr->setValue( myResSection, myResParameter, val );
453 }
454
455 void QtxResourceEdit::Item::setColor( const QColor& val )
456 {
457   QtxResourceMgr* resMgr = resourceMgr();
458   if ( resMgr )
459     resMgr->setValue( myResSection, myResParameter, val );
460 }
461
462 void QtxResourceEdit::Item::setFont( const QFont& val )
463 {
464   QtxResourceMgr* resMgr = resourceMgr();
465   if ( resMgr )
466     resMgr->setValue( myResSection, myResParameter, val );
467 }
468
469 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const int id ) const
470 {
471   return resourceEdit() ? resourceEdit()->item( id ) : 0;
472 }
473
474 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title ) const
475 {
476   return resourceEdit() ? resourceEdit()->item( title ) : 0;
477 }
478
479 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title, const int id ) const
480 {
481   return resourceEdit() ? resourceEdit()->item( title, id ) : 0;
482 }
483
484 int QtxResourceEdit::Item::generateId()
485 {
486   static int _id = 0;
487   return _id++;
488 }