Salome HOME
2e1d352466965e38d4c97d20d1932638ea42077d
[modules/gui.git] / src / LightApp / LightApp_Preferences.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      LightApp_Preferences.cxx
24 // Author:    Sergey TELKOV
25
26 #include "LightApp_Preferences.h"
27
28 /*!
29   Constructor.Initialize by resource manager and parent QWidget.
30 */
31 LightApp_Preferences::LightApp_Preferences( QtxResourceMgr* resMgr, QWidget* parent )
32 : SUIT_PreferenceMgr( resMgr, parent )
33 {
34 }
35
36 /*!
37   Destructor.
38 */
39 LightApp_Preferences::~LightApp_Preferences()
40 {
41 }
42
43 /*!
44   Adds preference.
45 */
46 int LightApp_Preferences::addPreference( const QString& label, const int pId, const int type,
47                                          const QString& section, const QString& param )
48 {
49   return addItem( label, pId, (SUIT_PreferenceMgr::PrefItemType)type, section, param );
50 }
51
52 /*!
53   Adds preference.
54 */
55 int LightApp_Preferences::addPreference( const QString& mod, const QString& label, const int pId,
56                                          const int type, const QString& section, const QString& param )
57 {
58   int id = addItem( label, pId, (SUIT_PreferenceMgr::PrefItemType)type, section, param );
59   if ( id != -1 && !mod.isEmpty() )
60     myPrefMod.insert( id, mod );
61   return id;
62 }
63
64 /*!
65   Remove module preferences.
66 */
67 void LightApp_Preferences::removeModule( const QString& mod )
68 {
69   QMutableMapIterator<int, QString> it( myPrefMod );
70   while ( it.hasNext() )
71   {
72     it.next();
73     if ( it.value() == mod )
74       it.remove();
75   }
76   removeItem( mod );
77 }
78
79 /*!
80   Checks: is preferences has module with name \a mod.
81 */
82 bool LightApp_Preferences::hasModule( const QString& mod ) const
83 {
84   bool res = false;
85   for ( PrefModuleMap::ConstIterator it = myPrefMod.begin(); it != myPrefMod.end() && !res; ++it )
86     res = it.value() == mod;
87   return res;
88 }
89
90 void LightApp_Preferences::activateItem( const QString& path )
91 {
92   activateItem( QStringList() << path );
93 }
94
95 void LightApp_Preferences::activateItem( const QStringList& path )
96 {
97   QtxPreferenceItem* item = root();
98   foreach( QString label, path )
99   {
100     if ( !item )
101       break;
102     item = item->findItem( label, false );
103   }
104   if ( item )
105   {
106     item->ensureVisible();
107     item->activate();
108   }
109 }
110
111 /*!Do nothing.*/
112 void LightApp_Preferences::onHelp()
113 {
114 }
115
116 /*!Store preferences on apply.*/
117 void LightApp_Preferences::onApply()
118 {
119   store();
120 }
121
122 /*!Emit preference changed.*/
123 void LightApp_Preferences::changedResources( const ResourceMap& map )
124 {
125   bool toRestart = false;
126   for ( ResourceMap::ConstIterator it = map.begin(); 
127         it != map.end(); ++it )
128   {
129     QString sec, param;
130     it.key()->resource( sec, param );
131     QString mod = module( it.key()->id() );
132     emit preferenceChanged( mod, sec, param );
133     toRestart = toRestart || it.key()->isRestartRequired();
134   }
135   if ( toRestart ) {
136     emit restartRequired();
137   }
138 }
139
140 /*!Gets module name by \a id, if exist.*/
141 QString LightApp_Preferences::module( const int id ) const
142 {
143   QString mod;
144   if ( myPrefMod.contains( id ) )
145     mod = myPrefMod[id];
146   return mod;
147 }