1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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/ or email : webmaster.salome@opencascade.com
20 // File: QtxPreferenceMgr.cxx
21 // Author: Sergey TELKOV
23 #include "QtxPreferenceMgr.h"
25 #include "QtxResourceMgr.h"
28 #include <QApplication>
31 \class QtxPreferenceItem::Updater
32 \brief Preference item updater.
36 class QtxPreferenceItem::Updater : public QObject
42 static Updater* instance();
44 void updateItem( QtxPreferenceItem* );
45 void removeItem( QtxPreferenceItem* );
48 virtual void customEvent( QEvent* );
51 QList<QtxPreferenceItem*> myItems;
52 static Updater* _Updater;
55 QtxPreferenceItem::Updater* QtxPreferenceItem::Updater::_Updater = 0;
61 QtxPreferenceItem::Updater::Updater()
69 QtxPreferenceItem::Updater::~Updater()
74 \brief Get the only updater instance.
76 \return the only updater instance
78 QtxPreferenceItem::Updater* QtxPreferenceItem::Updater::instance()
81 _Updater = new Updater();
86 \brief Update the preference item.
88 \param item preference item to be updated
90 void QtxPreferenceItem::Updater::updateItem( QtxPreferenceItem* item )
92 if ( !item || myItems.contains( item ) )
95 myItems.append( item );
96 QApplication::postEvent( this, new QEvent( QEvent::User ) );
100 \brief Called when preference item is removed.
102 \param item preference item being removed
104 void QtxPreferenceItem::Updater::removeItem( QtxPreferenceItem* item )
106 myItems.removeAll( item );
110 \brief Custom events provessing. Updates all the items.
112 \param e custom event (not used)
114 void QtxPreferenceItem::Updater::customEvent( QEvent* /*e*/ )
116 QList<QtxPreferenceItem*> lst = myItems;
117 for ( QList<QtxPreferenceItem*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
118 (*it)->updateContents();
122 \class QtxPreferenceItem
123 \brief Base class for implementing of all the preference items.
125 To implement any specific preference item, cubclass from the
126 QtxPreferenceItem and redefine store() and retrieve() methods.
131 \param parent parent preference item
133 QtxPreferenceItem::QtxPreferenceItem( QtxPreferenceItem* parent )
136 myRestartNeeded( false )
141 parent->insertItem( this );
146 \param title item title
147 \param parent parent preference item
149 QtxPreferenceItem::QtxPreferenceItem( const QString& title, QtxPreferenceItem* parent )
152 myRestartNeeded( false ),
158 parent->insertItem( this );
163 \param title item title
164 \param sect resource file section to be associated with the item
165 \param param resource file parameter to be associated with the item
166 \param parent parent preference item
168 QtxPreferenceItem::QtxPreferenceItem( const QString& title, const QString& sect,
169 const QString& param, QtxPreferenceItem* parent )
172 myRestartNeeded( false ),
180 parent->insertItem( this );
186 QtxPreferenceItem::~QtxPreferenceItem()
188 ItemList list = myChildren;
193 myParent->removeItem( this );
195 Updater::instance()->removeItem( this );
199 \brief Get unique item identifier.
202 int QtxPreferenceItem::id() const
208 \brief Get unique item type identifier.
211 int QtxPreferenceItem::rtti() const
213 return QtxPreferenceItem::RTTI();
217 \brief Specify unique item class identifier.
218 \return item class ID
220 int QtxPreferenceItem::RTTI()
226 \brief Get root preference item.
229 QtxPreferenceItem* QtxPreferenceItem::rootItem() const
231 QtxPreferenceItem* item = (QtxPreferenceItem*)this;
232 while ( item->parentItem() )
233 item = item->parentItem();
238 \brief Get parent preference item.
241 QtxPreferenceItem* QtxPreferenceItem::parentItem() const
247 \brief Append child preference item.
249 Removes (if necessary) the item from the previous parent.
251 \param item item to be added
254 void QtxPreferenceItem::appendItem( QtxPreferenceItem* item )
256 insertItem( item, 0 );
260 \brief Insert child preference item before specified item.
261 If the before item is 0 then new item is appended.
263 Removes (if necessary) the item from the previous parent.
265 \param item item to be added
266 \param before item before which is inserted new \aitem
269 void QtxPreferenceItem::insertItem( QtxPreferenceItem* item, QtxPreferenceItem* before )
274 if ( myChildren.contains( item ) && item == before )
277 if ( myChildren.contains( item ) )
278 myChildren.removeAll( item );
280 int idx = myChildren.indexOf( before );
282 idx = myChildren.count();
284 if ( item->parentItem() && item->parentItem() != this )
285 item->parentItem()->removeItem( item );
287 item->myParent = this;
288 myChildren.insert( idx, item );
294 \brief Remove child preference item.
295 \param item item to be removed
298 void QtxPreferenceItem::removeItem( QtxPreferenceItem* item )
300 if ( !item || !myChildren.contains( item ) )
304 myChildren.removeAll( item );
310 \brief Get all child preference items.
311 \param rec recursion boolean flag
312 \return list of child items
314 QList<QtxPreferenceItem*> QtxPreferenceItem::childItems( const bool rec ) const
316 QList<QtxPreferenceItem*> lst = myChildren;
319 for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end(); ++it )
320 lst += (*it)->childItems( rec );
327 \brief Get preference item depth.
330 int QtxPreferenceItem::depth() const
332 return parentItem() ? parentItem()->depth() + 1 : 0;
336 \brief Get child preference items number.
337 \return number of child items
340 int QtxPreferenceItem::count() const
342 return myChildren.count();
346 \brief Check if the item has children.
347 \return \c true if item does not have children
350 bool QtxPreferenceItem::isEmpty() const
352 return myChildren.isEmpty();
356 \brief Get preference item icon.
360 QIcon QtxPreferenceItem::icon() const
366 \brief Get preference item title.
370 QString QtxPreferenceItem::title() const
376 \brief Get resource file settings associated to the preference item.
377 \param sec used to return resource file section name
378 \param param used to return resource file parameter name
381 void QtxPreferenceItem::resource( QString& sec, QString& param ) const
388 \brief Set prefence item icon.
389 \param ico new item icon
392 void QtxPreferenceItem::setIcon( const QIcon& ico )
394 if ( myIcon.cacheKey() == ico.cacheKey() )
402 \brief Set preference item title .
403 \param title new item title
406 void QtxPreferenceItem::setTitle( const QString& title )
408 if ( myTitle == title )
416 \brief Assign resource file settings to the preference item.
417 \param sec resource file section name
418 \param param resource file parameter name
421 void QtxPreferenceItem::setResource( const QString& sec, const QString& param )
428 \brief Update preference item.
430 void QtxPreferenceItem::updateContents()
432 Updater::instance()->removeItem( this );
436 \brief Get preference item option value.
437 \param name option name
438 \return property value or null QVariant if option is not set
441 QVariant QtxPreferenceItem::option( const QString& name ) const
443 return optionValue( name.toLower() );
447 \brief Set preference item option value.
448 \param name option name
449 \param val new property value
452 void QtxPreferenceItem::setOption( const QString& name, const QVariant& val )
454 QVariant old = optionValue( name.toLower() );
455 setOptionValue( name.toLower(), val );
456 if ( old != optionValue( name.toLower() ) )
461 \brief Get variables auto-conversion option value
464 bool QtxPreferenceItem::isEvaluateValues() const
470 \brief Switch variables auto-conversion option on/off
471 \param on option value
473 void QtxPreferenceItem::setEvaluateValues( const bool on )
479 \brief Get restart needed option value
482 bool QtxPreferenceItem::isRestartRequired() const
484 return myRestartNeeded;
488 \brief Switch restart needed option on/off
489 \param on option value
491 void QtxPreferenceItem::setRestartRequired( const bool on )
493 myRestartNeeded = on;
497 \fn void QtxPreferenceItem::store();
498 \brief Save preference item (for example, to the resource file).
500 This method should be implemented in the subclasses.
506 \fn virtual void QtxPreferenceItem::retrieve();
507 \brief Restore preference item (for example, from the resource file).
509 This method should be implemented in the subclasses.
515 \brief Get the value of the associated resource file setting.
516 \return associated resource file setting value
517 \sa setResourceValue()
519 QString QtxPreferenceItem::resourceValue() const
525 \brief Get the value of the associated resource file setting.
526 \param val new associated resource file setting value
529 void QtxPreferenceItem::setResourceValue( const QString& val )
535 \brief Get the resources manager.
536 \return resource manager pointer or 0 if it is not defined
538 QtxResourceMgr* QtxPreferenceItem::resourceMgr() const
540 QtxPreferenceMgr* mgr = preferenceMgr();
541 return mgr ? mgr->resourceMgr() : 0;
545 \brief Get the parent preferences manager.
546 \return preferences manager or 0 if it is not defined
548 QtxPreferenceMgr* QtxPreferenceItem::preferenceMgr() const
550 return parentItem() ? parentItem()->preferenceMgr() : 0;
554 \brief Find the item by the specified identifier.
555 \param id child item ID
556 \param rec if \c true recursive search is done
557 \return child item or 0 if it is not found
559 QtxPreferenceItem* QtxPreferenceItem::findItem( const int id, const bool rec ) const
561 QtxPreferenceItem* item = 0;
562 for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
564 QtxPreferenceItem* i = *it;
568 item = i->findItem( id, rec );
574 \brief Find the item by the specified title.
575 \param title child item title
576 \param rec if \c true recursive search is done
577 \return child item or 0 if it is not found
579 QtxPreferenceItem* QtxPreferenceItem::findItem( const QString& title, const bool rec ) const
581 QtxPreferenceItem* item = 0;
582 for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
584 QtxPreferenceItem* i = *it;
585 if ( i->title() == title )
588 item = i->findItem( title, rec );
594 \brief Find the item by the specified title and identifier.
595 \param title child item title
596 \param id child item ID
597 \param rec if \c true recursive search is done
598 \return child item or 0 if it is not found
600 QtxPreferenceItem* QtxPreferenceItem::findItem( const QString& title, const int id, const bool rec ) const
602 QtxPreferenceItem* item = 0;
603 for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
605 QtxPreferenceItem* i = *it;
606 if ( i->title() == title && i->id() == id )
609 item = i->findItem( title, id, rec );
614 void QtxPreferenceItem::activate()
618 void QtxPreferenceItem::ensureVisible()
621 parentItem()->ensureVisible( this );
625 \brief Get integer resources value corresponding to the item.
626 \param val default value (returned if there is no such resource)
627 \return integer value of the associated resource
630 int QtxPreferenceItem::getInteger( const int val ) const
632 QtxResourceMgr* resMgr = resourceMgr();
633 return resMgr ? resMgr->integerValue( mySection, myParameter, val ) : val;
637 \brief Get double resources value corresponding to the item.
638 \param val default value (returned if there is no such resource)
639 \return double value of the associated resource
642 double QtxPreferenceItem::getDouble( const double val ) const
644 QtxResourceMgr* resMgr = resourceMgr();
645 return resMgr ? resMgr->doubleValue( mySection, myParameter, val ) : val;
649 \brief Get boolean resources value corresponding to the item.
650 \param val default value (returned if there is no such resource)
651 \return boolean value of the associated resource
654 bool QtxPreferenceItem::getBoolean( const bool val ) const
656 QtxResourceMgr* resMgr = resourceMgr();
657 return resMgr ? resMgr->booleanValue( mySection, myParameter, val ) : val;
661 \brief Get string resources value corresponding to the item.
662 \param val default value (returned if there is no such resource)
663 \return string value of the associated resource
666 QString QtxPreferenceItem::getString( const QString& val ) const
669 QtxResourceMgr* resMgr = resourceMgr();
671 resMgr->value( mySection, myParameter, res, isEvaluateValues() );
676 \brief Get color resources value corresponding to the item.
677 \param val default value (returned if there is no such resource)
678 \return color value of the associated resource
681 QColor QtxPreferenceItem::getColor( const QColor& val ) const
683 QtxResourceMgr* resMgr = resourceMgr();
684 return resMgr ? resMgr->colorValue( mySection, myParameter, val ) : val;
688 \brief Get font resources value corresponding to the item.
689 \param val default value (returned if there is no such resource)
690 \return font value of the associated resource
693 QFont QtxPreferenceItem::getFont( const QFont& val ) const
695 QtxResourceMgr* resMgr = resourceMgr();
696 return resMgr ? resMgr->fontValue( mySection, myParameter, val ) : val;
700 \brief Set integer resources value corresponding to the item.
704 void QtxPreferenceItem::setInteger( const int val )
706 QtxResourceMgr* resMgr = resourceMgr();
708 resMgr->setValue( mySection, myParameter, val );
712 \brief Set double resources value corresponding to the item.
716 void QtxPreferenceItem::setDouble( const double val )
718 QtxResourceMgr* resMgr = resourceMgr();
720 resMgr->setValue( mySection, myParameter, val );
724 \brief Set boolean resources value corresponding to the item.
728 void QtxPreferenceItem::setBoolean( const bool val )
730 QtxResourceMgr* resMgr = resourceMgr();
732 resMgr->setValue( mySection, myParameter, val );
736 \brief Set string resources value corresponding to the item.
740 void QtxPreferenceItem::setString( const QString& val )
742 QtxResourceMgr* resMgr = resourceMgr();
744 resMgr->setValue( mySection, myParameter, val );
748 \brief Set color resources value corresponding to the item.
752 void QtxPreferenceItem::setColor( const QColor& val )
754 QtxResourceMgr* resMgr = resourceMgr();
756 resMgr->setValue( mySection, myParameter, val );
760 \brief Set font resources value corresponding to the item.
764 void QtxPreferenceItem::setFont( const QFont& val )
766 QtxResourceMgr* resMgr = resourceMgr();
768 resMgr->setValue( mySection, myParameter, val );
772 \brief Callback function which is called when the child
773 preference item is added.
775 This function can be reimplemented in the subclasses to customize
776 child item addition operation. Base implementation does nothing.
778 \param item child item being added
779 \sa itemRemoved(), itemChanged()
781 void QtxPreferenceItem::itemAdded( QtxPreferenceItem* /*item*/ )
786 \brief Callback function which is called when the child
787 preference item is removed.
789 This function can be reimplemented in the subclasses to customize
790 child item removal operation. Base implementation does nothing.
792 \param item child item being removed
793 \sa itemAdded(), itemChanged()
795 void QtxPreferenceItem::itemRemoved( QtxPreferenceItem* /*item*/ )
800 \brief Callback function which is called when the child
801 preference item is modified.
803 This function can be reimplemented in the subclasses to customize
804 child item modifying operation. Base implementation does nothing.
806 \param item child item being modified
807 \sa itemAdded(), itemRemoved()
809 void QtxPreferenceItem::itemChanged( QtxPreferenceItem* )
813 void QtxPreferenceItem::ensureVisible( QtxPreferenceItem* )
819 \brief Initiate item updating.
821 void QtxPreferenceItem::triggerUpdate()
823 Updater::instance()->updateItem( this );
827 \brief Get preference item option value.
829 This function can be reimplemented in the subclasses.
830 Base implementation does nothing.
832 \param name option name
833 \return property value or null QVariant if option is not set
836 QVariant QtxPreferenceItem::optionValue( const QString& name ) const
839 if ( name == "eval" || name == "evaluation" || name == "subst" || name == "substitution" )
840 val = isEvaluateValues();
841 else if ( name == "restart" )
842 val = isRestartRequired();
843 else if ( name == "title" )
849 \brief Set preference item option value.
851 This function can be reimplemented in the subclasses.
852 Base implementation does nothing.
854 \param name option name
855 \param val new property value
858 void QtxPreferenceItem::setOptionValue( const QString& name, const QVariant& val )
860 if ( name == "eval" || name == "evaluation" || name == "subst" || name == "substitution" )
862 if ( val.canConvert( QVariant::Bool ) )
863 setEvaluateValues( val.toBool() );
865 if ( name == "restart" )
867 if ( val.canConvert( QVariant::Bool ) )
868 setRestartRequired( val.toBool() );
870 else if ( name == "title" )
872 if ( val.canConvert( QVariant::String ) )
873 setTitle( val.toString() );
878 \brief Initiate item changing call back operation.
880 void QtxPreferenceItem::sendItemChanges()
883 parentItem()->itemChanged( this );
887 \brief Generate unique preference item identifier.
888 \return unique item ID
890 int QtxPreferenceItem::generateId()
897 \class QtxPreferenceMgr
898 \brief Class for managing preferences items.
903 \param mgr resources manager
905 QtxPreferenceMgr::QtxPreferenceMgr( QtxResourceMgr* mgr )
906 : QtxPreferenceItem( 0 ),
914 QtxPreferenceMgr::~QtxPreferenceMgr()
919 \brief Get the resources manager.
920 \return resource manager pointer or 0 if it is not defined
922 QtxResourceMgr* QtxPreferenceMgr::resourceMgr() const
928 \brief Get the parent preferences manager.
929 \return pointer to itself
931 QtxPreferenceMgr* QtxPreferenceMgr::preferenceMgr() const
933 return (QtxPreferenceMgr*)this;
937 \brief Get preference item option value.
938 \param id preference item ID
939 \param propName option name
940 \return property value or null QVariant if option is not set
943 QVariant QtxPreferenceMgr::option( const int id, const QString& propName ) const
946 QtxPreferenceItem* i = findItem( id, true );
948 propValue = i->option( propName );
953 \brief Set preference item option value.
954 \param id preference item ID
955 \param propName option name
956 \param propValue new property value
959 void QtxPreferenceMgr::setOption( const int id, const QString& propName, const QVariant& propValue )
961 QtxPreferenceItem* i = findItem( id, true );
963 i->setOption( propName, propValue );
967 \brief Store all preferences item to the resource manager.
970 void QtxPreferenceMgr::store()
973 resourceValues( before );
975 QList<QtxPreferenceItem*> items = childItems( true );
976 for ( QList<QtxPreferenceItem*>::iterator it = items.begin(); it != items.end(); ++it )
980 resourceValues( after );
983 differentValues( before, after, changed );
985 changedResources( changed );
989 \brief Retrieve all preference items from the resource manager.
992 void QtxPreferenceMgr::retrieve()
994 QList<QtxPreferenceItem*> items = childItems( true );
995 for ( QList<QtxPreferenceItem*>::iterator it = items.begin(); it != items.end(); ++it )
1000 \brief Dumps all values to the backup container.
1003 void QtxPreferenceMgr::toBackup()
1006 resourceValues( myBackup );
1010 \brief Restore all values from the backup container.
1013 void QtxPreferenceMgr::fromBackup()
1016 resourceValues( before );
1018 setResourceValues( myBackup );
1021 resourceValues( after );
1023 ResourceMap changed;
1024 differentValues( before, after, changed );
1026 changedResources( changed );
1030 \brief Update preferences manager.
1032 Base implementation does nothing.
1034 void QtxPreferenceMgr::update()
1039 \brief Get all resources items values.
1040 \param map used as container filled with the resources values (<ID>:<value>)
1041 \sa setResourceValues()
1043 void QtxPreferenceMgr::resourceValues( QMap<int, QString>& map ) const
1046 QtxResourceMgr* resMgr = resourceMgr();
1047 QList<QtxPreferenceItem*> items = childItems( true );
1048 for ( QList<QtxPreferenceItem*>::const_iterator it = items.begin(); it != items.end(); ++it )
1050 QtxPreferenceItem* item = *it;
1051 item->resource( sect, name );
1052 if ( resMgr->hasValue( sect, name ) )
1053 map.insert( item->id(), item->resourceValue() );
1058 \brief Get all resources items values.
1059 \param map used as container filled with the resources values
1061 \sa setResourceValues()
1063 void QtxPreferenceMgr::resourceValues( ResourceMap& map ) const
1066 QtxResourceMgr* resMgr = resourceMgr();
1067 QList<QtxPreferenceItem*> items = childItems( true );
1068 for ( QList<QtxPreferenceItem*>::const_iterator it = items.begin(); it != items.end(); ++it )
1070 QtxPreferenceItem* item = *it;
1071 item->resource( sect, name );
1072 if ( resMgr->hasValue( sect, name ) )
1073 map.insert( item, item->resourceValue() );
1078 \brief Set all resources items values.
1079 \param map map with resources values (<ID>:<value>)
1080 \sa resourceValues()
1082 void QtxPreferenceMgr::setResourceValues( QMap<int, QString>& map ) const
1084 for ( QMap<int, QString>::const_iterator it = map.begin(); it != map.end(); ++it )
1086 QtxPreferenceItem* i = findItem( it.key(), true );
1088 i->setResourceValue( it.value() );
1093 \brief Set all resources items values.
1094 \param map map with resources values (<item>:<value>)
1095 \sa resourceValues()
1097 void QtxPreferenceMgr::setResourceValues( ResourceMap& map ) const
1099 for ( ResourceMap::const_iterator it = map.begin(); it != map.end(); ++it )
1100 it.key()->setResourceValue( it.value() );
1104 \brief Compare two maps of resources values to find differences.
1105 \param map1 first map
1106 \param map2 second map
1107 \param resMap map to be filled with different values
1108 \param fromFirst if \c true, then \a resMap will be filled with the values
1109 from \a map1, otherwise - from \a map2
1111 void QtxPreferenceMgr::differentValues( const QMap<int, QString>& map1, const QMap<int, QString>& map2,
1112 QMap<int, QString>& resMap, const bool fromFirst ) const
1115 const QMap<int, QString>& later = fromFirst ? map1 : map2;
1116 const QMap<int, QString>& early = fromFirst ? map2 : map1;
1118 for ( QMap<int, QString>::const_iterator it = later.begin(); it != later.end(); ++it )
1120 if ( !early.contains( it.key() ) || early[it.key()] != it.value() )
1121 resMap.insert( it.key(), it.value() );
1126 \brief Compare two maps of resources values to find differences.
1127 \param map1 first map
1128 \param map2 second map
1129 \param resMap map to be filled with different values
1130 \param fromFirst if \c true, then \a resMap will be filled with the values
1131 from \a map1, otherwise - from \a map2
1133 void QtxPreferenceMgr::differentValues( const ResourceMap& map1, const ResourceMap& map2,
1134 ResourceMap& resMap, const bool fromFirst ) const
1137 const ResourceMap& later = fromFirst ? map1 : map2;
1138 const ResourceMap& early = fromFirst ? map2 : map1;
1140 for ( ResourceMap::const_iterator it = later.begin(); it != later.end(); ++it )
1142 if ( !early.contains( it.key() ) || early[it.key()] != it.value() )
1143 resMap.insert( it.key(), it.value() );
1148 \brief Perform custom activity on resource changing.
1150 This method is called from store() and fromBackup() methods.
1151 Base implementation does nothing.
1153 \sa store(), fromBackup()
1155 void QtxPreferenceMgr::changedResources( const ResourceMap& )