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