From 796741791d3b7d4d645e9e0024d8c36bb1081521 Mon Sep 17 00:00:00 2001 From: vsr Date: Tue, 13 Sep 2005 10:05:26 +0000 Subject: [PATCH] Change order of resource files loading to allow customizing resources in SALOME-based applications --- src/Qtx/QtxResourceMgr.cxx | 66 ++++++++++---- src/Qtx/QtxResourceMgr.h | 172 +++++++++++++++++++++++++++++++++++++ 2 files changed, 219 insertions(+), 19 deletions(-) diff --git a/src/Qtx/QtxResourceMgr.cxx b/src/Qtx/QtxResourceMgr.cxx index 6d3cf15f1..75f1bd0fe 100644 --- a/src/Qtx/QtxResourceMgr.cxx +++ b/src/Qtx/QtxResourceMgr.cxx @@ -447,7 +447,7 @@ bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter ) + for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter ) { QDomElement val = doc.createElement( parameterTag() ); val.setAttribute( nameAttribute(), iter.key() ); @@ -698,7 +698,9 @@ void QtxResourceMgr::initialize( const bool autoLoad ) const QtxResourceMgr* that = (QtxResourceMgr*)this; - that->myResources.append( new Resources( userFileName( appName() ) ) ); + if ( !userFileName( appName() ).isEmpty() ) + that->myResources.append( new Resources( userFileName( appName() ) ) ); + for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it ) { QString path = Qtx::addSlash( *it ) + globalFileName( appName() ); @@ -1166,9 +1168,10 @@ void QtxResourceMgr::setCurrentFormat( const QString& fmt ) return; ResListIterator resIt( myResources ); - if ( resIt.current() ) + if ( myResources.count() > myDirList.count() && resIt.current() ) { resIt.current()->setFile( userFileName( appName() ) ); - ++resIt; + ++resIt; + } for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt ) resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) ); @@ -1305,16 +1308,22 @@ QStringList QtxResourceMgr::parameters( const QString& sec ) const { initialize(); - QMap map; - for ( ResListIterator it( myResources ); it.current(); ++it ) - { +#if defined(QTX_NO_INDEXED_MAP) + typedef QMap PMap; +#else + typedef IMap PMap; +#endif + PMap pmap; + ResListIterator it( myResources ); + it.toLast(); + for ( ; it.current(); --it ) { QStringList lst = it.current()->parameters( sec ); for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) - map.insert( *itr, 0 ); + pmap.insert( *itr, 0, false ); } QStringList res; - for ( QMap::ConstIterator iter = map.begin(); iter != map.end(); ++iter ) + for ( PMap::ConstIterator iter = pmap.begin(); iter != pmap.end(); ++iter ) res.append( iter.key() ); return res; @@ -1428,25 +1437,44 @@ void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l ) for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it ) trs.append( substMacro( *it, substMap ).stripWhiteSpace() ); - for ( QStringList::const_iterator itr = trs.begin(); itr != trs.end(); ++itr ) - loadTranslator( prefix, *itr ); + loadTranslators( prefix, trs ); } } -void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name ) +void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators ) { initialize(); QTranslator* trans = 0; - for ( ResListIterator it( myResources ); it.current() && !trans; ++it ) - trans = it.current()->loadTranslator( resSection(), prefix, name ); + ResListIterator it( myResources ); + it.toLast(); + for ( ; it.current(); --it ) { + for ( QStringList::const_iterator itr = translators.begin(); itr != translators.end(); ++itr ) { + trans = it.current()->loadTranslator( resSection(), prefix, *itr ); + if ( trans ) { + if ( !myTranslator[prefix].contains( trans ) ) + myTranslator[prefix].append( trans ); + qApp->installTranslator( trans ); + } + } + } +} - if ( !trans ) - return; +void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name ) +{ + initialize(); - if ( !myTranslator[prefix].contains( trans ) ) - myTranslator[prefix].append( trans ); - qApp->installTranslator( trans ); + QTranslator* trans = 0; + ResListIterator it( myResources ); + it.toLast(); + for ( ; it.current(); --it ) { + trans = it.current()->loadTranslator( resSection(), prefix, name ); + if ( trans ) { + if ( !myTranslator[prefix].contains( trans ) ) + myTranslator[prefix].append( trans ); + qApp->installTranslator( trans ); + } + } } void QtxResourceMgr::removeTranslators( const QString& prefix ) diff --git a/src/Qtx/QtxResourceMgr.h b/src/Qtx/QtxResourceMgr.h index 1785a6b36..e799ebee5 100644 --- a/src/Qtx/QtxResourceMgr.h +++ b/src/Qtx/QtxResourceMgr.h @@ -9,6 +9,7 @@ #include #include #include +#include class QPixmap; @@ -22,11 +23,181 @@ class QTX_EXPORT QtxResourceMgr class XmlFormat; class Resources; + template class IMap; + template class IMapConstIterator; + template class IMapIterator + { + private: + IMap* myMap; + int myIndex; + + public: + IMapIterator() : myMap( 0 ), myIndex( 0 ) { init(); } + IMapIterator( const IMap* m ) : myMap( const_cast< IMap* >( m ) ), myIndex( 0 ) { init(); } + IMapIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } + + bool operator==( const IMapIterator& i ) { return !operator!=( i ); } + bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; } + + operator bool() const { return myIndex >= 0; } + + const Key& key() const { return myMap->key( myIndex ); } + Value& data() { return myMap->value( myIndex ); } + const Value& data() const { return myMap->value( myIndex ); } + + Value& operator*() { return data(); } + + IMapIterator& operator++() { myIndex++; init(); return *this; } + IMapIterator operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; } + IMapIterator& operator--() { myIndex--; init(); return *this; } + IMapIterator operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; } + + private: + IMapIterator( const IMap* m, const int index ) : myMap( const_cast< IMap* >( m ) ), myIndex( index ) { init(); } + void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; } + + friend class IMap; + friend class IMapConstIterator; + }; + + template class IMapConstIterator + { + private: + IMap* myMap; + int myIndex; + + public: + IMapConstIterator() : myMap( 0 ), myIndex( 0 ) { init(); } + IMapConstIterator( const IMap* m ) : myMap( const_cast< IMap* >( m ) ), myIndex( 0 ) { init(); } + IMapConstIterator( const IMapConstIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } + IMapConstIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); } + + bool operator==( const IMapConstIterator& i ) { return !operator!=( i ); } + bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; } + + operator bool() const { return myIndex >= 0; } + + const Key& key() const { return myMap->key( myIndex ); } + const Value& data () const { return myMap->value( myIndex ); } + + const Value& operator*() const { return data(); } + + IMapConstIterator& operator++() { myIndex++; init(); return *this; } + IMapConstIterator operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; } + IMapConstIterator& operator--() { myIndex--; init(); return *this; } + IMapConstIterator operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; } + + private: + IMapConstIterator( const IMap* m, const int index ): myMap( const_cast< IMap* >( m ) ), myIndex( index ) { init(); } + void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; } + + friend class IMap; + }; + + template class IMap + { + private: + QValueList myKeys; + QMap myData; + Key dummyKey; + Value dummyValue; + + public: + typedef IMapIterator Iterator; + typedef IMapConstIterator ConstIterator; + friend class Iterator; + friend class ConstIterator; + + IMap() {} + IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {} + IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; } + + int count() const { return myData.count(); } + int size() const { return myData.count(); } + bool empty() const { return myData.empty(); } + bool isEmpty() const { return myData.empty(); } + + void clear() { myKeys.clear(); myData.clear(); } + + QValueList keys() const { return myKeys; } + QValueList values() const { QValueList l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; } + bool contains ( const Key& key ) const { return myData.contains( key ); } + + Iterator begin() { return Iterator( this ); } + Iterator end() { return Iterator( this, count() ); } + ConstIterator begin() const { return ConstIterator( this ); } + ConstIterator end() const { return ConstIterator( this, count() ); } + + Iterator insert( const Key& key, const Value& value, bool overwrite = true ) + { + if ( myData.find( key ) == myData.end() || overwrite ) { + if ( myData.find( key ) != myData.end() && overwrite ) + myKeys.remove( myKeys.find( key ) ); + myKeys.append( key ); + myData[ key ] = value; + } + return Iterator( this, index( key ) ); + } + Iterator replace( const Key& key, const Value& value ) + { + if ( myData.find( key ) == myData.end() ) + myKeys.append( key ); + myData[ key ] = value; + return Iterator( this, index( key ) ); + } + + int index( const Key& key ) const { return myKeys.findIndex( key ); } + Iterator at( const int index ) { return Iterator( this, index ); } + ConstIterator at( const int index ) const { return ConstIterator( this, index ); } + + Key& key( const int index ) + { + if( index < 0 || index >= myKeys.count() ) + return dummyKey; + return myKeys[index]; + } + Value& value( const int index ) + { + if( index < 0 || index >= myKeys.count() ) + return dummyValue; + return myData[ myKeys[index] ]; + } + Value& operator[]( const Key& key ) + { + if ( myData.find( key ) == myData.end() ) + insert( key, Value() ); + return myData[ key ]; + } + const Value& operator[]( const Key& key ) const + { + if ( myData.find( key ) == myData.end() ) + return dummyValue; + return myData[ key ]; + } + + void erase( Iterator it ) { remove( it ); } + void erase( const Key& key ) { remove( key ); } + void erase( const int index ) { remove( index ); } + void remove( Iterator it ) { if ( it.myMap != this ) return; remove( it.myIndex ); } + void remove( const Key& key ) { remove( index( key ) ); } + void remove( const int index ) + { + if( index >= 0 && index < myKeys.count() ) { + myData.remove( myKeys[ index ] ); + myKeys.remove( myKeys.at( index ) ); + } + } + }; + public: class Format; +#if defined(QTX_NO_INDEXED_MAP) typedef QMap Section; +#else + typedef IMap Section; +#endif public: QtxResourceMgr( const QString&, const QString& = QString::null ); @@ -92,6 +263,7 @@ public: void raiseTranslators( const QString& ); void removeTranslators( const QString& ); void loadTranslator( const QString&, const QString& ); + void loadTranslators( const QString&, const QStringList& ); QString path( const QString&, const QString&, const QString& ) const; -- 2.39.2