1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: QtxResourceMgr.cxx
24 // Author: Alexander SOLOVYOV, Sergey TELKOV
26 #include "QtxResourceMgr.h"
27 #include "QtxTranslator.h"
34 #include <QTextStream>
35 #include <QApplication>
36 #include <QLibraryInfo>
39 #include <QDomDocument>
40 #include <QDomElement>
46 /* XPM for the default pixmap */
47 static const char* pixmap_not_found_xpm[] = {
70 \class QtxResourceMgr::Resources
72 \brief Represents container for settings read from the resource file.
75 class QtxResourceMgr::Resources
78 typedef QMap<QString, Section> SectionMap;
79 typedef QMap<QString, QString> OptionsMap;
82 Resources( QtxResourceMgr*, const QString& );
86 void setFile( const QString& );
88 QString value( const QString&, const QString&, const bool, const OptionsMap& ) const;
89 void setValue( const QString&, const QString&, const QString& );
91 bool hasSection( const QString& ) const;
92 bool hasValue( const QString&, const QString& ) const;
94 void removeSection( const QString& );
95 void removeValue( const QString&, const QString& );
97 QPixmap loadPixmap( const QString&, const QString&, const QString&, const OptionsMap& ) const;
98 QTranslator* loadTranslator( const QString&, const QString&, const QString&, const OptionsMap& ) const;
102 QStringList sections() const;
103 QStringList parameters( const QString& ) const;
105 QString path( const QString&, const QString&, const QString&, const OptionsMap& ) const;
108 QtxResourceMgr* resMgr() const;
111 Section section( const QString& );
112 const Section section( const QString& ) const;
114 QString makeSubstitution( const QString&, const QString&, const QString&, const OptionsMap& ) const;
116 QString fileName( const QString&, const QString&, const QString&, const OptionsMap& ) const;
119 QtxResourceMgr* myMgr; //!< resources manager
120 SectionMap mySections; //!< sections map
121 QString myFileName; //!< resources file name
122 QMap<QString,QPixmap> myPixmapCache; //!< pixmaps cache
124 friend class QtxResourceMgr::Format;
129 \param mgr parent resources manager
130 \param fileName resources file name
132 QtxResourceMgr::Resources::Resources( QtxResourceMgr* mgr, const QString& fileName )
134 myFileName( fileName )
141 QtxResourceMgr::Resources::~Resources()
146 \brief Get resources file name.
148 This file is used to load/save operations.
153 QString QtxResourceMgr::Resources::file() const
159 \brief Set resources file name.
163 void QtxResourceMgr::Resources::setFile( const QString& fn )
169 \brief Get string representation of parameter value.
170 \param sect section name
171 \param name parameter name
172 \param subst if \c true, perform variables substitution
173 \return parameter value or null QString if there is no such parameter
174 \sa setValue(), makeSubstitution()
176 QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst, const OptionsMap& constants ) const
180 if ( hasValue( sect, name ) )
182 val = section( sect )[name];
184 val = makeSubstitution( val, sect, name, constants );
190 \brief Set parameter value.
191 \param sect section name
192 \param name parameter name
193 \param val parameter value
194 \sa value(), makeSubstitution()
196 void QtxResourceMgr::Resources::setValue( const QString& sect, const QString& name, const QString& val )
198 if ( !mySections.contains( sect ) )
199 mySections.insert( sect, Section() );
201 mySections[sect].insert( name, val );
205 \brief Check section existence.
206 \param sect section name
207 \return \c true if section exists
209 bool QtxResourceMgr::Resources::hasSection( const QString& sect ) const
211 return mySections.contains( sect );
215 \brief Check parameter existence.
216 \param sect section name
217 \param name parameter name
218 \return \c true if parameter exists in specified section
220 bool QtxResourceMgr::Resources::hasValue( const QString& sect, const QString& name ) const
222 return hasSection( sect ) && section( sect ).contains( name );
226 \brief Remove resourcs section.
227 \param sect secton name
229 void QtxResourceMgr::Resources::removeSection( const QString& sect )
231 mySections.remove( sect );
235 \brief Remove parameter from the section.
236 \param sect section name
237 \param name parameter name
239 void QtxResourceMgr::Resources::removeValue( const QString& sect, const QString& name )
241 if ( !mySections.contains( sect ) )
244 mySections[sect].remove( name );
246 if ( mySections[sect].isEmpty() )
247 mySections.remove( sect );
251 \brief Remove all sections.
253 void QtxResourceMgr::Resources::clear()
259 \brief Get all sections names.
260 \return list of section names
262 QStringList QtxResourceMgr::Resources::sections() const
264 return mySections.keys();
268 \brief Get all parameters name in specified section.
269 \param sec section name
270 \return list of settings names
272 QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const
274 if ( !hasSection( sec ) )
275 return QStringList();
277 return section( sec ).keys();
281 \brief Get absolute path to the file which name is defined by the parameter.
283 The file name is defined by \a name argument, while directory name is retrieved
284 from resources parameter \a prefix of section \a sec. Both directory and file name
285 can be relative. If the directory is relative, it is calculated from the initial
286 resources file name (see file()). Directory parameter can contain environment
287 variables, which are substituted automatically.
289 \param sec section name
290 \param prefix parameter containing directory name
291 \param name file name
292 \return absolute file path or null QString if file does not exist
293 \sa fileName(), file(), makeSubstitution()
295 QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name, const OptionsMap& constants ) const
297 QString filePath = fileName( sec, prefix, name, constants );
298 if ( !filePath.isEmpty() )
300 if ( !QFileInfo( filePath ).exists() )
301 filePath = QString();
307 \brief Get resource manager
308 \return resource manager pointer
310 QtxResourceMgr* QtxResourceMgr::Resources::resMgr() const
316 \brief Get resources section by specified name.
318 If section does not exist it is created (empty).
320 \param sn section name
321 \return resources section
323 QtxResourceMgr::Section QtxResourceMgr::Resources::section( const QString& sn )
325 if ( !mySections.contains( sn ) )
326 mySections.insert( sn, Section() );
328 return mySections[sn];
332 \brief Get resources section by specified name.
333 \param sn section name
334 \return resources section
336 const QtxResourceMgr::Section QtxResourceMgr::Resources::section( const QString& sn ) const
338 return mySections[sn];
342 \brief Get file path.
344 The file name is defined by \a name argument, while directory name is retrieved
345 from resources parameter \a prefix of section \a sec. Both directory and file name
346 can be relative. If the directory is relative, it is calculated from the initial
347 resources file name (see file()). Directory parameter can contain environment
348 variables, which are substituted automatically.
349 File existence is not checked.
351 \param sec section name
352 \param prefix parameter containing directory name
353 \param name file name
354 \return absolute file path or null QString if \a prefix parameter
355 does not exist in section \sec
356 \sa path(), file(), makeSubstitution()
358 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name, const OptionsMap& constants ) const
361 if ( !QFileInfo( name ).isRelative() )
367 if ( hasValue( sect, prefix ) )
369 path = value( sect, prefix, true, constants );
370 if ( !path.isEmpty() )
372 if ( QFileInfo( path ).isRelative() )
373 path = Qtx::addSlash( Qtx::dir( myFileName, true ) ) + path;
375 path = Qtx::addSlash( path ) + name;
379 if( !path.isEmpty() )
381 QString fname = QDir::toNativeSeparators( path );
382 QFileInfo inf( fname );
383 fname = inf.absoluteFilePath();
390 \brief Load and return pixmap from external file.
392 If QtxResourceMgr::isPixmapCached() is \c true then cached pixmap is returned
393 (if it is already loaded), otherwise it is loaded from file.
394 If the file name is invalid, null pixmap is returned.
396 \param sect section name
397 \param prefix parameter containing resources directory name
398 \param name pixmap file name
399 \return pixmap loaded from file
401 QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name, const OptionsMap& constants ) const
403 QString fname = fileName( sect, prefix, name, constants );
404 bool toCache = resMgr() ? resMgr()->isPixmapCached() : false;
406 if( toCache && myPixmapCache.contains( fname ) )
407 p = myPixmapCache[fname];
412 ( ( QMap<QString,QPixmap>& )myPixmapCache ).insert( fname, p );
418 \brief Load translator.
419 \param sect section name
420 \param prefix parameter containing resources directory
421 \param name translation file name
422 \return just created and loaded translator or 0 in case of error
424 QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name, const OptionsMap& constants ) const
426 QTranslator* trans = new QtxTranslator( 0 );
427 QString fname = QDir::toNativeSeparators( fileName( sect, prefix, name, constants ) );
428 if ( !trans->load( Qtx::file( fname, false ), Qtx::dir( fname ) ) )
437 \brief Substitute variables by their values.
439 Environment variable is substituted by its value. For other variables resource
440 manager tries to find value among defined resources parameters.
442 \param str string to be processed
443 \param sect section, where variables are searched
444 \param name name of variable which must be ignored during substitution
445 \return processed string (with all substitutions made)
447 QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name, const OptionsMap& constants ) const
451 QMap<QString, int> ignoreMap;
452 ignoreMap.insert( name, 0 );
454 int start( 0 ), len( 0 );
457 QString envName = Qtx::findEnvVar( res, start, len );
458 if ( envName.isNull() )
461 // First we look in the constants map
462 QString newStr = constants.value( envName, QString() );
464 // Then we check for environment variable
465 if ( newStr.isEmpty() && ::getenv( envName.toLatin1() ) )
466 newStr = QString( ::getenv( envName.toLatin1() ) );
468 if ( newStr.isEmpty() )
470 if ( ignoreMap.contains( envName ) )
476 if ( hasValue( sect, envName ) )
477 newStr = value( sect, envName, false, constants );
478 ignoreMap.insert( envName, 0 );
480 res.replace( start, len, newStr );
483 res.replace( "$$", "$" );
484 res.replace( "%%", "%" );
490 \class QtxResourceMgr::IniFormat
492 \brief Reader/writer for .ini resources files.
495 class QtxResourceMgr::IniFormat : public Format
502 virtual bool load( const QString&, QMap<QString, Section>& );
503 virtual bool save( const QString&, const QMap<QString, Section>& );
506 bool load( const QString&, QMap<QString, Section>&, QSet<QString>& );
512 QtxResourceMgr::IniFormat::IniFormat()
520 QtxResourceMgr::IniFormat::~IniFormat()
525 \brief Load resources from ini-file.
526 \param fname resources file name
527 \param secMap resources map to be filled in
528 \return \c true on success and \c false on error
530 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
532 QSet<QString> importHistory;
533 return load( fname, secMap, importHistory );
538 \brief Load resources from xml-file.
539 \param fname resources file name
540 \param secMap resources map to be filled in
541 \param importHistory list of already imported resources files (to prevent import loops)
542 \return \c true on success or \c false on error
544 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap, QSet<QString>& importHistory )
546 QString aFName = fname.trimmed();
547 if ( !QFileInfo( aFName ).exists() )
549 if ( QFileInfo( aFName + ".ini" ).exists() )
551 else if ( QFileInfo( aFName + ".INI" ).exists() )
554 return false; // file does not exist
556 QFileInfo aFinfo( aFName );
557 aFName = aFinfo.canonicalFilePath();
559 if ( !importHistory.contains( aFName ) )
560 importHistory.insert( aFName );
562 return true; // already imported (prevent import loops)
564 QFile file( aFName );
565 if ( !file.open( QFile::ReadOnly ) )
566 return false; // file is not accessible
568 QTextStream ts( &file );
575 QString separator = option( "separator" );
576 if ( separator.isNull() )
577 separator = QString( "=" );
579 QString comment = option( "comment" );
580 if ( comment.isNull() )
581 comment = QString( "#" );
585 data = ts.readLine();
591 data = data.trimmed();
592 if ( data.isEmpty() )
595 if ( data.startsWith( comment ) )
598 QRegExp rx( "^\\[([\\w\\s\\._]*)\\]$" );
599 if ( rx.indexIn( data ) != -1 )
601 section = rx.cap( 1 );
602 if ( section.isEmpty() )
605 qWarning() << "QtxResourceMgr: Empty section in line:" << line;
608 else if ( data.contains( separator ) && !section.isEmpty() )
610 int pos = data.indexOf( separator );
611 QString key = data.left( pos ).trimmed();
612 QString val = data.mid( pos + 1 ).trimmed();
613 secMap[section].insert( key, val );
615 else if ( section == "import" )
617 QString impFile = QDir::toNativeSeparators( Qtx::makeEnvVarSubst( data, Qtx::Always ) );
618 QFileInfo impFInfo( impFile );
619 if ( impFInfo.isRelative() )
620 impFInfo.setFile( aFinfo.absoluteDir(), impFile );
622 QMap<QString, Section> impMap;
623 if ( !load( impFInfo.absoluteFilePath(), impMap, importHistory ) )
625 qDebug() << "QtxResourceMgr: Error with importing file:" << data;
629 QMap<QString, Section>::const_iterator it = impMap.constBegin();
630 for ( ; it != impMap.constEnd() ; ++it )
632 if ( !secMap.contains( it.key() ) )
634 // insert full section
635 secMap.insert( it.key(), it.value() );
639 // insert all parameters from the section
640 Section::ConstIterator paramIt = it.value().begin();
641 for ( ; paramIt != it.value().end() ; ++paramIt )
643 if ( !secMap[it.key()].contains( paramIt.key() ) )
644 secMap[it.key()].insert( paramIt.key(), paramIt.value() );
653 if ( section.isEmpty() )
654 qWarning() << "QtxResourceMgr: Current section is empty";
656 qWarning() << "QtxResourceMgr: Error in line:" << line;
666 \brief Save resources to the ini-file.
667 \param fname resources file name
668 \param secMap resources map
669 \return \c true on success and \c false on error
671 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
673 if ( !Qtx::mkDir( QFileInfo( fname ).absolutePath() ) )
677 if ( !file.open( QFile::WriteOnly ) )
680 QTextStream ts( &file );
682 ts << "# This file is automatically created by SALOME application." << endl;
683 ts << "# Changes made in this file can be lost!" << endl;
687 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
689 QStringList data( QString( "[%1]" ).arg( it.key() ) );
690 for ( Section::ConstIterator iter = it.value().begin(); iter != it.value().end(); ++iter )
691 data.append( iter.key() + " = " + iter.value() );
694 for ( QStringList::ConstIterator itr = data.begin(); itr != data.end(); ++itr )
704 \class QtxResourceMgr::XmlFormat
706 \brief Reader/writer for .xml resources files.
709 class QtxResourceMgr::XmlFormat : public Format
716 virtual bool load( const QString&, QMap<QString, Section>& );
717 virtual bool save( const QString&, const QMap<QString, Section>& );
720 QString docTag() const;
721 QString sectionTag() const;
722 QString parameterTag() const;
723 QString importTag() const;
724 QString nameAttribute() const;
725 QString valueAttribute() const;
727 bool load( const QString&, QMap<QString, Section>&, QSet<QString>& );
733 QtxResourceMgr::XmlFormat::XmlFormat()
741 QtxResourceMgr::XmlFormat::~XmlFormat()
746 \brief Load resources from xml-file.
747 \param fname resources file name
748 \param secMap resources map to be filled in
749 \return \c true on success and \c false on error
751 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
753 QSet<QString> importHistory;
754 return load( fname, secMap, importHistory );
758 \brief Load resources from xml-file.
759 \param fname resources file name
760 \param secMap resources map to be filled in
761 \param importHistory list of already imported resources files (to prevent import loops)
762 \return \c true on success and \c false on error
764 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap, QSet<QString>& importHistory )
766 QString aFName = fname.trimmed();
767 if ( !QFileInfo( aFName ).exists() )
769 if ( QFileInfo( aFName + ".xml" ).exists() )
771 else if ( QFileInfo( aFName + ".XML" ).exists() )
774 return false; // file does not exist
776 QFileInfo aFinfo( aFName );
777 aFName = aFinfo.canonicalFilePath();
779 if ( !importHistory.contains( aFName ) )
780 importHistory.insert( aFName );
782 return true; // already imported (prevent import loops)
788 QFile file( aFName );
789 if ( !file.open( QFile::ReadOnly ) )
791 qDebug() << "QtxResourceMgr: File is not accessible:" << aFName;
797 res = doc.setContent( &file );
802 qDebug() << "QtxResourceMgr: File is empty:" << aFName;
806 QDomElement root = doc.documentElement();
807 if ( root.isNull() || root.tagName() != docTag() )
809 qDebug() << "QtxResourceMgr: Invalid root in file:" << aFName;
813 QDomNode sectNode = root.firstChild();
814 while ( res && !sectNode.isNull() )
816 res = sectNode.isElement();
819 QDomElement sectElem = sectNode.toElement();
820 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
822 QString section = sectElem.attribute( nameAttribute() );
823 QDomNode paramNode = sectNode.firstChild();
824 while ( res && !paramNode.isNull() )
826 res = paramNode.isElement();
829 QDomElement paramElem = paramNode.toElement();
830 if ( paramElem.tagName() == parameterTag() &&
831 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
833 QString paramName = paramElem.attribute( nameAttribute() );
834 QString paramValue = paramElem.attribute( valueAttribute() );
835 secMap[section].insert( paramName, paramValue );
839 qDebug() << "QtxResourceMgr: Invalid parameter element in file:" << aFName;
845 res = paramNode.isComment();
847 qDebug() << "QtxResourceMgr: Node is neither element nor comment in file:" << aFName;
850 paramNode = paramNode.nextSibling();
853 else if ( sectElem.tagName() == importTag() && sectElem.hasAttribute( nameAttribute() ) )
855 QString impFile = QDir::toNativeSeparators( Qtx::makeEnvVarSubst( sectElem.attribute( nameAttribute() ), Qtx::Always ) );
856 QFileInfo impFInfo( impFile );
857 if ( impFInfo.isRelative() )
858 impFInfo.setFile( aFinfo.absoluteDir(), impFile );
860 QMap<QString, Section> impMap;
861 if ( !load( impFInfo.absoluteFilePath(), impMap, importHistory ) )
863 qDebug() << "QtxResourceMgr: Error with importing file:" << sectElem.attribute( nameAttribute() );
867 QMap<QString, Section>::const_iterator it = impMap.constBegin();
868 for ( ; it != impMap.constEnd() ; ++it )
870 if ( !secMap.contains( it.key() ) )
872 // insert full section
873 secMap.insert( it.key(), it.value() );
877 // insert all parameters from the section
878 Section::ConstIterator paramIt = it.value().begin();
879 for ( ; paramIt != it.value().end() ; ++paramIt )
881 if ( !secMap[it.key()].contains( paramIt.key() ) )
882 secMap[it.key()].insert( paramIt.key(), paramIt.value() );
890 qDebug() << "QtxResourceMgr: Invalid section in file:" << aFName;
896 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
898 qDebug() << "QtxResourceMgr: Node is neither element nor comment in file:" << aFName;
901 sectNode = sectNode.nextSibling();
907 qDebug() << "QtxResourceMgr: File" << fname << "is loaded successfully";
912 \brief Save resources to the xml-file.
913 \param fname resources file name
914 \param secMap resources map
915 \return \c true on success and \c false on error
917 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
923 if ( !Qtx::mkDir( QFileInfo( fname ).absolutePath() ) )
927 if ( !file.open( QFile::WriteOnly ) )
930 QDomDocument doc( docTag() );
931 QDomComment comment = doc.createComment( "\nThis file is automatically created by SALOME application.\nChanges made in this file can be lost!\n" );
932 doc.appendChild( comment );
933 QDomElement root = doc.createElement( docTag() );
934 doc.appendChild( root );
936 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
938 QDomElement sect = doc.createElement( sectionTag() );
939 sect.setAttribute( nameAttribute(), it.key() );
940 root.appendChild( sect );
941 for ( Section::ConstIterator iter = it.value().begin(); iter != it.value().end(); ++iter )
943 QDomElement val = doc.createElement( parameterTag() );
944 val.setAttribute( nameAttribute(), iter.key() );
945 val.setAttribute( valueAttribute(), iter.value() );
946 sect.appendChild( val );
950 QTextStream ts( &file );
951 QStringList docStr = doc.toString().split( "\n" );
952 for ( QStringList::ConstIterator itr = docStr.begin(); itr != docStr.end(); ++itr )
963 \brief Get document tag name
964 \return XML document tag name
966 QString QtxResourceMgr::XmlFormat::docTag() const
968 QString tag = option( "doc_tag" );
970 tag = QString( "document" );
975 \brief Get section tag name
976 \return XML section tag name
978 QString QtxResourceMgr::XmlFormat::sectionTag() const
980 QString tag = option( "section_tag" );
982 tag = QString( "section" );
987 \brief Get parameter tag name
988 \return XML parameter tag name
990 QString QtxResourceMgr::XmlFormat::parameterTag() const
992 QString tag = option( "parameter_tag" );
994 tag = QString( "parameter" );
999 \brief Get import tag name
1000 \return XML import tag name
1002 QString QtxResourceMgr::XmlFormat::importTag() const
1004 QString tag = option( "import_tag" );
1005 if ( tag.isEmpty() )
1006 tag = QString( "import" );
1011 \brief Get parameter tag's "name" attribute name
1012 \return XML parameter tag's "name" attribute name
1014 QString QtxResourceMgr::XmlFormat::nameAttribute() const
1016 QString str = option( "name_attribute" );
1017 if ( str.isEmpty() )
1018 str = QString( "name" );
1023 \brief Get parameter tag's "value" attribute name
1024 \return XML parameter tag's "value" attribute name
1026 QString QtxResourceMgr::XmlFormat::valueAttribute() const
1028 QString str = option( "value_attribute" );
1029 if ( str.isEmpty() )
1030 str = QString( "value" );
1035 \class QtxResourceMgr::Format
1036 \brief Generic resources files reader/writer class.
1041 \param fmt format name (for example, "xml" or "ini")
1043 QtxResourceMgr::Format::Format( const QString& fmt )
1051 QtxResourceMgr::Format::~Format()
1056 \brief Get the format name.
1059 QString QtxResourceMgr::Format::format() const
1065 \brief Get options names.
1066 \return list of the format options
1068 QStringList QtxResourceMgr::Format::options() const
1070 return myOpt.keys();
1074 \brief Get the value of the option with specified name.
1076 If option doesn't exist then null QString is returned.
1078 \param opt option name
1079 \return option value
1081 QString QtxResourceMgr::Format::option( const QString& opt ) const
1084 if ( myOpt.contains( opt ) )
1090 \brief Set the value of the option with specified name.
1091 \param opt option name
1092 \param val option value
1094 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
1096 myOpt.insert( opt, val );
1100 \brief Load resources from the resource file.
1101 \param res resources object
1102 \return \c true on success and \c false on error
1104 bool QtxResourceMgr::Format::load( Resources* res )
1109 QMap<QString, Section> sections;
1110 bool status = load( res->myFileName, sections );
1112 res->mySections = sections;
1114 qDebug() << "QtxResourceMgr: Can't load resource file:" << res->myFileName;
1120 \brief Save resources to the resource file.
1121 \param res resources object
1122 \return \c true on success and \c false on error
1124 bool QtxResourceMgr::Format::save( Resources* res )
1129 Qtx::mkDir( Qtx::dir( res->myFileName ) );
1131 QtxResourceMgr* mgr = res->resMgr();
1132 QString name = mgr ? mgr->userFileName( mgr->appName(), false ) : res->myFileName;
1133 return save( name, res->mySections );
1137 \fn virtual bool QtxResourceMgr::Format::load( const QString& fname,
1138 QMap<QString, Section>& secMap )
1139 \brief Load resources from the specified resources file.
1141 Should be implemented in the successors.
1143 \param fname resources file name
1144 \param secMap resources map to be filled in
1145 \return \c true on success and \c false on error
1149 \fn virtual bool QtxResourceMgr::Format::save( const QString& fname,
1150 const QMap<QString, Section>& secMap )
1152 \brief Save resources to the specified resources file.
1154 Should be implemented in the successors.
1156 \param fname resources file name
1157 \param secMap resources map
1158 \return \c true on success and \c false on error
1162 \class QtxResourceMgr
1163 \brief Application resources manager.
1165 This class can be used to define settings, save/load settings and
1166 application preferences to the resource file(s), load translation files
1167 (internationalization mechanism), load pixmaps and other resources from
1168 external files, etc.
1170 Currently it supports .ini and .xml resources file formats. To implement
1171 own resources file format, inherit from the Format class and implement virtual
1172 Format::load() and Format::save() methods.
1174 Resources manager is initialized by the (symbolic) name of the application.
1175 The parameter \a resVarTemplate specifies the template for the environment
1176 variable which should point to the resource directory or list of directories.
1177 Environment variable name is calculated by substitution of "%1" substring in
1178 the \a resVarTemplate parameter (if it contains such substring) by the
1179 application name (\a appName).
1180 By default, \a resVarTemplate is set to "%1Resources". For example, if the application name
1181 is "MyApp", the environment variable "MyAppResources" will be inspected in this case.
1183 Resource manager can handle several global application configuration files and
1184 one user configuration file. Location of global configuration files is defined
1185 by the environment variable (see above) and these files are always read-only.
1186 The name of the global configuration files is retrieved by calling virtual method
1187 globalFileName() which can be redefined in the QtxResourceMgr class successors.
1188 User configuration file always situated in the user's home directory. It's name
1189 is defined by calling virtual method userFileName() which can be also redefined
1190 in the QtxResourceMgr class successors. This is the only file which the preferences
1191 changed by the user during the application session are written to (usually
1192 when the application closes).
1194 Resources environment variable should contain one or several resource directories
1195 (separated by ";" symbol on Windows and ":" or ";" on Linux). Each resource directory
1196 can contain application global configuration file. The user configuration file has
1197 the highest priority, for the global configuration files the priority is decreasing from
1198 left to right, i.e. the first directory in the directoris list, defined by the
1199 resources environment variable has higher priority. Priority has the meaning when
1200 searching requested resources (application preference, pixmap file name, translation
1203 When retrieving preferences, it is sometimes helpful to ignore values coming from the
1204 user preference file and take into account only global preferences.
1205 To do this, use setWorkingMode() method passing QtxResourceMgr::IgnoreUserValues enumerator
1208 Resources manager operates with such terms like options, sections and parameters.
1209 Parametets are named application resources, for example, application preferences like
1210 integer, double, boolean or string values, pictures, font and color definitions, etc.
1211 Parameters are organized inside the resources files into the named groups - sections.
1212 Options are special kind of resoures which allow customizing resource files interpreting.
1213 For example, by default language settings are defined in the resource file in the
1214 section "language". It is possible to change this section name by setting "language"
1215 option to another value (see setOption()).
1217 Retrieving preferences values can be done by using one of value() methods, each returns
1218 \c true if the corresponding preference is found. Another way is to use integerValue(),
1219 doubleValue(), etc methods, which allow specifying default value which is used if the
1220 specified preference is not found. Removing of preferences or sections can be done using
1221 remove(const QString& sect) or remove(const QString& sect, const QString& name) methods.
1222 To add the preference or to change exiting preference value use setValue() methods family.
1223 Methods hasSection() and hasValue() can be used to check existence of section or
1224 preference (in the specified section). List of all sections can be retrieved with the
1225 sections() method, and list of all settings names in some specified section can be
1226 obtained with parameters() method.
1228 Pixmaps can be loaded with the loadPixmap() methods. If the specified pixmap is not found,
1229 the default one is returned. Default pixmap can be set by setDefaultPixmap().
1231 One of the key feature of the resources manager is support of application
1232 internationalization mechanism. Translation files for the specified language can be loaded
1233 with loadLanguage() method.
1237 \brief Constructs the resource manager.
1238 \param appName application name
1239 \param resVarTemplate resource environment variable pattern
1241 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
1242 : myAppName( appName ),
1243 myCheckExist( true ),
1245 myIsPixmapCached( true ),
1246 myHasUserValues( true ),
1247 myWorkingMode( AllowUserValues )
1249 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
1250 if ( envVar.contains( "%1" ) )
1251 envVar = envVar.arg( appName );
1254 if ( ::getenv( envVar.toLatin1() ) )
1255 dirs = ::getenv( envVar.toLatin1() );
1257 QString dirsep = ";"; // for Windows: ";" is used as directories separator
1259 QString dirsep = "[:|;]"; // for Linux: both ":" and ";" can be used
1261 setDirList( dirs.split( QRegExp( dirsep ), QString::SkipEmptyParts ) );
1263 installFormat( new XmlFormat() );
1264 installFormat( new IniFormat() );
1266 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
1272 Destroy the resource manager and free allocated memory.
1274 QtxResourceMgr::~QtxResourceMgr()
1276 QStringList prefList = myTranslator.keys();
1277 for ( QStringList::ConstIterator it = prefList.begin(); it != prefList.end(); ++it )
1278 removeTranslators( *it );
1280 qDeleteAll( myResources );
1281 qDeleteAll( myFormats );
1283 delete myDefaultPix;
1287 \brief Get the application name.
1288 \return application name
1290 QString QtxResourceMgr::appName() const
1296 \brief Get the "check existance" flag
1298 If this flag is \c true then preference can be set (with setValue() method)
1299 only if it doesn't exist or if the value is changed.
1301 \return \c true if "check existance" flag is set
1303 bool QtxResourceMgr::checkExisting() const
1305 return myCheckExist;
1309 \brief Set the "check existance" flag.
1310 \param on new flag value
1312 void QtxResourceMgr::setCheckExisting( const bool on )
1318 \brief Get the resource directories list.
1320 Home user directory (where the user application configuration file is situated)
1321 is not included. This is that directories list defined by the application
1322 resources environment variable.
1324 \return list of directories names
1326 QStringList QtxResourceMgr::dirList() const
1332 \brief Initialise resources manager.
1334 Prepare the resources containers and load resources (if \a autoLoad is \c true).
1336 \param autoLoad if \c true (default) then all resources are loaded
1338 void QtxResourceMgr::initialize( const bool autoLoad ) const
1340 if ( !myResources.isEmpty() )
1343 QtxResourceMgr* that = (QtxResourceMgr*)this;
1345 if ( !userFileName( appName() ).isEmpty() )
1346 that->myResources.append( new Resources( that, userFileName( appName() ) ) );
1348 that->myHasUserValues = myResources.count() > 0;
1350 for ( QStringList::ConstIterator it = myDirList.begin(); it != myDirList.end(); ++it )
1352 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
1353 that->myResources.append( new Resources( that, path ) );
1361 \brief Get "cached pixmaps" option value.
1363 Resources manager allows possibility to cache loaded pixmaps that allow to
1364 improve application performance. This feature is turned on by default - all
1365 loaded pixmaps are stored in the internal map. Switching of this feature on/off
1366 can be done by setIsPixmapCached() method.
1368 \return \c true if pixmap cache is turned on
1369 \sa setIsPixmapCached()
1371 bool QtxResourceMgr::isPixmapCached() const
1373 return myIsPixmapCached;
1377 \brief Switch "cached pixmaps" option on/off.
1378 \param on enable pixmap cache if \c true and disable it if \c false
1379 \sa isPixmapCached()
1381 void QtxResourceMgr::setIsPixmapCached( const bool on )
1383 myIsPixmapCached = on;
1387 \brief Remove all resources from the resources manager.
1389 void QtxResourceMgr::clear()
1391 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
1396 \brief Get current working mode.
1398 \return current working mode
1399 \sa setWorkingMode(), value(), hasValue(), hasSection(), setValue()
1401 QtxResourceMgr::WorkingMode QtxResourceMgr::workingMode() const
1403 return myWorkingMode;
1407 \brief Set resource manager's working mode.
1409 The resource manager can operate in the following working modes:
1410 * AllowUserValues : methods values(), hasValue(), hasSection() take into account user values (default)
1411 * IgnoreUserValues : methods values(), hasValue(), hasSection() do not take into account user values
1413 Note, that setValue() method always put the value to the user settings file.
1415 \param mode new working mode
1416 \return previous working mode
1417 \sa workingMode(), value(), hasValue(), hasSection(), setValue()
1419 QtxResourceMgr::WorkingMode QtxResourceMgr::setWorkingMode( WorkingMode mode )
1421 WorkingMode m = myWorkingMode;
1422 myWorkingMode = mode;
1427 \brief Get interger parameter value.
1428 \param sect section name
1429 \param name parameter name
1430 \param iVal parameter to return resulting integer value
1431 \return \c true if parameter is found and \c false if parameter is not found
1432 (in this case \a iVal value is undefined)
1434 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
1437 if ( !value( sect, name, val, true ) )
1441 iVal = val.toInt( &ok );
1447 \brief Get double parameter value.
1448 \param sect section name
1449 \param name parameter name
1450 \param dVal parameter to return resulting double value
1451 \return \c true if parameter is found and \c false if parameter is not found
1452 (in this case \a dVal value is undefined)
1454 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
1457 if ( !value( sect, name, val, true ) )
1461 dVal = val.toDouble( &ok );
1467 \brief Get boolean parameter value.
1468 \param sect section name
1469 \param name parameter name
1470 \param bVal parameter to return resulting boolean value
1471 \return \c true if parameter is found and \c false if parameter is not found
1472 (in this case \a bVal value is undefined)
1474 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
1477 if ( !value( sect, name, val, true ) )
1480 static QMap<QString, bool> boolMap;
1481 if ( boolMap.isEmpty() )
1483 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
1484 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
1487 val = val.toLower();
1488 bool res = boolMap.contains( val );
1490 bVal = boolMap[val];
1493 double num = val.toDouble( &res );
1502 \brief Get color parameter value.
1503 \param sect section name
1504 \param name parameter name
1505 \param cVal parameter to return resulting color value
1506 \return \c true if parameter is found and \c false if parameter is not found
1507 (in this case \a cVal value is undefined)
1509 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
1512 if ( !value( sect, name, val, true ) )
1515 return Qtx::stringToColor( val, cVal );
1519 \brief Get font parameter value.
1520 \param sect section name
1521 \param name parameter name
1522 \param fVal parameter to return resulting font value
1523 \return \c true if parameter is found and \c false if parameter is not found
1524 (in this case \a fVal value is undefined)
1526 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
1529 if ( !value( sect, name, val, true ) )
1532 QStringList fontDescr = val.split( ",", QString::SkipEmptyParts );
1534 if ( fontDescr.count() < 2 )
1537 QString family = fontDescr[0];
1538 if ( family.isEmpty() )
1541 fVal = QFont( family );
1543 for ( int i = 1; i < (int)fontDescr.count(); i++ )
1545 QString curval = fontDescr[i].trimmed().toLower();
1546 if ( curval == QString( "bold" ) )
1547 fVal.setBold( true );
1548 else if ( curval == QString( "italic" ) )
1549 fVal.setItalic( true );
1550 else if ( curval == QString( "underline" ) )
1551 fVal.setUnderline( true );
1552 else if ( curval == QString( "shadow" ) || curval == QString( "overline" ) )
1553 fVal.setOverline( true );
1557 int ps = curval.toInt( &isOk );
1559 fVal.setPointSize( ps );
1567 \brief Get byte array parameter value.
1568 \param sect section name
1569 \param name parameter name
1570 \param baVal parameter to return resulting byte array value
1571 \return \c true if parameter is found and \c false if parameter is not found
1572 (in this case \a baVal value is undefined)
1574 bool QtxResourceMgr::value( const QString& sect, const QString& name, QByteArray& baVal ) const
1577 if ( !value( sect, name, val, true ) )
1580 if ( val.startsWith( "@ByteArray(" ) && val.endsWith( ')' ) ) {
1581 baVal = QByteArray( val.midRef( 11, val.size() - 12 ).toLatin1() );
1584 if ( val.startsWith( "@ByteArray[" ) && val.endsWith( ']' ) ) {
1585 val = val.mid( 11, val.size() - 12 );
1588 QStringList lst = val.split( QRegExp( "[\\s|,]" ), QString::SkipEmptyParts );
1589 for ( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it )
1593 if ( str.startsWith( "#" ) )
1599 int num = str.toInt( &ok, base );
1600 if ( !ok || num < 0 || num > 255 )
1603 baVal.append( (char)num );
1606 return !baVal.isEmpty();
1610 \brief Get linear gradient parameter value.
1611 \param sect section name
1612 \param name parameter name
1613 \param gVal parameter to return resulting linear gradient value
1614 \return \c true if parameter is found and \c false if parameter is not found
1615 (in this case \a gVal value is undefined)
1617 bool QtxResourceMgr::value( const QString& sect, const QString& name, QLinearGradient& gVal ) const
1620 if ( !value( sect, name, val, true ) )
1623 return Qtx::stringToLinearGradient( val, gVal );
1627 \brief Get radial gradient parameter value.
1628 \param sect section name
1629 \param name parameter name
1630 \param gVal parameter to return resulting radial gradient value
1631 \return \c true if parameter is found and \c false if parameter is not found
1632 (in this case \a gVal value is undefined)
1634 bool QtxResourceMgr::value( const QString& sect, const QString& name, QRadialGradient& gVal ) const
1637 if ( !value( sect, name, val, true ) )
1640 return Qtx::stringToRadialGradient( val, gVal );
1644 \brief Get conical gradient parameter value.
1645 \param sect section name
1646 \param name parameter name
1647 \param gVal parameter to return resulting conical gradient value
1648 \return \c true if parameter is found and \c false if parameter is not found
1649 (in this case \a gVal value is undefined)
1651 bool QtxResourceMgr::value( const QString& sect, const QString& name, QConicalGradient& gVal ) const
1654 if ( !value( sect, name, val, true ) )
1657 return Qtx::stringToConicalGradient( val, gVal );
1661 \brief Get background parameter value.
1662 \param sect section name
1663 \param name parameter name
1664 \param bgVal parameter to return resulting background value
1665 \return \c true if parameter is found and \c false if parameter is not found
1666 (in this case \a bgVal value is undefined)
1668 bool QtxResourceMgr::value( const QString& sect, const QString& name, Qtx::BackgroundData& bgVal ) const
1671 if ( !value( sect, name, val, true ) )
1674 bgVal = Qtx::stringToBackground( val );
1675 return bgVal.isValid();
1679 \brief Get string parameter value (native format).
1680 \param sect section name
1681 \param name parameter name
1682 \param val parameter to return resulting byte array value
1683 \param subst if \c true perform environment variables substitution
1684 \return \c true if parameter is found and \c false if parameter is not found
1685 (in this case \a val value is undefined)
1687 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
1693 ResList::ConstIterator it = myResources.begin();
1694 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1697 for ( ; it != myResources.end() && !ok; ++it )
1699 ok = (*it)->hasValue( sect, name );
1701 val = (*it)->value( sect, name, subst, myConstants );
1708 \brief Get interger parameter value.
1710 If the specified parameter is not found or can not be converted to the integer value,
1711 the specified default value is returned instead.
1713 \param sect section name
1714 \param name parameter name
1715 \param def default value
1716 \return parameter value (or default value if parameter is not found)
1718 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
1721 if ( !value( sect, name, val ) )
1727 \brief Get double parameter value.
1729 If the specified parameter is not found or can not be converted to the double value,
1730 the specified default value is returned instead.
1732 \param sect section name
1733 \param name parameter name
1734 \param def default value
1735 \return parameter value (or default value if parameter is not found)
1737 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
1740 if ( !value( sect, name, val ) )
1746 \brief Get boolean parameter value.
1748 If the specified parameter is not found or can not be converted to the boolean value,
1749 the specified default value is returned instead.
1751 \param sect section name
1752 \param name parameter name
1753 \param def default value
1754 \return parameter value (or default value if parameter is not found)
1756 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
1759 if ( !value( sect, name, val ) )
1765 \brief Get font parameter value.
1767 If the specified parameter is not found or can not be converted to the font value,
1768 the specified default value is returned instead.
1770 \param sect section name
1771 \param name parameter name
1772 \param def default value
1773 \return parameter value (or default value if parameter is not found)
1775 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
1778 if( !value( sect, name, font ) )
1784 \brief Get color parameter value.
1786 If the specified parameter is not found or can not be converted to the color value,
1787 the specified default value is returned instead.
1789 \param sect section name
1790 \param name parameter name
1791 \param def default value
1792 \return parameter value (or default value if parameter is not found)
1794 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
1797 if ( !value( sect, name, val ) )
1803 \brief Get string parameter value.
1805 If the specified parameter is not found, the specified default value is returned instead.
1807 \param sect section name
1808 \param name parameter name
1809 \param def default value
1810 \return parameter value (or default value if parameter is not found)
1812 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def, const bool subst ) const
1815 if ( !value( sect, name, val, subst ) )
1821 \brief Get byte array parameter value.
1823 If the specified parameter is not found, the specified default value is returned instead.
1825 \param sect section name
1826 \param name parameter name
1827 \param def default value
1828 \return parameter value (or default value if parameter is not found)
1830 QByteArray QtxResourceMgr::byteArrayValue( const QString& sect, const QString& name, const QByteArray& def ) const
1833 if ( !value( sect, name, val ) )
1839 \brief Get linear gradient parameter value.
1841 If the specified parameter is not found, the specified default value is returned instead.
1843 \param sect section name
1844 \param name parameter name
1845 \param def default value
1846 \return parameter value (or default value if parameter is not found)
1848 QLinearGradient QtxResourceMgr::linearGradientValue( const QString& sect, const QString& name, const QLinearGradient& def ) const
1850 QLinearGradient val;
1851 if ( !value( sect, name, val ) )
1857 \brief Get radial gradient parameter value.
1859 If the specified parameter is not found, the specified default value is returned instead.
1861 \param sect section name
1862 \param name parameter name
1863 \param def default value
1864 \return parameter value (or default value if parameter is not found)
1866 QRadialGradient QtxResourceMgr::radialGradientValue( const QString& sect, const QString& name, const QRadialGradient& def ) const
1868 QRadialGradient val;
1869 if ( !value( sect, name, val ) )
1875 \brief Get conical gradient parameter value.
1877 If the specified parameter is not found, the specified default value is returned instead.
1879 \param sect section name
1880 \param name parameter name
1881 \param def default value
1882 \return parameter value (or default value if parameter is not found)
1884 QConicalGradient QtxResourceMgr::conicalGradientValue( const QString& sect, const QString& name, const QConicalGradient& def ) const
1886 QConicalGradient val;
1887 if ( !value( sect, name, val ) )
1893 \brief Get background parameter value.
1895 If the specified parameter is not found, the specified default value is returned instead.
1897 \param sect section name
1898 \param name parameter name
1899 \param def default value
1900 \return parameter value (or default value if parameter is not found)
1902 Qtx::BackgroundData QtxResourceMgr::backgroundValue( const QString& sect, const QString& name, const Qtx::BackgroundData& def ) const
1904 Qtx::BackgroundData val;
1905 if ( !value( sect, name, val ) )
1911 \brief Check parameter existence.
1912 \param sect section name
1913 \param name parameter name
1914 \return \c true if parameter exists in specified section
1916 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
1922 ResList::ConstIterator it = myResources.begin();
1923 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1926 for ( ; it != myResources.end() && !ok; ++it )
1927 ok = (*it)->hasValue( sect, name );
1933 \brief Check section existence.
1934 \param sect section name
1935 \return \c true if section exists
1937 bool QtxResourceMgr::hasSection( const QString& sect ) const
1943 ResList::ConstIterator it = myResources.begin();
1944 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1947 for ( ; it != myResources.end() && !ok; ++it )
1948 ok = (*it)->hasSection( sect );
1954 \brief Set integer parameter value.
1955 \param sect section name
1956 \param name parameter name
1957 \param val parameter value
1959 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1962 if ( checkExisting() && value( sect, name, res ) && res == val )
1965 setResource( sect, name, QString::number( val ) );
1969 \brief Set double parameter value.
1970 \param sect section name
1971 \param name parameter name
1972 \param val parameter value
1974 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1977 if ( checkExisting() && value( sect, name, res ) && res == val )
1980 setResource( sect, name, QString::number( val, 'g', 12 ) );
1984 \brief Set boolean parameter value.
1985 \param sect section name
1986 \param name parameter name
1987 \param val parameter value
1989 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1992 if ( checkExisting() && value( sect, name, res ) && res == val )
1995 setResource( sect, name, QString( val ? "true" : "false" ) );
1999 \brief Set color parameter value.
2000 \param sect section name
2001 \param name parameter name
2002 \param val parameter value
2004 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
2007 if ( checkExisting() && value( sect, name, res ) && res == val )
2010 setResource( sect, name, Qtx::colorToString( val ) );
2014 \brief Set font parameter value.
2015 \param sect section name
2016 \param name parameter name
2017 \param val parameter value
2019 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val )
2022 if ( checkExisting() && value( sect, name, res ) && res == val )
2025 QStringList fontDescr;
2026 fontDescr.append( val.family() );
2028 fontDescr.append( "Bold" );
2030 fontDescr.append( "Italic" );
2031 if ( val.underline() )
2032 fontDescr.append( "Underline" );
2033 if ( val.overline() )
2034 fontDescr.append( "Overline" );
2035 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
2037 setResource( sect, name, fontDescr.join( "," ) );
2041 \brief Set string parameter value.
2042 \param sect section name
2043 \param name parameter name
2044 \param val parameter value
2046 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
2049 if ( checkExisting() && value( sect, name, res ) && res == val )
2052 setResource( sect, name, val );
2056 \brief Set byte array parameter value.
2057 \param sect section name
2058 \param name parameter name
2059 \param val parameter value
2061 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QByteArray& val )
2064 if ( checkExisting() && value( sect, name, res ) && res == val )
2069 for ( int i = 0; i < val.size(); i++ )
2071 ::sprintf( buf, "#%02X", (unsigned char)val.at( i ) );
2072 lst.append( QString( buf ) );
2075 QString result = QString( "@ByteArray[%1]" ).arg( lst.join( " " ) );
2076 setResource( sect, name, result );
2080 \brief Set linear gradient parameter value.
2081 \param sect section name
2082 \param name parameter name
2083 \param val parameter value
2085 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QLinearGradient& val )
2087 QLinearGradient res;
2088 if ( checkExisting() && value( sect, name, res ) && res == val )
2091 setResource( sect, name, Qtx::gradientToString( val ) );
2095 \brief Set radial gradient parameter value.
2096 \param sect section name
2097 \param name parameter name
2098 \param val parameter value
2100 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QRadialGradient& val )
2102 QRadialGradient res;
2103 if ( checkExisting() && value( sect, name, res ) && res == val )
2106 setResource( sect, name, Qtx::gradientToString( val ) );
2110 \brief Set conical gradient parameter value.
2111 \param sect section name
2112 \param name parameter name
2113 \param val parameter value
2115 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QConicalGradient& val )
2117 QConicalGradient res;
2118 if ( checkExisting() && value( sect, name, res ) && res == val )
2121 setResource( sect, name, Qtx::gradientToString( val ) );
2125 \brief Set background parameter value.
2126 \param sect section name
2127 \param name parameter name
2128 \param val parameter value
2130 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const Qtx::BackgroundData& val )
2132 Qtx::BackgroundData res;
2133 if ( checkExisting() && value( sect, name, res ) && res == val )
2136 setResource( sect, name, Qtx::backgroundToString( val ) );
2140 \brief Remove resources section.
2141 \param sect section name
2143 void QtxResourceMgr::remove( const QString& sect )
2147 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2148 (*it)->removeSection( sect );
2152 \brief Remove the specified parameter.
2153 \param sect section name
2154 \param name parameter name
2156 void QtxResourceMgr::remove( const QString& sect, const QString& name )
2160 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2161 (*it)->removeValue( sect, name );
2165 \brief Get current configuration files format.
2166 \return configuration files format name
2168 QString QtxResourceMgr::currentFormat() const
2171 if ( !myFormats.isEmpty() )
2172 fmt = myFormats[0]->format();
2177 \brief Set current configuration files format.
2178 \param fmt configuration files format name
2180 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
2182 Format* form = format( fmt );
2186 myFormats.removeAll( form );
2187 myFormats.prepend( form );
2189 if ( myResources.isEmpty() )
2192 ResList::Iterator resIt = myResources.begin();
2193 if ( myResources.count() > myDirList.count() && resIt != myResources.end() )
2195 (*resIt)->setFile( userFileName( appName() ) );
2199 for ( QStringList::ConstIterator it = myDirList.begin(); it != myDirList.end() && resIt != myResources.end(); ++it, ++resIt )
2200 (*resIt)->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
2204 \brief Get configuration files format by specified format name.
2205 \param fmt configuration files format name
2206 \return format object or 0 if format is not defined
2208 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
2211 for ( FormatList::ConstIterator it = myFormats.begin(); it != myFormats.end() && !form; ++it )
2213 if ( (*it)->format() == fmt )
2221 \brief Install configuration files format.
2223 Added format becomes current.
2225 \param form format object to be installed
2227 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
2229 if ( !myFormats.contains( form ) )
2230 myFormats.prepend( form );
2234 \brief Remove configuration files format.
2235 \param form format object to be uninstalled
2237 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
2239 myFormats.removeAll( form );
2243 \brief Get resource format options names.
2244 \return list of options names
2246 QStringList QtxResourceMgr::options() const
2248 return myOptions.keys();
2252 \brief Get the string value of the specified resources format option.
2254 If option does not exist, null QString is returned.
2256 \param opt option name
2257 \return option value
2258 \sa setOption(), options()
2260 QString QtxResourceMgr::option( const QString& opt ) const
2263 if ( myOptions.contains( opt ) )
2264 val = myOptions[opt];
2269 \brief Set the string value of the specified resources format option.
2270 \param opt option name
2271 \param val option value
2272 \sa option(), options()
2274 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
2276 myOptions.insert( opt, val );
2280 \brief Get names of all known constants.
2281 \return list of constants names
2282 \sa constant(), setConstant
2284 QStringList QtxResourceMgr::constants() const
2286 return myConstants.keys();
2290 \brief Get the value of the known constant.
2292 If constant is not set, null QString is returned.
2294 \param name constant name
2295 \return constant value
2296 \sa setConstant(), constants()
2298 QString QtxResourceMgr::constant( const QString& name ) const
2300 return myConstants.value( name, QString() );
2304 \brief Set the value of the constant.
2305 \param name constant name
2306 \param value constant value
2307 \sa constants(), constants()
2309 void QtxResourceMgr::setConstant( const QString& name, const QString& value )
2311 if ( !name.isEmpty() )
2312 myConstants.insert( name, value );
2316 \brief Load all resources from all resource files (global and user).
2317 \return \c true on success and \c false on error
2320 bool QtxResourceMgr::load()
2322 initialize( false );
2324 Format* fmt = format( currentFormat() );
2329 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2330 res = fmt->load( *it ) && res;
2336 \brief Import resources from specified resource file.
2337 \param fname resources file name
2338 \return \c true on success and \c false on error
2340 bool QtxResourceMgr::import( const QString& fname )
2342 Format* fmt = format( currentFormat() );
2346 if ( myResources.isEmpty() || !myHasUserValues )
2349 Resources* r = myResources[0];
2353 QString old = r->file();
2354 r->setFile( fname );
2355 bool res = fmt->load( r );
2361 \brief Save all resources to the user resource files.
2362 \return \c true on success and \c false on error
2364 bool QtxResourceMgr::save()
2366 initialize( false );
2368 Format* fmt = format( currentFormat() );
2372 if ( myResources.isEmpty() || !myHasUserValues )
2375 bool result = fmt->save( myResources[0] );
2383 \brief Get all sections names.
2384 \return list of section names
2386 QStringList QtxResourceMgr::sections() const
2390 QMap<QString, int> map;
2392 ResList::ConstIterator it = myResources.begin();
2393 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2396 for ( ; it != myResources.end(); ++it )
2398 QStringList lst = (*it)->sections();
2399 for ( QStringList::ConstIterator itr = lst.begin(); itr != lst.end(); ++itr )
2400 map.insert( *itr, 0 );
2407 \brief Get all sections names matching specified regular expression.
2408 \param re searched regular expression
2409 \return list of sections names
2411 QStringList QtxResourceMgr::sections(const QRegExp& re) const
2413 return sections().filter( re );
2417 \brief Get all sections names with the prefix specified by passed
2418 list of parent sections names.
2420 Sub-sections are separated inside the section name by the sections
2421 separator token, for example "splash:color:label".
2423 \param names parent sub-sections names
2424 \return list of sections names
2426 QStringList QtxResourceMgr::sections(const QStringList& names) const
2428 QStringList nm = names;
2430 QRegExp re( QString( "^%1$" ).arg( nm.join( sectionsToken() ) ) );
2431 return sections( re );
2435 \brief Get list of sub-sections names for the specified parent section name.
2437 Sub-sections are separated inside the section name by the sections
2438 separator token, for example "splash:color:label".
2440 \param section parent sub-section name
2441 \param full if \c true return full names of child sub-sections, if \c false,
2442 return only top-level sub-sections names
2443 \return list of sub-sections names
2445 QStringList QtxResourceMgr::subSections(const QString& section, const bool full) const
2447 QStringList names = sections( QStringList() << section );
2448 QMutableListIterator<QString> it( names );
2449 while ( it.hasNext() ) {
2450 QString name = it.next().mid( section.size() + 1 ).trimmed();
2451 if ( name.isEmpty() ) {
2455 if ( !full ) name = name.split( sectionsToken() ).first();
2456 it.setValue( name );
2458 names.removeDuplicates();
2464 \brief Get all parameters name in specified section.
2465 \param sec section name
2466 \return list of settings names
2468 QStringList QtxResourceMgr::parameters( const QString& sec ) const
2472 #if defined(QTX_NO_INDEXED_MAP)
2473 typedef QMap<QString, int> PMap;
2475 typedef IMap<QString, int> PMap;
2479 Resources* ur = !myResources.isEmpty() && workingMode() == IgnoreUserValues ? myResources[0] : 0;
2481 QListIterator<Resources*> it( myResources );
2483 while ( it.hasPrevious() )
2485 Resources* r = it.previous();
2486 if ( r == ur ) break;
2487 QStringList lst = r->parameters( sec );
2488 for ( QStringList::ConstIterator itr = lst.begin(); itr != lst.end(); ++itr )
2489 #if defined(QTX_NO_INDEXED_MAP)
2490 if ( !pmap.contains( *itr ) ) pmap.insert( *itr, 0 );
2492 pmap.insert( *itr, 0, false );
2500 \brief Get all parameters name in specified
2501 list of sub-sections names.
2503 Sub-sections are separated inside the section name by the sections
2504 separator token, for example "splash:color:label".
2506 \param names parent sub-sections names
2507 \return list of settings names
2509 QStringList QtxResourceMgr::parameters( const QStringList& names ) const
2511 return parameters( names.join( sectionsToken() ) );
2515 \brief Get absolute path to the file which name is defined by the parameter.
2517 The file name is defined by \a name argument, while directory name is retrieved
2518 from resources parameter \a prefix of section \a sec. Both directory and file name
2519 can be relative. If the directory is relative, it is calculated from the initial
2520 resources file name. Directory parameter can contain environment
2521 variables, which are substituted automatically.
2523 \param sec section name
2524 \param prefix parameter containing directory name
2525 \param name file name
2526 \return absolute file path or null QString if file does not exist
2528 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
2532 ResList::ConstIterator it = myResources.begin();
2533 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2536 for ( ; it != myResources.end() && res.isEmpty(); ++it )
2537 res = (*it)->path( sect, prefix, name, myConstants );
2542 \brief Get application resources section name.
2544 By default, application resources section name is "resources" but
2545 it can be changed by setting the "res_section_name" resources manager option.
2547 \return section corresponding to the resources directories
2548 \sa option(), setOption()
2550 QString QtxResourceMgr::resSection() const
2552 QString res = option( "res_section_name" );
2553 if ( res.isEmpty() )
2554 res = QString( "resources" );
2559 \brief Get application language section name.
2561 By default, application language section name is "language" but
2562 it can be changed by setting the "lang_section_name" resources manager option.
2564 \return section corresponding to the application language settings
2565 \sa option(), setOption()
2567 QString QtxResourceMgr::langSection() const
2569 QString res = option( "lang_section_name" );
2570 if ( res.isEmpty() )
2571 res = QString( "language" );
2576 \brief Get sections separator token.
2578 By default, sections separator token is colon symbol ":" but
2579 it can be changed by setting the "section_token" resources manager option.
2581 \return string corresponding to the current section separator token
2582 \sa option(), setOption()
2584 QString QtxResourceMgr::sectionsToken() const
2586 QString res = option( "section_token" );
2587 if ( res.isEmpty() )
2588 res = QString( ":" );
2593 \brief Get default pixmap.
2595 Default pixmap is used when requested pixmap resource is not found.
2597 \return default pixmap
2598 \sa setDefaultPixmap(), loadPixmap()
2600 QPixmap QtxResourceMgr::defaultPixmap() const
2602 static QPixmap* defpx = 0;
2604 defpx = new QPixmap( pixmap_not_found_xpm );
2606 return myDefaultPix ? *myDefaultPix : *defpx;
2610 \brief Set default pixmap.
2612 Default pixmap is used when requested pixmap resource is not found.
2614 \param pix default pixmap
2615 \sa defaultPixmap(), loadPixmap()
2617 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
2619 delete myDefaultPix;
2623 myDefaultPix = new QPixmap( pix );
2627 \brief Load pixmap resource.
2628 \param prefix parameter which refers to the resources directory (directories)
2629 \param name pixmap file name
2630 \return pixmap loaded from the file
2631 \sa defaultPixmap(), setDefaultPixmap()
2633 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
2635 return loadPixmap( prefix, name, true );
2639 \brief Load pixmap resource.
2641 \param prefix parameter which refers to the resources directory (directories)
2642 \param name pixmap file name
2643 \param useDef if \c false, default pixmap is not returned if resource is not found,
2644 in this case null pixmap is returned instead
2645 \return pixmap loaded from the file
2646 \sa defaultPixmap(), setDefaultPixmap()
2648 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
2650 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
2654 \brief Load pixmap resource.
2656 \param prefix parameter which refers to the resources directory (directories)
2657 \param name pixmap file name
2658 \param defPix default which should be used if the resource file doesn't exist
2659 \return pixmap loaded from the file
2660 \sa defaultPixmap(), setDefaultPixmap()
2662 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
2668 ResList::ConstIterator it = myResources.begin();
2669 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2672 for ( ; it != myResources.end() && pix.isNull(); ++it )
2673 pix = (*it)->loadPixmap( resSection(), prefix, name, myConstants );
2680 \brief Specify default language for the application.
2682 QString QtxResourceMgr::defaultLanguage() const
2688 \brief Load translation files according to the specified language.
2690 Names of the translation files are calculated according to the pattern specified
2691 by the "translators" option (this option is read from the section "language" of resources files).
2692 By default, "%P_msg_%L.qm" pattern is used.
2693 Keywords \%A, \%P, \%L in the pattern are substituted by the application name, prefix and language name
2695 For example, for prefix "SUIT" and language "en", all translation files "SUIT_msg_en.qm" are searched and
2698 If prefix is empty or null string, all translation files specified in the "resources" section of resources
2699 files are loaded (actually, the section is retrieved from resSection() method).
2700 If language is not specified, it is retrieved from the langSection() method, and if the latest is also empty,
2701 by default "en" (English) language is used.
2702 By default, settings from the user preferences file are also loaded (if user resource file is valid,
2703 see userFileName()). To avoid loading user settings, pass \c false as first parameter.
2705 \param pref parameter which defines translation context (for example, package name)
2706 \param l language name
2708 \sa resSection(), langSection(), loadTranslators()
2710 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
2714 QMap<QChar, QString> substMap;
2715 substMap.insert( 'A', appName() );
2718 if ( lang.isEmpty() )
2719 lang = defaultLanguage();
2720 if ( lang.isEmpty() )
2721 value( langSection(), "language", lang );
2723 if ( lang.isEmpty() )
2725 lang = QString( "en" );
2726 qWarning() << "QtxResourceMgr: Language not specified. Assumed:" << lang;
2729 substMap.insert( 'L', lang );
2732 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
2734 QStringList translators = option( "translators" ).split( "|", QString::SkipEmptyParts );
2735 QStringList newTranslators = trs.split( "|", QString::SkipEmptyParts );
2736 for ( int i = 0; i < (int)newTranslators.count(); i++ )
2738 if ( translators.indexOf( newTranslators[i] ) < 0 )
2739 translators += newTranslators[i];
2741 setOption( "translators", translators.join( "|" ) );
2744 QStringList trList = option( "translators" ).split( "|", QString::SkipEmptyParts );
2745 if ( trList.isEmpty() )
2747 trList.append( "%P_msg_%L.qm" );
2748 qWarning() << "QtxResourceMgr: Translators not defined. Assumed:" << trList[0];
2751 QStringList prefixList;
2752 if ( !pref.isEmpty() )
2753 prefixList.append( pref );
2755 prefixList = parameters( resSection() );
2757 if ( pref.isEmpty() && lang != "en" ) {
2758 // load Qt resources
2759 QString qt_translations = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
2760 QString qt_dir_trpath = Qtx::qtDir( "translations" );
2761 QTranslator* trans = new QtxTranslator( 0 );
2762 if ( trans->load( QString("qt_%1").arg( lang ), qt_translations ) || trans->load( QString("qt_%1").arg( lang ), qt_dir_trpath ) ) {
2763 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2767 for ( QStringList::ConstIterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
2769 QString prefix = *iter;
2770 substMap.insert( 'P', prefix );
2773 for ( QStringList::ConstIterator it = trList.begin(); it != trList.end(); ++it )
2774 trs.append( substMacro( *it, substMap ).trimmed() );
2776 loadTranslators( prefix, trs );
2781 \brief Load translation files for the specified translation context.
2782 \param prefix parameter which defines translation context (for example, package name)
2783 \param translators list of translation files
2786 void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators )
2792 ResList::ConstIterator iter = myResources.begin();
2793 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2796 for ( ; iter != myResources.end(); ++iter )
2797 lst.prepend( *iter );
2799 QTranslator* trans = 0;
2801 for ( ResList::Iterator it = lst.begin(); it != lst.end(); ++it )
2803 for ( QStringList::ConstIterator itr = translators.begin(); itr != translators.end(); ++itr )
2805 trans = (*it)->loadTranslator( resSection(), prefix, *itr, myConstants );
2808 if ( !myTranslator[prefix].contains( trans ) )
2809 myTranslator[prefix].append( trans );
2810 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2817 \brief Load translation file.
2818 \param prefix parameter which defines translation context (for example, package name)
2819 \param name translator file name
2820 \sa loadLanguage(), loadTranslators()
2822 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
2826 QTranslator* trans = 0;
2828 Resources* ur = !myResources.isEmpty() && workingMode() == IgnoreUserValues ? myResources[0] : 0;
2830 QListIterator<Resources*> it( myResources );
2832 while ( it.hasPrevious() )
2834 Resources* r = it.previous();
2835 if ( r == ur ) break;
2837 trans = r->loadTranslator( resSection(), prefix, name, myConstants );
2840 if ( !myTranslator[prefix].contains( trans ) )
2841 myTranslator[prefix].append( trans );
2842 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2848 \brief Add custom translator.
2849 \param prefix parameter which defines translation context (for example, package name)
2850 \param translator translator being installed
2851 \sa loadLanguage(), loadTranslators()
2853 void QtxResourceMgr::addTranslator( const QString& prefix, QTranslator* translator )
2857 if ( !myTranslator[prefix].contains( translator ) ) {
2858 myTranslator[prefix].append( translator );
2859 if ( QApplication::instance() )
2860 QApplication::instance()->installTranslator( translator );
2866 \brief Remove all translators corresponding to the specified translation context.
2867 \param prefix parameter which defines translation context (for example, package name)
2869 void QtxResourceMgr::removeTranslators( const QString& prefix )
2871 if ( !myTranslator.contains( prefix ) )
2874 for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
2876 if ( QApplication::instance() ) QApplication::instance()->removeTranslator( *it );
2880 myTranslator.remove( prefix );
2884 \brief Move all translators corresponding to the specified translation context
2885 to the top of translators stack (increase their priority).
2886 \param prefix parameter which defines translation context (for example, package name)
2888 void QtxResourceMgr::raiseTranslators( const QString& prefix )
2890 if ( !myTranslator.contains( prefix ) )
2893 for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
2895 if ( QApplication::instance() ) {
2896 QApplication::instance()->removeTranslator( *it );
2897 QApplication::instance()->installTranslator( *it );
2903 \brief Copy all parameters to the user resources in order to
2904 saved them lately in the user home folder.
2906 void QtxResourceMgr::refresh()
2908 QStringList sl = sections();
2909 for ( QStringList::ConstIterator it = sl.begin(); it != sl.end(); ++it )
2911 QStringList pl = parameters( *it );
2912 for ( QStringList::ConstIterator itr = pl.begin(); itr != pl.end(); ++itr )
2913 setResource( *it, *itr, stringValue( *it, *itr ) );
2918 \brief Set the resource directories (where global confguration files are searched).
2920 This function also clears all currently set resources.
2922 \param dl directories list
2924 void QtxResourceMgr::setDirList( const QStringList& dl )
2927 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2930 myResources.clear();
2934 \brief Set parameter value.
2935 \param sect section name
2936 \param name parameter name
2937 \param val parameter value
2939 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
2943 if ( !myResources.isEmpty() && myHasUserValues )
2944 myResources.first()->setValue( sect, name, val );
2948 \brief Get user configuration file name.
2950 This method can be redefined in the successor class to customize the user configuration file name.
2951 User configuration file is always situated in the user's home directory. By default .<appName>rc
2952 file is used on Linux (e.g. .MyApprc) and <appName>.<format> under Windows (e.g. MyApp.xml).
2954 Parameter \a for_load (not used in default implementation) specifies the usage mode, i.e. if
2955 user configuration file is opened for reading or writing. This allows customizing a way of application
2956 resources initializing (for example, if the user configuraion file includes version number and there is
2957 no file corresponding to this version in the user's home directory, it could be good idea to try
2958 the configuration file from the previous versions of the application).
2960 \param appName application name
2961 \param for_load boolean flag indicating that file is opened for loading or saving (not used in default implementation)
2962 \return user configuration file name
2963 \sa globalFileName()
2965 QString QtxResourceMgr::userFileName( const QString& appName, const bool /*for_load*/ ) const
2968 QString pathName = QDir::homePath();
2969 QString cfgAppName = QApplication::applicationName();
2970 if ( !cfgAppName.isEmpty() )
2971 pathName = Qtx::addSlash( Qtx::addSlash( pathName ) + QString( ".config" ) ) + cfgAppName;
2974 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
2976 fileName = QString( "%1rc" ).arg( appName );
2977 // VSR 24/09/2012: issue 0021781: do not prepend filename with "."
2978 // when user file is stored in ~/.config/<appname> directory
2979 if ( cfgAppName.isEmpty() )
2980 fileName.prepend( "." );
2983 if ( !fileName.isEmpty() )
2984 pathName = Qtx::addSlash( pathName ) + fileName;
2990 \brief Get global configuration file name.
2992 This method can be redefined in the successor class to customize the global configuration file name.
2993 Global configuration files are searched in the directories specified by the application resources
2994 environment variable (e.g. MyAppResources). By default <appName>.<format> file name is used
2997 \param appName application name
2998 \return global configuration file name
3001 QString QtxResourceMgr::globalFileName( const QString& appName ) const
3003 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
3007 \brief This function is called after user configuration file is saved.
3008 Can be redefined in the successor classes, default implementation does nothing.
3010 void QtxResourceMgr::saved()
3015 \brief Perform substitution of the patterns like \%A, \%B, etc by values from the map.
3017 Used by loadLanguage().
3019 \param src sring to be processed
3020 \param substMap map of values for replacing
3021 \return processed string
3023 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
3027 QRegExp rx( "%[A-Za-z%]" );
3030 while ( ( idx = rx.indexIn( trg, idx ) ) >= 0 )
3032 QChar spec = trg.at( idx + 1 );
3036 else if ( substMap.contains( spec ) )
3037 subst = substMap[spec];
3039 if ( !subst.isEmpty() )
3041 trg.replace( idx, rx.matchedLength(), subst );
3042 idx += subst.length();
3045 idx += rx.matchedLength();