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 ) )
48 QtxResourceMgr::Resources::~Resources()
53 Returns name of resource file
54 This file is used to load/save operations
56 QString QtxResourceMgr::Resources::file() const
62 Sets name of resource file
63 \param fn - name of file
65 void QtxResourceMgr::Resources::setFile( const QString& fn )
71 Returns string representation of parameter value
72 Returns QString::null if there is no such parameter
74 \param sect - name of section
75 \param name - name of parameter
76 \param subst - if it is true, then the substitution of variables
77 will be done with help of makeSubstitution method
78 \sa makeSubstitution()
80 QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const
84 if ( hasValue( sect, name ) )
86 val = section( sect )[name];
88 val = makeSubstitution( val, sect, name );
94 Sets value by it's string representation
96 \param sect - name of section
97 \param name - name of parameter
98 \param val - string value
100 void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val )
102 Section& s = section( sect );
103 s.insert( name, val );
107 \return true if section exists
108 \param sect - name of section
110 bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const
112 return mySections.contains( sect );
116 \return true if parameter exists in section
117 \param sect - name of section
118 \param name - name of parameter
120 bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const
122 return hasSection( sect ) && section( sect ).contains( name );
126 Removes section from resources
127 \param sect - name of section
129 void QtxResourceMgr::Resources::removeSection( const QString& sect )
131 mySections.remove( sect );
135 Removes parameter from section
136 \param sect - name of section
137 \param name - name of parameter
139 void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name )
141 if ( !hasSection( sect ) )
144 Section& s = section( sect );
148 mySections.remove( sect );
154 void QtxResourceMgr::Resources::clear()
160 \return list of section names
162 QStringList QtxResourceMgr::Resources::sections() const
164 return mySections.keys();
168 \return list of parameter names from section
169 \param sec - name of section
171 QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const
173 if ( !hasSection( sec ) )
174 return QStringList();
176 return section( sec ).keys();
180 \return path of file from directory built by parameter
181 \return QString::null if built path doesn't exist
182 \param sec - name of section
183 \param prefix - name of parameter containing some path
184 \param name - name of file
186 QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const
188 QString filePath = fileName( sec, prefix, name );
189 if ( !filePath.isEmpty() )
191 if ( !QFileInfo( filePath ).exists() )
192 filePath = QString::null;
198 \return corresponding resource manager
200 QtxResourceMgr* QtxResourceMgr::Resources::resMgr() const
206 \return instance of section by it's name. Section will be created if it doesn't exist
208 QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn )
210 if ( !mySections.contains( sn ) )
211 mySections.insert( sn, Section() );
213 return mySections[sn];
217 \return instance of section by it's name. Section will be created if it doesn't exist
219 const QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) const
221 return mySections[sn];
225 \return full path of file
226 \param sect - name of section
227 \param prefix - name of parameter containing some path
228 \param name - name of file
230 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
233 if ( hasValue( sect, prefix ) )
235 path = value( sect, prefix, true );
236 if ( !path.isEmpty() )
238 if ( QFileInfo( path ).isRelative() )
239 path = Qtx::addSlash( QFileInfo( myFileName ).dirPath( true ) ) + path;
241 path = Qtx::addSlash( path ) + name;
244 if( !path.isEmpty() )
246 QString fname = QDir::convertSeparators( path );
247 QFileInfo inf( fname );
248 fname = inf.absFilePath();
255 \return QPixmap loaded from file
256 \param sect - name of section
257 \param prefix - name of parameter containing some path
258 \param name - name of picture file
260 QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
262 QString fname = fileName( sect, prefix, name );
263 bool toCache = resMgr() ? resMgr()->isPixmapCached() : false;
265 if( toCache && myPixmapCache.contains( fname ) )
266 p = myPixmapCache[fname];
271 ( ( QMap<QString,QPixmap>& )myPixmapCache ).insert( fname, p );
277 \return just created and loaded translator
278 \param sect - name of section
279 \param prefix - name of parameter containing some path
280 \param name - name of file
282 QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const
284 QTranslator* trans = new QTranslator( 0 );
285 if ( !trans->load( fileName( sect, prefix, name ) ) )
294 Finds in string variables by patterns: ${name} or $(name) or %name%
295 \return first found name or QString::null if there is no ones
296 \param str - string where the search is processed
297 \param start - integer value for returning start position of variable
298 \param len - integer value for returning length of variable
300 QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const
302 QString varName = QString::null;
305 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_]*)\\%" );
307 int pos = rx.search( str, start );
311 len = rx.matchedLength();
312 QStringList caps = rx.capturedTexts();
313 for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
314 varName = *caps.at( i );
320 Substitutes variables by its' values. If variable is from enviroment,
321 it will be replaced by environment value. If it isn't, method tries to
322 find it's value among resources
323 \return new variant of string 'str'
324 \param str - string to process substitution
325 \param sect - section, in which the variables will be finding
326 \param name - name of variable which must be ignored during substitution
328 QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const
332 QMap<QString, int> ignoreMap;
333 ignoreMap.insert( name, 0 );
335 int start( 0 ), len( 0 );
338 QString envName = environmentVariable( res, start, len );
339 if ( envName.isNull() )
342 QString newStr = QString::null;
343 if ( ::getenv( envName ) )
344 newStr = QString( ::getenv( envName ) );
346 if ( newStr.isNull() )
348 if ( ignoreMap.contains( envName ) )
354 if ( hasValue( sect, envName ) )
355 newStr = value( sect, envName, false );
356 ignoreMap.insert( envName, 0 );
358 res.replace( start, len, newStr );
365 Class: QtxResourceMgr::IniFormat
368 class QtxResourceMgr::IniFormat : public Format
375 virtual bool load( const QString&, QMap<QString, Section>& );
376 virtual bool save( const QString&, const QMap<QString, Section>& );
382 QtxResourceMgr::IniFormat::IniFormat()
390 QtxResourceMgr::IniFormat::~IniFormat()
395 Loads resources from ini-file to map of sections
396 \param fname - name of resource file
397 \param secMap - map of sections
399 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
402 if ( !file.open( IO_ReadOnly ) )
405 QTextStream ts( &file );
412 QString separator = option( "separator" );
413 if ( separator.isNull() )
414 separator = QString( "=" );
416 QString comment = option( "comment" );
417 if ( comment.isNull() )
418 comment = QString( "#" );
422 data = ts.readLine();
428 data = data.stripWhiteSpace();
429 if ( data.isEmpty() )
432 if ( data.startsWith( comment ) )
435 QRegExp rx( "^\\[([\\w\\s\\._]*)\\]$" );
436 if ( rx.search( data ) != -1 )
438 section = rx.cap( 1 );
439 if ( section.isEmpty() )
442 qWarning( QString( "Empty section in line %1" ).arg( line ) );
445 else if ( data.contains( "=" ) && !section.isEmpty() )
447 int pos = data.find( separator );
448 QString key = data.left( pos ).stripWhiteSpace();
449 QString val = data.mid( pos + 1 ).stripWhiteSpace();
450 secMap[section].insert( key, val );
455 section.isEmpty() ? qWarning( "Current section is empty" ) :
456 qWarning( QString( "Error in line: %1" ).arg( line ) );
466 Saves map of sections to resource ini-file
467 \param fname - name of resource file
468 \param secMap - map of sections
470 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
473 if ( !file.open( IO_WriteOnly ) )
477 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
479 QString data = QString( "[%1]\n" ).arg( it.key() );
480 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
481 data += iter.key() + " = " + iter.data() + "\n";
484 res = file.writeBlock( data.latin1(), data.length() ) == (int)data.length();
493 Class: QtxResourceMgr::XmlFormat
497 class QtxResourceMgr::XmlFormat : public Format
504 virtual bool load( const QString&, QMap<QString, Section>& );
505 virtual bool save( const QString&, const QMap<QString, Section>& );
508 QString docTag() const;
509 QString sectionTag() const;
510 QString parameterTag() const;
511 QString nameAttribute() const;
512 QString valueAttribute() const;
518 QtxResourceMgr::XmlFormat::XmlFormat()
526 QtxResourceMgr::XmlFormat::~XmlFormat()
531 Loads resources from xml-file to map of sections
532 \param fname - name of resource file
533 \param secMap - map of sections
535 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
542 if ( !file.open( IO_ReadOnly ) )
544 qDebug( "File cannot be opened" );
550 res = doc.setContent( &file );
555 qDebug( "File is empty" );
559 QDomElement root = doc.documentElement();
560 if ( root.isNull() || root.tagName() != docTag() )
562 qDebug( "Invalid root" );
566 QDomNode sectNode = root.firstChild();
567 while ( res && !sectNode.isNull() )
569 res = sectNode.isElement();
572 QDomElement sectElem = sectNode.toElement();
573 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
575 QString section = sectElem.attribute( nameAttribute() );
576 QDomNode paramNode = sectNode.firstChild();
577 while ( res && !paramNode.isNull() )
579 res = paramNode.isElement();
582 QDomElement paramElem = paramNode.toElement();
583 if ( paramElem.tagName() == parameterTag() &&
584 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
586 QString paramName = paramElem.attribute( nameAttribute() );
587 QString paramValue = paramElem.attribute( valueAttribute() );
589 secMap[section].insert( paramName, paramValue );
593 qDebug( "Invalid parameter element" );
599 res = paramNode.isComment();
601 qDebug( "Node isn't element nor comment" );
604 paramNode = paramNode.nextSibling();
609 qDebug( "Invalid section" );
615 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
617 qDebug( "Node isn't element nor comment" );
620 sectNode = sectNode.nextSibling();
626 qDebug( QString( "File '%1' is loaded successfully" ).arg( fname ) );
631 Saves map of sections to resource xml-file
632 \param fname - name of resource file
633 \param secMap - map of sections
635 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
642 if ( !file.open( IO_WriteOnly ) )
645 QDomDocument doc( docTag() );
646 QDomElement root = doc.createElement( docTag() );
647 doc.appendChild( root );
649 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
651 QDomElement sect = doc.createElement( sectionTag() );
652 sect.setAttribute( nameAttribute(), it.key() );
653 root.appendChild( sect );
654 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
656 QDomElement val = doc.createElement( parameterTag() );
657 val.setAttribute( nameAttribute(), iter.key() );
658 val.setAttribute( valueAttribute(), iter.data() );
659 sect.appendChild( val );
663 QString docStr = doc.toString();
664 res = file.writeBlock( docStr.latin1(), docStr.length() ) == (int)docStr.length();
673 \return XML tag of document
675 QString QtxResourceMgr::XmlFormat::docTag() const
677 QString tag = option( "doc_tag" );
679 tag = QString( "document" );
684 \return XML tag of section
686 QString QtxResourceMgr::XmlFormat::sectionTag() const
688 QString tag = option( "section_tag" );
690 tag = QString( "section" );
695 \return XML tag of parameter
697 QString QtxResourceMgr::XmlFormat::parameterTag() const
699 QString tag = option( "parameter_tag" );
701 tag = QString( "parameter" );
706 \return XML attribute of parameter name
708 QString QtxResourceMgr::XmlFormat::nameAttribute() const
710 QString str = option( "name_attribute" );
712 str = QString( "name" );
717 \return XML attribute of parameter value
719 QString QtxResourceMgr::XmlFormat::valueAttribute() const
721 QString str = option( "value_attribute" );
723 str = QString( "value" );
728 Class: QtxResourceMgr::Format
733 \brief Constructs the format object with specified name.
734 \param fmt - name of the format
736 QtxResourceMgr::Format::Format( const QString& fmt )
742 \brief Destructs the format object.
744 QtxResourceMgr::Format::~Format()
749 \brief Returns the format name.
751 QString QtxResourceMgr::Format::format() const
757 \brief Returns the string list of the format options.
759 QStringList QtxResourceMgr::Format::options() const
765 \brief Returns the value of the option with specified name.
766 If option doesn't exist then empty string returned.
767 \param opt - name of the option
769 QString QtxResourceMgr::Format::option( const QString& opt ) const
772 if ( myOpt.contains( opt ) )
778 \brief Sets the value of the option with specified name.
779 \param opt - name of the option
780 \param opt - value of the option
782 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
784 myOpt.insert( opt, val );
788 \brief Perform the loading of the resources from resource file.
789 \param res - resources object which will be loaded
791 bool QtxResourceMgr::Format::load( Resources* res )
796 QMap<QString, Section> sections;
797 bool status = load( res->myFileName, sections );
799 res->mySections = sections;
801 qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", res->myFileName.latin1() );
807 \brief Perform the saving of the resources into resource file.
808 \param res - resources object which will be saved
810 bool QtxResourceMgr::Format::save( Resources* res )
815 Qtx::mkDir( Qtx::dir( res->myFileName ) );
817 QtxResourceMgr* mgr = res->resMgr();
818 QString name = mgr ? mgr->userFileName( mgr->appName(), false ) : res->myFileName;
819 return save( name, res->mySections );
823 Class: QtxResourceMgr
828 \brief Constructs the resource manager object for application.
829 \param appName - name of the application which resources will be used.
830 \param resVarTemplate - template for the resource environment variable name which
831 should point to the resource directory list.
832 Default value is "%1Resources". Its mean that for application
833 with name "MyApp" environment variable "MyAppResources" will
834 be used. Template may not have the parameter '%1' substituted
835 by application name. In this case this string will be used as
836 is without substitution.
837 Resource environment variable should contains one or several resource directories
838 separated by symbol ';'. Resource directories list transfered into the setDirList().
839 These directories and the user home directory used for the loading application resources.
840 Each of the resource directories can contains resource file. The name of this file defined
841 by the function globalFileName(). Resource file name in the user home defined by the
842 function userFileName(). Any resource looking firstly in the user home resources then
843 resource directories used in the specified order. All setted resources always stored into
844 the resource file at the user home. Only user home resource file is saved.
845 If you want to ignore of loading of Local User Preferences, you needs setup setIngoreUserValues()
848 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
849 : myAppName( appName ),
850 myCheckExist( true ),
851 myIsPixmapCached( true ),
852 myIsIgnoreUserValues( false )
854 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
855 if ( envVar.contains( "%1" ) )
856 envVar = envVar.arg( appName );
859 if ( ::getenv( envVar ) )
860 dirs = ::getenv( envVar );
862 QString dirsep = ";"; // for Windows: ";" is used as directories separator
864 QString dirsep = "[:|;]"; // for Linux: both ":" and ";" can be used
866 setDirList( QStringList::split( QRegExp(dirsep), dirs ) );
868 installFormat( new XmlFormat() );
869 installFormat( new IniFormat() );
871 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
875 \brief Destructs the resource manager object and free allocated memory.
877 QtxResourceMgr::~QtxResourceMgr()
879 QStringList prefList = myTranslator.keys();
880 for ( QStringList::const_iterator it = prefList.begin(); it != prefList.end(); ++it )
881 removeTranslators( *it );
885 \brief Returns the application name.
887 QString QtxResourceMgr::appName() const
893 \brief Returns the checking of the existance flag. If its 'true' then resource
894 will be setted into the manager only if it doesn't exist or has different
895 value that existing value.
897 bool QtxResourceMgr::checkExisting() const
903 \brief Sets the checking of the existance flag.
904 \param on - boolean value of the flag.
906 void QtxResourceMgr::setCheckExisting( const bool on )
912 \brief Returns the resource directories list except user home directory.
914 QStringList QtxResourceMgr::dirList() const
920 \brief Initialise the manager. Prepare the resource containers and load resources.
921 \param autoLoad - if 'true' then all resources will be loaded.
923 void QtxResourceMgr::initialize( const bool autoLoad ) const
925 if ( !myResources.isEmpty() )
928 QtxResourceMgr* that = (QtxResourceMgr*)this;
930 if ( !userFileName( appName() ).isEmpty() )
931 that->myResources.append( new Resources( this, userFileName( appName() ) ) );
933 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it )
935 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
936 that->myResources.append( new Resources( this, path ) );
944 \brief Return true if all loaded pixmaps are stored in internal map; by default: true
946 bool QtxResourceMgr::isPixmapCached() const
948 return myIsPixmapCached;
952 \brief Set true, if it is necessary to store all loaded pixmap in internal map
953 (it accelerates following calls of loadPixmap)
955 void QtxResourceMgr::setIsPixmapCached( const bool on )
957 myIsPixmapCached = on;
961 \brief Removes all resources from the manager.
963 void QtxResourceMgr::clear()
965 for ( ResListIterator it( myResources ); it.current(); ++it )
966 it.current()->clear();
970 Set state 'ignore user values'.
971 If it is true, then all resources loaded from user home directory is ignored
973 void QtxResourceMgr::setIgnoreUserValues( const bool val )
975 myIsIgnoreUserValues = val;
979 \return state 'ignore user values'
981 bool QtxResourceMgr::ignoreUserValues() const
983 return myIsIgnoreUserValues;
987 \brief Get the resource value as integer. Returns 'true' if it successfull otherwise
989 \param sect - Resource section name which contains resource.
990 \param name - Name of the resource.
991 \param iVal - Reference on the variable which should contains the resource output.
993 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
996 if ( !value( sect, name, val, true ) )
1000 iVal = val.toInt( &ok );
1006 \brief Get the resource value as double. Returns 'true' if it successfull otherwise
1008 \param sect - Resource section name which contains resource.
1009 \param name - Name of the resource.
1010 \param dVal - Reference on the variable which should contains the resource output.
1012 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
1015 if ( !value( sect, name, val, true ) )
1019 dVal = val.toDouble( &ok );
1025 \brief Get the resource value as boolean. Returns 'true' if it successfull otherwise
1027 \param sect - Resource section name which contains resource.
1028 \param name - Name of the resource.
1029 \param bVal - Reference on the variable which should contains the resource output.
1031 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
1034 if ( !value( sect, name, val, true ) )
1037 static QMap<QString, bool> boolMap;
1038 if ( boolMap.isEmpty() )
1040 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
1041 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
1045 bool res = boolMap.contains( val );
1047 bVal = boolMap[val];
1050 double num = val.toDouble( &res );
1059 \brief Get the resource value as color. Returns 'true' if it successfull otherwise
1061 \param sect - Resource section name which contains resource.
1062 \param name - Name of the resource.
1063 \param cVal - Reference on the variable which should contains the resource output.
1065 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
1068 if ( !value( sect, name, val, true ) )
1072 QStringList vals = QStringList::split( ",", val, true );
1075 for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it )
1076 nums.append( (*it).toInt( &res ) );
1078 if ( res && nums.count() >= 3 )
1079 cVal.setRgb( nums[0], nums[1], nums[2] );
1082 int pack = val.toInt( &res );
1084 Qtx::rgbSet( pack, cVal );
1091 \brief Get the resource value as font. Returns 'true' if it successfull otherwise
1093 \param sect - Resource section name which contains resource.
1094 \param name - Name of the resource.
1095 \param fVal - Reference on the variable which should contains the resource output.
1097 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
1100 if ( !value( sect, name, val, true ) )
1103 QStringList fontDescr = QStringList::split( ",", val );
1105 if ( fontDescr.count() < 2 )
1108 QString family = fontDescr[0];
1109 if ( family.isEmpty() )
1112 fVal = QFont( family );
1114 for ( int i = 1; i < (int)fontDescr.count(); i++ )
1116 QString curval = fontDescr[i].stripWhiteSpace().lower();
1117 if ( curval == QString( "bold" ) )
1118 fVal.setBold( true );
1119 else if ( curval == QString( "italic" ) )
1120 fVal.setItalic( true );
1121 else if ( curval == QString( "underline" ) )
1122 fVal.setUnderline( true );
1126 int ps = curval.toInt( &isOk );
1128 fVal.setPointSize( ps );
1136 \brief Get the resource value as string (native format). Returns 'true' if it
1137 successfull otherwise returns 'false'.
1138 \param sect - Resource section name which contains resource.
1139 \param name - Name of the resource.
1140 \param val - Reference on the variable which should contains the resource output.
1141 \param subst - If 'true' then manager substitute reference on environment variables
1142 and other resources by thier values. Default value of this parameter
1145 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
1151 ResListIterator it( myResources );
1152 if ( ignoreUserValues() )
1155 for ( ; it.current() && !ok; ++it )
1157 ok = it.current()->hasValue( sect, name );
1159 val = it.current()->value( sect, name, subst );
1166 \brief Returns the integer resource value. If resource can not be found or converted
1167 then specified default value will be returned.
1168 \param sect - Resource section name which contains resource.
1169 \param name - Name of the resource.
1170 \param def - Default resource value which will be used when resource not found.
1172 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
1175 if ( !value( sect, name, val ) )
1181 \brief Returns the double resource value. If resource can not be found or converted
1182 then specified default value will be returned.
1183 \param sect - Resource section name which contains resource.
1184 \param name - Name of the resource.
1185 \param def - Default resource value which will be used when resource not found.
1187 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
1190 if ( !value( sect, name, val ) )
1196 \brief Returns the boolean resource value. If resource can not be found or converted
1197 then specified default value will be returned.
1198 \param sect - Resource section name which contains resource.
1199 \param name - Name of the resource.
1200 \param def - Default resource value which will be used when resource not found.
1202 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
1205 if ( !value( sect, name, val ) )
1211 \brief Returns the font resource value. If resource can not be found or converted
1212 then specified default value will be returned.
1213 \param sect - Resource section name which contains resource.
1214 \param name - Name of the resource.
1215 \param def - Default resource value which will be used when resource not found.
1217 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
1220 if( !value( sect, name, font ) )
1226 \brief Returns the color resource value. If resource can not be found or converted
1227 then specified default value will be returned.
1228 \param sect - Resource section name which contains resource.
1229 \param name - Name of the resource.
1230 \param def - Default resource value which will be used when resource not found.
1232 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
1235 if ( !value( sect, name, val ) )
1241 \brief Returns the string resource value. If resource can not be found or converted
1242 then specified default value will be returned.
1243 \param sect - Resource section name which contains resource.
1244 \param name - Name of the resource.
1245 \param def - Default resource value which will be used when resource not found.
1247 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def ) const
1250 if ( !value( sect, name, val ) )
1256 \brief Checks existance of the specified resource.
1257 \param sect - Resource section name which contains resource.
1258 \param name - Name of the resource.
1260 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
1265 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1266 ok = it.current()->hasValue( sect, name );
1272 \brief Checks existance of the specified resource section.
1273 \param sect - Resource section name which contains resource.
1275 bool QtxResourceMgr::hasSection( const QString& sect ) const
1280 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1281 ok = it.current()->hasSection( sect );
1287 \brief Sets the integer resource value.
1288 \param sect - Resource section name.
1289 \param name - Name of the resource.
1290 \param val - Resource value.
1292 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1295 if ( checkExisting() && value( sect, name, res ) && res == val )
1298 setResource( sect, name, QString::number( val ) );
1302 \brief Sets the double resource value.
1303 \param sect - Resource section name.
1304 \param name - Name of the resource.
1305 \param val - Resource value.
1307 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1310 if ( checkExisting() && value( sect, name, res ) && res == val )
1313 setResource( sect, name, QString::number( val, 'g', 12 ) );
1317 \brief Sets the boolean resource value.
1318 \param sect - Resource section name.
1319 \param name - Name of the resource.
1320 \param val - Resource value.
1322 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1325 if ( checkExisting() && value( sect, name, res ) && res == val )
1328 setResource( sect, name, QString( val ? "true" : "false" ) );
1332 \brief Sets the color resource value.
1333 \param sect - Resource section name.
1334 \param name - Name of the resource.
1335 \param val - Resource value.
1337 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
1340 if ( checkExisting() && value( sect, name, res ) && res == val )
1343 setResource( sect, name, QString( "%1, %2, %3" ).arg( val.red() ).arg( val.green() ).arg( val.blue() ) );
1347 \brief Sets the font resource value.
1348 \param sect - Resource section name.
1349 \param name - Name of the resource.
1350 \param val - Resource value.
1352 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val )
1355 if ( checkExisting() && value( sect, name, res ) && res == val )
1358 QStringList fontDescr;
1359 fontDescr.append( val.family() );
1361 fontDescr.append( "Bold" );
1363 fontDescr.append( "Italic" );
1364 if ( val.underline() )
1365 fontDescr.append( "Underline" );
1366 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
1368 setResource( sect, name, fontDescr.join( "," ) );
1372 \brief Sets the string resource value.
1373 \param sect - Resource section name.
1374 \param name - Name of the resource.
1375 \param val - Resource value.
1377 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
1380 if ( checkExisting() && value( sect, name, res ) && res == val )
1383 setResource( sect, name, val );
1387 \brief Remove the all specified resource section.
1388 \param sect - Resource section name.
1390 void QtxResourceMgr::remove( const QString& sect )
1394 for ( ResListIterator it( myResources ); it.current(); ++it )
1395 it.current()->removeSection( sect );
1399 \brief Remove the specified resource.
1400 \param sect - Resource section name.
1401 \param name - Name of the resource.
1403 void QtxResourceMgr::remove( const QString& sect, const QString& name )
1407 for ( ResListIterator it( myResources ); it.current(); ++it )
1408 it.current()->removeValue( sect, name );
1412 \brief Returns the current format which operates with resource files.
1414 QString QtxResourceMgr::currentFormat() const
1417 if ( !myFormats.isEmpty() )
1418 fmt = myFormats.getFirst()->format();
1423 \brief Sets the current format which operates with resource files.
1424 \param fmt - Resource format name.
1426 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
1428 Format* form = format( fmt );
1432 myFormats.remove( form );
1433 myFormats.prepend( form );
1435 if ( myResources.isEmpty() )
1438 ResListIterator resIt( myResources );
1439 if ( myResources.count() > myDirList.count() && resIt.current() ) {
1440 resIt.current()->setFile( userFileName( appName() ) );
1444 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt )
1445 resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
1449 \brief Returns the resource format object by it name.
1450 \param fmt - Resource format name.
1452 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
1455 for ( FormatListIterator it( myFormats ); it.current() && !form; ++it )
1457 if ( it.current()->format() == fmt )
1458 form = it.current();
1465 \brief Add the resource format to the manager. Newly added become current.
1466 \param form - Resource format object.
1468 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
1470 if ( !myFormats.contains( form ) )
1471 myFormats.prepend( form );
1475 \brief Remove the resource format from the manager.
1476 \param form - Resource format object.
1478 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
1480 myFormats.remove( form );
1484 \brief Returns the string list of the resource format options names.
1486 QStringList QtxResourceMgr::options() const
1488 return myOptions.keys();
1492 \brief Returns the string value for the specified option. If option doesn't exist
1493 then empty string will be returned.
1494 \param opt - Option name.
1496 QString QtxResourceMgr::option( const QString& opt ) const
1499 if ( myOptions.contains( opt ) )
1500 val = myOptions[opt];
1505 \brief Sets the string value for the specified option.
1506 \param opt - Option name.
1507 \param val - Option value.
1509 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
1511 myOptions.insert( opt, val );
1515 \brief Load the all resources from the resource files.
1517 bool QtxResourceMgr::load()
1519 initialize( false );
1521 Format* fmt = format( currentFormat() );
1526 for ( ResListIterator it( myResources ); it.current(); ++it )
1527 res = fmt->load( it.current() ) && res;
1533 \brief Import some file with resources
1535 bool QtxResourceMgr::import( const QString& fname )
1537 Format* fmt = format( currentFormat() );
1541 Resources* r = myResources.getFirst();
1545 QString old = r->file();
1546 r->setFile( fname );
1547 bool res = fmt->load( r );
1553 \brief Save the changed resources in to the user resource file.
1555 bool QtxResourceMgr::save()
1557 initialize( false );
1559 Format* fmt = format( currentFormat() );
1563 if ( myResources.isEmpty() )
1566 return fmt->save( myResources.getFirst() );
1570 \brief Returns the string list of the existing section names..
1572 QStringList QtxResourceMgr::sections() const
1576 QMap<QString, int> map;
1577 for ( ResListIterator it( myResources ); it.current(); ++it )
1579 QStringList lst = it.current()->sections();
1580 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1581 map.insert( *itr, 0 );
1585 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1586 res.append( iter.key() );
1592 \brief Returns the string list of the existing resource names in the specified section.
1593 \param sec - Resource section name.
1595 QStringList QtxResourceMgr::parameters( const QString& sec ) const
1599 #if defined(QTX_NO_INDEXED_MAP)
1600 typedef QMap<QString, int> PMap;
1602 typedef IMap<QString, int> PMap;
1605 ResListIterator it( myResources );
1607 for ( ; it.current(); --it ) {
1608 QStringList lst = it.current()->parameters( sec );
1609 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1610 pmap.insert( *itr, 0, false );
1614 for ( PMap::ConstIterator iter = pmap.begin(); iter != pmap.end(); ++iter )
1615 res.append( iter.key() );
1621 \return path of file from directory built by parameter
1622 \return QString::null if built path doesn't exist
1623 \param sec - name of section
1624 \param prefix - name of parameter containing some path
1625 \param name - name of file
1627 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
1630 for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it )
1631 res = it.current()->path( sect, prefix, name );
1636 \return section corresponding to resources paths
1638 QString QtxResourceMgr::resSection() const
1640 QString res = option( "res_section_name" );
1641 if ( res.isEmpty() )
1642 res = QString( "resources" );
1647 \return section corresponding to language settings
1649 QString QtxResourceMgr::langSection() const
1651 QString res = option( "lang_section_name" );
1652 if ( res.isEmpty() )
1653 res = QString( "language" );
1658 \return default image used when during loading the image file doesn't exist
1660 QPixmap QtxResourceMgr::defaultPixmap() const
1662 return myDefaultPix;
1666 Set image as default image used when during loading the image file doesn't exist
1669 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
1675 \return image loaded from file
1676 \param prefix - name of parameter containing some path
1677 \param name - name of file
1679 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
1681 return loadPixmap( prefix, name, true );
1685 \return image loaded from file
1686 \param prefix - name of parameter containing some path
1687 \param name - name of file
1688 \param useDef - indicates if it is possible to use default image returning by defaultPixmap() method.
1689 If it is false, the empty pixmap will be used as default
1692 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
1694 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
1698 Finds in all sections an existing path corresponding to 'prefix' parameter
1699 and load image with name 'name' from this folder
1701 \return image loaded from file
1702 \param prefix - name of parameter containing some path
1703 \param name - name of file
1704 \param defPix - default image used when file doesn't exist
1706 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
1711 for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it )
1712 pix = it.current()->loadPixmap( resSection(), prefix, name );
1719 Loads translator for language
1720 Name of translator file is constructed by list returning by option "translators" or,
1721 if it is empty, by predefined pattern "%P_msg_%L.qm". It is recommended to used in translators
1722 name the strings %A, %P, %L whose will be replaced by application name, prefix and language name correspondingly
1724 \param pref - name of parameter containing path to translator's file.
1725 If it is empty, the list of parameters from resource section ( resSection() )
1728 \param l - name of language. If it is empty, then value of parameter "language"
1729 from language section ( langSection() ) is used. If it is also empty, then
1730 predefined name "en" is used
1732 \sa resSection(), langSection()
1734 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
1738 QMap<QChar, QString> substMap;
1739 substMap.insert( 'A', appName() );
1742 if ( lang.isEmpty() )
1743 value( langSection(), "language", lang );
1745 if ( lang.isEmpty() )
1747 lang = QString( "en" );
1748 qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) );
1751 substMap.insert( 'L', lang );
1754 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
1756 QStringList translators = QStringList::split( "|", option( "translators" ) );
1757 QStringList newTranslators = QStringList::split( "|", trs );
1758 for ( uint i = 0; i < newTranslators.count(); i++ )
1759 if ( translators.find( newTranslators[i] ) == translators.end() )
1760 translators += newTranslators[i];
1761 setOption( "translators", translators.join( "|" ) );
1764 QStringList trList = QStringList::split( "|", option( "translators" ) );
1765 if ( trList.isEmpty() )
1767 trList.append( "%P_msg_%L.qm" );
1768 qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) );
1771 QStringList prefixList;
1772 if ( !pref.isEmpty() )
1773 prefixList.append( pref );
1775 prefixList = parameters( resSection() );
1777 for ( QStringList::const_iterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
1779 QString prefix = *iter;
1780 substMap.insert( 'P', prefix );
1783 for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it )
1784 trs.append( substMacro( *it, substMap ).stripWhiteSpace() );
1786 loadTranslators( prefix, trs );
1791 Loads translators by path and list of files
1793 \param prefix - value of this parameter must contain path
1794 \param translators - list of translators' files
1796 void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators )
1800 QTranslator* trans = 0;
1801 ResListIterator it( myResources );
1803 for ( ; it.current(); --it )
1805 for ( QStringList::const_iterator itr = translators.begin(); itr != translators.end(); ++itr )
1807 trans = it.current()->loadTranslator( resSection(), prefix, *itr );
1810 if ( !myTranslator[prefix].contains( trans ) )
1811 myTranslator[prefix].append( trans );
1812 qApp->installTranslator( trans );
1819 Loads translator by path and file name
1821 \param prefix - value of this parameter must contain path
1822 \param name - name of translator file
1824 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
1828 QTranslator* trans = 0;
1829 ResListIterator it( myResources );
1831 for ( ; it.current(); --it )
1833 trans = it.current()->loadTranslator( resSection(), prefix, name );
1836 if ( !myTranslator[prefix].contains( trans ) )
1837 myTranslator[prefix].append( trans );
1838 qApp->installTranslator( trans );
1844 Remove all translators corresponding to prefix
1846 \param prefix - parameter containing path
1848 void QtxResourceMgr::removeTranslators( const QString& prefix )
1850 if ( !myTranslator.contains( prefix ) )
1853 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1855 qApp->removeTranslator( it.current() );
1856 delete it.current();
1859 myTranslator.remove( prefix );
1863 Moves translators corresponding to prefix to the top of translator stack
1865 \param prefix - parameter containing path
1867 void QtxResourceMgr::raiseTranslators( const QString& prefix )
1869 if ( !myTranslator.contains( prefix ) )
1872 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1874 qApp->removeTranslator( it.current() );
1875 qApp->installTranslator( it.current() );
1880 Copies all resources to user resources, so that they will be saved in user home folder
1882 void QtxResourceMgr::refresh()
1884 QStringList sl = sections();
1885 for ( QStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
1887 QStringList pl = parameters( *it );
1888 for ( QStringList::const_iterator itr = pl.begin(); itr != pl.end(); ++itr )
1889 setResource( *it, *itr, stringValue( *it, *itr ) );
1894 \brief Sets the resource directories list except user home directory and clear resources
1896 void QtxResourceMgr::setDirList( const QStringList& dl )
1899 for ( ResListIterator it( myResources ); it.current(); ++it )
1900 delete it.current();
1902 myResources.clear();
1907 \param sect - name of section
1908 \param name - name of parameter
1909 \param val - string representation of value
1911 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
1915 if ( !myResources.isEmpty() )
1916 myResources.first()->setValue( sect, name, val );
1920 \return name of resource file, which is being found in user home directory
1921 \param appName - name of application
1922 \param for_load - flag indicating that file will be used for loading (true) or for saving(false)
1923 It makes possible to use different resource files for loading and saving
1925 QString QtxResourceMgr::userFileName( const QString& appName, const bool /*for_load*/ ) const
1928 QString pathName = QDir::homeDirPath();
1931 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1933 fileName = QString( ".%1rc" ).arg( appName );
1936 if ( !fileName.isEmpty() )
1937 pathName = Qtx::addSlash( pathName ) + fileName;
1943 \return name of resource file, which is being found in all resource directories, except user home
1945 QString QtxResourceMgr::globalFileName( const QString& appName ) const
1947 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1951 Replaced substrings by pattern %A, %B, etc by values from map
1953 \param src - string to be processed
1954 \param substMap - map of values for replacing
1956 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
1960 QRegExp rx( "%[A-Za-z%]" );
1963 while ( ( idx = rx.search( trg, idx ) ) >= 0 )
1965 QChar spec = trg.at( idx + 1 );
1969 else if ( substMap.contains( spec ) )
1970 subst = substMap[spec];
1972 if ( !subst.isEmpty() )
1974 trg.replace( idx, rx.matchedLength(), subst );
1975 idx += subst.length();
1978 idx += rx.matchedLength();