Salome HOME
NRI : Merge from V1_2.
[modules/kernel.git] / src / SALOMEGUI / QAD_Settings.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_Settings.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "QAD_Settings.h"
31
32 #include <stdio.h>
33
34 // QT Includes
35 #include <qfile.h>
36 #include <qregexp.h>
37 #include <qstring.h>
38 #include <qtextstream.h>
39
40 /*!
41     Class QAD_Settings is a base class for different kind of settings
42     (identifying strings and values). 
43 */
44 QAD_Settings::QAD_Settings(QString _sectionName,
45                            QChar _nameStarter, QChar _nameStopper,
46                            QChar _valueStarter, QChar _valueStopper)
47 {
48   sectionName  = _sectionName;
49   nameStarter  = _nameStarter;
50   nameStopper  = _nameStopper;
51   valueStarter = _valueStarter;
52   valueStopper = _valueStopper;
53   settingList.setAutoDelete(true);
54 }
55
56 /*!
57     Constructor.
58 */
59 QAD_Settings::QAD_Settings(QString _sectionName,
60                            QChar _nameStarter,
61                            QChar _separator,
62                            QChar _valueStopper)
63 {
64   sectionName  = _sectionName;
65   nameStarter  = _nameStarter;
66   nameStopper  = _separator;
67   valueStopper = _valueStopper;
68   valueStarter=0;
69   settingList.setAutoDelete(true);
70 }
71 /*!
72     Constructor.
73 */
74 QAD_Settings::QAD_Settings(QChar _nameStopper, QChar _valueStarter,
75                            QChar _valueStopper)
76 {
77   sectionName = "";
78   nameStarter = 0;
79   nameStopper  = _nameStopper;
80   valueStarter = _valueStarter;
81   valueStopper = _valueStopper;
82   settingList.setAutoDelete(true);
83 }
84
85
86 /*!
87     Destructor.
88 */
89 QAD_Settings::~QAD_Settings()
90 {
91 }
92
93
94 /*!
95     Clears all settings.
96 */
97 void QAD_Settings::clear()
98 {
99   settingList.clear();
100 }
101
102
103 /*!
104     Adds a setting. If a setting with the given name is already defined it
105     gets overwritten.
106 */
107 void QAD_Settings::addSetting(QString _name, QString _value, bool _autoValue)
108 {
109   // Remove all settings with the same name:
110   removeSettings(_name.simplifyWhiteSpace());
111
112   // Add new setting:
113   QAD_Setting* newSetting = new QAD_Setting(_name.simplifyWhiteSpace(),
114                                             _value.simplifyWhiteSpace(),
115                                             _autoValue);
116   settingList.append(newSetting);
117 }
118
119
120 /*!
121     Adds an int setting. If a setting with the given name is already defined it
122     gets overwritten.
123 */
124 void QAD_Settings::addSetting(QString _name, int _value, bool _autoValue)
125 {
126   QString strVal;
127   strVal.setNum(_value);
128
129   addSetting(_name, strVal, _autoValue);
130 }
131
132 /*!
133     Adds a double setting. If a setting with the given name is already defined it
134     gets overwritten.
135 */
136 void QAD_Settings::addSetting(QString _name, double _value, bool _autoValue)
137 {
138   QString strVal;
139   strVal.setNum(_value);
140
141   addSetting(_name, strVal, _autoValue);
142 }
143
144
145 /*!
146     Removes all setting with the given name (should be unique anyway).
147 */
148 bool QAD_Settings::removeSettings(QString name)
149 {
150   QAD_Setting* s;
151   bool ret=false;
152
153   for(s=settingList.first(); s!=0;) {
154     if(s->getName()==name) {
155       settingList.remove(s);
156       s=settingList.current();
157       ret=true;
158     }
159     else {
160       s=settingList.next();
161     }
162   }
163
164   return ret;
165 }
166
167
168 /*!
169     Removes all setting which are not automatically generated.
170 */
171 void QAD_Settings::removeNonAutoSettings()
172 {
173   QAD_Setting* s;
174
175   for(s=settingList.first(); s!=0;) {
176     if(!s->isAutoValue()) {
177       settingList.remove(s);
178       s=settingList.current();
179     }
180     else {
181       s=settingList.next();
182     }
183   }
184 }
185
186
187 /*!
188    Gets the setting value of the given setting.
189 */
190 QString QAD_Settings::getSetting(QString name)
191 {
192   QAD_Setting* s;
193   
194   for(s=settingList.first(); s!=0; s=settingList.next()) {
195     if(s->getName()==name) return s->getValue();
196   }
197
198   return "";
199 }
200
201 /*!
202     Replaces variables in the given string by their values.
203 */
204 QString QAD_Settings::replace(QString str)
205 {
206   if(!str.contains('$')) return str;
207
208   QAD_Setting* t;
209   QString result=str.copy();
210   int i;
211   bool found;
212
213   // Replace until we have no occurences of variables or recursive counter>16:
214   do {
215     found=false;
216     for(t=settingList.first(); t!=0; t=settingList.next()) {
217       if(result.contains('$')) {
218         i=0;
219         do {
220           i = result.find(QRegExp("\\$" + t->getName() + "[^a-zA-z0-9_]"), i);
221           if(i!=-1) {
222             result.replace(i, (int)t->getName().length()+1, t->getValue());
223             found=true;
224           }
225         }while(i!=-1);
226       }
227       else goto end;
228     }
229   } while(found);
230
231   end:
232
233   return result;
234 }
235
236 /*!
237   Returns true if list contains given setting
238 */
239 bool QAD_Settings::hasSetting(QString name)
240 {
241   QAD_Setting* s;
242   
243   for(s=settingList.first(); s!=0; s=settingList.next()) {
244     if(s->getName()==name) return true;
245   }
246   return false;
247 }
248
249 // EOF
250