1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 #ifndef QTX_RESOURCEMGR_H
20 #define QTX_RESOURCEMGR_H
28 #include <qstringlist.h>
29 #include <qtranslator.h>
30 #include <qvaluelist.h>
35 #pragma warning( disable:4251 )
42 class QTX_EXPORT QtxResourceMgr
51 template <class Key, class Value> class IMap;
52 template <class Key, class Value> class IMapIterator;
53 template <class Key, class Value> class IMapConstIterator;
55 #ifdef QTX_NO_INDEXED_MAP
56 typedef QMap<QString, QString> Section;
58 typedef IMap<QString, QString> Section;
62 QtxResourceMgr( const QString&, const QString& = QString::null );
63 virtual ~QtxResourceMgr();
65 QString appName() const;
66 QStringList dirList() const;
68 bool checkExisting() const;
69 virtual void setCheckExisting( const bool );
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;
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;
87 bool hasSection( const QString& ) const;
88 bool hasValue( const QString&, const QString& ) const;
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& );
97 void remove( const QString& );
98 void remove( const QString&, const QString& );
100 QString currentFormat() const;
101 void setCurrentFormat( const QString& );
103 Format* format( const QString& ) const;
104 void installFormat( Format* );
105 void removeFormat( Format* );
107 QStringList options() const;
108 QString option( const QString& ) const;
109 void setOption( const QString&, const QString& );
111 QPixmap defaultPixmap() const;
112 virtual void setDefaultPixmap( const QPixmap& );
114 QString resSection() const;
115 QString langSection() const;
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 );
122 void raiseTranslators( const QString& );
123 void removeTranslators( const QString& );
124 void loadTranslator( const QString&, const QString& );
125 void loadTranslators( const QString&, const QStringList& );
127 QString path( const QString&, const QString&, const QString& ) const;
132 QStringList sections() const;
133 QStringList parameters( const QString& ) const;
138 virtual void setDirList( const QStringList& );
139 virtual void setResource( const QString&, const QString&, const QString& );
141 virtual QString userFileName( const QString& ) const;
142 virtual QString globalFileName( const QString& ) const;
145 void initialize( const bool = true ) const;
146 QString substMacro( const QString&, const QMap<QChar, QString>& ) const;
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;
155 typedef QPtrList<QTranslator> TransList;
156 typedef QMap<QString, TransList> TransListMap;
157 typedef QPtrListIterator<QTranslator> TransListIterator;
161 QStringList myDirList;
162 FormatList myFormats;
163 OptionsMap myOptions;
166 TransListMap myTranslator;
167 QPixmap myDefaultPix;
171 Class: QtxResourceMgr::Format
174 class QTX_EXPORT QtxResourceMgr::Format
177 Format( const QString& );
180 QString format() const;
182 QStringList options() const;
183 QString option( const QString& ) const;
184 void setOption( const QString&, const QString& );
186 bool load( Resources* );
187 bool save( Resources* );
190 virtual bool load( const QString&, QMap<QString, Section>& ) = 0;
191 virtual bool save( const QString&, const QMap<QString, Section>& ) = 0;
195 QMap<QString, QString> myOpt;
199 Class: QtxResourceMgr::Resources
202 class QtxResourceMgr::Resources
205 Resources( const QString& );
206 virtual ~Resources();
208 QString file() const;
209 void setFile( const QString& );
211 QString value( const QString&, const QString&, const bool ) const;
212 void setValue( const QString&, const QString&, const QString& );
214 bool hasSection( const QString& ) const;
215 bool hasValue( const QString&, const QString& ) const;
217 void removeSection( const QString& );
218 void removeValue( const QString&, const QString& );
220 QPixmap loadPixmap( const QString&, const QString&, const QString& ) const;
221 QTranslator* loadTranslator( const QString&, const QString&, const QString& ) const;
223 QString environmentVariable( const QString&, int&, int& ) const;
224 QString makeSubstitution( const QString&, const QString&, const QString& ) const;
228 QStringList sections() const;
229 QStringList parameters( const QString& ) const;
231 QString path( const QString&, const QString&, const QString& ) const;
234 Section& section( const QString& );
235 const Section& section( const QString& ) const;
237 QString fileName( const QString&, const QString&, const QString& ) const;
240 typedef QMap<QString, Section> SectionMap;
243 SectionMap mySections;
246 friend class QtxResourceMgr::Format;
250 Class: QtxResourceMgr::IMapIterator
253 template <class Key, class Value> class QtxResourceMgr::IMapIterator
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(); }
260 bool operator==( const IMapIterator& i ) { return !operator!=( i ); }
261 bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
263 operator bool() const { return myIndex >= 0; }
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 ); }
269 Value& operator*() { return data(); }
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; }
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; }
281 IMap<Key,Value>* myMap;
284 friend class IMap<Key, Value>;
285 friend class IMapConstIterator<Key, Value>;
289 Class: QtxResourceMgr::IMapConstIterator
292 template <class Key, class Value> class QtxResourceMgr::IMapConstIterator
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(); }
300 bool operator==( const IMapConstIterator& i ) { return !operator!=( i ); }
301 bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
303 operator bool() const { return myIndex >= 0; }
305 const Key& key() const { return myMap->key( myIndex ); }
306 const Value& data() const { return myMap->value( myIndex ); }
308 const Value& operator*() const { return data(); }
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; }
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; }
320 IMap<Key,Value>* myMap;
323 friend class IMap<Key,Value>;
327 Class: QtxResourceMgr::IMap
330 template <class Key, class Value> class QtxResourceMgr::IMap
333 typedef IMapIterator<Key,Value> Iterator;
334 typedef IMapConstIterator<Key,Value> ConstIterator;
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; }
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(); }
346 void clear() { myKeys.clear(); myData.clear(); }
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 ); }
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() ); }
357 Iterator insert( const Key& key, const Value& value, bool overwrite = true )
359 if ( myData.find( key ) == myData.end() || overwrite )
361 if ( myData.find( key ) != myData.end() && overwrite )
362 myKeys.remove( myKeys.find( key ) );
363 myKeys.append( key );
366 return Iterator( this, index( key ) );
369 Iterator replace( const Key& key, const Value& value )
371 if ( myData.find( key ) == myData.end() )
372 myKeys.append( key );
373 myData[ key ] = value;
374 return Iterator( this, index( key ) );
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 ); }
381 Key& key( const int index )
383 if ( index < 0 || index >= (int)myKeys.count() )
385 return myKeys[index];
388 Value& value( const int index )
390 if ( index < 0 || index >= (int)myKeys.count() )
392 return myData[ myKeys[index] ];
395 Value& operator[]( const Key& key )
397 if ( myData.find( key ) == myData.end() )
398 insert( key, Value() );
399 return myData[ key ];
402 const Value& operator[]( const Key& key ) const
404 if ( myData.find( key ) == myData.end() )
406 return myData[ key ];
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 )
416 if ( index >= 0 && index < (int)myKeys.count() )
418 myData.remove( myKeys[ index ] );
419 myKeys.remove( myKeys.at( index ) );
424 QValueList<Key> myKeys;
425 QMap<Key,Value> myData;
429 friend class IMapIterator<Key,Value>;
430 friend class IMapConstIterator<Key,Value>;