Back to index

salome-gui  6.5.0
QtxPreferenceMgr.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File:      QtxPreferenceMgr.cxx
00021 // Author:    Sergey TELKOV
00022 //
00023 #include "QtxPreferenceMgr.h"
00024 
00025 #include "QtxResourceMgr.h"
00026 
00027 #include <QEvent>
00028 #include <QApplication>
00029 
00036 class QtxPreferenceItem::Updater : public QObject
00037 {
00038   Updater();
00039 public:
00040   ~Updater();
00041 
00042   static Updater*           instance();
00043 
00044   void                      updateItem( QtxPreferenceItem* );
00045   void                      removeItem( QtxPreferenceItem* );
00046 
00047 protected:
00048   virtual void              customEvent( QEvent* );
00049 
00050 private:
00051   QList<QtxPreferenceItem*> myItems;
00052   static Updater*           _Updater;
00053 };
00054 
00055 QtxPreferenceItem::Updater* QtxPreferenceItem::Updater::_Updater = 0;
00056 
00061 QtxPreferenceItem::Updater::Updater()
00062 {
00063 }
00064 
00069 QtxPreferenceItem::Updater::~Updater()
00070 {
00071 }
00072 
00078 QtxPreferenceItem::Updater* QtxPreferenceItem::Updater::instance()
00079 {
00080   if ( !_Updater )
00081     _Updater = new Updater();
00082   return _Updater;
00083 }
00084 
00090 void QtxPreferenceItem::Updater::updateItem( QtxPreferenceItem* item )
00091 {
00092   if ( !item || myItems.contains( item ) )
00093     return;
00094 
00095   myItems.append( item );
00096   QApplication::postEvent( this, new QEvent( QEvent::User ) );
00097 }
00098 
00104 void QtxPreferenceItem::Updater::removeItem( QtxPreferenceItem* item )
00105 {
00106   myItems.removeAll( item );
00107 }
00108 
00114 void QtxPreferenceItem::Updater::customEvent( QEvent* /*e*/ )
00115 {
00116   QList<QtxPreferenceItem*> lst = myItems;
00117   for ( QList<QtxPreferenceItem*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
00118     (*it)->updateContents();
00119 }
00120 
00133 QtxPreferenceItem::QtxPreferenceItem( QtxPreferenceItem* parent )
00134 : myParent( 0 ),
00135 myEval( true )
00136 {
00137   myId = generateId();
00138 
00139   if ( parent )
00140     parent->insertItem( this );
00141 }
00142 
00148 QtxPreferenceItem::QtxPreferenceItem( const QString& title, QtxPreferenceItem* parent )
00149 : myParent( 0 ),
00150   myTitle( title )
00151 {
00152   myId = generateId();
00153 
00154   if ( parent )
00155     parent->insertItem( this );
00156 }
00157 
00165 QtxPreferenceItem::QtxPreferenceItem( const QString& title, const QString& sect,
00166                                       const QString& param, QtxPreferenceItem* parent )
00167 : myParent( 0 ),
00168   myTitle( title ),
00169   mySection( sect ),
00170   myParameter( param )
00171 {
00172   myId = generateId();
00173 
00174   if ( parent )
00175     parent->insertItem( this );
00176 }
00177 
00181 QtxPreferenceItem::~QtxPreferenceItem()
00182 {
00183   ItemList list = myChildren;
00184   myChildren.clear();
00185   qDeleteAll( list );
00186 
00187   if ( myParent )
00188     myParent->removeItem( this );
00189 
00190   Updater::instance()->removeItem( this );
00191 }
00192 
00197 int QtxPreferenceItem::id() const
00198 {
00199   return myId;
00200 }
00201 
00206 int QtxPreferenceItem::rtti() const
00207 {
00208   return QtxPreferenceItem::RTTI();
00209 }
00210 
00215 int QtxPreferenceItem::RTTI()
00216 {
00217   return 1;
00218 }
00219 
00224 QtxPreferenceItem* QtxPreferenceItem::rootItem() const
00225 {
00226   QtxPreferenceItem* item = (QtxPreferenceItem*)this;
00227   while ( item->parentItem() )
00228     item = item->parentItem();
00229   return item;
00230 }
00231 
00236 QtxPreferenceItem* QtxPreferenceItem::parentItem() const
00237 {
00238   return myParent;
00239 }
00240 
00249 void QtxPreferenceItem::appendItem( QtxPreferenceItem* item )
00250 {
00251   insertItem( item, 0 );
00252 }
00253 
00264 void QtxPreferenceItem::insertItem( QtxPreferenceItem* item, QtxPreferenceItem* before )
00265 {
00266   if ( !item )
00267     return;
00268 
00269   if ( myChildren.contains( item ) && item == before )
00270     return;
00271 
00272   if ( myChildren.contains( item ) )
00273     myChildren.removeAll( item );
00274 
00275   int idx = myChildren.indexOf( before );
00276   if ( idx < 0 )
00277     idx = myChildren.count();
00278 
00279   if ( item->parentItem() && item->parentItem() != this )
00280     item->parentItem()->removeItem( item );
00281 
00282   item->myParent = this;
00283   myChildren.insert( idx, item );
00284 
00285   itemAdded( item );
00286 }
00287 
00293 void QtxPreferenceItem::removeItem( QtxPreferenceItem* item )
00294 {
00295   if ( !item || !myChildren.contains( item ) )
00296     return;
00297 
00298   item->myParent = 0;
00299   myChildren.removeAll( item );
00300 
00301   itemRemoved( item );
00302 }
00303 
00309 QList<QtxPreferenceItem*> QtxPreferenceItem::childItems( const bool rec ) const
00310 {
00311   QList<QtxPreferenceItem*> lst = myChildren;
00312   if ( rec )
00313   {
00314     for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end(); ++it )
00315       lst += (*it)->childItems( rec );
00316   }
00317 
00318   return lst;
00319 }
00320 
00325 int QtxPreferenceItem::depth() const
00326 {
00327   return parentItem() ? parentItem()->depth() + 1 : 0;
00328 }
00329 
00335 int QtxPreferenceItem::count() const
00336 {
00337   return myChildren.count();
00338 }
00339 
00345 bool QtxPreferenceItem::isEmpty() const
00346 {
00347   return myChildren.isEmpty();
00348 }
00349 
00355 QIcon QtxPreferenceItem::icon() const
00356 {
00357   return myIcon;
00358 }
00359 
00365 QString QtxPreferenceItem::title() const
00366 {
00367   return myTitle;
00368 }
00369 
00376 void QtxPreferenceItem::resource( QString& sec, QString& param ) const
00377 {
00378   sec = mySection;
00379   param = myParameter;
00380 }
00381 
00387 void QtxPreferenceItem::setIcon( const QIcon& ico )
00388 {
00389   if ( myIcon.serialNumber() == ico.serialNumber() )
00390     return;
00391 
00392   myIcon = ico;
00393   sendItemChanges();
00394 }
00395 
00401 void QtxPreferenceItem::setTitle( const QString& title )
00402 {
00403   if ( myTitle == title )
00404     return;
00405 
00406   myTitle = title;
00407   sendItemChanges();
00408 }
00409 
00416 void QtxPreferenceItem::setResource( const QString& sec, const QString& param )
00417 {
00418   mySection = sec;
00419   myParameter = param;
00420 }
00421 
00425 void QtxPreferenceItem::updateContents()
00426 {
00427   Updater::instance()->removeItem( this );
00428 }
00429 
00436 QVariant QtxPreferenceItem::option( const QString& name ) const
00437 {
00438   return optionValue( name.toLower() );
00439 }
00440 
00447 void QtxPreferenceItem::setOption( const QString& name, const QVariant& val )
00448 {
00449   QVariant old = optionValue( name.toLower() );
00450   setOptionValue( name.toLower(), val );
00451   if ( old != optionValue( name.toLower() ) )
00452     sendItemChanges();
00453 }
00454 
00455 bool QtxPreferenceItem::isEvaluateValues() const
00456 {
00457   return myEval;
00458 }
00459 
00460 void QtxPreferenceItem::setEvaluateValues( const bool on )
00461 {
00462   myEval = on;
00463 }
00464 
00488 QString QtxPreferenceItem::resourceValue() const
00489 {
00490   return getString();
00491 }
00492 
00498 void QtxPreferenceItem::setResourceValue( const QString& val )
00499 {
00500   setString( val );
00501 }
00502 
00507 QtxResourceMgr* QtxPreferenceItem::resourceMgr() const
00508 {
00509   QtxPreferenceMgr* mgr = preferenceMgr();
00510   return mgr ? mgr->resourceMgr() : 0;
00511 }
00512 
00517 QtxPreferenceMgr* QtxPreferenceItem::preferenceMgr() const
00518 {
00519   return parentItem() ? parentItem()->preferenceMgr() : 0;
00520 }
00521 
00528 QtxPreferenceItem* QtxPreferenceItem::findItem( const int id, const bool rec ) const
00529 {
00530   QtxPreferenceItem* item = 0;
00531   for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
00532   {
00533     QtxPreferenceItem* i = *it;
00534     if ( i->id() == id )
00535       item = i;
00536     else if ( rec )
00537       item = i->findItem( id, rec );
00538   }
00539   return item;
00540 }
00541 
00548 QtxPreferenceItem* QtxPreferenceItem::findItem( const QString& title, const bool rec ) const
00549 {
00550   QtxPreferenceItem* item = 0;
00551   for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
00552   {
00553     QtxPreferenceItem* i = *it;
00554     if ( i->title() == title )
00555       item = i;
00556     else if ( rec )
00557       item = i->findItem( title, rec );
00558   }
00559   return item;
00560 }
00561 
00569 QtxPreferenceItem* QtxPreferenceItem::findItem( const QString& title, const int id, const bool rec ) const
00570 {
00571   QtxPreferenceItem* item = 0;
00572   for ( ItemList::const_iterator it = myChildren.begin(); it != myChildren.end() && !item; ++it )
00573   {
00574     QtxPreferenceItem* i = *it;
00575     if ( i->title() == title && i->id() == id )
00576       item = i;
00577     else if ( rec )
00578       item = i->findItem( title, id, rec );
00579   }
00580   return item;
00581 }
00582 
00583 void QtxPreferenceItem::activate()
00584 {
00585 }
00586 
00587 void QtxPreferenceItem::ensureVisible()
00588 {
00589   if ( parentItem() )
00590     parentItem()->ensureVisible( this );
00591 }
00592 
00599 int QtxPreferenceItem::getInteger( const int val ) const
00600 {
00601   QtxResourceMgr* resMgr = resourceMgr();
00602   return resMgr ? resMgr->integerValue( mySection, myParameter, val ) : val;
00603 }
00604 
00611 double QtxPreferenceItem::getDouble( const double val ) const
00612 {
00613   QtxResourceMgr* resMgr = resourceMgr();
00614   return resMgr ? resMgr->doubleValue( mySection, myParameter, val ) : val;
00615 }
00616 
00623 bool QtxPreferenceItem::getBoolean( const bool val ) const
00624 {
00625   QtxResourceMgr* resMgr = resourceMgr();
00626   return resMgr ? resMgr->booleanValue( mySection, myParameter, val ) : val;
00627 }
00628 
00635 QString QtxPreferenceItem::getString( const QString& val ) const
00636 {
00637   QString res = val;
00638   QtxResourceMgr* resMgr = resourceMgr();
00639   if ( resMgr )
00640     resMgr->value( mySection, myParameter, res, isEvaluateValues() );
00641   return res;
00642 }
00643 
00650 QColor QtxPreferenceItem::getColor( const QColor& val ) const
00651 {
00652   QtxResourceMgr* resMgr = resourceMgr();
00653   return resMgr ? resMgr->colorValue( mySection, myParameter, val ) : val;
00654 }
00655 
00662 QFont QtxPreferenceItem::getFont( const QFont& val ) const
00663 {
00664   QtxResourceMgr* resMgr = resourceMgr();
00665   return resMgr ? resMgr->fontValue( mySection, myParameter, val ) : val;
00666 }
00667 
00673 void QtxPreferenceItem::setInteger( const int val )
00674 {
00675   QtxResourceMgr* resMgr = resourceMgr();
00676   if ( resMgr )
00677     resMgr->setValue( mySection, myParameter, val );
00678 }
00679 
00685 void QtxPreferenceItem::setDouble( const double val )
00686 {
00687   QtxResourceMgr* resMgr = resourceMgr();
00688   if ( resMgr )
00689     resMgr->setValue( mySection, myParameter, val );
00690 }
00691 
00697 void QtxPreferenceItem::setBoolean( const bool val )
00698 {
00699   QtxResourceMgr* resMgr = resourceMgr();
00700   if ( resMgr )
00701     resMgr->setValue( mySection, myParameter, val );
00702 }
00703 
00709 void QtxPreferenceItem::setString( const QString& val )
00710 {
00711   QtxResourceMgr* resMgr = resourceMgr();
00712   if ( resMgr )
00713     resMgr->setValue( mySection, myParameter, val );
00714 }
00715 
00721 void QtxPreferenceItem::setColor( const QColor& val )
00722 {
00723   QtxResourceMgr* resMgr = resourceMgr();
00724   if ( resMgr )
00725     resMgr->setValue( mySection, myParameter, val );
00726 }
00727 
00733 void QtxPreferenceItem::setFont( const QFont& val )
00734 {
00735   QtxResourceMgr* resMgr = resourceMgr();
00736   if ( resMgr )
00737     resMgr->setValue( mySection, myParameter, val );
00738 }
00739 
00750 void QtxPreferenceItem::itemAdded( QtxPreferenceItem* /*item*/ )
00751 {
00752 }
00753 
00764 void QtxPreferenceItem::itemRemoved( QtxPreferenceItem* /*item*/ )
00765 {
00766 }
00767 
00778 void QtxPreferenceItem::itemChanged( QtxPreferenceItem* )
00779 {
00780 }
00781 
00782 void QtxPreferenceItem::ensureVisible( QtxPreferenceItem* )
00783 {
00784   ensureVisible();
00785 }
00786 
00790 void QtxPreferenceItem::triggerUpdate()
00791 {
00792   Updater::instance()->updateItem( this );
00793 }
00794 
00805 QVariant QtxPreferenceItem::optionValue( const QString& name ) const
00806 {
00807   QVariant val;
00808   if ( name == "eval" || name == "evaluation" || name == "subst" || name == "substitution" )
00809     val = isEvaluateValues();
00810   else if ( name == "title" )
00811     val = title();
00812   return val;
00813 }
00814 
00825 void QtxPreferenceItem::setOptionValue( const QString& name, const QVariant& val )
00826 {
00827   if ( name == "eval" || name == "evaluation" || name == "subst" || name == "substitution" )
00828   {
00829     if ( val.canConvert( QVariant::Bool ) )
00830       setEvaluateValues( val.toBool() );
00831   }
00832   else if ( name == "title" )
00833   {
00834     if ( val.canConvert( QVariant::String ) )
00835       setTitle( val.toString() );
00836   }
00837 }
00838 
00842 void QtxPreferenceItem::sendItemChanges()
00843 {
00844   if ( parentItem() )
00845     parentItem()->itemChanged( this );
00846 }
00847 
00852 int QtxPreferenceItem::generateId()
00853 {
00854   static int _id = 0;
00855   return _id++;
00856 }
00857 
00867 QtxPreferenceMgr::QtxPreferenceMgr( QtxResourceMgr* mgr )
00868 : QtxPreferenceItem( 0 ),
00869   myResMgr( mgr )
00870 {
00871 }
00872 
00876 QtxPreferenceMgr::~QtxPreferenceMgr()
00877 {
00878 }
00879 
00884 QtxResourceMgr* QtxPreferenceMgr::resourceMgr() const
00885 {
00886   return myResMgr;
00887 }
00888 
00893 QtxPreferenceMgr* QtxPreferenceMgr::preferenceMgr() const
00894 {
00895   return (QtxPreferenceMgr*)this;
00896 }
00897 
00905 QVariant QtxPreferenceMgr::option( const int id, const QString& propName ) const
00906 {
00907   QVariant propValue;
00908   QtxPreferenceItem* i = findItem( id, true );
00909   if ( i )
00910     propValue = i->option( propName );
00911   return propValue;
00912 }
00913 
00921 void QtxPreferenceMgr::setOption( const int id, const QString& propName, const QVariant& propValue )
00922 {
00923   QtxPreferenceItem* i = findItem( id, true );
00924   if ( i )
00925     i->setOption( propName, propValue );
00926 }
00927 
00932 void QtxPreferenceMgr::store()
00933 {
00934   ResourceMap before;
00935   resourceValues( before );
00936 
00937   QList<QtxPreferenceItem*> items = childItems( true );
00938   for ( QList<QtxPreferenceItem*>::iterator it = items.begin(); it != items.end(); ++it )
00939     (*it)->store();
00940 
00941   ResourceMap after;
00942   resourceValues( after );
00943 
00944   ResourceMap changed;
00945   differentValues( before, after, changed );
00946 
00947   changedResources( changed );
00948 }
00949 
00954 void QtxPreferenceMgr::retrieve()
00955 {
00956   QList<QtxPreferenceItem*> items = childItems( true );
00957   for ( QList<QtxPreferenceItem*>::iterator it = items.begin(); it != items.end(); ++it )
00958     (*it)->retrieve();
00959 }
00960 
00965 void QtxPreferenceMgr::toBackup()
00966 {
00967   myBackup.clear();
00968   resourceValues( myBackup );
00969 }
00970 
00975 void QtxPreferenceMgr::fromBackup()
00976 {
00977   ResourceMap before;
00978   resourceValues( before );
00979 
00980   setResourceValues( myBackup );
00981 
00982   ResourceMap after;
00983   resourceValues( after );
00984 
00985   ResourceMap changed;
00986   differentValues( before, after, changed );
00987 
00988   changedResources( changed );
00989 }
00990 
00996 void QtxPreferenceMgr::update()
00997 {
00998 }
00999 
01005 void QtxPreferenceMgr::resourceValues( QMap<int, QString>& map ) const
01006 {
01007   QString sect, name;
01008   QtxResourceMgr* resMgr = resourceMgr();
01009   QList<QtxPreferenceItem*> items = childItems( true );
01010   for ( QList<QtxPreferenceItem*>::const_iterator it = items.begin(); it != items.end(); ++it )
01011   {
01012     QtxPreferenceItem* item = *it;
01013     item->resource( sect, name );
01014     if ( resMgr->hasValue( sect, name ) )
01015       map.insert( item->id(), item->resourceValue() );
01016   }
01017 }
01018 
01025 void QtxPreferenceMgr::resourceValues( ResourceMap& map ) const
01026 {
01027   QString sect, name;
01028   QtxResourceMgr* resMgr = resourceMgr();
01029   QList<QtxPreferenceItem*> items = childItems( true );
01030   for ( QList<QtxPreferenceItem*>::const_iterator it = items.begin(); it != items.end(); ++it )
01031   {
01032     QtxPreferenceItem* item = *it;
01033     item->resource( sect, name );
01034     if ( resMgr->hasValue( sect, name ) )
01035       map.insert( item, item->resourceValue() );
01036   }
01037 }
01038 
01044 void QtxPreferenceMgr::setResourceValues( QMap<int, QString>& map ) const
01045 {
01046   for ( QMap<int, QString>::const_iterator it = map.begin(); it != map.end(); ++it )
01047   {
01048     QtxPreferenceItem* i = findItem( it.key(), true );
01049     if ( i )
01050       i->setResourceValue( it.value() );
01051   }
01052 }
01053 
01059 void QtxPreferenceMgr::setResourceValues( ResourceMap& map ) const
01060 {
01061   for ( ResourceMap::const_iterator it = map.begin(); it != map.end(); ++it )
01062     it.key()->setResourceValue( it.value() );
01063 }
01064 
01073 void QtxPreferenceMgr::differentValues( const QMap<int, QString>& map1, const QMap<int, QString>& map2,
01074                                         QMap<int, QString>& resMap, const bool fromFirst ) const
01075 {
01076   resMap.clear();
01077   const QMap<int, QString>& later = fromFirst ? map1 : map2;
01078   const QMap<int, QString>& early = fromFirst ? map2 : map1;
01079 
01080   for ( QMap<int, QString>::const_iterator it = later.begin(); it != later.end(); ++it )
01081   {
01082     if ( !early.contains( it.key() ) || early[it.key()] != it.value() )
01083       resMap.insert( it.key(), it.value() );
01084   }
01085 }
01086 
01095 void QtxPreferenceMgr::differentValues( const ResourceMap& map1, const ResourceMap& map2,
01096                                         ResourceMap& resMap, const bool fromFirst ) const
01097 {
01098   resMap.clear();
01099   const ResourceMap& later = fromFirst ? map1 : map2;
01100   const ResourceMap& early = fromFirst ? map2 : map1;
01101 
01102   for ( ResourceMap::const_iterator it = later.begin(); it != later.end(); ++it )
01103   {
01104     if ( !early.contains( it.key() ) || early[it.key()] != it.value() )
01105       resMap.insert( it.key(), it.value() );
01106   }
01107 }
01108 
01117 void QtxPreferenceMgr::changedResources( const ResourceMap& )
01118 {
01119 }