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 #include "QtxResourceMgr.h"
25 #include <qtranslator.h>
26 #include <qapplication.h>
35 Class: QtxResourceMgr::Resources
39 QtxResourceMgr::Resources::Resources( const QtxResourceMgr* mgr, const QString& fileName )
40 : myFileName( fileName ),
41 myMgr( const_cast<QtxResourceMgr*>( mgr ) )
45 QtxResourceMgr::Resources::~Resources()
49 QString QtxResourceMgr::Resources::file() const
54 void QtxResourceMgr::Resources::setFile( const QString& fn )
59 QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const
63 if ( hasValue( sect, name ) )
65 val = section( sect )[name];
67 val = makeSubstitution( val, sect, name );
72 void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val )
74 Section& s = section( sect );
75 s.insert( name, val );
78 bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const
80 return mySections.contains( sect );
83 bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const
85 return hasSection( sect ) && section( sect ).contains( name );
88 void QtxResourceMgr::Resources::removeSection( const QString& sect )
90 mySections.remove( sect );
93 void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name )
95 if ( !hasSection( sect ) )
98 Section& s = section( sect );
102 mySections.remove( sect );
105 void QtxResourceMgr::Resources::clear()
110 QStringList QtxResourceMgr::Resources::sections() const
112 return mySections.keys();
115 QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const
117 if ( !hasSection( sec ) )
118 return QStringList();
120 return section( sec ).keys();
123 QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const
125 QString filePath = fileName( sec, prefix, name );
126 if ( !filePath.isEmpty() )
128 if ( !QFileInfo( filePath ).exists() )
129 filePath = QString::null;
134 QtxResourceMgr* QtxResourceMgr::Resources::resMgr() const
139 QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn )
141 if ( !mySections.contains( sn ) )
142 mySections.insert( sn, Section() );
144 return mySections[sn];
147 const QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) const
149 return mySections[sn];
152 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
155 if ( hasValue( sect, prefix ) )
157 path = value( sect, prefix, true );
158 if ( !path.isEmpty() )
160 if ( QFileInfo( path ).isRelative() )
161 path = Qtx::addSlash( QFileInfo( myFileName ).dirPath( true ) ) + path;
163 path = Qtx::addSlash( path ) + name;
166 if( !path.isEmpty() )
168 QString fname = QDir::convertSeparators( path );
169 QFileInfo inf( fname );
170 fname = inf.absFilePath();
176 QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
178 QString fname = fileName( sect, prefix, name );
179 bool toCache = resMgr() ? resMgr()->isPixmapCached() : false;
181 if( toCache && myPixmapCache.contains( fname ) )
182 p = myPixmapCache[fname];
187 ( ( QMap<QString,QPixmap>& )myPixmapCache ).insert( fname, p );
192 QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const
194 QTranslator* trans = new QTranslator( 0 );
195 if ( !trans->load( fileName( sect, prefix, name ) ) )
203 QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const
205 QString varName = QString::null;
208 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_]*)\\%" );
210 int pos = rx.search( str, start );
214 len = rx.matchedLength();
215 QStringList caps = rx.capturedTexts();
216 for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
217 varName = *caps.at( i );
222 QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const
226 QMap<QString, int> ignoreMap;
227 ignoreMap.insert( name, 0 );
229 int start( 0 ), len( 0 );
232 QString envName = environmentVariable( res, start, len );
233 if ( envName.isNull() )
236 QString newStr = QString::null;
237 if ( ::getenv( envName ) )
238 newStr = QString( ::getenv( envName ) );
240 if ( newStr.isNull() )
242 if ( ignoreMap.contains( envName ) )
248 if ( hasValue( sect, envName ) )
249 newStr = value( sect, envName, false );
250 ignoreMap.insert( envName, 0 );
252 res.replace( start, len, newStr );
259 Class: QtxResourceMgr::IniFormat
263 class QtxResourceMgr::IniFormat : public Format
270 virtual bool load( const QString&, QMap<QString, Section>& );
271 virtual bool save( const QString&, const QMap<QString, Section>& );
274 QtxResourceMgr::IniFormat::IniFormat()
279 QtxResourceMgr::IniFormat::~IniFormat()
283 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
286 if ( !file.open( IO_ReadOnly ) )
289 QTextStream ts( &file );
296 QString separator = option( "separator" );
297 if ( separator.isNull() )
298 separator = QString( "=" );
300 QString comment = option( "comment" );
301 if ( comment.isNull() )
302 comment = QString( "#" );
306 data = ts.readLine();
312 data = data.stripWhiteSpace();
313 if ( data.isEmpty() )
316 if ( data.startsWith( comment ) )
319 QRegExp rx( "^\\[([\\w\\s\\._]*)\\]$" );
320 if ( rx.search( data ) != -1 )
322 section = rx.cap( 1 );
323 if ( section.isEmpty() )
326 qWarning( QString( "Empty section in line %1" ).arg( line ) );
329 else if ( data.contains( "=" ) && !section.isEmpty() )
331 int pos = data.find( separator );
332 QString key = data.left( pos ).stripWhiteSpace();
333 QString val = data.mid( pos + 1 ).stripWhiteSpace();
334 secMap[section].insert( key, val );
339 section.isEmpty() ? qWarning( "Current section is empty" ) :
340 qWarning( QString( "Error in line: %1" ).arg( line ) );
349 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
352 if ( !file.open( IO_WriteOnly ) )
356 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
358 QString data = QString( "[%1]\n" ).arg( it.key() );
359 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
360 data += iter.key() + " = " + iter.data() + "\n";
363 res = file.writeBlock( data.latin1(), data.length() ) == (int)data.length();
372 Class: QtxResourceMgr::XmlFormat
376 class QtxResourceMgr::XmlFormat : public Format
383 virtual bool load( const QString&, QMap<QString, Section>& );
384 virtual bool save( const QString&, const QMap<QString, Section>& );
387 QString docTag() const;
388 QString sectionTag() const;
389 QString parameterTag() const;
390 QString nameAttribute() const;
391 QString valueAttribute() const;
394 QtxResourceMgr::XmlFormat::XmlFormat()
399 QtxResourceMgr::XmlFormat::~XmlFormat()
403 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
410 if ( !file.open( IO_ReadOnly ) )
415 res = doc.setContent( &file );
421 QDomElement root = doc.documentElement();
422 if ( root.isNull() || root.tagName() != docTag() )
425 QDomNode sectNode = root.firstChild();
426 while ( res && !sectNode.isNull() )
428 res = sectNode.isElement();
431 QDomElement sectElem = sectNode.toElement();
432 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
434 QString section = sectElem.attribute( nameAttribute() );
435 QDomNode paramNode = sectNode.firstChild();
436 while ( res && !paramNode.isNull() )
438 res = paramNode.isElement();
441 QDomElement paramElem = paramNode.toElement();
442 if ( paramElem.tagName() == parameterTag() &&
443 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
445 QString paramName = paramElem.attribute( nameAttribute() );
446 QString paramValue = paramElem.attribute( valueAttribute() );
448 secMap[section].insert( paramName, paramValue );
454 res = paramNode.isComment();
456 paramNode = paramNode.nextSibling();
463 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
465 sectNode = sectNode.nextSibling();
473 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
480 if ( !file.open( IO_WriteOnly ) )
483 QDomDocument doc( docTag() );
484 QDomElement root = doc.createElement( docTag() );
485 doc.appendChild( root );
487 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
489 QDomElement sect = doc.createElement( sectionTag() );
490 sect.setAttribute( nameAttribute(), it.key() );
491 root.appendChild( sect );
492 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
494 QDomElement val = doc.createElement( parameterTag() );
495 val.setAttribute( nameAttribute(), iter.key() );
496 val.setAttribute( valueAttribute(), iter.data() );
497 sect.appendChild( val );
501 QString docStr = doc.toString();
502 res = file.writeBlock( docStr.latin1(), docStr.length() ) == (int)docStr.length();
510 QString QtxResourceMgr::XmlFormat::docTag() const
512 QString tag = option( "doc_tag" );
514 tag = QString( "document" );
518 QString QtxResourceMgr::XmlFormat::sectionTag() const
520 QString tag = option( "section_tag" );
522 tag = QString( "section" );
526 QString QtxResourceMgr::XmlFormat::parameterTag() const
528 QString tag = option( "parameter_tag" );
530 tag = QString( "parameter" );
534 QString QtxResourceMgr::XmlFormat::nameAttribute() const
536 QString str = option( "name_attribute" );
538 str = QString( "name" );
542 QString QtxResourceMgr::XmlFormat::valueAttribute() const
544 QString str = option( "value_attribute" );
546 str = QString( "value" );
551 Class: QtxResourceMgr::Format
556 \brief Constructs the format object with specified name.
557 \param fmt - name of the format
559 QtxResourceMgr::Format::Format( const QString& fmt )
565 \brief Destructs the format object.
567 QtxResourceMgr::Format::~Format()
572 \brief Returns the format name.
574 QString QtxResourceMgr::Format::format() const
580 \brief Returns the string list of the format options.
582 QStringList QtxResourceMgr::Format::options() const
588 \brief Returns the value of the option with specified name.
589 If option doesn't exist then empty string returned.
590 \param opt - name of the option
592 QString QtxResourceMgr::Format::option( const QString& opt ) const
595 if ( myOpt.contains( opt ) )
601 \brief Sets the value of the option with specified name.
602 \param opt - name of the option
603 \param opt - value of the option
605 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
607 myOpt.insert( opt, val );
611 \brief Perform the loading of the resources from resource file.
612 \param res - resources object which will be loaded
614 bool QtxResourceMgr::Format::load( Resources* res )
619 QMap<QString, Section> sections;
620 bool status = load( res->myFileName, sections );
622 res->mySections = sections;
624 qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", res->myFileName.latin1() );
630 \brief Perform the saving of the resources into resource file.
631 \param res - resources object which will be saved
633 bool QtxResourceMgr::Format::save( Resources* res )
638 Qtx::mkDir( Qtx::dir( res->myFileName ) );
640 return save( res->myFileName, res->mySections );
644 Class: QtxResourceMgr
649 \brief Constructs the resource manager object for application.
650 \param appName - name of the application which resources will be used.
651 \param resVarTemplate - template for the resource environment variable name which
652 should point to the resource directory list.
653 Default value is "%1Resources". Its mean that for application
654 with name "MyApp" environment variable "MyAppResources" will
655 be used. Template may not have the parameter '%1' substituted
656 by application name. In this case this string will be used as
657 is without substitution.
658 Resource environment variable should contains one or several resource directories
659 separated by symbol ';'. Resource directories list transfered into the setDirList().
660 These directories and the user home directory used for the loading application resources.
661 Each of the resource directories can contains resource file. The name of this file defined
662 by the function globalFileName(). Resource file name in the user home defined by the
663 function userFileName(). Any resource looking firstly in the user home resources then
664 resource directories used in the specified order. All setted resources always stored into
665 the resource file at the user home. Only user home resource file is saved.
666 If you want to ignore of loading of Local User Preferences, you needs setup setIngoreUserValues()
669 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
670 : myAppName( appName ),
671 myCheckExist( true ),
672 myIsPixmapCached( true ),
673 myIsIgnoreUserValues( false )
675 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
676 if ( envVar.contains( "%1" ) )
677 envVar = envVar.arg( appName );
680 if ( ::getenv( envVar ) )
681 dirs = ::getenv( envVar );
683 QString dirsep = ";"; // for Windows: ";" is used as directories separator
685 QString dirsep = "[:|;]"; // for Linux: both ":" and ";" can be used
687 setDirList( QStringList::split( QRegExp(dirsep), dirs ) );
689 installFormat( new XmlFormat() );
690 installFormat( new IniFormat() );
692 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
696 \brief Destructs the resource manager object and free allocated memory.
698 QtxResourceMgr::~QtxResourceMgr()
700 QStringList prefList = myTranslator.keys();
701 for ( QStringList::const_iterator it = prefList.begin(); it != prefList.end(); ++it )
702 removeTranslators( *it );
706 \brief Returns the application name.
708 QString QtxResourceMgr::appName() const
714 \brief Returns the checking of the existance flag. If its 'true' then resource
715 will be setted into the manager only if it doesn't exist or has different
716 value that existing value.
718 bool QtxResourceMgr::checkExisting() const
724 \brief Sets the checking of the existance flag.
725 \param on - boolean value of the flag.
727 void QtxResourceMgr::setCheckExisting( const bool on )
733 \brief Returns the resource directories list except user home directory.
735 QStringList QtxResourceMgr::dirList() const
741 \brief Initialise the manager. Prepare the resource containers and load resources.
742 \param autoLoad - if 'true' then all resources will be loaded.
744 void QtxResourceMgr::initialize( const bool autoLoad ) const
746 if ( !myResources.isEmpty() )
749 QtxResourceMgr* that = (QtxResourceMgr*)this;
751 if ( !userFileName( appName() ).isEmpty() )
752 that->myResources.append( new Resources( this, userFileName( appName() ) ) );
754 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it )
756 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
757 that->myResources.append( new Resources( this, path ) );
765 \brief Return true if all loaded pixmaps are stored in internal map; by default: true
767 bool QtxResourceMgr::isPixmapCached() const
769 return myIsPixmapCached;
773 \brief Set true, if it is necessary to store all loaded pixmap in internal map
774 (it accelerates following calls of loadPixmap)
776 void QtxResourceMgr::setIsPixmapCached( const bool on )
778 myIsPixmapCached = on;
782 \brief Removes all resources from the manager.
784 void QtxResourceMgr::clear()
786 for ( ResListIterator it( myResources ); it.current(); ++it )
787 it.current()->clear();
790 void QtxResourceMgr::setIgnoreUserValues( const bool val )
792 myIsIgnoreUserValues = val;
795 bool QtxResourceMgr::ignoreUserValues() const
797 return myIsIgnoreUserValues;
801 \brief Get the resource value as integer. Returns 'true' if it successfull otherwise
803 \param sect - Resource section name which contains resource.
804 \param name - Name of the resource.
805 \param iVal - Reference on the variable which should contains the resource output.
807 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
810 if ( !value( sect, name, val, true ) )
814 iVal = val.toInt( &ok );
820 \brief Get the resource value as double. Returns 'true' if it successfull otherwise
822 \param sect - Resource section name which contains resource.
823 \param name - Name of the resource.
824 \param dVal - Reference on the variable which should contains the resource output.
826 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
829 if ( !value( sect, name, val, true ) )
833 dVal = val.toDouble( &ok );
839 \brief Get the resource value as boolean. Returns 'true' if it successfull otherwise
841 \param sect - Resource section name which contains resource.
842 \param name - Name of the resource.
843 \param bVal - Reference on the variable which should contains the resource output.
845 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
848 if ( !value( sect, name, val, true ) )
851 static QMap<QString, bool> boolMap;
852 if ( boolMap.isEmpty() )
854 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
855 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
859 bool res = boolMap.contains( val );
864 double num = val.toDouble( &res );
873 \brief Get the resource value as color. Returns 'true' if it successfull otherwise
875 \param sect - Resource section name which contains resource.
876 \param name - Name of the resource.
877 \param cVal - Reference on the variable which should contains the resource output.
879 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
882 if ( !value( sect, name, val, true ) )
886 QStringList vals = QStringList::split( ",", val, true );
889 for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it )
890 nums.append( (*it).toInt( &res ) );
892 if ( res && nums.count() >= 3 )
893 cVal.setRgb( nums[0], nums[1], nums[2] );
896 int pack = val.toInt( &res );
898 Qtx::rgbSet( pack, cVal );
905 \brief Get the resource value as font. Returns 'true' if it successfull otherwise
907 \param sect - Resource section name which contains resource.
908 \param name - Name of the resource.
909 \param fVal - Reference on the variable which should contains the resource output.
911 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
914 if ( !value( sect, name, val, true ) )
917 QStringList fontDescr = QStringList::split( ",", val );
919 if ( fontDescr.count() < 2 )
922 QString family = fontDescr[0];
923 if ( family.isEmpty() )
926 fVal = QFont( family );
928 for ( int i = 1; i < (int)fontDescr.count(); i++ )
930 QString curval = fontDescr[i].stripWhiteSpace().lower();
931 if ( curval == QString( "bold" ) )
932 fVal.setBold( true );
933 else if ( curval == QString( "italic" ) )
934 fVal.setItalic( true );
935 else if ( curval == QString( "underline" ) )
936 fVal.setUnderline( true );
940 int ps = curval.toInt( &isOk );
942 fVal.setPointSize( ps );
950 \brief Get the resource value as string (native format). Returns 'true' if it
951 successfull otherwise returns 'false'.
952 \param sect - Resource section name which contains resource.
953 \param name - Name of the resource.
954 \param val - Reference on the variable which should contains the resource output.
955 \param subst - If 'true' then manager substitute reference on environment variables
956 and other resources by thier values. Default value of this parameter
959 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
965 ResListIterator it( myResources );
966 if ( ignoreUserValues() )
969 for ( ; it.current() && !ok; ++it )
971 ok = it.current()->hasValue( sect, name );
973 val = it.current()->value( sect, name, subst );
980 \brief Returns the integer resource value. If resource can not be found or converted
981 then specified default value will be returned.
982 \param sect - Resource section name which contains resource.
983 \param name - Name of the resource.
984 \param def - Default resource value which will be used when resource not found.
986 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
989 if ( !value( sect, name, val ) )
995 \brief Returns the double resource value. If resource can not be found or converted
996 then specified default value will be returned.
997 \param sect - Resource section name which contains resource.
998 \param name - Name of the resource.
999 \param def - Default resource value which will be used when resource not found.
1001 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
1004 if ( !value( sect, name, val ) )
1010 \brief Returns the boolean resource value. If resource can not be found or converted
1011 then specified default value will be returned.
1012 \param sect - Resource section name which contains resource.
1013 \param name - Name of the resource.
1014 \param def - Default resource value which will be used when resource not found.
1016 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
1019 if ( !value( sect, name, val ) )
1025 \brief Returns the font resource value. If resource can not be found or converted
1026 then specified default value will be returned.
1027 \param sect - Resource section name which contains resource.
1028 \param name - Name of the resource.
1029 \param def - Default resource value which will be used when resource not found.
1031 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
1034 if( !value( sect, name, font ) )
1040 \brief Returns the color resource value. If resource can not be found or converted
1041 then specified default value will be returned.
1042 \param sect - Resource section name which contains resource.
1043 \param name - Name of the resource.
1044 \param def - Default resource value which will be used when resource not found.
1046 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
1049 if ( !value( sect, name, val ) )
1055 \brief Returns the string resource value. If resource can not be found or converted
1056 then specified default value will be returned.
1057 \param sect - Resource section name which contains resource.
1058 \param name - Name of the resource.
1059 \param def - Default resource value which will be used when resource not found.
1061 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def ) const
1064 if ( !value( sect, name, val ) )
1070 \brief Checks existance of the specified resource.
1071 \param sect - Resource section name which contains resource.
1072 \param name - Name of the resource.
1074 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
1079 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1080 ok = it.current()->hasValue( sect, name );
1086 \brief Checks existance of the specified resource section.
1087 \param sect - Resource section name which contains resource.
1089 bool QtxResourceMgr::hasSection( const QString& sect ) const
1094 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1095 ok = it.current()->hasSection( sect );
1101 \brief Sets the integer resource value.
1102 \param sect - Resource section name.
1103 \param name - Name of the resource.
1104 \param val - Resource value.
1106 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1109 if ( checkExisting() && value( sect, name, res ) && res == val )
1112 setResource( sect, name, QString::number( val ) );
1116 \brief Sets the double resource value.
1117 \param sect - Resource section name.
1118 \param name - Name of the resource.
1119 \param val - Resource value.
1121 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1124 if ( checkExisting() && value( sect, name, res ) && res == val )
1127 setResource( sect, name, QString::number( val, 'g', 12 ) );
1131 \brief Sets the boolean resource value.
1132 \param sect - Resource section name.
1133 \param name - Name of the resource.
1134 \param val - Resource value.
1136 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1139 if ( checkExisting() && value( sect, name, res ) && res == val )
1142 setResource( sect, name, QString( val ? "true" : "false" ) );
1146 \brief Sets the color resource value.
1147 \param sect - Resource section name.
1148 \param name - Name of the resource.
1149 \param val - Resource value.
1151 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
1154 if ( checkExisting() && value( sect, name, res ) && res == val )
1157 setResource( sect, name, QString( "%1, %2, %3" ).arg( val.red() ).arg( val.green() ).arg( val.blue() ) );
1161 \brief Sets the font resource value.
1162 \param sect - Resource section name.
1163 \param name - Name of the resource.
1164 \param val - Resource value.
1166 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val )
1169 if ( checkExisting() && value( sect, name, res ) && res == val )
1172 QStringList fontDescr;
1173 fontDescr.append( val.family() );
1175 fontDescr.append( "Bold" );
1177 fontDescr.append( "Italic" );
1178 if ( val.underline() )
1179 fontDescr.append( "Underline" );
1180 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
1182 setResource( sect, name, fontDescr.join( "," ) );
1186 \brief Sets the string resource value.
1187 \param sect - Resource section name.
1188 \param name - Name of the resource.
1189 \param val - Resource value.
1191 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
1194 if ( checkExisting() && value( sect, name, res ) && res == val )
1197 setResource( sect, name, val );
1201 \brief Remove the all specified resource section.
1202 \param sect - Resource section name.
1204 void QtxResourceMgr::remove( const QString& sect )
1208 for ( ResListIterator it( myResources ); it.current(); ++it )
1209 it.current()->removeSection( sect );
1213 \brief Remove the specified resource.
1214 \param sect - Resource section name.
1215 \param name - Name of the resource.
1217 void QtxResourceMgr::remove( const QString& sect, const QString& name )
1221 for ( ResListIterator it( myResources ); it.current(); ++it )
1222 it.current()->removeValue( sect, name );
1226 \brief Returns the current format which operates with resource files.
1228 QString QtxResourceMgr::currentFormat() const
1231 if ( !myFormats.isEmpty() )
1232 fmt = myFormats.getFirst()->format();
1237 \brief Sets the current format which operates with resource files.
1238 \param fmt - Resource format name.
1240 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
1242 Format* form = format( fmt );
1246 myFormats.remove( form );
1247 myFormats.prepend( form );
1249 if ( myResources.isEmpty() )
1252 ResListIterator resIt( myResources );
1253 if ( myResources.count() > myDirList.count() && resIt.current() ) {
1254 resIt.current()->setFile( userFileName( appName() ) );
1258 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt )
1259 resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
1263 \brief Returns the resource format object by it name.
1264 \param fmt - Resource format name.
1266 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
1269 for ( FormatListIterator it( myFormats ); it.current() && !form; ++it )
1271 if ( it.current()->format() == fmt )
1272 form = it.current();
1279 \brief Add the resource format to the manager. Newly added become current.
1280 \param form - Resource format object.
1282 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
1284 if ( !myFormats.contains( form ) )
1285 myFormats.prepend( form );
1289 \brief Remove the resource format from the manager.
1290 \param form - Resource format object.
1292 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
1294 myFormats.remove( form );
1298 \brief Returns the string list of the resource format options names.
1300 QStringList QtxResourceMgr::options() const
1302 return myOptions.keys();
1306 \brief Returns the string value for the specified option. If option doesn't exist
1307 then empty string will be returned.
1308 \param opt - Option name.
1310 QString QtxResourceMgr::option( const QString& opt ) const
1313 if ( myOptions.contains( opt ) )
1314 val = myOptions[opt];
1319 \brief Sets the string value for the specified option.
1320 \param opt - Option name.
1321 \param val - Option value.
1323 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
1325 myOptions.insert( opt, val );
1329 \brief Load the all resources from the resource files.
1331 bool QtxResourceMgr::load()
1333 initialize( false );
1335 Format* fmt = format( currentFormat() );
1340 for ( ResListIterator it( myResources ); it.current(); ++it )
1341 res = fmt->load( it.current() ) && res;
1347 \brief Save the changed resources in to the user resource file.
1349 bool QtxResourceMgr::save()
1351 initialize( false );
1353 Format* fmt = format( currentFormat() );
1357 if ( myResources.isEmpty() )
1360 return fmt->save( myResources.getFirst() );
1364 \brief Returns the string list of the existing section names..
1366 QStringList QtxResourceMgr::sections() const
1370 QMap<QString, int> map;
1371 for ( ResListIterator it( myResources ); it.current(); ++it )
1373 QStringList lst = it.current()->sections();
1374 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1375 map.insert( *itr, 0 );
1379 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1380 res.append( iter.key() );
1386 \brief Returns the string list of the existing resource names in the specified section.
1387 \param sec - Resource section name.
1389 QStringList QtxResourceMgr::parameters( const QString& sec ) const
1393 #if defined(QTX_NO_INDEXED_MAP)
1394 typedef QMap<QString, int> PMap;
1396 typedef IMap<QString, int> PMap;
1399 ResListIterator it( myResources );
1401 for ( ; it.current(); --it ) {
1402 QStringList lst = it.current()->parameters( sec );
1403 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1404 pmap.insert( *itr, 0, false );
1408 for ( PMap::ConstIterator iter = pmap.begin(); iter != pmap.end(); ++iter )
1409 res.append( iter.key() );
1414 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
1417 for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it )
1418 res = it.current()->path( sect, prefix, name );
1422 QString QtxResourceMgr::resSection() const
1424 QString res = option( "res_section_name" );
1425 if ( res.isEmpty() )
1426 res = QString( "resources" );
1430 QString QtxResourceMgr::langSection() const
1432 QString res = option( "lang_section_name" );
1433 if ( res.isEmpty() )
1434 res = QString( "language" );
1438 QPixmap QtxResourceMgr::defaultPixmap() const
1440 return myDefaultPix;
1443 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
1448 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
1450 return loadPixmap( prefix, name, true );
1453 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
1455 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
1458 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
1463 for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it )
1464 pix = it.current()->loadPixmap( resSection(), prefix, name );
1470 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
1474 QMap<QChar, QString> substMap;
1475 substMap.insert( 'A', appName() );
1478 if ( lang.isEmpty() )
1479 value( langSection(), "language", lang );
1481 if ( lang.isEmpty() )
1483 lang = QString( "en" );
1484 qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) );
1487 substMap.insert( 'L', lang );
1490 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
1492 QStringList translators = QStringList::split( "|", option( "translators" ) );
1493 QStringList newTranslators = QStringList::split( "|", trs );
1494 for ( uint i = 0; i < newTranslators.count(); i++ )
1495 if ( translators.find( newTranslators[i] ) == translators.end() )
1496 translators += newTranslators[i];
1497 setOption( "translators", translators.join( "|" ) );
1500 QStringList trList = QStringList::split( "|", option( "translators" ) );
1501 if ( trList.isEmpty() )
1503 trList.append( "%P_msg_%L.qm" );
1504 qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) );
1507 QStringList prefixList;
1508 if ( !pref.isEmpty() )
1509 prefixList.append( pref );
1511 prefixList = parameters( resSection() );
1513 for ( QStringList::const_iterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
1515 QString prefix = *iter;
1516 substMap.insert( 'P', prefix );
1519 for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it )
1520 trs.append( substMacro( *it, substMap ).stripWhiteSpace() );
1522 loadTranslators( prefix, trs );
1526 void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators )
1530 QTranslator* trans = 0;
1531 ResListIterator it( myResources );
1533 for ( ; it.current(); --it )
1535 for ( QStringList::const_iterator itr = translators.begin(); itr != translators.end(); ++itr )
1537 trans = it.current()->loadTranslator( resSection(), prefix, *itr );
1540 if ( !myTranslator[prefix].contains( trans ) )
1541 myTranslator[prefix].append( trans );
1542 qApp->installTranslator( trans );
1548 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
1552 QTranslator* trans = 0;
1553 ResListIterator it( myResources );
1555 for ( ; it.current(); --it )
1557 trans = it.current()->loadTranslator( resSection(), prefix, name );
1560 if ( !myTranslator[prefix].contains( trans ) )
1561 myTranslator[prefix].append( trans );
1562 qApp->installTranslator( trans );
1567 void QtxResourceMgr::removeTranslators( const QString& prefix )
1569 if ( !myTranslator.contains( prefix ) )
1572 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1574 qApp->removeTranslator( it.current() );
1575 delete it.current();
1578 myTranslator.remove( prefix );
1581 void QtxResourceMgr::raiseTranslators( const QString& prefix )
1583 if ( !myTranslator.contains( prefix ) )
1586 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1588 qApp->removeTranslator( it.current() );
1589 qApp->installTranslator( it.current() );
1593 void QtxResourceMgr::refresh()
1595 QStringList sl = sections();
1596 for ( QStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
1598 QStringList pl = parameters( *it );
1599 for ( QStringList::const_iterator itr = pl.begin(); itr != pl.end(); ++itr )
1600 setResource( *it, *itr, stringValue( *it, *itr ) );
1604 void QtxResourceMgr::setDirList( const QStringList& dl )
1607 for ( ResListIterator it( myResources ); it.current(); ++it )
1608 delete it.current();
1610 myResources.clear();
1613 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
1617 if ( !myResources.isEmpty() )
1618 myResources.first()->setValue( sect, name, val );
1621 QString QtxResourceMgr::userFileName( const QString& appName ) const
1624 QString pathName = QDir::homeDirPath();
1627 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1629 fileName = QString( ".%1rc" ).arg( appName );
1632 if ( !fileName.isEmpty() )
1633 pathName = Qtx::addSlash( pathName ) + fileName;
1638 QString QtxResourceMgr::globalFileName( const QString& appName ) const
1640 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1643 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
1647 QRegExp rx( "%[A-Za-z%]" );
1650 while ( ( idx = rx.search( trg, idx ) ) >= 0 )
1652 QChar spec = trg.at( idx + 1 );
1656 else if ( substMap.contains( spec ) )
1657 subst = substMap[spec];
1659 if ( !subst.isEmpty() )
1661 trg.replace( idx, rx.matchedLength(), subst );
1662 idx += subst.length();
1665 idx += rx.matchedLength();