1 #include "QtxResourceMgr.h"
7 #include <qtranslator.h>
8 #include <qapplication.h>
17 Class: QtxResourceMgr::Resources
21 QtxResourceMgr::Resources::Resources( const QString& fileName )
22 : myFileName( fileName )
26 QtxResourceMgr::Resources::~Resources()
30 QString QtxResourceMgr::Resources::file() const
35 void QtxResourceMgr::Resources::setFile( const QString& fn )
40 QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const
44 if ( hasValue( sect, name ) )
46 val = section( sect )[name];
48 val = makeSubstitution( val, sect, name );
53 void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val )
55 Section& s = section( sect );
56 s.insert( name, val );
59 bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const
61 return mySections.contains( sect );
64 bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const
66 return hasSection( sect ) && section( sect ).contains( name );
69 void QtxResourceMgr::Resources::removeSection( const QString& sect )
71 mySections.remove( sect );
74 void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name )
76 if ( !hasSection( sect ) )
79 Section& s = section( sect );
83 mySections.remove( sect );
86 void QtxResourceMgr::Resources::clear()
91 QStringList QtxResourceMgr::Resources::sections() const
93 return mySections.keys();
96 QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const
98 if ( !hasSection( sec ) )
101 return section( sec ).keys();
104 QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const
106 QString filePath = fileName( sec, prefix, name );
107 if ( !filePath.isEmpty() )
109 if ( !QFileInfo( filePath ).exists() )
110 filePath = QString::null;
115 QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn )
117 if ( !mySections.contains( sn ) )
118 mySections.insert( sn, Section() );
120 return mySections[sn];
123 const QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) const
125 return mySections[sn];
128 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
131 if ( hasValue( sect, prefix ) )
133 path = value( sect, prefix, true );
134 if ( !path.isEmpty() )
136 if ( QFileInfo( path ).isRelative() )
137 path = Qtx::addSlash( QFileInfo( myFileName ).dirPath( true ) ) + path;
139 path = Qtx::addSlash( path ) + name;
142 return QDir::convertSeparators( path );
145 QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
147 return QPixmap( fileName( sect, prefix, name ) );
150 QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const
152 QTranslator* trans = new QTranslator( 0 );
153 if ( !trans->load( fileName( sect, prefix, name ) ) )
161 QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const
163 QString varName = QString::null;
166 QRegExp rx( "\\$\\{([a-zA-Z]+[a-zA-Z0-9_]*)\\}|\\$\\(([a-zA-Z]+[a-zA-Z0-9_]*)\\)|\\$([a-zA-Z]+[a-zA-Z0-9_]*)|\\%([a-zA-Z]+[a-zA-Z0-9_]*)\\%" );
168 int pos = rx.search( str, start );
172 len = rx.matchedLength();
173 QStringList caps = rx.capturedTexts();
174 for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
175 varName = *caps.at( i );
180 QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const
184 QMap<QString, int> ignoreMap;
185 ignoreMap.insert( name, 0 );
187 int start( 0 ), len( 0 );
190 QString envName = environmentVariable( res, start, len );
191 if ( envName.isNull() )
194 QString newStr = QString::null;
195 if ( ::getenv( envName ) )
196 newStr = QString( ::getenv( envName ) );
198 if ( newStr.isNull() )
200 if ( ignoreMap.contains( envName ) )
206 if ( hasValue( sect, envName ) )
207 newStr = value( sect, envName, false );
208 ignoreMap.insert( envName, 0 );
210 res.replace( start, len, newStr );
217 Class: QtxResourceMgr::IniFormat
221 class QtxResourceMgr::IniFormat : public Format
228 virtual bool load( const QString&, QMap<QString, Section>& );
229 virtual bool save( const QString&, const QMap<QString, Section>& );
232 QtxResourceMgr::IniFormat::IniFormat()
237 QtxResourceMgr::IniFormat::~IniFormat()
241 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
244 if ( !file.open( IO_ReadOnly ) )
247 QTextStream ts( &file );
254 QString separator = option( "separator" );
255 if ( separator.isNull() )
256 separator = QString( "=" );
258 QString comment = option( "comment" );
259 if ( comment.isNull() )
260 comment = QString( "#" );
264 data = ts.readLine();
270 data = data.stripWhiteSpace();
271 if ( data.isEmpty() )
274 if ( data.startsWith( comment ) )
277 QRegExp rx( "^\\[([\\w\\s]*)\\]$" );
278 if ( rx.search( data ) != -1 )
280 section = rx.cap( 1 );
281 if ( section.isEmpty() )
284 qWarning( QString( "Empty section in line %1" ).arg( line ) );
287 else if ( data.contains( "=" ) && !section.isEmpty() )
289 int pos = data.find( separator );
290 QString key = data.left( pos - 1 ).stripWhiteSpace();
291 QString val = data.mid( pos + 1 ).stripWhiteSpace();
292 secMap[section].insert( key, val );
297 section.isEmpty() ? qWarning( "Current section is empty" ) :
298 qWarning( QString( "Error in line: %1" ).arg( line ) );
307 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
310 if ( !file.open( IO_WriteOnly ) )
314 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
316 QString data = QString( "[%1]\n" ).arg( it.key() );
317 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
318 data += iter.key() + " = " + iter.data() + "\n";
321 res = file.writeBlock( data.latin1(), data.length() ) == (int)data.length();
330 Class: QtxResourceMgr::XmlFormat
334 class QtxResourceMgr::XmlFormat : public Format
341 virtual bool load( const QString&, QMap<QString, Section>& );
342 virtual bool save( const QString&, const QMap<QString, Section>& );
345 QString docTag() const;
346 QString sectionTag() const;
347 QString parameterTag() const;
348 QString nameAttribute() const;
349 QString valueAttribute() const;
352 QtxResourceMgr::XmlFormat::XmlFormat()
357 QtxResourceMgr::XmlFormat::~XmlFormat()
361 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
368 if ( !file.open( IO_ReadOnly ) )
373 res = doc.setContent( &file );
379 QDomElement root = doc.documentElement();
380 if ( root.isNull() || root.tagName() != docTag() )
383 QDomNode sectNode = root.firstChild();
384 while ( res && !sectNode.isNull() )
386 res = sectNode.isElement();
389 QDomElement sectElem = sectNode.toElement();
390 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
392 QString section = sectElem.attribute( nameAttribute() );
393 QDomNode paramNode = sectNode.firstChild();
394 while ( res && !paramNode.isNull() )
396 res = paramNode.isElement();
399 QDomElement paramElem = paramNode.toElement();
400 if ( paramElem.tagName() == parameterTag() &&
401 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
403 QString paramName = paramElem.attribute( nameAttribute() );
404 QString paramValue = paramElem.attribute( valueAttribute() );
406 secMap[section].insert( paramName, paramValue );
412 res = paramNode.isComment();
414 paramNode = paramNode.nextSibling();
421 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
423 sectNode = sectNode.nextSibling();
431 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
438 if ( !file.open( IO_WriteOnly ) )
441 QDomDocument doc( docTag() );
442 QDomElement root = doc.createElement( docTag() );
443 doc.appendChild( root );
445 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
447 QDomElement sect = doc.createElement( sectionTag() );
448 sect.setAttribute( nameAttribute(), it.key() );
449 root.appendChild( sect );
450 for ( QMap<QString, QString>::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
452 QDomElement val = doc.createElement( parameterTag() );
453 val.setAttribute( nameAttribute(), iter.key() );
454 val.setAttribute( valueAttribute(), iter.data() );
455 sect.appendChild( val );
459 QString docStr = doc.toString();
460 res = file.writeBlock( docStr.latin1(), docStr.length() ) == (int)docStr.length();
468 QString QtxResourceMgr::XmlFormat::docTag() const
470 QString tag = option( "doc_tag" );
472 tag = QString( "document" );
476 QString QtxResourceMgr::XmlFormat::sectionTag() const
478 QString tag = option( "section_tag" );
480 tag = QString( "section" );
484 QString QtxResourceMgr::XmlFormat::parameterTag() const
486 QString tag = option( "parameter_tag" );
488 tag = QString( "parameter" );
492 QString QtxResourceMgr::XmlFormat::nameAttribute() const
494 QString str = option( "name_attribute" );
496 str = QString( "name" );
500 QString QtxResourceMgr::XmlFormat::valueAttribute() const
502 QString str = option( "value_attribute" );
504 str = QString( "value" );
509 Class: QtxResourceMgr::Format
513 QtxResourceMgr::Format::Format( const QString& fmt )
518 QtxResourceMgr::Format::~Format()
522 QString QtxResourceMgr::Format::format() const
527 QStringList QtxResourceMgr::Format::options() const
532 QString QtxResourceMgr::Format::option( const QString& opt ) const
535 if ( myOpt.contains( opt ) )
539 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
541 myOpt.insert( opt, val );
544 bool QtxResourceMgr::Format::load( Resources* res )
549 QMap<QString, Section> sections;
550 bool status = load( res->myFileName, sections );
552 res->mySections = sections;
554 qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", res->myFileName.latin1() );
559 bool QtxResourceMgr::Format::save( Resources* res )
564 Qtx::mkDir( Qtx::dir( res->myFileName ) );
566 return save( res->myFileName, res->mySections );
570 Class: QtxResourceMgr
574 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
575 : myAppName( appName )
577 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
578 if ( envVar.contains( "%1" ) )
579 envVar = envVar.arg( appName );
582 if ( ::getenv( envVar ) )
583 dirs = ::getenv( envVar );
585 setDirList( QStringList::split( ";", dirs ) );
587 installFormat( new XmlFormat() );
588 installFormat( new IniFormat() );
590 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
593 QtxResourceMgr::~QtxResourceMgr()
595 QStringList prefList = myTranslator.keys();
596 for ( QStringList::const_iterator it = prefList.begin(); it != prefList.end(); ++it )
597 removeTranslators( *it );
600 QString QtxResourceMgr::appName() const
605 QStringList QtxResourceMgr::dirList() const
610 void QtxResourceMgr::setDirList( const QStringList& dl )
613 for ( ResListIterator it( myResources ); it.current(); ++it )
619 void QtxResourceMgr::initialize( const bool autoLoad ) const
621 if ( !myResources.isEmpty() )
624 QtxResourceMgr* that = (QtxResourceMgr*)this;
626 that->myResources.append( new Resources( userFileName( appName() ) ) );
627 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it )
629 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
630 that->myResources.append( new Resources( path ) );
637 void QtxResourceMgr::clear()
639 for ( ResListIterator it( myResources ); it.current(); ++it )
640 it.current()->clear();
643 QString QtxResourceMgr::currentSection() const
648 void QtxResourceMgr::setCurrentSection( const QString& str )
653 bool QtxResourceMgr::value( const QString& name, int& val ) const
655 return value( currentSection(), name, val );
658 bool QtxResourceMgr::value( const QString& name, double& val ) const
660 return value( currentSection(), name, val );
663 bool QtxResourceMgr::value( const QString& name, bool& val ) const
665 return value( currentSection(), name, val );
668 bool QtxResourceMgr::value( const QString& name, QColor& val ) const
670 return value( currentSection(), name, val );
673 bool QtxResourceMgr::value( const QString& name, QFont& val ) const
675 return value( currentSection(), name, val );
678 bool QtxResourceMgr::value( const QString& name, QString& val, const bool subst ) const
680 return value( currentSection(), name, val, subst );
683 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
686 if ( !value( sect, name, val, true ) )
690 iVal = val.toInt( &ok );
695 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
698 if ( !value( sect, name, val, true ) )
702 dVal = val.toDouble( &ok );
707 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
710 if ( !value( sect, name, val, true ) )
713 static QMap<QString, bool> boolMap;
714 if ( boolMap.isEmpty() )
716 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
717 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
721 bool res = boolMap.contains( val );
726 double num = val.toDouble( &res );
734 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
737 if ( !value( sect, name, val, true ) )
741 QStringList vals = QStringList::split( ",", val, true );
744 for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it )
745 nums.append( (*it).toInt( &res ) );
747 if ( res && nums.count() >= 3 )
748 cVal.setRgb( nums[0], nums[1], nums[2] );
751 int pack = val.toInt( &res );
753 Qtx::rgbSet( pack, cVal );
759 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
761 QString val = stringValue( sect, name, "" ).stripWhiteSpace();
762 QStringList font_values = QStringList::split( val, "," );
763 if( font_values.count()<2 || font_values.count()>4 )
766 QString family = font_values[0];
767 bool isBold = false, isItalic = false, isOk = false;
769 for( int i=1, n=font_values.count(); i<n; i++ )
771 if( !isBold && font_values[i].lower()=="bold" )
773 else if( !isItalic && font_values[i].lower()=="italic" )
777 pSize = font_values[i].toInt( &isOk );
783 if( pSize>0 && !family.isEmpty() )
785 fVal = QFont( family, pSize );
786 fVal.setBold( isBold );
787 fVal.setItalic( isItalic );
794 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
799 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
801 ok = it.current()->hasValue( sect, name );
803 val = it.current()->value( sect, name, subst );
809 int QtxResourceMgr::integerValue( const QString& name, const int def ) const
811 return integerValue( currentSection(), name, def );
814 double QtxResourceMgr::doubleValue( const QString& name, const double def ) const
816 return doubleValue( currentSection(), name, def );
819 bool QtxResourceMgr::booleanValue( const QString& name, const bool def ) const
821 return booleanValue( currentSection(), name, def );
824 QFont QtxResourceMgr::fontValue( const QString& name, const QFont& def ) const
826 return fontValue( currentSection(), name, def );
829 QColor QtxResourceMgr::colorValue( const QString& name, const QColor& def ) const
831 return colorValue( currentSection(), name, def );
834 QString QtxResourceMgr::stringValue( const QString& name, const char* def ) const
836 return stringValue( currentSection(), name, def );
839 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
842 if ( !value( sect, name, val ) )
847 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
850 if ( !value( sect, name, val ) )
855 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
858 if ( !value( sect, name, val ) )
863 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
866 if( !value( sect, name, font ) )
871 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
874 if ( !value( sect, name, val ) )
879 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const char* def ) const
882 if ( !value( sect, name, val ) )
887 bool QtxResourceMgr::hasValue( const QString& name ) const
889 return hasValue( currentSection(), name );
892 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
897 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
898 ok = it.current()->hasValue( sect, name );
903 bool QtxResourceMgr::hasSection( const QString& sect ) const
908 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
909 ok = it.current()->hasSection( sect );
914 void QtxResourceMgr::setValue( const QString& name, int val )
916 setValue( currentSection(), name, val );
919 void QtxResourceMgr::setValue( const QString& name, double val )
921 setValue( currentSection(), name, val );
924 void QtxResourceMgr::setValue( const QString& name, bool val )
926 setValue( currentSection(), name, val );
929 void QtxResourceMgr::setValue( const QString& name, const QColor& val )
931 setValue( currentSection(), name, val );
934 void QtxResourceMgr::setValue( const QString& name, const QFont& val )
936 setValue( currentSection(), name, val );
939 void QtxResourceMgr::setValue( const QString& name, const QString& val )
941 setValue( currentSection(), name, val );
944 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
946 setValue( sect, name, QString::number( val ) );
949 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
951 setValue( sect, name, QString::number( val, 'g', 12 ) );
954 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
956 setValue( sect, name, QString( val ? "true" : "false" ) );
959 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
961 setValue( sect, name, QString( "%1, %2, %3").arg( val.red() ).arg( val.green() ).arg( val.blue() ) );
964 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& f )
967 val.append( f.family() );
969 val.append( "Bold" );
971 val.append( "Italic" );
972 val.append( QString( "%1" ).arg( f.pointSize() ) );
974 setValue( sect, name, val.join( "," ) );
977 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
981 if ( !myResources.isEmpty() )
982 myResources.first()->setValue( sect, name, val );
985 void QtxResourceMgr::remove( const QString& name )
987 remove( currentSection(), name );
990 void QtxResourceMgr::remove( const QString& sect, const QString& name )
994 for ( ResListIterator it( myResources ); it.current(); ++it )
995 it.current()->removeValue( sect, name );
998 void QtxResourceMgr::removeSection( const QString& sect )
1002 for ( ResListIterator it( myResources ); it.current(); ++it )
1003 it.current()->removeSection( sect );
1006 QString QtxResourceMgr::currentFormat() const
1009 if ( !myFormats.isEmpty() )
1010 fmt = myFormats.getFirst()->format();
1014 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
1016 Format* form = format( fmt );
1020 myFormats.remove( form );
1021 myFormats.prepend( form );
1023 if ( myResources.isEmpty() )
1026 ResListIterator resIt( myResources );
1027 if ( resIt.current() )
1028 resIt.current()->setFile( userFileName( appName() ) );
1031 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt )
1032 resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
1035 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
1038 for ( FormatListIterator it( myFormats ); it.current() && !form; ++it )
1040 if ( it.current()->format() == fmt )
1041 form = it.current();
1047 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
1049 if ( !myFormats.contains( form ) )
1050 myFormats.prepend( form );
1053 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
1055 myFormats.remove( form );
1058 QStringList QtxResourceMgr::options() const
1060 return myOptions.keys();
1063 QString QtxResourceMgr::option( const QString& opt ) const
1066 if ( myOptions.contains( opt ) )
1067 val = myOptions[opt];
1071 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
1073 myOptions.insert( opt, val );
1076 bool QtxResourceMgr::load()
1078 initialize( false );
1080 Format* fmt = format( currentFormat() );
1085 for ( ResListIterator it( myResources ); it.current(); ++it )
1086 res = fmt->load( it.current() ) && res;
1091 bool QtxResourceMgr::save()
1093 initialize( false );
1095 Format* fmt = format( currentFormat() );
1099 if ( myResources.isEmpty() )
1102 return fmt->save( myResources.getFirst() );
1105 QStringList QtxResourceMgr::sections() const
1109 QMap<QString, int> map;
1110 for ( ResListIterator it( myResources ); it.current(); ++it )
1112 QStringList lst = it.current()->sections();
1113 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1114 map.insert( *itr, 0 );
1118 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1119 res.append( iter.key() );
1124 QStringList QtxResourceMgr::parameters( const QString& sec ) const
1128 QMap<QString, int> map;
1129 for ( ResListIterator it( myResources ); it.current(); ++it )
1131 QStringList lst = it.current()->parameters( sec );
1132 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1133 map.insert( *itr, 0 );
1137 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1138 res.append( iter.key() );
1143 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
1146 for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it )
1147 res = it.current()->path( sect, prefix, name );
1151 QString QtxResourceMgr::resSection() const
1153 QString res = option( "res_section_name" );
1154 if ( res.isEmpty() )
1155 res = QString( "resources" );
1159 QString QtxResourceMgr::langSection() const
1161 QString res = option( "lang_section_name" );
1162 if ( res.isEmpty() )
1163 res = QString( "language" );
1167 QPixmap QtxResourceMgr::defaultPixmap() const
1169 return myDefaultPix;
1172 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
1177 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
1179 return loadPixmap( prefix, name, true );
1182 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
1184 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
1187 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
1192 for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it )
1193 pix = it.current()->loadPixmap( resSection(), prefix, name );
1199 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
1203 QMap<QChar, QString> substMap;
1204 substMap.insert( 'A', appName() );
1207 if ( lang.isEmpty() )
1208 value( langSection(), "language", lang );
1210 if ( lang.isEmpty() )
1212 lang = QString( "en" );
1213 qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) );
1216 substMap.insert( 'L', lang );
1219 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
1221 QStringList translators = QStringList::split( "|", option( "translators" ) );
1222 QStringList newTranslators = QStringList::split( "|", trs );
1223 for ( uint i = 0; i < newTranslators.count(); i++ )
1224 if ( translators.find( newTranslators[i] ) == translators.end() )
1225 translators += newTranslators[i];
1226 setOption( "translators", translators.join( "|" ) );
1229 QStringList trList = QStringList::split( "|", option( "translators" ) );
1230 if ( trList.isEmpty() )
1232 trList.append( "%P_msg_%L.qm" );
1233 qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) );
1236 QStringList prefixList;
1237 if ( !pref.isEmpty() )
1238 prefixList.append( pref );
1240 prefixList = parameters( resSection() );
1242 for ( QStringList::const_iterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
1244 QString prefix = *iter;
1245 substMap.insert( 'P', prefix );
1248 for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it )
1249 trs.append( substMacro( *it, substMap ).stripWhiteSpace() );
1251 for ( QStringList::const_iterator itr = trs.begin(); itr != trs.end(); ++itr )
1252 loadTranslator( prefix, *itr );
1256 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
1260 QTranslator* trans = 0;
1261 for ( ResListIterator it( myResources ); it.current() && !trans; ++it )
1262 trans = it.current()->loadTranslator( resSection(), prefix, name );
1267 if ( !myTranslator[prefix].contains( trans ) )
1268 myTranslator[prefix].append( trans );
1269 qApp->installTranslator( trans );
1272 void QtxResourceMgr::removeTranslators( const QString& prefix )
1274 if ( !myTranslator.contains( prefix ) )
1277 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1279 qApp->removeTranslator( it.current() );
1280 delete it.current();
1283 myTranslator.remove( prefix );
1286 void QtxResourceMgr::raiseTranslators( const QString& prefix )
1288 if ( !myTranslator.contains( prefix ) )
1291 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1293 qApp->removeTranslator( it.current() );
1294 qApp->installTranslator( it.current() );
1298 void QtxResourceMgr::refresh()
1300 QStringList sl = sections();
1301 for ( QStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
1303 QStringList pl = parameters( *it );
1304 for ( QStringList::const_iterator itr = pl.begin(); itr != pl.end(); ++itr )
1305 setValue( *it, *itr, stringValue( *it, *itr ) );
1309 QString QtxResourceMgr::userFileName( const QString& appName ) const
1312 QString pathName = QDir::homeDirPath();
1315 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1317 fileName = QString( ".%1rc" ).arg( appName );
1320 if ( !fileName.isEmpty() )
1321 pathName = Qtx::addSlash( pathName ) + fileName;
1326 QString QtxResourceMgr::globalFileName( const QString& appName ) const
1328 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1331 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
1335 QRegExp rx( "%[A-Za-z%]" );
1338 while ( ( idx = rx.search( trg, idx ) ) >= 0 )
1340 QChar spec = trg.at( idx + 1 );
1344 else if ( substMap.contains( spec ) )
1345 subst = substMap[spec];
1347 if ( !subst.isEmpty() )
1349 trg.replace( idx, rx.matchedLength(), subst );
1350 idx += subst.length();
1353 idx += rx.matchedLength();