Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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 /*!
28   Constructor
29 */
30 QtxResourceEdit::QtxResourceEdit( QtxResourceMgr* mgr )
31 : myResMgr( mgr )
32 {
33 }
34
35 /*!
36   Destructor
37 */
38 QtxResourceEdit::~QtxResourceEdit()
39 {
40   ItemMap items;
41   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
42     items.insert( it.key(), it.data() );
43
44   for ( ItemMap::ConstIterator itr = items.begin(); itr != items.end(); ++itr )
45     if ( myItems.contains( itr.key() ) )
46       delete itr.data();
47 }
48
49 /*!
50   \return assigned resource manager
51 */
52 QtxResourceMgr* QtxResourceEdit::resourceMgr() const
53 {
54   return myResMgr;
55 }
56
57 /*!
58   Adds new item
59   \param label - label of widget to edit preference
60   \param pId - parent item id
61   \param type - type of item
62   \param section - section of resource assigned with item
63   \param param - name of resource assigned with item
64 */
65 int QtxResourceEdit::addItem( const QString& label, const int pId, const int type,
66                               const QString& section, const QString& param )
67 {
68   Item* i = createItem( label, type, pId );
69   if ( !i )
70     return -1;
71
72   if ( !myItems.contains( i->id() ) )
73   {
74     myItems.insert( i->id(), i );
75
76     i->setTitle( label );
77     i->setResource( section, param );
78
79     if ( !i->parentItem() && !myChildren.contains( i ) )
80       myChildren.append( i );
81
82     itemAdded( i );
83   }
84
85   return i->id();
86 }
87
88 /*!
89   \return value of item property
90   \param id - item id
91   \propName - propertyName
92 */
93 QVariant QtxResourceEdit::itemProperty( const int id, const QString& propName ) const
94 {
95   QVariant propValue;
96   Item* i = item( id );
97   if ( i )
98     propValue = i->property( propName );
99   return propValue;
100 }
101
102 /*!
103   Sets value of item property
104   \param id - item id
105   \propName - propertyName
106   \propValue - new value of property
107 */
108 void QtxResourceEdit::setItemProperty( const int id, const QString& propName, const QVariant& propValue )
109 {
110   Item* i = item( id );
111   if ( i )
112     i->setProperty( propName, propValue );
113 }
114
115 /*!
116   \return resource assigned with item
117   \param id - item id
118   \param section - to return section of resource
119   \param param - to return name of resource
120 */
121 void QtxResourceEdit::resource( const int id, QString& sec, QString& param ) const
122 {
123   Item* i = item( id );
124   if ( i )
125     i->resource( sec, param );
126 }
127
128 /*!
129   Stores all values to resource manager
130 */
131 void QtxResourceEdit::store()
132 {
133   QMap<Item*, QString> before;
134   resourceValues( before );
135
136   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
137     it.data()->store();
138
139   QMap<Item*, QString> after;
140   resourceValues( after );
141
142   QMap<Item*, QString> changed;
143   differentValues( before, after, changed );
144
145   changedResources( changed );
146 }
147
148 /*!
149   Retrieve all values from resource manager
150 */
151 void QtxResourceEdit::retrieve()
152 {
153   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
154     it.data()->retrieve();
155 }
156
157 /*!
158   Stores all values to backup container
159 */
160 void QtxResourceEdit::toBackup()
161 {
162   myBackup.clear();
163   resourceValues( myBackup );
164 }
165
166 /*!
167   Retrieve all values from backup container
168 */
169 void QtxResourceEdit::fromBackup()
170 {
171   QMap<Item*, QString> before;
172   resourceValues( before );
173
174   setResourceValues( myBackup );
175
176   QMap<Item*, QString> after;
177   resourceValues( after );
178
179   QMap<Item*, QString> changed;
180   differentValues( before, after, changed );
181
182   changedResources( changed );
183 }
184
185 /*!
186   Updates resource edit (default implementation is empty)
187 */
188 void QtxResourceEdit::update()
189 {
190 }
191
192 /*!
193   \return item by it's id
194   \param id - item id 
195 */
196 QtxResourceEdit::Item* QtxResourceEdit::item( const int id ) const
197 {
198   Item* i = 0;
199   if ( myItems.contains( id ) )
200     i = myItems[id];
201   return i;
202 }
203
204 /*!
205   \return item by it's title (finds first item)
206   \param title - item title 
207 */
208 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title ) const
209 {
210   Item* i = 0;
211   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
212   {
213     if ( it.data()->title() == title )
214       i = it.data();
215   }
216   return i;
217 }
218
219 /*!
220   \return item by it's title and parent id
221   \param title - item title 
222   \param pId - parent id
223 */
224 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title, const int pId ) const
225 {
226   Item* i = 0;
227   Item* pItem = item( pId );
228   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
229   {
230     if ( it.data()->parentItem() == pItem && it.data()->title() == title )
231       i = it.data();
232   }
233   return i;
234 }
235
236 /*!
237   Creates item
238   \return new item
239   \param label - text of label for new item
240   \param type - type of new item
241   \param pId - parent id
242 */
243 QtxResourceEdit::Item* QtxResourceEdit::createItem( const QString& label, const int type, const int pId )
244 {
245   Item* i = 0;
246   if ( pId < 0 )
247     i = createItem( label, type );
248   else
249   {
250     Item* pItem = item( pId );
251     if ( pItem )
252     {
253       i = pItem->createItem( label, type );
254       pItem->insertChild( i );
255     }
256   }
257
258   return i;
259 }
260
261 /*!
262   Removes item
263   \param item - item to be removed
264 */
265 void QtxResourceEdit::removeItem( Item* item )
266 {
267   if ( !item )
268     return;
269
270   myChildren.remove( item );
271   myItems.remove( item->id() );
272
273   itemRemoved( item );
274 }
275
276 /*!
277   \return children items of resource edit
278   \param lst - list of items to be filled with children
279 */
280 void QtxResourceEdit::childItems( QPtrList<Item>& lst ) const
281 {
282   lst.clear();
283   for ( QPtrListIterator<Item> it( myChildren ); it.current(); ++it )
284     lst.append( it.current() );
285 }
286
287 /*!
288   \return all resources values from widgets
289   \param map - map to be filled by resources values
290 */
291 void QtxResourceEdit::resourceValues( QMap<int, QString>& map ) const
292 {
293   QString sect, name;
294   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
295   {
296     it.data()->resource( sect, name );
297     if( myResMgr->hasValue( sect, name ) )
298       map.insert( it.key(), it.data()->resourceValue() );
299   }
300 }
301
302 /*!
303   \return all resources values from widgets
304   \param map - map to be filled by resources values
305 */
306 void QtxResourceEdit::resourceValues( QMap<Item*, QString>& map ) const
307 {
308   QString sect, name;
309   for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
310   {
311     it.data()->resource( sect, name );
312     if( myResMgr->hasValue( sect, name ) )
313       map.insert( it.data(), it.data()->resourceValue() );
314   }
315 }
316
317 /*!
318   Sets to widgets all resources values from map
319   \param map - map with resources values
320 */
321 void QtxResourceEdit::setResourceValues( QMap<int, QString>& map ) const
322 {
323   for ( QMap<int, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
324   {
325     Item* i = item( it.key() );
326     if ( i )
327       i->setResourceValue( it.data() );
328   }
329 }
330
331 /*!
332   Sets to widgets all resources values from map
333   \param map - map with resources values
334 */
335 void QtxResourceEdit::setResourceValues( QMap<Item*, QString>& map ) const
336 {
337   for ( QMap<Item*, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
338     it.key()->setResourceValue( it.data() );
339 }
340
341 /*!
342    Compares two map of resources values and finds different ones
343    \param map1 - first map
344    \param map2 - second map
345    \param resMap - map to be filled with different values
346    \param fromFirst - if it is true, then resMap will be filled with values from first map, otherwise - from second
347 */
348 void QtxResourceEdit::differentValues( const QMap<int, QString>& map1, const QMap<int, QString>& map2,
349                                        QMap<int, QString>& resMap, const bool fromFirst ) const
350 {
351   resMap.clear();
352   const QMap<int, QString>& later = fromFirst ? map1 : map2;
353   const QMap<int, QString>& early = fromFirst ? map2 : map1;
354
355   for ( QMap<int, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
356   {
357     if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
358       resMap.insert( it.key(), it.data() );
359   }
360 }
361
362 /*!
363    Compares two map of resources values and finds different ones
364    \param map1 - first map
365    \param map2 - second map
366    \param resMap - map to be filled with different values
367    \param fromFirst - if it is true, then resMap will be filled with values from first map, otherwise - from second
368 */
369 void QtxResourceEdit::differentValues( const QMap<Item*, QString>& map1, const QMap<Item*, QString>& map2,
370                                        QMap<Item*, QString>& resMap, const bool fromFirst ) const
371 {
372   resMap.clear();
373   const QMap<Item*, QString>& later = fromFirst ? map1 : map2;
374   const QMap<Item*, QString>& early = fromFirst ? map2 : map1;
375
376   for ( QMap<Item*, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
377   {
378     if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
379       resMap.insert( it.key(), it.data() );
380   }
381 }
382
383 /*!
384   Makes some activity on resource changing (called from store() method)
385   \sa store()
386 */
387 void QtxResourceEdit::changedResources( const QMap<Item*, QString>& )
388 {
389 }
390
391 /*!
392   Some activity on item addition (default implementation is empty)
393 */
394 void QtxResourceEdit::itemAdded( Item* )
395 {
396 }
397
398 /*!
399   Some activity on item removing (default implementation is empty)
400 */
401 void QtxResourceEdit::itemRemoved( Item* )
402 {
403 }
404
405 /*!
406   Constructor
407 */
408 QtxResourceEdit::Item::Item( QtxResourceEdit* edit, Item* parent )
409 : myEdit( edit ),
410 myParent( 0 )
411 {
412   myId = generateId();
413
414   if ( parent )
415     parent->insertChild( this );
416 }
417
418 /*!
419   Destructor
420 */
421 QtxResourceEdit::Item::~Item()
422 {
423   if ( resourceEdit() )
424     resourceEdit()->removeItem( this );
425 }
426
427 /*!
428   \return id of item
429 */
430 int QtxResourceEdit::Item::id() const
431 {
432   return myId;
433 }
434
435 /*!
436   \return parent item 
437 */
438 QtxResourceEdit::Item* QtxResourceEdit::Item::parentItem() const
439 {
440   return myParent;
441 }
442
443 /*!
444   Appends child and (if necessary) removes item from old parent
445   \param item - item to be added
446 */
447 void QtxResourceEdit::Item::insertChild( Item* item )
448 {
449   if ( !item || myChildren.contains( item ) )
450     return;
451
452   if ( item->parentItem() && item->parentItem() != this )
453     item->parentItem()->removeChild( item );
454
455   item->myParent = this;
456   myChildren.append( item );
457 }
458
459 /*!
460   Removes child
461   \param item - item to be removed
462 */
463 void QtxResourceEdit::Item::removeChild( Item* item )
464 {
465   if ( !item || !myChildren.contains( item ) )
466     return;
467
468   myChildren.remove( item );
469   item->myParent = 0;
470 }
471
472 /*!
473   Fills list with children items
474   \param lst - list to be filled with
475 */
476 void QtxResourceEdit::Item::childItems( QPtrList<Item>& lst ) const
477 {
478   for ( ItemListIterator it( myChildren ); it.current(); ++it )
479     lst.append( it.current() );
480 }
481
482 /*!
483   \return true if there is no children of this item
484 */
485 bool QtxResourceEdit::Item::isEmpty() const
486 {
487   return myChildren.isEmpty();
488 }
489
490 /*!
491   \return title of item
492 */
493 QString QtxResourceEdit::Item::title() const
494 {
495   return myTitle;
496 }
497
498 /*!
499   \return assigned resource placement
500   \param sec - to return section
501   \param param - to return param name
502 */
503 void QtxResourceEdit::Item::resource( QString& sec, QString& param ) const
504 {
505   sec = myResSection;
506   param = myResParameter;
507 }
508
509 /*!
510   Sets item title 
511   \param title - new item title
512 */
513 void QtxResourceEdit::Item::setTitle( const QString& title )
514 {
515   myTitle = title;
516 }
517
518 /*!
519   Assigns new resource to item
520   \param sec - section
521   \param sec - param name
522 */
523 void QtxResourceEdit::Item::setResource( const QString& sec, const QString& param )
524 {
525   myResSection = sec;
526   myResParameter = param;
527 }
528
529 /*!
530   Updates item (default implementation is empty)
531 */
532 void QtxResourceEdit::Item::update()
533 {
534 }
535
536 /*!
537   \return property value
538 */
539 QVariant QtxResourceEdit::Item::property( const QString& ) const
540 {
541   return QVariant();
542 }
543
544 /*!
545   Sets property value
546 */
547 void QtxResourceEdit::Item::setProperty( const QString&, const QVariant& )
548 {
549 }
550
551 /*!
552   \return value of assigned resource
553 */
554 QString QtxResourceEdit::Item::resourceValue() const
555 {
556   return getString();
557 }
558
559 /*!
560   Sets value of assigned resource
561   \param val - new value
562 */
563 void QtxResourceEdit::Item::setResourceValue( const QString& val )
564 {
565   setString( val );
566 }
567
568 /*!
569   \return corresponding resource manager
570 */
571 QtxResourceMgr* QtxResourceEdit::Item::resourceMgr() const
572 {
573   QtxResourceMgr* resMgr = 0;
574   if ( resourceEdit() )
575     resMgr = resourceEdit()->resourceMgr();
576   return resMgr;
577 }
578
579 /*!
580   \return corresponding resource edit
581 */
582 QtxResourceEdit* QtxResourceEdit::Item::resourceEdit() const
583 {
584   return myEdit;
585 }
586
587 /*!
588   \return integer value of resource corresponding to item
589   \param val - default value (it is returned if there is no such resource)
590 */
591 int QtxResourceEdit::Item::getInteger( const int val ) const
592 {
593   QtxResourceMgr* resMgr = resourceMgr();
594   return resMgr ? resMgr->integerValue( myResSection, myResParameter, val ) : val;
595 }
596
597 /*!
598   \return double value of resource corresponding to item
599   \param val - default value (it is returned if there is no such resource)
600 */
601 double QtxResourceEdit::Item::getDouble( const double val ) const
602 {
603   QtxResourceMgr* resMgr = resourceMgr();
604   return resMgr ? resMgr->doubleValue( myResSection, myResParameter, val ) : val;
605 }
606
607 /*!
608   \return boolean value of resource corresponding to item
609   \param val - default value (it is returned if there is no such resource)
610 */
611 bool QtxResourceEdit::Item::getBoolean( const bool val ) const
612 {
613   QtxResourceMgr* resMgr = resourceMgr();
614   return resMgr ? resMgr->booleanValue( myResSection, myResParameter, val ) : val;
615 }
616
617 /*!
618   \return string value of resource corresponding to item
619   \param val - default value (it is returned if there is no such resource)
620 */
621 QString QtxResourceEdit::Item::getString( const QString& val ) const
622 {
623   QtxResourceMgr* resMgr = resourceMgr();
624   return resMgr ? resMgr->stringValue( myResSection, myResParameter, val ) : val;
625 }
626
627 /*!
628   \return color value of resource corresponding to item
629   \param val - default value (it is returned if there is no such resource)
630 */
631 QColor QtxResourceEdit::Item::getColor( const QColor& val ) const
632 {
633   QtxResourceMgr* resMgr = resourceMgr();
634   return resMgr ? resMgr->colorValue( myResSection, myResParameter, val ) : val;
635 }
636
637 /*!
638   \return font value of resource corresponding to item
639   \param val - default value (it is returned if there is no such resource)
640 */
641 QFont QtxResourceEdit::Item::getFont( const QFont& val ) const
642 {
643   QtxResourceMgr* resMgr = resourceMgr();
644   return resMgr ? resMgr->fontValue( myResSection, myResParameter, val ) : val;
645 }
646
647 /*!
648   Sets value of resource
649   \param val - value
650 */
651 void QtxResourceEdit::Item::setInteger( const int val )
652 {
653   QtxResourceMgr* resMgr = resourceMgr();
654   if ( resMgr )
655     resMgr->setValue( myResSection, myResParameter, val );
656 }
657
658 /*!
659   Sets value of resource
660   \param val - value
661 */
662 void QtxResourceEdit::Item::setDouble( const double val )
663 {
664   QtxResourceMgr* resMgr = resourceMgr();
665   if ( resMgr )
666     resMgr->setValue( myResSection, myResParameter, val );
667 }
668
669 /*!
670   Sets value of resource
671   \param val - value
672 */
673 void QtxResourceEdit::Item::setBoolean( const bool val )
674 {
675   QtxResourceMgr* resMgr = resourceMgr();
676   if ( resMgr )
677     resMgr->setValue( myResSection, myResParameter, val );
678 }
679
680 /*!
681   Sets value of resource
682   \param val - value
683 */
684 void QtxResourceEdit::Item::setString( const QString& val )
685 {
686   QtxResourceMgr* resMgr = resourceMgr();
687   if ( resMgr )
688     resMgr->setValue( myResSection, myResParameter, val );
689 }
690
691 /*!
692   Sets value of resource
693   \param val - value
694 */
695 void QtxResourceEdit::Item::setColor( const QColor& val )
696 {
697   QtxResourceMgr* resMgr = resourceMgr();
698   if ( resMgr )
699     resMgr->setValue( myResSection, myResParameter, val );
700 }
701
702 /*!
703   Sets value of resource
704   \param val - value
705 */
706 void QtxResourceEdit::Item::setFont( const QFont& val )
707 {
708   QtxResourceMgr* resMgr = resourceMgr();
709   if ( resMgr )
710     resMgr->setValue( myResSection, myResParameter, val );
711 }
712
713 /*!
714   \return other item
715   \param id - other item id
716 */
717 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const int id ) const
718 {
719   return resourceEdit() ? resourceEdit()->item( id ) : 0;
720 }
721
722 /*!
723   \return other item
724   \param title - other item title
725 */
726 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title ) const
727 {
728   return resourceEdit() ? resourceEdit()->item( title ) : 0;
729 }
730
731 /*!
732   \return other item
733   \param title - other item title
734   \param id - parent item id
735 */
736 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title, const int id ) const
737 {
738   return resourceEdit() ? resourceEdit()->item( title, id ) : 0;
739 }
740
741 /*!
742   \return free item id
743 */
744 int QtxResourceEdit::Item::generateId()
745 {
746   static int _id = 0;
747   return _id++;
748 }