1 // Copyright (C) 2007-2024 CEA, EDF, 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, subclass 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.
514 /*! \brief Restore preference item (for example, from the resource file, ignoring user preferences).
515 If not overridden, it is equivalent to \ref retrieve().
517 void QtxPreferenceItem::retrieveDefault()
523 \brief Get the value of the associated resource file setting.
524 \return associated resource file setting value
525 \sa setResourceValue()
527 QString QtxPreferenceItem::resourceValue() const
533 \brief Get the value of the associated resource file setting.
534 \param val new associated resource file setting value
537 void QtxPreferenceItem::setResourceValue( const QString& val )
543 \brief Get the resources manager.
544 \return resource manager pointer or 0 if it is not defined
546 QtxResourceMgr* QtxPreferenceItem::resourceMgr() const
548 QtxPreferenceMgr* mgr = preferenceMgr();
549 return mgr ? mgr->resourceMgr() : 0;
553 \brief Get the parent preferences manager.
554 \return preferences manager or 0 if it is not defined
556 QtxPreferenceMgr* QtxPreferenceItem::preferenceMgr() const
558 return parentItem() ? parentItem()->preferenceMgr() : 0;
562 \brief Find the item by the specified identifier.
563 \param id child item ID
564 \param rec if \c true recursive search is done
565 \return child item or 0 if it is not found
567 QtxPreferenceItem* QtxPreferenceItem::findItem( const int id, const bool rec ) const
569 QtxPreferenceItem* item = 0;
570 for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
572 QtxPreferenceItem* i = *it;
576 item = i->findItem( id, rec );
582 \brief Find the item by the specified title.
583 \param title child item title
584 \param rec if \c true recursive search is done
585 \return child item or 0 if it is not found
587 QtxPreferenceItem* QtxPreferenceItem::findItem( const QString& title, const bool rec ) const
589 QtxPreferenceItem* item = 0;
590 for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
592 QtxPreferenceItem* i = *it;
593 if ( i->title() == title )
596 item = i->findItem( title, rec );
602 \brief Find the item by the specified title and identifier.
603 \param title child item title
604 \param id child item ID
605 \param rec if \c true recursive search is done
606 \return child item or 0 if it is not found
608 QtxPreferenceItem* QtxPreferenceItem::findItem( const QString& title, const int id, const bool rec ) const
610 QtxPreferenceItem* item = 0;
611 for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
613 QtxPreferenceItem* i = *it;
614 if ( i->title() == title && i->id() == id )
617 item = i->findItem( title, id, rec );
622 void QtxPreferenceItem::activate()
626 void QtxPreferenceItem::ensureVisible()
629 parentItem()->ensureVisible( this );
633 \brief Get integer resources value corresponding to the item.
634 \param val default value (returned if there is no such resource)
635 \return integer value of the associated resource
638 int QtxPreferenceItem::getInteger( const int val ) const
640 QtxResourceMgr* resMgr = resourceMgr();
641 return resMgr ? resMgr->integerValue( mySection, myParameter, val ) : val;
645 \brief Get double resources value corresponding to the item.
646 \param val default value (returned if there is no such resource)
647 \return double value of the associated resource
650 double QtxPreferenceItem::getDouble( const double val ) const
652 QtxResourceMgr* resMgr = resourceMgr();
653 return resMgr ? resMgr->doubleValue( mySection, myParameter, val ) : val;
657 \brief Get boolean resources value corresponding to the item.
658 \param val default value (returned if there is no such resource)
659 \return boolean value of the associated resource
662 bool QtxPreferenceItem::getBoolean( const bool val ) const
664 QtxResourceMgr* resMgr = resourceMgr();
665 return resMgr ? resMgr->booleanValue( mySection, myParameter, val ) : val;
669 \brief Get string resources value corresponding to the item.
670 \param val default value (returned if there is no such resource)
671 \return string value of the associated resource
674 QString QtxPreferenceItem::getString( const QString& val ) const
677 QtxResourceMgr* resMgr = resourceMgr();
679 resMgr->value( mySection, myParameter, res, isEvaluateValues() );
684 \brief Get color resources value corresponding to the item.
685 \param val default value (returned if there is no such resource)
686 \return color value of the associated resource
689 QColor QtxPreferenceItem::getColor( const QColor& val ) const
691 QtxResourceMgr* resMgr = resourceMgr();
692 return resMgr ? resMgr->colorValue( mySection, myParameter, val ) : val;
696 \brief Get font resources value corresponding to the item.
697 \param val default value (returned if there is no such resource)
698 \return font value of the associated resource
701 QFont QtxPreferenceItem::getFont( const QFont& val ) const
703 QtxResourceMgr* resMgr = resourceMgr();
704 return resMgr ? resMgr->fontValue( mySection, myParameter, val ) : val;
708 \brief Set integer resources value corresponding to the item.
712 void QtxPreferenceItem::setInteger( const int val )
714 QtxResourceMgr* resMgr = resourceMgr();
716 resMgr->setValue( mySection, myParameter, val );
720 \brief Set double resources value corresponding to the item.
724 void QtxPreferenceItem::setDouble( const double val )
726 QtxResourceMgr* resMgr = resourceMgr();
728 resMgr->setValue( mySection, myParameter, val );
732 \brief Set boolean resources value corresponding to the item.
736 void QtxPreferenceItem::setBoolean( const bool val )
738 QtxResourceMgr* resMgr = resourceMgr();
740 resMgr->setValue( mySection, myParameter, val );
744 \brief Set string resources value corresponding to the item.
748 void QtxPreferenceItem::setString( const QString& val )
750 QtxResourceMgr* resMgr = resourceMgr();
752 resMgr->setValue( mySection, myParameter, val );
756 \brief Set color resources value corresponding to the item.
760 void QtxPreferenceItem::setColor( const QColor& val )
762 QtxResourceMgr* resMgr = resourceMgr();
764 resMgr->setValue( mySection, myParameter, val );
768 \brief Set font resources value corresponding to the item.
772 void QtxPreferenceItem::setFont( const QFont& val )
774 QtxResourceMgr* resMgr = resourceMgr();
776 resMgr->setValue( mySection, myParameter, val );
780 \brief Callback function which is called when the child
781 preference item is added.
783 This function can be reimplemented in the subclasses to customize
784 child item addition operation. Base implementation does nothing.
786 \param item child item being added
787 \sa itemRemoved(), itemChanged()
789 void QtxPreferenceItem::itemAdded( QtxPreferenceItem* /*item*/ )
794 \brief Callback function which is called when the child
795 preference item is removed.
797 This function can be reimplemented in the subclasses to customize
798 child item removal operation. Base implementation does nothing.
800 \param item child item being removed
801 \sa itemAdded(), itemChanged()
803 void QtxPreferenceItem::itemRemoved( QtxPreferenceItem* /*item*/ )
808 \brief Callback function which is called when the child
809 preference item is modified.
811 This function can be reimplemented in the subclasses to customize
812 child item modifying operation. Base implementation does nothing.
814 \param item child item being modified
815 \sa itemAdded(), itemRemoved()
817 void QtxPreferenceItem::itemChanged( QtxPreferenceItem* )
821 void QtxPreferenceItem::ensureVisible( QtxPreferenceItem* )
827 \brief Initiate item updating.
829 void QtxPreferenceItem::triggerUpdate()
831 Updater::instance()->updateItem( this );
835 \brief Get preference item option value.
837 This function can be reimplemented in the subclasses.
838 Base implementation does nothing.
840 \param name option name
841 \return property value or null QVariant if option is not set
844 QVariant QtxPreferenceItem::optionValue( const QString& name ) const
847 if ( name == "eval" || name == "evaluation" || name == "subst" || name == "substitution" )
848 val = isEvaluateValues();
849 else if ( name == "restart" )
850 val = isRestartRequired();
851 else if ( name == "title" )
857 \brief Set preference item option value.
859 This function can be reimplemented in the subclasses.
860 Base implementation does nothing.
862 \param name option name
863 \param val new property value
866 void QtxPreferenceItem::setOptionValue( const QString& name, const QVariant& val )
868 if ( name == "eval" || name == "evaluation" || name == "subst" || name == "substitution" )
870 if ( val.canConvert( QVariant::Bool ) )
871 setEvaluateValues( val.toBool() );
873 if ( name == "restart" )
875 if ( val.canConvert( QVariant::Bool ) )
876 setRestartRequired( val.toBool() );
878 else if ( name == "title" )
880 if ( val.canConvert( QVariant::String ) )
881 setTitle( val.toString() );
886 \brief Initiate item changing call back operation.
888 void QtxPreferenceItem::sendItemChanges()
891 parentItem()->itemChanged( this );
895 \brief Generate unique preference item identifier.
896 \return unique item ID
898 int QtxPreferenceItem::generateId()
905 \class QtxPreferenceMgr
906 \brief Class for managing preferences items.
911 \param mgr resources manager
913 QtxPreferenceMgr::QtxPreferenceMgr( QtxResourceMgr* mgr )
914 : QtxPreferenceItem( 0 ),
922 QtxPreferenceMgr::~QtxPreferenceMgr()
927 \brief Get the resources manager.
928 \return resource manager pointer or 0 if it is not defined
930 QtxResourceMgr* QtxPreferenceMgr::resourceMgr() const
936 \brief Get the parent preferences manager.
937 \return pointer to itself
939 QtxPreferenceMgr* QtxPreferenceMgr::preferenceMgr() const
941 return (QtxPreferenceMgr*)this;
945 \brief Get preference item option value.
946 \param id preference item ID
947 \param propName option name
948 \return property value or null QVariant if option is not set
951 QVariant QtxPreferenceMgr::option( const int id, const QString& propName ) const
954 QtxPreferenceItem* i = findItem( id, true );
956 propValue = i->option( propName );
961 \brief Set preference item option value.
962 \param id preference item ID
963 \param propName option name
964 \param propValue new property value
967 void QtxPreferenceMgr::setOption( const int id, const QString& propName, const QVariant& propValue )
969 QtxPreferenceItem* i = findItem( id, true );
971 i->setOption( propName, propValue );
975 \brief Store all preferences item to the resource manager.
978 void QtxPreferenceMgr::store()
981 resourceValues( before );
983 QList<QtxPreferenceItem*> items = childItems( true );
984 for ( QList<QtxPreferenceItem*>::iterator it = items.begin(); it != items.end(); ++it )
988 resourceValues( after );
991 differentValues( before, after, changed );
993 changedResources( changed );
997 \brief Retrieve all preference items from the resource manager.
1000 void QtxPreferenceMgr::retrieve()
1002 QList<QtxPreferenceItem*> items = childItems( true );
1003 for ( QList<QtxPreferenceItem*>::iterator it = items.begin(); it != items.end(); ++it )
1008 \brief Retrieve all preference items from the resource manager, ignoring user preferences.
1011 void QtxPreferenceMgr::retrieveDefault()
1013 QList<QtxPreferenceItem*> items = childItems( true );
1014 for ( QList<QtxPreferenceItem*>::iterator it = items.begin(); it != items.end(); ++it )
1015 (*it)->retrieveDefault();
1019 \brief Dumps all values to the backup container.
1022 void QtxPreferenceMgr::toBackup()
1025 resourceValues( myBackup );
1029 \brief Restore all values from the backup container.
1032 void QtxPreferenceMgr::fromBackup()
1035 resourceValues( before );
1037 setResourceValues( myBackup );
1040 resourceValues( after );
1042 ResourceMap changed;
1043 differentValues( before, after, changed );
1045 changedResources( changed );
1049 \brief Update preferences manager.
1051 Base implementation does nothing.
1053 void QtxPreferenceMgr::update()
1058 \brief Get all resources items values.
1059 \param map used as container filled with the resources values (<ID>:<value>)
1060 \sa setResourceValues()
1062 void QtxPreferenceMgr::resourceValues( QMap<int, QString>& map ) const
1065 QtxResourceMgr* resMgr = resourceMgr();
1066 QList<QtxPreferenceItem*> items = childItems( true );
1067 for ( QList<QtxPreferenceItem*>::const_iterator it = items.begin(); it != items.end(); ++it )
1069 QtxPreferenceItem* item = *it;
1070 item->resource( sect, name );
1071 if ( resMgr->hasValue( sect, name ) )
1072 map.insert( item->id(), item->resourceValue() );
1077 \brief Get all resources items values.
1078 \param map used as container filled with the resources values
1080 \sa setResourceValues()
1082 void QtxPreferenceMgr::resourceValues( ResourceMap& map ) const
1085 QtxResourceMgr* resMgr = resourceMgr();
1086 QList<QtxPreferenceItem*> items = childItems( true );
1087 for ( QList<QtxPreferenceItem*>::const_iterator it = items.begin(); it != items.end(); ++it )
1089 QtxPreferenceItem* item = *it;
1090 item->resource( sect, name );
1091 if ( resMgr->hasValue( sect, name ) )
1092 map.insert( item, item->resourceValue() );
1097 \brief Set all resources items values.
1098 \param map map with resources values (<ID>:<value>)
1099 \sa resourceValues()
1101 void QtxPreferenceMgr::setResourceValues( QMap<int, QString>& map ) const
1103 for ( QMap<int, QString>::const_iterator it = map.begin(); it != map.end(); ++it )
1105 QtxPreferenceItem* i = findItem( it.key(), true );
1107 i->setResourceValue( it.value() );
1112 \brief Set all resources items values.
1113 \param map map with resources values (<item>:<value>)
1114 \sa resourceValues()
1116 void QtxPreferenceMgr::setResourceValues( ResourceMap& map ) const
1118 for ( ResourceMap::const_iterator it = map.begin(); it != map.end(); ++it )
1119 it.key()->setResourceValue( it.value() );
1123 \brief Compare two maps of resources values to find differences.
1124 \param map1 first map
1125 \param map2 second map
1126 \param resMap map to be filled with different values
1127 \param fromFirst if \c true, then \a resMap will be filled with the values
1128 from \a map1, otherwise - from \a map2
1130 void QtxPreferenceMgr::differentValues( const QMap<int, QString>& map1, const QMap<int, QString>& map2,
1131 QMap<int, QString>& resMap, const bool fromFirst ) const
1134 const QMap<int, QString>& later = fromFirst ? map1 : map2;
1135 const QMap<int, QString>& early = fromFirst ? map2 : map1;
1137 for ( QMap<int, QString>::const_iterator it = later.begin(); it != later.end(); ++it )
1139 if ( !early.contains( it.key() ) || early[it.key()] != it.value() )
1140 resMap.insert( it.key(), it.value() );
1145 \brief Compare two maps of resources values to find differences.
1146 \param map1 first map
1147 \param map2 second map
1148 \param resMap map to be filled with different values
1149 \param fromFirst if \c true, then \a resMap will be filled with the values
1150 from \a map1, otherwise - from \a map2
1152 void QtxPreferenceMgr::differentValues( const ResourceMap& map1, const ResourceMap& map2,
1153 ResourceMap& resMap, const bool fromFirst ) const
1156 const ResourceMap& later = fromFirst ? map1 : map2;
1157 const ResourceMap& early = fromFirst ? map2 : map1;
1159 for ( ResourceMap::const_iterator it = later.begin(); it != later.end(); ++it )
1161 if ( !early.contains( it.key() ) || early[it.key()] != it.value() )
1162 resMap.insert( it.key(), it.value() );
1167 \brief Perform custom activity on resource changing.
1169 This method is called from store() and fromBackup() methods.
1170 Base implementation does nothing.
1172 \sa store(), fromBackup()
1174 void QtxPreferenceMgr::changedResources( const ResourceMap& )