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