1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 // File: QtxResourceEdit.cxx
20 // Author: Sergey TELKOV
22 #include "QtxResourceEdit.h"
24 #include "QtxResourceMgr.h"
30 QtxResourceEdit::QtxResourceEdit( QtxResourceMgr* mgr )
38 QtxResourceEdit::~QtxResourceEdit()
41 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
42 items.insert( it.key(), it.data() );
44 for ( ItemMap::ConstIterator itr = items.begin(); itr != items.end(); ++itr )
45 if ( myItems.contains( itr.key() ) )
50 \return assigned resource manager
52 QtxResourceMgr* QtxResourceEdit::resourceMgr() const
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
65 int QtxResourceEdit::addItem( const QString& label, const int pId, const int type,
66 const QString& section, const QString& param )
68 Item* i = createItem( label, type, pId );
72 if ( !myItems.contains( i->id() ) )
74 myItems.insert( i->id(), i );
77 i->setResource( section, param );
79 if ( !i->parentItem() && !myChildren.contains( i ) )
80 myChildren.append( i );
89 \return value of item property
91 \propName - propertyName
93 QVariant QtxResourceEdit::itemProperty( const int id, const QString& propName ) const
98 propValue = i->property( propName );
103 Sets value of item property
105 \propName - propertyName
106 \propValue - new value of property
108 void QtxResourceEdit::setItemProperty( const int id, const QString& propName, const QVariant& propValue )
110 Item* i = item( id );
112 i->setProperty( propName, propValue );
116 \return resource assigned with item
118 \param section - to return section of resource
119 \param param - to return name of resource
121 void QtxResourceEdit::resource( const int id, QString& sec, QString& param ) const
123 Item* i = item( id );
125 i->resource( sec, param );
129 Stores all values to resource manager
131 void QtxResourceEdit::store()
133 QMap<Item*, QString> before;
134 resourceValues( before );
136 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
139 QMap<Item*, QString> after;
140 resourceValues( after );
142 QMap<Item*, QString> changed;
143 differentValues( before, after, changed );
145 changedResources( changed );
149 Retrieve all values from resource manager
151 void QtxResourceEdit::retrieve()
153 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
154 it.data()->retrieve();
158 Stores all values to backup container
160 void QtxResourceEdit::toBackup()
163 resourceValues( myBackup );
167 Retrieve all values from backup container
169 void QtxResourceEdit::fromBackup()
171 QMap<Item*, QString> before;
172 resourceValues( before );
174 setResourceValues( myBackup );
176 QMap<Item*, QString> after;
177 resourceValues( after );
179 QMap<Item*, QString> changed;
180 differentValues( before, after, changed );
182 changedResources( changed );
186 Updates resource edit (default implementation is empty)
188 void QtxResourceEdit::update()
193 \return item by it's id
196 QtxResourceEdit::Item* QtxResourceEdit::item( const int id ) const
199 if ( myItems.contains( id ) )
205 \return item by it's title (finds first item)
206 \param title - item title
208 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title ) const
211 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
213 if ( it.data()->title() == title )
220 \return item by it's title and parent id
221 \param title - item title
222 \param pId - parent id
224 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title, const int pId ) const
227 Item* pItem = item( pId );
228 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
230 if ( it.data()->parentItem() == pItem && it.data()->title() == title )
239 \param label - text of label for new item
240 \param type - type of new item
241 \param pId - parent id
243 QtxResourceEdit::Item* QtxResourceEdit::createItem( const QString& label, const int type, const int pId )
247 i = createItem( label, type );
250 Item* pItem = item( pId );
253 i = pItem->createItem( label, type );
254 pItem->insertChild( i );
263 \param item - item to be removed
265 void QtxResourceEdit::removeItem( Item* item )
270 myChildren.remove( item );
271 myItems.remove( item->id() );
277 \return children items of resource edit
278 \param lst - list of items to be filled with children
280 void QtxResourceEdit::childItems( QPtrList<Item>& lst ) const
283 for ( QPtrListIterator<Item> it( myChildren ); it.current(); ++it )
284 lst.append( it.current() );
288 \return all resources values from widgets
289 \param map - map to be filled by resources values
291 void QtxResourceEdit::resourceValues( QMap<int, QString>& map ) const
294 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
296 it.data()->resource( sect, name );
297 if( myResMgr->hasValue( sect, name ) )
298 map.insert( it.key(), it.data()->resourceValue() );
303 \return all resources values from widgets
304 \param map - map to be filled by resources values
306 void QtxResourceEdit::resourceValues( QMap<Item*, QString>& map ) const
309 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
311 it.data()->resource( sect, name );
312 if( myResMgr->hasValue( sect, name ) )
313 map.insert( it.data(), it.data()->resourceValue() );
318 Sets to widgets all resources values from map
319 \param map - map with resources values
321 void QtxResourceEdit::setResourceValues( QMap<int, QString>& map ) const
323 for ( QMap<int, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
325 Item* i = item( it.key() );
327 i->setResourceValue( it.data() );
332 Sets to widgets all resources values from map
333 \param map - map with resources values
335 void QtxResourceEdit::setResourceValues( QMap<Item*, QString>& map ) const
337 for ( QMap<Item*, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
338 it.key()->setResourceValue( it.data() );
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
348 void QtxResourceEdit::differentValues( const QMap<int, QString>& map1, const QMap<int, QString>& map2,
349 QMap<int, QString>& resMap, const bool fromFirst ) const
352 const QMap<int, QString>& later = fromFirst ? map1 : map2;
353 const QMap<int, QString>& early = fromFirst ? map2 : map1;
355 for ( QMap<int, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
357 if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
358 resMap.insert( it.key(), it.data() );
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
369 void QtxResourceEdit::differentValues( const QMap<Item*, QString>& map1, const QMap<Item*, QString>& map2,
370 QMap<Item*, QString>& resMap, const bool fromFirst ) const
373 const QMap<Item*, QString>& later = fromFirst ? map1 : map2;
374 const QMap<Item*, QString>& early = fromFirst ? map2 : map1;
376 for ( QMap<Item*, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
378 if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
379 resMap.insert( it.key(), it.data() );
384 Makes some activity on resource changing (called from store() method)
387 void QtxResourceEdit::changedResources( const QMap<Item*, QString>& )
392 Some activity on item addition (default implementation is empty)
394 void QtxResourceEdit::itemAdded( Item* )
399 Some activity on item removing (default implementation is empty)
401 void QtxResourceEdit::itemRemoved( Item* )
408 QtxResourceEdit::Item::Item( QtxResourceEdit* edit, Item* parent )
415 parent->insertChild( this );
421 QtxResourceEdit::Item::~Item()
423 if ( resourceEdit() )
424 resourceEdit()->removeItem( this );
430 int QtxResourceEdit::Item::id() const
438 QtxResourceEdit::Item* QtxResourceEdit::Item::parentItem() const
444 Appends child and (if necessary) removes item from old parent
445 \param item - item to be added
447 void QtxResourceEdit::Item::insertChild( Item* item )
449 if ( !item || myChildren.contains( item ) )
452 if ( item->parentItem() && item->parentItem() != this )
453 item->parentItem()->removeChild( item );
455 item->myParent = this;
456 myChildren.append( item );
461 \param item - item to be removed
463 void QtxResourceEdit::Item::removeChild( Item* item )
465 if ( !item || !myChildren.contains( item ) )
468 myChildren.remove( item );
473 Fills list with children items
474 \param lst - list to be filled with
476 void QtxResourceEdit::Item::childItems( QPtrList<Item>& lst ) const
478 for ( ItemListIterator it( myChildren ); it.current(); ++it )
479 lst.append( it.current() );
483 \return true if there is no children of this item
485 bool QtxResourceEdit::Item::isEmpty() const
487 return myChildren.isEmpty();
491 \return title of item
493 QString QtxResourceEdit::Item::title() const
499 \return assigned resource placement
500 \param sec - to return section
501 \param param - to return param name
503 void QtxResourceEdit::Item::resource( QString& sec, QString& param ) const
506 param = myResParameter;
511 \param title - new item title
513 void QtxResourceEdit::Item::setTitle( const QString& title )
519 Assigns new resource to item
521 \param sec - param name
523 void QtxResourceEdit::Item::setResource( const QString& sec, const QString& param )
526 myResParameter = param;
530 Updates item (default implementation is empty)
532 void QtxResourceEdit::Item::update()
537 \return property value
539 QVariant QtxResourceEdit::Item::property( const QString& ) const
547 void QtxResourceEdit::Item::setProperty( const QString&, const QVariant& )
552 \return value of assigned resource
554 QString QtxResourceEdit::Item::resourceValue() const
560 Sets value of assigned resource
561 \param val - new value
563 void QtxResourceEdit::Item::setResourceValue( const QString& val )
569 \return corresponding resource manager
571 QtxResourceMgr* QtxResourceEdit::Item::resourceMgr() const
573 QtxResourceMgr* resMgr = 0;
574 if ( resourceEdit() )
575 resMgr = resourceEdit()->resourceMgr();
580 \return corresponding resource edit
582 QtxResourceEdit* QtxResourceEdit::Item::resourceEdit() const
588 \return integer value of resource corresponding to item
589 \param val - default value (it is returned if there is no such resource)
591 int QtxResourceEdit::Item::getInteger( const int val ) const
593 QtxResourceMgr* resMgr = resourceMgr();
594 return resMgr ? resMgr->integerValue( myResSection, myResParameter, val ) : val;
598 \return double value of resource corresponding to item
599 \param val - default value (it is returned if there is no such resource)
601 double QtxResourceEdit::Item::getDouble( const double val ) const
603 QtxResourceMgr* resMgr = resourceMgr();
604 return resMgr ? resMgr->doubleValue( myResSection, myResParameter, val ) : val;
608 \return boolean value of resource corresponding to item
609 \param val - default value (it is returned if there is no such resource)
611 bool QtxResourceEdit::Item::getBoolean( const bool val ) const
613 QtxResourceMgr* resMgr = resourceMgr();
614 return resMgr ? resMgr->booleanValue( myResSection, myResParameter, val ) : val;
618 \return string value of resource corresponding to item
619 \param val - default value (it is returned if there is no such resource)
621 QString QtxResourceEdit::Item::getString( const QString& val ) const
623 QtxResourceMgr* resMgr = resourceMgr();
624 return resMgr ? resMgr->stringValue( myResSection, myResParameter, val ) : val;
628 \return color value of resource corresponding to item
629 \param val - default value (it is returned if there is no such resource)
631 QColor QtxResourceEdit::Item::getColor( const QColor& val ) const
633 QtxResourceMgr* resMgr = resourceMgr();
634 return resMgr ? resMgr->colorValue( myResSection, myResParameter, val ) : val;
638 \return font value of resource corresponding to item
639 \param val - default value (it is returned if there is no such resource)
641 QFont QtxResourceEdit::Item::getFont( const QFont& val ) const
643 QtxResourceMgr* resMgr = resourceMgr();
644 return resMgr ? resMgr->fontValue( myResSection, myResParameter, val ) : val;
648 Sets value of resource
651 void QtxResourceEdit::Item::setInteger( const int val )
653 QtxResourceMgr* resMgr = resourceMgr();
655 resMgr->setValue( myResSection, myResParameter, val );
659 Sets value of resource
662 void QtxResourceEdit::Item::setDouble( const double val )
664 QtxResourceMgr* resMgr = resourceMgr();
666 resMgr->setValue( myResSection, myResParameter, val );
670 Sets value of resource
673 void QtxResourceEdit::Item::setBoolean( const bool val )
675 QtxResourceMgr* resMgr = resourceMgr();
677 resMgr->setValue( myResSection, myResParameter, val );
681 Sets value of resource
684 void QtxResourceEdit::Item::setString( const QString& val )
686 QtxResourceMgr* resMgr = resourceMgr();
688 resMgr->setValue( myResSection, myResParameter, val );
692 Sets value of resource
695 void QtxResourceEdit::Item::setColor( const QColor& val )
697 QtxResourceMgr* resMgr = resourceMgr();
699 resMgr->setValue( myResSection, myResParameter, val );
703 Sets value of resource
706 void QtxResourceEdit::Item::setFont( const QFont& val )
708 QtxResourceMgr* resMgr = resourceMgr();
710 resMgr->setValue( myResSection, myResParameter, val );
715 \param id - other item id
717 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const int id ) const
719 return resourceEdit() ? resourceEdit()->item( id ) : 0;
724 \param title - other item title
726 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title ) const
728 return resourceEdit() ? resourceEdit()->item( title ) : 0;
733 \param title - other item title
734 \param id - parent item id
736 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title, const int id ) const
738 return resourceEdit() ? resourceEdit()->item( title, id ) : 0;
744 int QtxResourceEdit::Item::generateId()