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 QString& fileName )
40 : myFileName( fileName )
44 QtxResourceMgr::Resources::~Resources()
48 QString QtxResourceMgr::Resources::file() const
53 void QtxResourceMgr::Resources::setFile( const QString& fn )
58 QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const
62 if ( hasValue( sect, name ) )
64 val = section( sect )[name];
66 val = makeSubstitution( val, sect, name );
71 void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val )
73 Section& s = section( sect );
74 s.insert( name, val );
77 bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const
79 return mySections.contains( sect );
82 bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const
84 return hasSection( sect ) && section( sect ).contains( name );
87 void QtxResourceMgr::Resources::removeSection( const QString& sect )
89 mySections.remove( sect );
92 void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name )
94 if ( !hasSection( sect ) )
97 Section& s = section( sect );
101 mySections.remove( sect );
104 void QtxResourceMgr::Resources::clear()
109 QStringList QtxResourceMgr::Resources::sections() const
111 return mySections.keys();
114 QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const
116 if ( !hasSection( sec ) )
117 return QStringList();
119 return section( sec ).keys();
122 QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const
124 QString filePath = fileName( sec, prefix, name );
125 if ( !filePath.isEmpty() )
127 if ( !QFileInfo( filePath ).exists() )
128 filePath = QString::null;
133 QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn )
135 if ( !mySections.contains( sn ) )
136 mySections.insert( sn, Section() );
138 return mySections[sn];
141 const QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) const
143 return mySections[sn];
146 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
149 if ( hasValue( sect, prefix ) )
151 path = value( sect, prefix, true );
152 if ( !path.isEmpty() )
154 if ( QFileInfo( path ).isRelative() )
155 path = Qtx::addSlash( QFileInfo( myFileName ).dirPath( true ) ) + path;
157 path = Qtx::addSlash( path ) + name;
160 return QDir::convertSeparators( path );
163 QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
165 return QPixmap( fileName( sect, prefix, name ) );
168 QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const
170 QTranslator* trans = new QTranslator( 0 );
171 if ( !trans->load( fileName( sect, prefix, name ) ) )
179 QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const
181 QString varName = QString::null;
184 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_]*)\\%" );
186 int pos = rx.search( str, start );
190 len = rx.matchedLength();
191 QStringList caps = rx.capturedTexts();
192 for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
193 varName = *caps.at( i );
198 QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const
202 QMap<QString, int> ignoreMap;
203 ignoreMap.insert( name, 0 );
205 int start( 0 ), len( 0 );
208 QString envName = environmentVariable( res, start, len );
209 if ( envName.isNull() )
212 QString newStr = QString::null;
213 if ( ::getenv( envName ) )
214 newStr = QString( ::getenv( envName ) );
216 if ( newStr.isNull() )
218 if ( ignoreMap.contains( envName ) )
224 if ( hasValue( sect, envName ) )
225 newStr = value( sect, envName, false );
226 ignoreMap.insert( envName, 0 );
228 res.replace( start, len, newStr );
235 Class: QtxResourceMgr::IniFormat
239 class QtxResourceMgr::IniFormat : public Format
246 virtual bool load( const QString&, QMap<QString, Section>& );
247 virtual bool save( const QString&, const QMap<QString, Section>& );
250 QtxResourceMgr::IniFormat::IniFormat()
255 QtxResourceMgr::IniFormat::~IniFormat()
259 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
262 if ( !file.open( IO_ReadOnly ) )
265 QTextStream ts( &file );
272 QString separator = option( "separator" );
273 if ( separator.isNull() )
274 separator = QString( "=" );
276 QString comment = option( "comment" );
277 if ( comment.isNull() )
278 comment = QString( "#" );
282 data = ts.readLine();
288 data = data.stripWhiteSpace();
289 if ( data.isEmpty() )
292 if ( data.startsWith( comment ) )
295 QRegExp rx( "^\\[([\\w\\s\\._]*)\\]$" );
296 if ( rx.search( data ) != -1 )
298 section = rx.cap( 1 );
299 if ( section.isEmpty() )
302 qWarning( QString( "Empty section in line %1" ).arg( line ) );
305 else if ( data.contains( "=" ) && !section.isEmpty() )
307 int pos = data.find( separator );
308 QString key = data.left( pos ).stripWhiteSpace();
309 QString val = data.mid( pos + 1 ).stripWhiteSpace();
310 secMap[section].insert( key, val );
315 section.isEmpty() ? qWarning( "Current section is empty" ) :
316 qWarning( QString( "Error in line: %1" ).arg( line ) );
325 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
328 if ( !file.open( IO_WriteOnly ) )
332 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
334 QString data = QString( "[%1]\n" ).arg( it.key() );
335 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
336 data += iter.key() + " = " + iter.data() + "\n";
339 res = file.writeBlock( data.latin1(), data.length() ) == (int)data.length();
348 Class: QtxResourceMgr::XmlFormat
352 class QtxResourceMgr::XmlFormat : public Format
359 virtual bool load( const QString&, QMap<QString, Section>& );
360 virtual bool save( const QString&, const QMap<QString, Section>& );
363 QString docTag() const;
364 QString sectionTag() const;
365 QString parameterTag() const;
366 QString nameAttribute() const;
367 QString valueAttribute() const;
370 QtxResourceMgr::XmlFormat::XmlFormat()
375 QtxResourceMgr::XmlFormat::~XmlFormat()
379 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
386 if ( !file.open( IO_ReadOnly ) )
391 res = doc.setContent( &file );
397 QDomElement root = doc.documentElement();
398 if ( root.isNull() || root.tagName() != docTag() )
401 QDomNode sectNode = root.firstChild();
402 while ( res && !sectNode.isNull() )
404 res = sectNode.isElement();
407 QDomElement sectElem = sectNode.toElement();
408 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
410 QString section = sectElem.attribute( nameAttribute() );
411 QDomNode paramNode = sectNode.firstChild();
412 while ( res && !paramNode.isNull() )
414 res = paramNode.isElement();
417 QDomElement paramElem = paramNode.toElement();
418 if ( paramElem.tagName() == parameterTag() &&
419 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
421 QString paramName = paramElem.attribute( nameAttribute() );
422 QString paramValue = paramElem.attribute( valueAttribute() );
424 secMap[section].insert( paramName, paramValue );
430 res = paramNode.isComment();
432 paramNode = paramNode.nextSibling();
439 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
441 sectNode = sectNode.nextSibling();
449 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
456 if ( !file.open( IO_WriteOnly ) )
459 QDomDocument doc( docTag() );
460 QDomElement root = doc.createElement( docTag() );
461 doc.appendChild( root );
463 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
465 QDomElement sect = doc.createElement( sectionTag() );
466 sect.setAttribute( nameAttribute(), it.key() );
467 root.appendChild( sect );
468 for ( Section::ConstIterator iter = it.data().begin(); iter != it.data().end(); ++iter )
470 QDomElement val = doc.createElement( parameterTag() );
471 val.setAttribute( nameAttribute(), iter.key() );
472 val.setAttribute( valueAttribute(), iter.data() );
473 sect.appendChild( val );
477 QString docStr = doc.toString();
478 res = file.writeBlock( docStr.latin1(), docStr.length() ) == (int)docStr.length();
486 QString QtxResourceMgr::XmlFormat::docTag() const
488 QString tag = option( "doc_tag" );
490 tag = QString( "document" );
494 QString QtxResourceMgr::XmlFormat::sectionTag() const
496 QString tag = option( "section_tag" );
498 tag = QString( "section" );
502 QString QtxResourceMgr::XmlFormat::parameterTag() const
504 QString tag = option( "parameter_tag" );
506 tag = QString( "parameter" );
510 QString QtxResourceMgr::XmlFormat::nameAttribute() const
512 QString str = option( "name_attribute" );
514 str = QString( "name" );
518 QString QtxResourceMgr::XmlFormat::valueAttribute() const
520 QString str = option( "value_attribute" );
522 str = QString( "value" );
527 Class: QtxResourceMgr::Format
532 \brief Constructs the format object with specified name.
533 \param fmt - name of the format
535 QtxResourceMgr::Format::Format( const QString& fmt )
541 \brief Destructs the format object.
543 QtxResourceMgr::Format::~Format()
548 \brief Returns the format name.
550 QString QtxResourceMgr::Format::format() const
556 \brief Returns the string list of the format options.
558 QStringList QtxResourceMgr::Format::options() const
564 \brief Returns the value of the option with specified name.
565 If option doesn't exist then empty string returned.
566 \param opt - name of the option
568 QString QtxResourceMgr::Format::option( const QString& opt ) const
571 if ( myOpt.contains( opt ) )
577 \brief Sets the value of the option with specified name.
578 \param opt - name of the option
579 \param opt - value of the option
581 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
583 myOpt.insert( opt, val );
587 \brief Perform the loading of the resources from resource file.
588 \param res - resources object which will be loaded
590 bool QtxResourceMgr::Format::load( Resources* res )
595 QMap<QString, Section> sections;
596 bool status = load( res->myFileName, sections );
598 res->mySections = sections;
600 qDebug( "QtxResourceMgr: Could not load resource file \"%s\"", res->myFileName.latin1() );
606 \brief Perform the saving of the resources into resource file.
607 \param res - resources object which will be saved
609 bool QtxResourceMgr::Format::save( Resources* res )
614 Qtx::mkDir( Qtx::dir( res->myFileName ) );
616 return save( res->myFileName, res->mySections );
620 Class: QtxResourceMgr
625 \brief Constructs the resource manager object for application.
626 \param appName - name of the application which resources will be used.
627 \param resVarTemplate - template for the resource environment variable name which
628 should point to the resource directory list.
629 Default value is "%1Resources". Its mean that for application
630 with name "MyApp" environment variable "MyAppResources" will
631 be used. Template may not have the parameter '%1' substituted
632 by application name. In this case this string will be used as
633 is without substitution.
634 Resource environment variable should contains one or several resource directories
635 separated by symbol ';'. Resource directories list transfered into the setDirList().
636 These directories and the user home directory used for the loading application resources.
637 Each of the resource directories can contains resource file. The name of this file defined
638 by the function globalFileName(). Resource file name in the user home defined by the
639 function userFileName(). Any resource looking firstly in the user home resources then
640 resource directories used in the specified order. All setted resources always stored into
641 the resource file at the user home. Only user home resource file is saved.
643 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
644 : myAppName( appName ),
647 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
648 if ( envVar.contains( "%1" ) )
649 envVar = envVar.arg( appName );
652 if ( ::getenv( envVar ) )
653 dirs = ::getenv( envVar );
655 QString dirsep = ";"; // for Windows: ";" is used as directories separator
657 QString dirsep = "[:|;]"; // for Linux: both ":" and ";" can be used
659 setDirList( QStringList::split( QRegExp(dirsep), dirs ) );
661 installFormat( new XmlFormat() );
662 installFormat( new IniFormat() );
664 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
668 \brief Destructs the resource manager object and free allocated memory.
670 QtxResourceMgr::~QtxResourceMgr()
672 QStringList prefList = myTranslator.keys();
673 for ( QStringList::const_iterator it = prefList.begin(); it != prefList.end(); ++it )
674 removeTranslators( *it );
678 \brief Returns the application name.
680 QString QtxResourceMgr::appName() const
686 \brief Returns the checking of the existance flag. If its 'true' then resource
687 will be setted into the manager only if it doesn't exist or has different
688 value that existing value.
690 bool QtxResourceMgr::checkExisting() const
696 \brief Sets the checking of the existance flag.
697 \param on - boolean value of the flag.
699 void QtxResourceMgr::setCheckExisting( const bool on )
705 \brief Returns the resource directories list except user home directory.
707 QStringList QtxResourceMgr::dirList() const
713 \brief Initialise the manager. Prepare the resource containers and load resources.
714 \param autoLoad - if 'true' then all resources will be loaded.
716 void QtxResourceMgr::initialize( const bool autoLoad ) const
718 if ( !myResources.isEmpty() )
721 QtxResourceMgr* that = (QtxResourceMgr*)this;
723 if ( !userFileName( appName() ).isEmpty() )
724 that->myResources.append( new Resources( userFileName( appName() ) ) );
726 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it )
728 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
729 that->myResources.append( new Resources( path ) );
737 \brief Removes all resources from the manager.
739 void QtxResourceMgr::clear()
741 for ( ResListIterator it( myResources ); it.current(); ++it )
742 it.current()->clear();
746 \brief Get the resource value as integer. Returns 'true' if it successfull otherwise
748 \param sect - Resource section name which contains resource.
749 \param name - Name of the resource.
750 \param iVal - Reference on the variable which should contains the resource output.
752 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
755 if ( !value( sect, name, val, true ) )
759 iVal = val.toInt( &ok );
765 \brief Get the resource value as double. Returns 'true' if it successfull otherwise
767 \param sect - Resource section name which contains resource.
768 \param name - Name of the resource.
769 \param dVal - Reference on the variable which should contains the resource output.
771 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
774 if ( !value( sect, name, val, true ) )
778 dVal = val.toDouble( &ok );
784 \brief Get the resource value as boolean. Returns 'true' if it successfull otherwise
786 \param sect - Resource section name which contains resource.
787 \param name - Name of the resource.
788 \param bVal - Reference on the variable which should contains the resource output.
790 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
793 if ( !value( sect, name, val, true ) )
796 static QMap<QString, bool> boolMap;
797 if ( boolMap.isEmpty() )
799 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
800 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
804 bool res = boolMap.contains( val );
809 double num = val.toDouble( &res );
818 \brief Get the resource value as color. Returns 'true' if it successfull otherwise
820 \param sect - Resource section name which contains resource.
821 \param name - Name of the resource.
822 \param cVal - Reference on the variable which should contains the resource output.
824 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
827 if ( !value( sect, name, val, true ) )
831 QStringList vals = QStringList::split( ",", val, true );
834 for ( QStringList::const_iterator it = vals.begin(); it != vals.end() && res; ++it )
835 nums.append( (*it).toInt( &res ) );
837 if ( res && nums.count() >= 3 )
838 cVal.setRgb( nums[0], nums[1], nums[2] );
841 int pack = val.toInt( &res );
843 Qtx::rgbSet( pack, cVal );
850 \brief Get the resource value as font. Returns 'true' if it successfull otherwise
852 \param sect - Resource section name which contains resource.
853 \param name - Name of the resource.
854 \param fVal - Reference on the variable which should contains the resource output.
856 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
859 if ( !value( sect, name, val, true ) )
862 QStringList fontDescr = QStringList::split( ",", val );
864 if ( fontDescr.count() < 2 )
867 QString family = fontDescr[0];
868 if ( family.isEmpty() )
871 fVal = QFont( family );
873 for ( int i = 1; i < (int)fontDescr.count(); i++ )
875 QString curval = fontDescr[i].stripWhiteSpace().lower();
876 if ( curval == QString( "bold" ) )
877 fVal.setBold( true );
878 else if ( curval == QString( "italic" ) )
879 fVal.setItalic( true );
880 else if ( curval == QString( "underline" ) )
881 fVal.setUnderline( true );
885 int ps = curval.toInt( &isOk );
887 fVal.setPointSize( ps );
895 \brief Get the resource value as string (native format). Returns 'true' if it
896 successfull otherwise returns 'false'.
897 \param sect - Resource section name which contains resource.
898 \param name - Name of the resource.
899 \param val - Reference on the variable which should contains the resource output.
900 \param subst - If 'true' then manager substitute reference on environment variables
901 and other resources by thier values. Default value of this parameter
904 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
909 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
911 ok = it.current()->hasValue( sect, name );
913 val = it.current()->value( sect, name, subst );
920 \brief Returns the integer resource value. If resource can not be found or converted
921 then specified default value will be returned.
922 \param sect - Resource section name which contains resource.
923 \param name - Name of the resource.
924 \param def - Default resource value which will be used when resource not found.
926 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
929 if ( !value( sect, name, val ) )
935 \brief Returns the double resource value. If resource can not be found or converted
936 then specified default value will be returned.
937 \param sect - Resource section name which contains resource.
938 \param name - Name of the resource.
939 \param def - Default resource value which will be used when resource not found.
941 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
944 if ( !value( sect, name, val ) )
950 \brief Returns the boolean resource value. If resource can not be found or converted
951 then specified default value will be returned.
952 \param sect - Resource section name which contains resource.
953 \param name - Name of the resource.
954 \param def - Default resource value which will be used when resource not found.
956 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
959 if ( !value( sect, name, val ) )
965 \brief Returns the font resource value. If resource can not be found or converted
966 then specified default value will be returned.
967 \param sect - Resource section name which contains resource.
968 \param name - Name of the resource.
969 \param def - Default resource value which will be used when resource not found.
971 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
974 if( !value( sect, name, font ) )
980 \brief Returns the color 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 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
989 if ( !value( sect, name, val ) )
995 \brief Returns the string 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 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def ) const
1004 if ( !value( sect, name, val ) )
1010 \brief Checks existance of the specified resource.
1011 \param sect - Resource section name which contains resource.
1012 \param name - Name of the resource.
1014 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
1019 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1020 ok = it.current()->hasValue( sect, name );
1026 \brief Checks existance of the specified resource section.
1027 \param sect - Resource section name which contains resource.
1029 bool QtxResourceMgr::hasSection( const QString& sect ) const
1034 for ( ResListIterator it( myResources ); it.current() && !ok; ++it )
1035 ok = it.current()->hasSection( sect );
1041 \brief Sets the integer resource value.
1042 \param sect - Resource section name.
1043 \param name - Name of the resource.
1044 \param val - Resource value.
1046 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1049 if ( checkExisting() && value( sect, name, res ) && res == val )
1052 setResource( sect, name, QString::number( val ) );
1056 \brief Sets the double resource value.
1057 \param sect - Resource section name.
1058 \param name - Name of the resource.
1059 \param val - Resource value.
1061 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1064 if ( checkExisting() && value( sect, name, res ) && res == val )
1067 setResource( sect, name, QString::number( val, 'g', 12 ) );
1071 \brief Sets the boolean resource value.
1072 \param sect - Resource section name.
1073 \param name - Name of the resource.
1074 \param val - Resource value.
1076 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1079 if ( checkExisting() && value( sect, name, res ) && res == val )
1082 setResource( sect, name, QString( val ? "true" : "false" ) );
1086 \brief Sets the color resource value.
1087 \param sect - Resource section name.
1088 \param name - Name of the resource.
1089 \param val - Resource value.
1091 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
1094 if ( checkExisting() && value( sect, name, res ) && res == val )
1097 setResource( sect, name, QString( "%1, %2, %3" ).arg( val.red() ).arg( val.green() ).arg( val.blue() ) );
1101 \brief Sets the font 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, const QFont& val )
1109 if ( checkExisting() && value( sect, name, res ) && res == val )
1112 QStringList fontDescr;
1113 fontDescr.append( val.family() );
1115 fontDescr.append( "Bold" );
1117 fontDescr.append( "Italic" );
1118 if ( val.underline() )
1119 fontDescr.append( "Underline" );
1120 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
1122 setResource( sect, name, fontDescr.join( "," ) );
1126 \brief Sets the string resource value.
1127 \param sect - Resource section name.
1128 \param name - Name of the resource.
1129 \param val - Resource value.
1131 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
1134 if ( checkExisting() && value( sect, name, res ) && res == val )
1137 setResource( sect, name, val );
1141 \brief Remove the all specified resource section.
1142 \param sect - Resource section name.
1144 void QtxResourceMgr::remove( const QString& sect )
1148 for ( ResListIterator it( myResources ); it.current(); ++it )
1149 it.current()->removeSection( sect );
1153 \brief Remove the specified resource.
1154 \param sect - Resource section name.
1155 \param name - Name of the resource.
1157 void QtxResourceMgr::remove( const QString& sect, const QString& name )
1161 for ( ResListIterator it( myResources ); it.current(); ++it )
1162 it.current()->removeValue( sect, name );
1166 \brief Returns the current format which operates with resource files.
1168 QString QtxResourceMgr::currentFormat() const
1171 if ( !myFormats.isEmpty() )
1172 fmt = myFormats.getFirst()->format();
1177 \brief Sets the current format which operates with resource files.
1178 \param fmt - Resource format name.
1180 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
1182 Format* form = format( fmt );
1186 myFormats.remove( form );
1187 myFormats.prepend( form );
1189 if ( myResources.isEmpty() )
1192 ResListIterator resIt( myResources );
1193 if ( myResources.count() > myDirList.count() && resIt.current() ) {
1194 resIt.current()->setFile( userFileName( appName() ) );
1198 for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end() && resIt.current(); ++it, ++resIt )
1199 resIt.current()->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
1203 \brief Returns the resource format object by it name.
1204 \param fmt - Resource format name.
1206 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
1209 for ( FormatListIterator it( myFormats ); it.current() && !form; ++it )
1211 if ( it.current()->format() == fmt )
1212 form = it.current();
1219 \brief Add the resource format to the manager. Newly added become current.
1220 \param form - Resource format object.
1222 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
1224 if ( !myFormats.contains( form ) )
1225 myFormats.prepend( form );
1229 \brief Remove the resource format from the manager.
1230 \param form - Resource format object.
1232 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
1234 myFormats.remove( form );
1238 \brief Returns the string list of the resource format options names.
1240 QStringList QtxResourceMgr::options() const
1242 return myOptions.keys();
1246 \brief Returns the string value for the specified option. If option doesn't exist
1247 then empty string will be returned.
1248 \param opt - Option name.
1250 QString QtxResourceMgr::option( const QString& opt ) const
1253 if ( myOptions.contains( opt ) )
1254 val = myOptions[opt];
1259 \brief Sets the string value for the specified option.
1260 \param opt - Option name.
1261 \param val - Option value.
1263 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
1265 myOptions.insert( opt, val );
1269 \brief Load the all resources from the resource files.
1271 bool QtxResourceMgr::load()
1273 initialize( false );
1275 Format* fmt = format( currentFormat() );
1280 for ( ResListIterator it( myResources ); it.current(); ++it )
1281 res = fmt->load( it.current() ) && res;
1287 \brief Save the changed resources in to the user resource file.
1289 bool QtxResourceMgr::save()
1291 initialize( false );
1293 Format* fmt = format( currentFormat() );
1297 if ( myResources.isEmpty() )
1300 return fmt->save( myResources.getFirst() );
1304 \brief Returns the string list of the existing section names..
1306 QStringList QtxResourceMgr::sections() const
1310 QMap<QString, int> map;
1311 for ( ResListIterator it( myResources ); it.current(); ++it )
1313 QStringList lst = it.current()->sections();
1314 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1315 map.insert( *itr, 0 );
1319 for ( QMap<QString, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
1320 res.append( iter.key() );
1326 \brief Returns the string list of the existing resource names in the specified section.
1327 \param sec - Resource section name.
1329 QStringList QtxResourceMgr::parameters( const QString& sec ) const
1333 #if defined(QTX_NO_INDEXED_MAP)
1334 typedef QMap<QString, int> PMap;
1336 typedef IMap<QString, int> PMap;
1339 ResListIterator it( myResources );
1341 for ( ; it.current(); --it ) {
1342 QStringList lst = it.current()->parameters( sec );
1343 for ( QStringList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
1344 pmap.insert( *itr, 0, false );
1348 for ( PMap::ConstIterator iter = pmap.begin(); iter != pmap.end(); ++iter )
1349 res.append( iter.key() );
1354 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
1357 for ( ResListIterator it( myResources ); it.current() && res.isEmpty(); ++it )
1358 res = it.current()->path( sect, prefix, name );
1362 QString QtxResourceMgr::resSection() const
1364 QString res = option( "res_section_name" );
1365 if ( res.isEmpty() )
1366 res = QString( "resources" );
1370 QString QtxResourceMgr::langSection() const
1372 QString res = option( "lang_section_name" );
1373 if ( res.isEmpty() )
1374 res = QString( "language" );
1378 QPixmap QtxResourceMgr::defaultPixmap() const
1380 return myDefaultPix;
1383 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
1388 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
1390 return loadPixmap( prefix, name, true );
1393 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
1395 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
1398 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
1403 for ( ResListIterator it( myResources ); it.current() && pix.isNull(); ++it )
1404 pix = it.current()->loadPixmap( resSection(), prefix, name );
1410 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
1414 QMap<QChar, QString> substMap;
1415 substMap.insert( 'A', appName() );
1418 if ( lang.isEmpty() )
1419 value( langSection(), "language", lang );
1421 if ( lang.isEmpty() )
1423 lang = QString( "en" );
1424 qWarning( QString( "Language not specified. Assumed: %1" ).arg( lang ) );
1427 substMap.insert( 'L', lang );
1430 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
1432 QStringList translators = QStringList::split( "|", option( "translators" ) );
1433 QStringList newTranslators = QStringList::split( "|", trs );
1434 for ( uint i = 0; i < newTranslators.count(); i++ )
1435 if ( translators.find( newTranslators[i] ) == translators.end() )
1436 translators += newTranslators[i];
1437 setOption( "translators", translators.join( "|" ) );
1440 QStringList trList = QStringList::split( "|", option( "translators" ) );
1441 if ( trList.isEmpty() )
1443 trList.append( "%P_msg_%L.qm" );
1444 qWarning( QString( "Translators not defined. Assumed: %1" ).arg( trList.first() ) );
1447 QStringList prefixList;
1448 if ( !pref.isEmpty() )
1449 prefixList.append( pref );
1451 prefixList = parameters( resSection() );
1453 for ( QStringList::const_iterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
1455 QString prefix = *iter;
1456 substMap.insert( 'P', prefix );
1459 for ( QStringList::const_iterator it = trList.begin(); it != trList.end(); ++it )
1460 trs.append( substMacro( *it, substMap ).stripWhiteSpace() );
1462 loadTranslators( prefix, trs );
1466 void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators )
1470 QTranslator* trans = 0;
1471 ResListIterator it( myResources );
1473 for ( ; it.current(); --it )
1475 for ( QStringList::const_iterator itr = translators.begin(); itr != translators.end(); ++itr )
1477 trans = it.current()->loadTranslator( resSection(), prefix, *itr );
1480 if ( !myTranslator[prefix].contains( trans ) )
1481 myTranslator[prefix].append( trans );
1482 qApp->installTranslator( trans );
1488 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
1492 QTranslator* trans = 0;
1493 ResListIterator it( myResources );
1495 for ( ; it.current(); --it )
1497 trans = it.current()->loadTranslator( resSection(), prefix, name );
1500 if ( !myTranslator[prefix].contains( trans ) )
1501 myTranslator[prefix].append( trans );
1502 qApp->installTranslator( trans );
1507 void QtxResourceMgr::removeTranslators( const QString& prefix )
1509 if ( !myTranslator.contains( prefix ) )
1512 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1514 qApp->removeTranslator( it.current() );
1515 delete it.current();
1518 myTranslator.remove( prefix );
1521 void QtxResourceMgr::raiseTranslators( const QString& prefix )
1523 if ( !myTranslator.contains( prefix ) )
1526 for ( TransListIterator it( myTranslator[prefix] ); it.current(); ++it )
1528 qApp->removeTranslator( it.current() );
1529 qApp->installTranslator( it.current() );
1533 void QtxResourceMgr::refresh()
1535 QStringList sl = sections();
1536 for ( QStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
1538 QStringList pl = parameters( *it );
1539 for ( QStringList::const_iterator itr = pl.begin(); itr != pl.end(); ++itr )
1540 setResource( *it, *itr, stringValue( *it, *itr ) );
1544 void QtxResourceMgr::setDirList( const QStringList& dl )
1547 for ( ResListIterator it( myResources ); it.current(); ++it )
1548 delete it.current();
1550 myResources.clear();
1553 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
1557 if ( !myResources.isEmpty() )
1558 myResources.first()->setValue( sect, name, val );
1561 QString QtxResourceMgr::userFileName( const QString& appName ) const
1564 QString pathName = QDir::homeDirPath();
1567 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1569 fileName = QString( ".%1rc" ).arg( appName );
1572 if ( !fileName.isEmpty() )
1573 pathName = Qtx::addSlash( pathName ) + fileName;
1578 QString QtxResourceMgr::globalFileName( const QString& appName ) const
1580 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
1583 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
1587 QRegExp rx( "%[A-Za-z%]" );
1590 while ( ( idx = rx.search( trg, idx ) ) >= 0 )
1592 QChar spec = trg.at( idx + 1 );
1596 else if ( substMap.contains( spec ) )
1597 subst = substMap[spec];
1599 if ( !subst.isEmpty() )
1601 trg.replace( idx, rx.matchedLength(), subst );
1602 idx += subst.length();
1605 idx += rx.matchedLength();