Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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   bool            isPixmapCached() const;
72   void            setIsPixmapCached( const bool );
73
74   void            clear();
75
76   void            setIgnoreUserValues( const bool = true );
77   bool            ignoreUserValues() const;
78
79   bool            value( const QString&, const QString&, int& ) const;
80   bool            value( const QString&, const QString&, double& ) const;
81   bool            value( const QString&, const QString&, bool& ) const;
82   bool            value( const QString&, const QString&, QColor& ) const;
83   bool            value( const QString&, const QString&, QFont& ) const;  
84   bool            value( const QString&, const QString&, QString&, const bool = true ) const;
85
86   int             integerValue( const QString&, const QString&, const int = 0 ) const;
87   double          doubleValue( const QString&, const QString&, const double = 0 ) const;
88   bool            booleanValue( const QString&, const QString&, const bool = false ) const;
89   QFont           fontValue( const QString&, const QString&, const QFont& = QFont() ) const;
90   QColor          colorValue( const QString&, const QString&, const QColor& = QColor() ) const;
91   QString         stringValue( const QString&, const QString&, const QString& = QString::null ) const;
92
93   bool            hasSection( const QString& ) const;
94   bool            hasValue( const QString&, const QString& ) const;
95
96   void            setValue( const QString&, const QString&, const int );
97   void            setValue( const QString&, const QString&, const double );
98   void            setValue( const QString&, const QString&, const bool );
99   void            setValue( const QString&, const QString&, const QFont& );
100   void            setValue( const QString&, const QString&, const QColor& );
101   void            setValue( const QString&, const QString&, const QString& );
102
103   void            remove( const QString& );
104   void            remove( const QString&, const QString& );
105
106   QString         currentFormat() const;
107   void            setCurrentFormat( const QString& );
108
109   Format*         format( const QString& ) const;
110   void            installFormat( Format* );
111   void            removeFormat( Format* );
112
113   QStringList     options() const;
114   QString         option( const QString& ) const;
115   void            setOption( const QString&, const QString& );
116
117   QPixmap         defaultPixmap() const;
118   virtual void    setDefaultPixmap( const QPixmap& );
119
120   QString         resSection() const;
121   QString         langSection() const;
122
123   QPixmap         loadPixmap( const QString&, const QString& ) const;
124   QPixmap         loadPixmap( const QString&, const QString&, const bool ) const;
125   QPixmap         loadPixmap( const QString&, const QString&, const QPixmap& ) const;
126   void            loadLanguage( const QString& = QString::null, const QString& = QString::null );
127
128   void            raiseTranslators( const QString& );
129   void            removeTranslators( const QString& );
130   void            loadTranslator( const QString&, const QString& );
131   void            loadTranslators( const QString&, const QStringList& );
132
133   QString         path( const QString&, const QString&, const QString& ) const;
134
135   bool            load();
136   bool            import( const QString& );
137   bool            save();
138
139   QStringList     sections() const;
140   QStringList     parameters( const QString& ) const;
141
142   void            refresh();
143
144 protected:
145   virtual void    setDirList( const QStringList& );
146   virtual void    setResource( const QString&, const QString&, const QString& );
147
148   virtual QString userFileName( const QString&, const bool = true ) const;
149   virtual QString globalFileName( const QString& ) const;
150
151 private:
152   void            initialize( const bool = true ) const;
153   QString         substMacro( const QString&, const QMap<QChar, QString>& ) const;
154
155 private:
156   typedef QPtrList<Resources>           ResList;
157   typedef QPtrList<Format>              FormatList;
158   typedef QMap<QString, QString>        OptionsMap;
159   typedef QPtrListIterator<Resources>   ResListIterator;
160   typedef QPtrListIterator<Format>      FormatListIterator;
161
162   typedef QPtrList<QTranslator>         TransList;
163   typedef QMap<QString, TransList>      TransListMap;
164   typedef QPtrListIterator<QTranslator> TransListIterator;
165
166 private:
167   QString         myAppName;
168   QStringList     myDirList;
169   FormatList      myFormats;
170   OptionsMap      myOptions;
171   ResList         myResources;
172   bool            myCheckExist;
173   TransListMap    myTranslator;
174   QPixmap         myDefaultPix;
175   bool            myIsPixmapCached;
176
177   bool            myIsIgnoreUserValues;
178 };
179
180 /*!
181   Class: QtxResourceMgr::Format
182 */
183
184 class QTX_EXPORT QtxResourceMgr::Format
185 {
186 public:
187   Format( const QString& );
188   ~Format();
189
190   QString                format() const;
191
192   QStringList            options() const;
193   QString                option( const QString& ) const;
194   void                   setOption( const QString&, const QString& );
195
196   bool                   load( Resources* );
197   bool                   save( Resources* );
198
199 protected:
200   virtual bool           load( const QString&, QMap<QString, Section>& ) = 0;
201   virtual bool           save( const QString&, const QMap<QString, Section>& ) = 0;
202
203 private:
204   QString                myFmt;
205   QMap<QString, QString> myOpt;
206 };
207
208 /*!
209   Class: QtxResourceMgr::Resources
210 */
211
212 class QtxResourceMgr::Resources
213 {
214 public:
215   Resources( const QtxResourceMgr*, const QString& );
216   virtual ~Resources();
217
218   QString                file() const;
219   void                   setFile( const QString& );
220
221   QString                value( const QString&, const QString&, const bool ) const;
222   void                   setValue( const QString&, const QString&, const QString& );
223
224   bool                   hasSection( const QString& ) const;
225   bool                   hasValue( const QString&, const QString& ) const;
226
227   void                   removeSection( const QString& );
228   void                   removeValue( const QString&, const QString& );
229
230   QPixmap                loadPixmap( const QString&, const QString&, const QString& ) const;
231   QTranslator*           loadTranslator( const QString&, const QString&, const QString& ) const;
232
233   QString                environmentVariable( const QString&, int&, int& ) const;
234   QString                makeSubstitution( const QString&, const QString&, const QString& ) const;
235
236   void                   clear();
237
238   QStringList            sections() const;
239   QStringList            parameters( const QString& ) const;
240
241   QString                path( const QString&, const QString&, const QString& ) const;
242
243 protected:
244   QtxResourceMgr*        resMgr() const;
245
246 private:
247   Section&               section( const QString& );
248   const Section&         section( const QString& ) const;
249
250   QString                fileName( const QString&, const QString&, const QString& ) const;
251
252 private:
253   typedef QMap<QString, Section> SectionMap;
254
255 private:
256   SectionMap             mySections;
257   QString                myFileName;
258   QMap<QString,QPixmap>  myPixmapCache;
259   QtxResourceMgr*        myMgr;
260
261   friend class QtxResourceMgr::Format;
262 };
263
264 /*!
265   Class: QtxResourceMgr::IMapIterator
266 */
267
268 template <class Key, class Value> class QtxResourceMgr::IMapIterator
269 {
270 public:
271   IMapIterator()                           : myMap( 0 ), myIndex( 0 )                                   { init(); }
272   IMapIterator( const IMap<Key,Value>* m ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( 0 ) { init(); }
273   IMapIterator( const IMapIterator& i )    : myMap( i.myMap ), myIndex( i.myIndex )                     { init(); }
274
275   bool operator==( const IMapIterator& i ) { return !operator!=( i );                                   }
276   bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
277   
278   operator bool() const { return myIndex >= 0; }
279
280   const Key&   key() const  { return myMap->key( myIndex );   }
281   Value&       data()       { return myMap->value( myIndex ); }
282   const Value& data() const { return myMap->value( myIndex ); }
283
284   Value& operator*() { return data(); }
285
286   IMapIterator& operator++()      { myIndex++; init(); return *this;                     }
287   IMapIterator  operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; }
288   IMapIterator& operator--()      { myIndex--; init(); return *this;                     }
289   IMapIterator  operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; }
290
291 private:
292   IMapIterator( const IMap<Key,Value>* m, const int index ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
293   void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
294
295 private:
296   IMap<Key,Value>* myMap;
297   int              myIndex;
298
299   friend class IMap<Key, Value>;
300   friend class IMapConstIterator<Key, Value>;
301 };
302
303 /*!
304   Class: QtxResourceMgr::IMapConstIterator
305 */
306
307 template <class Key, class Value> class QtxResourceMgr::IMapConstIterator
308 {
309 public:
310   IMapConstIterator()                                    : myMap( 0 ), myIndex( 0 )                                    { init(); }
311   IMapConstIterator( const IMap<Key,Value>* m )          : myMap( const_cast< IMap<Key,Value>* >( m )  ), myIndex( 0 ) { init(); }
312   IMapConstIterator( const IMapConstIterator& i )        : myMap( i.myMap ), myIndex( i.myIndex )                      { init(); }
313   IMapConstIterator( const IMapIterator<Key, Value>& i ) : myMap( i.myMap ), myIndex( i.myIndex )                      { init(); }
314   
315   bool operator==( const IMapConstIterator& i ) { return !operator!=( i );                                   }
316   bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
317   
318   operator bool() const { return myIndex >= 0; }
319   
320   const Key&   key() const  { return myMap->key( myIndex );   }
321   const Value& data() const { return myMap->value( myIndex ); }
322   
323   const Value& operator*() const { return data(); }
324   
325   IMapConstIterator& operator++()      { myIndex++; init(); return *this;                          }
326   IMapConstIterator  operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; }
327   IMapConstIterator& operator--()      { myIndex--; init(); return *this;                          }
328   IMapConstIterator  operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; }
329   
330 private:
331   IMapConstIterator( const IMap<Key,Value>* m, const int index ): myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
332   void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
333   
334 private:
335   IMap<Key,Value>* myMap;
336   int              myIndex;
337   
338   friend class IMap<Key,Value>;
339 };
340
341 /*!
342   Class: QtxResourceMgr::IMap
343 */
344
345 template <class Key, class Value> class QtxResourceMgr::IMap
346 {
347 public:
348   typedef IMapIterator<Key,Value>      Iterator;
349   typedef IMapConstIterator<Key,Value> ConstIterator;
350
351 public:
352   IMap() {}
353   IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {}
354   IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; }
355   
356   int  count() const   { return myData.count(); }
357   int  size() const    { return myData.count(); }
358   bool empty() const   { return myData.empty(); }
359   bool isEmpty() const { return myData.empty(); }
360   
361   void clear() { myKeys.clear(); myData.clear(); }
362   
363   QValueList<Key>   keys()   const { return myKeys; }
364   QValueList<Value> values() const { QValueList<Value> l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; }
365   bool              contains ( const Key& key ) const { return myData.contains( key ); }
366   
367   Iterator      begin()       { return Iterator( this );               }
368   Iterator      end()         { return Iterator( this, count() );      }
369   ConstIterator begin() const { return ConstIterator( this );          }
370   ConstIterator end() const   { return ConstIterator( this, count() ); }
371   
372   Iterator insert( const Key& key, const Value& value, bool overwrite = true )
373   { 
374     if ( myData.find( key ) == myData.end() || overwrite )
375     {
376       if ( myData.find( key ) != myData.end() && overwrite )
377         myKeys.remove( myKeys.find( key ) );
378       myKeys.append( key );
379       myData[key] = value;
380     }
381     return Iterator( this, index( key ) );
382   }
383
384   Iterator replace( const Key& key, const Value& value )
385   { 
386     if ( myData.find( key ) == myData.end() )
387       myKeys.append( key );
388     myData[ key ] = value;
389     return Iterator( this, index( key ) );
390   }
391
392   int           index( const Key& key ) const { return myKeys.findIndex( key );      }
393   Iterator      at( const int index )         { return Iterator( this, index );      }
394   ConstIterator at( const int index ) const   { return ConstIterator( this, index ); }
395
396   Key& key( const int index )
397   {
398     if ( index < 0 || index >= (int)myKeys.count() ) 
399       return dummyKey;
400     return myKeys[index];
401   }
402
403   Value& value( const int index )
404   {
405     if ( index < 0 || index >= (int)myKeys.count() ) 
406       return dummyValue;
407     return myData[ myKeys[index] ];
408   }
409
410   Value& operator[]( const Key& key )
411   {
412     if ( myData.find( key ) == myData.end() )
413       insert( key, Value() );
414     return myData[ key ];
415   }
416
417   const Value& operator[]( const Key& key ) const
418   {
419     if ( myData.find( key ) == myData.end() )
420       return dummyValue;
421     return myData[ key ];
422   }
423
424   void erase( Iterator it )     { remove( it );    }
425   void erase( const Key& key )  { remove( key );   }
426   void erase( const int index ) { remove( index ); }
427   void remove( Iterator it )    { if ( it.myMap != this ) return; remove( it.myIndex ); }
428   void remove( const Key& key ) { remove( index( key ) ); }
429   void remove( const int index )
430   {
431     if ( index >= 0 && index < (int)myKeys.count() )
432     {
433       myData.remove( myKeys[ index ] );
434       myKeys.remove( myKeys.at( index ) );
435     }
436   }
437
438 private:
439   QValueList<Key> myKeys;
440   QMap<Key,Value> myData;
441   Key             dummyKey;
442   Value           dummyValue;
443
444   friend class IMapIterator<Key,Value>;
445   friend class IMapConstIterator<Key,Value>;
446 };
447
448 #endif