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 );
71 bool isPixmapCached() const;
72 void setIsPixmapCached( const bool );
76 void setIgnoreUserValues( const bool = true );
77 bool ignoreUserValues() const;
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;
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;
93 bool hasSection( const QString& ) const;
94 bool hasValue( const QString&, const QString& ) const;
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& );
103 void remove( const QString& );
104 void remove( const QString&, const QString& );
106 QString currentFormat() const;
107 void setCurrentFormat( const QString& );
109 Format* format( const QString& ) const;
110 void installFormat( Format* );
111 void removeFormat( Format* );
113 QStringList options() const;
114 QString option( const QString& ) const;
115 void setOption( const QString&, const QString& );
117 QPixmap defaultPixmap() const;
118 virtual void setDefaultPixmap( const QPixmap& );
120 QString resSection() const;
121 QString langSection() const;
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 );
128 void raiseTranslators( const QString& );
129 void removeTranslators( const QString& );
130 void loadTranslator( const QString&, const QString& );
131 void loadTranslators( const QString&, const QStringList& );
133 QString path( const QString&, const QString&, const QString& ) const;
136 bool import( const QString& );
139 QStringList sections() const;
140 QStringList parameters( const QString& ) const;
145 virtual void setDirList( const QStringList& );
146 virtual void setResource( const QString&, const QString&, const QString& );
148 virtual QString userFileName( const QString&, const bool = true ) const;
149 virtual QString globalFileName( const QString& ) const;
152 void initialize( const bool = true ) const;
153 QString substMacro( const QString&, const QMap<QChar, QString>& ) const;
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;
162 typedef QPtrList<QTranslator> TransList;
163 typedef QMap<QString, TransList> TransListMap;
164 typedef QPtrListIterator<QTranslator> TransListIterator;
168 QStringList myDirList;
169 FormatList myFormats;
170 OptionsMap myOptions;
173 TransListMap myTranslator;
174 QPixmap myDefaultPix;
175 bool myIsPixmapCached;
177 bool myIsIgnoreUserValues;
181 Class: QtxResourceMgr::Format
184 class QTX_EXPORT QtxResourceMgr::Format
187 Format( const QString& );
190 QString format() const;
192 QStringList options() const;
193 QString option( const QString& ) const;
194 void setOption( const QString&, const QString& );
196 bool load( Resources* );
197 bool save( Resources* );
200 virtual bool load( const QString&, QMap<QString, Section>& ) = 0;
201 virtual bool save( const QString&, const QMap<QString, Section>& ) = 0;
205 QMap<QString, QString> myOpt;
209 Class: QtxResourceMgr::Resources
212 class QtxResourceMgr::Resources
215 Resources( const QtxResourceMgr*, const QString& );
216 virtual ~Resources();
218 QString file() const;
219 void setFile( const QString& );
221 QString value( const QString&, const QString&, const bool ) const;
222 void setValue( const QString&, const QString&, const QString& );
224 bool hasSection( const QString& ) const;
225 bool hasValue( const QString&, const QString& ) const;
227 void removeSection( const QString& );
228 void removeValue( const QString&, const QString& );
230 QPixmap loadPixmap( const QString&, const QString&, const QString& ) const;
231 QTranslator* loadTranslator( const QString&, const QString&, const QString& ) const;
233 QString environmentVariable( const QString&, int&, int& ) const;
234 QString makeSubstitution( const QString&, const QString&, const QString& ) const;
238 QStringList sections() const;
239 QStringList parameters( const QString& ) const;
241 QString path( const QString&, const QString&, const QString& ) const;
244 QtxResourceMgr* resMgr() const;
247 Section& section( const QString& );
248 const Section& section( const QString& ) const;
250 QString fileName( const QString&, const QString&, const QString& ) const;
253 typedef QMap<QString, Section> SectionMap;
256 SectionMap mySections;
258 QMap<QString,QPixmap> myPixmapCache;
259 QtxResourceMgr* myMgr;
261 friend class QtxResourceMgr::Format;
265 Class: QtxResourceMgr::IMapIterator
268 template <class Key, class Value> class QtxResourceMgr::IMapIterator
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(); }
275 bool operator==( const IMapIterator& i ) { return !operator!=( i ); }
276 bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
278 operator bool() const { return myIndex >= 0; }
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 ); }
284 Value& operator*() { return data(); }
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; }
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; }
296 IMap<Key,Value>* myMap;
299 friend class IMap<Key, Value>;
300 friend class IMapConstIterator<Key, Value>;
304 Class: QtxResourceMgr::IMapConstIterator
307 template <class Key, class Value> class QtxResourceMgr::IMapConstIterator
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(); }
315 bool operator==( const IMapConstIterator& i ) { return !operator!=( i ); }
316 bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
318 operator bool() const { return myIndex >= 0; }
320 const Key& key() const { return myMap->key( myIndex ); }
321 const Value& data() const { return myMap->value( myIndex ); }
323 const Value& operator*() const { return data(); }
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; }
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; }
335 IMap<Key,Value>* myMap;
338 friend class IMap<Key,Value>;
342 Class: QtxResourceMgr::IMap
345 template <class Key, class Value> class QtxResourceMgr::IMap
348 typedef IMapIterator<Key,Value> Iterator;
349 typedef IMapConstIterator<Key,Value> ConstIterator;
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; }
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(); }
361 void clear() { myKeys.clear(); myData.clear(); }
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 ); }
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() ); }
372 Iterator insert( const Key& key, const Value& value, bool overwrite = true )
374 if ( myData.find( key ) == myData.end() || overwrite )
376 if ( myData.find( key ) != myData.end() && overwrite )
377 myKeys.remove( myKeys.find( key ) );
378 myKeys.append( key );
381 return Iterator( this, index( key ) );
384 Iterator replace( const Key& key, const Value& value )
386 if ( myData.find( key ) == myData.end() )
387 myKeys.append( key );
388 myData[ key ] = value;
389 return Iterator( this, index( key ) );
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 ); }
396 Key& key( const int index )
398 if ( index < 0 || index >= (int)myKeys.count() )
400 return myKeys[index];
403 Value& value( const int index )
405 if ( index < 0 || index >= (int)myKeys.count() )
407 return myData[ myKeys[index] ];
410 Value& operator[]( const Key& key )
412 if ( myData.find( key ) == myData.end() )
413 insert( key, Value() );
414 return myData[ key ];
417 const Value& operator[]( const Key& key ) const
419 if ( myData.find( key ) == myData.end() )
421 return myData[ key ];
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 )
431 if ( index >= 0 && index < (int)myKeys.count() )
433 myData.remove( myKeys[ index ] );
434 myKeys.remove( myKeys.at( index ) );
439 QValueList<Key> myKeys;
440 QMap<Key,Value> myData;
444 friend class IMapIterator<Key,Value>;
445 friend class IMapConstIterator<Key,Value>;