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"
27 Class: QtxResourceEdit
28 Descr: Class for managing preferences items
31 QtxResourceEdit::QtxResourceEdit( QtxResourceMgr* mgr )
36 QtxResourceEdit::~QtxResourceEdit()
38 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
42 QtxResourceMgr* QtxResourceEdit::resourceMgr() const
47 int QtxResourceEdit::addItem( const QString& label, const int pId, const int type,
48 const QString& section, const QString& param )
50 Item* i = createItem( label, type, pId );
54 if ( !myItems.contains( i->id() ) )
56 myItems.insert( i->id(), i );
59 i->setResource( section, param );
61 if ( !i->parentItem() && !myChildren.contains( i ) )
62 myChildren.append( i );
70 QVariant QtxResourceEdit::itemProperty( const int id, const QString& propName ) const
75 propValue = i->property( propName );
79 void QtxResourceEdit::setItemProperty( const int id, const QString& propName, const QVariant& propValue )
83 i->setProperty( propName, propValue );
86 void QtxResourceEdit::resource( const int id, QString& sec, QString& param ) const
90 i->resource( sec, param );
93 void QtxResourceEdit::store()
95 QMap<Item*, QString> before;
96 resourceValues( before );
98 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
101 QMap<Item*, QString> after;
102 resourceValues( after );
104 QMap<Item*, QString> changed;
105 differentValues( before, after, changed );
107 changedResources( changed );
110 void QtxResourceEdit::retrieve()
112 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
113 it.data()->retrieve();
116 void QtxResourceEdit::toBackup()
119 resourceValues( myBackup );
122 void QtxResourceEdit::fromBackup()
124 QMap<Item*, QString> before;
125 resourceValues( before );
127 setResourceValues( myBackup );
129 QMap<Item*, QString> after;
130 resourceValues( after );
132 QMap<Item*, QString> changed;
133 differentValues( before, after, changed );
135 changedResources( changed );
138 void QtxResourceEdit::update()
142 QtxResourceEdit::Item* QtxResourceEdit::item( const int id ) const
145 if ( myItems.contains( id ) )
150 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title ) const
153 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
155 if ( it.data()->title() == title )
161 QtxResourceEdit::Item* QtxResourceEdit::item( const QString& title, const int pId ) const
164 Item* pItem = item( pId );
165 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end() && !i; ++it )
167 if ( it.data()->parentItem() == pItem && it.data()->title() == title )
173 QtxResourceEdit::Item* QtxResourceEdit::createItem( const QString& label, const int type, const int pId )
177 i = createItem( label, type );
180 Item* pItem = item( pId );
183 i = pItem->createItem( label, type );
184 pItem->insertChild( i );
191 void QtxResourceEdit::removeItem( Item* item )
196 myChildren.remove( item );
197 myItems.remove( item->id() );
202 void QtxResourceEdit::childItems( QPtrList<Item>& lst ) const
205 for ( QPtrListIterator<Item> it( myChildren ); it.current(); ++it )
206 lst.append( it.current() );
209 void QtxResourceEdit::resourceValues( QMap<int, QString>& map ) const
212 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
214 it.data()->resource( sect, name );
215 if( myResMgr->hasValue( sect, name ) )
216 map.insert( it.key(), it.data()->resourceValue() );
220 void QtxResourceEdit::resourceValues( QMap<Item*, QString>& map ) const
223 for ( ItemMap::ConstIterator it = myItems.begin(); it != myItems.end(); ++it )
225 it.data()->resource( sect, name );
226 if( myResMgr->hasValue( sect, name ) )
227 map.insert( it.data(), it.data()->resourceValue() );
231 void QtxResourceEdit::setResourceValues( QMap<int, QString>& map ) const
233 for ( QMap<int, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
235 Item* i = item( it.key() );
237 i->setResourceValue( it.data() );
241 void QtxResourceEdit::setResourceValues( QMap<Item*, QString>& map ) const
243 for ( QMap<Item*, QString>::ConstIterator it = map.begin(); it != map.end(); ++it )
244 it.key()->setResourceValue( it.data() );
248 void QtxResourceEdit::differentValues( const QMap<int, QString>& map1, const QMap<int, QString>& map2,
249 QMap<int, QString>& resMap, const bool fromFirst ) const
252 const QMap<int, QString>& later = fromFirst ? map1 : map2;
253 const QMap<int, QString>& early = fromFirst ? map2 : map1;
255 for ( QMap<int, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
257 if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
258 resMap.insert( it.key(), it.data() );
262 void QtxResourceEdit::differentValues( const QMap<Item*, QString>& map1, const QMap<Item*, QString>& map2,
263 QMap<Item*, QString>& resMap, const bool fromFirst ) const
266 const QMap<Item*, QString>& later = fromFirst ? map1 : map2;
267 const QMap<Item*, QString>& early = fromFirst ? map2 : map1;
269 for ( QMap<Item*, QString>::ConstIterator it = later.begin(); it != later.end(); ++it )
271 if ( !early.contains( it.key() ) || early[it.key()] != it.data() )
272 resMap.insert( it.key(), it.data() );
276 void QtxResourceEdit::changedResources( const QMap<Item*, QString>& )
280 void QtxResourceEdit::itemAdded( Item* )
284 void QtxResourceEdit::itemRemoved( Item* )
289 Class: QtxResourceEdit::Item
290 Descr: Class for incapsulation of one preference item
293 QtxResourceEdit::Item::Item( QtxResourceEdit* edit, Item* parent )
300 parent->insertChild( this );
303 QtxResourceEdit::Item::~Item()
305 if ( resourceEdit() )
306 resourceEdit()->removeItem( this );
309 int QtxResourceEdit::Item::id() const
314 QtxResourceEdit::Item* QtxResourceEdit::Item::parentItem() const
319 void QtxResourceEdit::Item::insertChild( Item* item )
321 if ( !item || myChildren.contains( item ) )
324 if ( item->parentItem() && item->parentItem() != this )
325 item->parentItem()->removeChild( item );
327 item->myParent = this;
328 myChildren.append( item );
331 void QtxResourceEdit::Item::removeChild( Item* item )
333 if ( !item || !myChildren.contains( item ) )
336 myChildren.remove( item );
340 void QtxResourceEdit::Item::childItems( QPtrList<Item>& lst ) const
342 for ( ItemListIterator it( myChildren ); it.current(); ++it )
343 lst.append( it.current() );
346 bool QtxResourceEdit::Item::isEmpty() const
348 return myChildren.isEmpty();
351 QString QtxResourceEdit::Item::title() const
356 void QtxResourceEdit::Item::resource( QString& sec, QString& param ) const
359 param = myResParameter;
362 void QtxResourceEdit::Item::setTitle( const QString& title )
367 void QtxResourceEdit::Item::setResource( const QString& sec, const QString& param )
370 myResParameter = param;
373 void QtxResourceEdit::Item::update()
377 QVariant QtxResourceEdit::Item::property( const QString& ) const
382 void QtxResourceEdit::Item::setProperty( const QString&, const QVariant& )
386 QString QtxResourceEdit::Item::resourceValue() const
391 void QtxResourceEdit::Item::setResourceValue( const QString& val )
396 QtxResourceMgr* QtxResourceEdit::Item::resourceMgr() const
398 QtxResourceMgr* resMgr = 0;
399 if ( resourceEdit() )
400 resMgr = resourceEdit()->resourceMgr();
404 QtxResourceEdit* QtxResourceEdit::Item::resourceEdit() const
409 int QtxResourceEdit::Item::getInteger( const int val ) const
411 QtxResourceMgr* resMgr = resourceMgr();
412 return resMgr ? resMgr->integerValue( myResSection, myResParameter, val ) : val;
415 double QtxResourceEdit::Item::getDouble( const double val ) const
417 QtxResourceMgr* resMgr = resourceMgr();
418 return resMgr ? resMgr->doubleValue( myResSection, myResParameter, val ) : val;
421 bool QtxResourceEdit::Item::getBoolean( const bool val ) const
423 QtxResourceMgr* resMgr = resourceMgr();
424 return resMgr ? resMgr->booleanValue( myResSection, myResParameter, val ) : val;
427 QString QtxResourceEdit::Item::getString( const QString& val ) const
429 QtxResourceMgr* resMgr = resourceMgr();
430 return resMgr ? resMgr->stringValue( myResSection, myResParameter, val ) : val;
433 QColor QtxResourceEdit::Item::getColor( const QColor& val ) const
435 QtxResourceMgr* resMgr = resourceMgr();
436 return resMgr ? resMgr->colorValue( myResSection, myResParameter, val ) : val;
439 QFont QtxResourceEdit::Item::getFont( const QFont& val ) const
441 QtxResourceMgr* resMgr = resourceMgr();
442 return resMgr ? resMgr->fontValue( myResSection, myResParameter, val ) : val;
445 void QtxResourceEdit::Item::setInteger( const int val )
447 QtxResourceMgr* resMgr = resourceMgr();
449 resMgr->setValue( myResSection, myResParameter, val );
452 void QtxResourceEdit::Item::setDouble( const double val )
454 QtxResourceMgr* resMgr = resourceMgr();
456 resMgr->setValue( myResSection, myResParameter, val );
459 void QtxResourceEdit::Item::setBoolean( const bool val )
461 QtxResourceMgr* resMgr = resourceMgr();
463 resMgr->setValue( myResSection, myResParameter, val );
466 void QtxResourceEdit::Item::setString( const QString& val )
468 QtxResourceMgr* resMgr = resourceMgr();
470 resMgr->setValue( myResSection, myResParameter, val );
473 void QtxResourceEdit::Item::setColor( const QColor& val )
475 QtxResourceMgr* resMgr = resourceMgr();
477 resMgr->setValue( myResSection, myResParameter, val );
480 void QtxResourceEdit::Item::setFont( const QFont& val )
482 QtxResourceMgr* resMgr = resourceMgr();
484 resMgr->setValue( myResSection, myResParameter, val );
487 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const int id ) const
489 return resourceEdit() ? resourceEdit()->item( id ) : 0;
492 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title ) const
494 return resourceEdit() ? resourceEdit()->item( title ) : 0;
497 QtxResourceEdit::Item* QtxResourceEdit::Item::item( const QString& title, const int id ) const
499 return resourceEdit() ? resourceEdit()->item( title, id ) : 0;
502 int QtxResourceEdit::Item::generateId()