]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxResourceMgr.h
Salome HOME
Bug fix for SSPP10924
[modules/gui.git] / src / Qtx / QtxResourceMgr.h
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 #ifndef QTX_RESOURCEMGR_H
20 #define QTX_RESOURCEMGR_H
21
22 #include "Qtx.h"
23
24 #include <qmap.h>
25 #include <qcolor.h>
26 #include <qfont.h>
27 #include <qpixmap.h>
28 #include <qstringlist.h>
29 #include <qtranslator.h>
30 #include <qvaluelist.h>
31
32 class QPixmap;
33
34 #ifdef WIN32
35 #pragma warning( disable:4251 )
36 #endif
37
38 /*!
39   Class: QtxResourceMgr
40 */
41
42 class QTX_EXPORT QtxResourceMgr
43 {
44   class IniFormat;
45   class XmlFormat;
46   class Resources;
47
48 public:
49   class Format;
50
51   template <class Key, class Value> class IMap;
52   template <class Key, class Value> class IMapIterator;
53   template <class Key, class Value> class IMapConstIterator;
54
55 #ifdef QTX_NO_INDEXED_MAP
56   typedef QMap<QString, QString> Section;
57 #else
58   typedef IMap<QString, QString> Section;
59 #endif
60
61 public:
62   QtxResourceMgr( const QString&, const QString& = QString::null );
63   virtual ~QtxResourceMgr();
64
65   QString         appName() const;
66   QStringList     dirList() const;
67
68   bool            checkExisting() const;
69   virtual void    setCheckExisting( const bool );
70
71   void            clear();
72
73   bool            value( const QString&, const QString&, int& ) const;
74   bool            value( const QString&, const QString&, double& ) const;
75   bool            value( const QString&, const QString&, bool& ) const;
76   bool            value( const QString&, const QString&, QColor& ) const;
77   bool            value( const QString&, const QString&, QFont& ) const;  
78   bool            value( const QString&, const QString&, QString&, const bool = true ) const;
79
80   int             integerValue( const QString&, const QString&, const int = 0 ) const;
81   double          doubleValue( const QString&, const QString&, const double = 0 ) const;
82   bool            booleanValue( const QString&, const QString&, const bool = false ) const;
83   QFont           fontValue( const QString&, const QString&, const QFont& = QFont() ) const;
84   QColor          colorValue( const QString&, const QString&, const QColor& = QColor() ) const;
85   QString         stringValue( const QString&, const QString&, const QString& = QString::null ) const;
86
87   bool            hasSection( const QString& ) const;
88   bool            hasValue( const QString&, const QString& ) const;
89
90   void            setValue( const QString&, const QString&, const int );
91   void            setValue( const QString&, const QString&, const double );
92   void            setValue( const QString&, const QString&, const bool );
93   void            setValue( const QString&, const QString&, const QFont& );
94   void            setValue( const QString&, const QString&, const QColor& );
95   void            setValue( const QString&, const QString&, const QString& );
96
97   void            remove( const QString& );
98   void            remove( const QString&, const QString& );
99
100   QString         currentFormat() const;
101   void            setCurrentFormat( const QString& );
102
103   Format*         format( const QString& ) const;
104   void            installFormat( Format* );
105   void            removeFormat( Format* );
106
107   QStringList     options() const;
108   QString         option( const QString& ) const;
109   void            setOption( const QString&, const QString& );
110
111   QPixmap         defaultPixmap() const;
112   virtual void    setDefaultPixmap( const QPixmap& );
113
114   QString         resSection() const;
115   QString         langSection() const;
116
117   QPixmap         loadPixmap( const QString&, const QString& ) const;
118   QPixmap         loadPixmap( const QString&, const QString&, const bool ) const;
119   QPixmap         loadPixmap( const QString&, const QString&, const QPixmap& ) const;
120   void            loadLanguage( const QString& = QString::null, const QString& = QString::null );
121
122   void            raiseTranslators( const QString& );
123   void            removeTranslators( const QString& );
124   void            loadTranslator( const QString&, const QString& );
125   void            loadTranslators( const QString&, const QStringList& );
126
127   QString         path( const QString&, const QString&, const QString& ) const;
128
129   bool            load();
130   bool            save();
131
132   QStringList     sections() const;
133   QStringList     parameters( const QString& ) const;
134
135   void            refresh();
136
137 protected:
138   virtual void    setDirList( const QStringList& );
139   virtual void    setResource( const QString&, const QString&, const QString& );
140
141   virtual QString userFileName( const QString& ) const;
142   virtual QString globalFileName( const QString& ) const;
143
144 private:
145   void            initialize( const bool = true ) const;
146   QString         substMacro( const QString&, const QMap<QChar, QString>& ) const;
147
148 private:
149   typedef QPtrList<Resources>           ResList;
150   typedef QPtrList<Format>              FormatList;
151   typedef QMap<QString, QString>        OptionsMap;
152   typedef QPtrListIterator<Resources>   ResListIterator;
153   typedef QPtrListIterator<Format>      FormatListIterator;
154
155   typedef QPtrList<QTranslator>         TransList;
156   typedef QMap<QString, TransList>      TransListMap;
157   typedef QPtrListIterator<QTranslator> TransListIterator;
158
159 private:
160   QString         myAppName;
161   QStringList     myDirList;
162   FormatList      myFormats;
163   OptionsMap      myOptions;
164   ResList         myResources;
165   bool            myCheckExist;
166   TransListMap    myTranslator;
167   QPixmap         myDefaultPix;
168 };
169
170 /*!
171   Class: QtxResourceMgr::Format
172 */
173
174 class QTX_EXPORT QtxResourceMgr::Format
175 {
176 public:
177   Format( const QString& );
178   ~Format();
179
180   QString                format() const;
181
182   QStringList            options() const;
183   QString                option( const QString& ) const;
184   void                   setOption( const QString&, const QString& );
185
186   bool                   load( Resources* );
187   bool                   save( Resources* );
188
189 protected:
190   virtual bool           load( const QString&, QMap<QString, Section>& ) = 0;
191   virtual bool           save( const QString&, const QMap<QString, Section>& ) = 0;
192
193 private:
194   QString                myFmt;
195   QMap<QString, QString> myOpt;
196 };
197
198 /*!
199   Class: QtxResourceMgr::Resources
200 */
201
202 class QtxResourceMgr::Resources
203 {
204 public:
205   Resources( const QString& );
206   virtual ~Resources();
207
208   QString                file() const;
209   void                   setFile( const QString& );
210
211   QString                value( const QString&, const QString&, const bool ) const;
212   void                   setValue( const QString&, const QString&, const QString& );
213
214   bool                   hasSection( const QString& ) const;
215   bool                   hasValue( const QString&, const QString& ) const;
216
217   void                   removeSection( const QString& );
218   void                   removeValue( const QString&, const QString& );
219
220   QPixmap                loadPixmap( const QString&, const QString&, const QString& ) const;
221   QTranslator*           loadTranslator( const QString&, const QString&, const QString& ) const;
222
223   QString                environmentVariable( const QString&, int&, int& ) const;
224   QString                makeSubstitution( const QString&, const QString&, const QString& ) const;
225
226   void                   clear();
227
228   QStringList            sections() const;
229   QStringList            parameters( const QString& ) const;
230
231   QString                path( const QString&, const QString&, const QString& ) const;
232
233 private:
234   Section&               section( const QString& );
235   const Section&         section( const QString& ) const;
236
237   QString                fileName( const QString&, const QString&, const QString& ) const;
238
239 private:
240   typedef QMap<QString, Section> SectionMap;
241
242 private:
243   SectionMap             mySections;
244   QString                myFileName;
245
246   friend class QtxResourceMgr::Format;
247 };
248
249 /*!
250   Class: QtxResourceMgr::IMapIterator
251 */
252
253 template <class Key, class Value> class QtxResourceMgr::IMapIterator
254 {
255 public:
256   IMapIterator()                           : myMap( 0 ), myIndex( 0 )                                   { init(); }
257   IMapIterator( const IMap<Key,Value>* m ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( 0 ) { init(); }
258   IMapIterator( const IMapIterator& i )    : myMap( i.myMap ), myIndex( i.myIndex )                     { init(); }
259
260   bool operator==( const IMapIterator& i ) { return !operator!=( i );                                   }
261   bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
262   
263   operator bool() const { return myIndex >= 0; }
264
265   const Key&   key() const  { return myMap->key( myIndex );   }
266   Value&       data()       { return myMap->value( myIndex ); }
267   const Value& data() const { return myMap->value( myIndex ); }
268
269   Value& operator*() { return data(); }
270
271   IMapIterator& operator++()      { myIndex++; init(); return *this;                     }
272   IMapIterator  operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; }
273   IMapIterator& operator--()      { myIndex--; init(); return *this;                     }
274   IMapIterator  operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; }
275
276 private:
277   IMapIterator( const IMap<Key,Value>* m, const int index ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
278   void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
279
280 private:
281   IMap<Key,Value>* myMap;
282   int              myIndex;
283
284   friend class IMap<Key, Value>;
285   friend class IMapConstIterator<Key, Value>;
286 };
287
288 /*!
289   Class: QtxResourceMgr::IMapConstIterator
290 */
291
292 template <class Key, class Value> class QtxResourceMgr::IMapConstIterator
293 {
294 public:
295   IMapConstIterator()                                    : myMap( 0 ), myIndex( 0 )                                    { init(); }
296   IMapConstIterator( const IMap<Key,Value>* m )          : myMap( const_cast< IMap<Key,Value>* >( m )  ), myIndex( 0 ) { init(); }
297   IMapConstIterator( const IMapConstIterator& i )        : myMap( i.myMap ), myIndex( i.myIndex )                      { init(); }
298   IMapConstIterator( const IMapIterator<Key, Value>& i ) : myMap( i.myMap ), myIndex( i.myIndex )                      { init(); }
299   
300   bool operator==( const IMapConstIterator& i ) { return !operator!=( i );                                   }
301   bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
302   
303   operator bool() const { return myIndex >= 0; }
304   
305   const Key&   key() const  { return myMap->key( myIndex );   }
306   const Value& data() const { return myMap->value( myIndex ); }
307   
308   const Value& operator*() const { return data(); }
309   
310   IMapConstIterator& operator++()      { myIndex++; init(); return *this;                          }
311   IMapConstIterator  operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; }
312   IMapConstIterator& operator--()      { myIndex--; init(); return *this;                          }
313   IMapConstIterator  operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; }
314   
315 private:
316   IMapConstIterator( const IMap<Key,Value>* m, const int index ): myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
317   void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
318   
319 private:
320   IMap<Key,Value>* myMap;
321   int              myIndex;
322   
323   friend class IMap<Key,Value>;
324 };
325
326 /*!
327   Class: QtxResourceMgr::IMap
328 */
329
330 template <class Key, class Value> class QtxResourceMgr::IMap
331 {
332 public:
333   typedef IMapIterator<Key,Value>      Iterator;
334   typedef IMapConstIterator<Key,Value> ConstIterator;
335
336 public:
337   IMap() {}
338   IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {}
339   IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; }
340   
341   int  count() const   { return myData.count(); }
342   int  size() const    { return myData.count(); }
343   bool empty() const   { return myData.empty(); }
344   bool isEmpty() const { return myData.empty(); }
345   
346   void clear() { myKeys.clear(); myData.clear(); }
347   
348   QValueList<Key>   keys()   const { return myKeys; }
349   QValueList<Value> values() const { QValueList<Value> l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; }
350   bool              contains ( const Key& key ) const { return myData.contains( key ); }
351   
352   Iterator      begin()       { return Iterator( this );               }
353   Iterator      end()         { return Iterator( this, count() );      }
354   ConstIterator begin() const { return ConstIterator( this );          }
355   ConstIterator end() const   { return ConstIterator( this, count() ); }
356   
357   Iterator insert( const Key& key, const Value& value, bool overwrite = true )
358   { 
359     if ( myData.find( key ) == myData.end() || overwrite )
360     {
361       if ( myData.find( key ) != myData.end() && overwrite )
362         myKeys.remove( myKeys.find( key ) );
363       myKeys.append( key );
364       myData[key] = value;
365     }
366     return Iterator( this, index( key ) );
367   }
368
369   Iterator replace( const Key& key, const Value& value )
370   { 
371     if ( myData.find( key ) == myData.end() )
372       myKeys.append( key );
373     myData[ key ] = value;
374     return Iterator( this, index( key ) );
375   }
376
377   int           index( const Key& key ) const { return myKeys.findIndex( key );      }
378   Iterator      at( const int index )         { return Iterator( this, index );      }
379   ConstIterator at( const int index ) const   { return ConstIterator( this, index ); }
380
381   Key& key( const int index )
382   {
383     if ( index < 0 || index >= (int)myKeys.count() ) 
384       return dummyKey;
385     return myKeys[index];
386   }
387
388   Value& value( const int index )
389   {
390     if ( index < 0 || index >= (int)myKeys.count() ) 
391       return dummyValue;
392     return myData[ myKeys[index] ];
393   }
394
395   Value& operator[]( const Key& key )
396   {
397     if ( myData.find( key ) == myData.end() )
398       insert( key, Value() );
399     return myData[ key ];
400   }
401
402   const Value& operator[]( const Key& key ) const
403   {
404     if ( myData.find( key ) == myData.end() )
405       return dummyValue;
406     return myData[ key ];
407   }
408
409   void erase( Iterator it )     { remove( it );    }
410   void erase( const Key& key )  { remove( key );   }
411   void erase( const int index ) { remove( index ); }
412   void remove( Iterator it )    { if ( it.myMap != this ) return; remove( it.myIndex ); }
413   void remove( const Key& key ) { remove( index( key ) ); }
414   void remove( const int index )
415   {
416     if ( index >= 0 && index < (int)myKeys.count() )
417     {
418       myData.remove( myKeys[ index ] );
419       myKeys.remove( myKeys.at( index ) );
420     }
421   }
422
423 private:
424   QValueList<Key> myKeys;
425   QMap<Key,Value> myData;
426   Key             dummyKey;
427   Value           dummyValue;
428
429   friend class IMapIterator<Key,Value>;
430   friend class IMapConstIterator<Key,Value>;
431 };
432
433 #endif