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 QString tmpValue = Qtx::getenv( envName );
466 if ( newStr.isEmpty() && !tmpValue.isEmpty() )
469 if ( newStr.isEmpty() )
471 if ( ignoreMap.contains( envName ) )
477 if ( hasValue( sect, envName ) )
478 newStr = value( sect, envName, false, constants );
479 ignoreMap.insert( envName, 0 );
481 res.replace( start, len, newStr );
484 res.replace( "$$", "$" );
485 res.replace( "%%", "%" );
491 \class QtxResourceMgr::IniFormat
493 \brief Reader/writer for .ini resources files.
496 class QtxResourceMgr::IniFormat : public Format
503 virtual bool load( const QString&, QMap<QString, Section>& );
504 virtual bool save( const QString&, const QMap<QString, Section>& );
507 bool load( const QString&, QMap<QString, Section>&, QSet<QString>& );
513 QtxResourceMgr::IniFormat::IniFormat()
521 QtxResourceMgr::IniFormat::~IniFormat()
526 \brief Load resources from ini-file.
527 \param fname resources file name
528 \param secMap resources map to be filled in
529 \return \c true on success and \c false on error
531 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap )
533 QSet<QString> importHistory;
534 return load( fname, secMap, importHistory );
539 \brief Load resources from xml-file.
540 \param fname resources file name
541 \param secMap resources map to be filled in
542 \param importHistory list of already imported resources files (to prevent import loops)
543 \return \c true on success or \c false on error
545 bool QtxResourceMgr::IniFormat::load( const QString& fname, QMap<QString, Section>& secMap, QSet<QString>& importHistory )
547 QString aFName = fname.trimmed();
548 if ( !QFileInfo( aFName ).exists() )
550 if ( QFileInfo( aFName + ".ini" ).exists() )
552 else if ( QFileInfo( aFName + ".INI" ).exists() )
555 return false; // file does not exist
557 QFileInfo aFinfo( aFName );
558 aFName = aFinfo.canonicalFilePath();
560 if ( !importHistory.contains( aFName ) )
561 importHistory.insert( aFName );
563 return true; // already imported (prevent import loops)
565 QFile file( aFName );
566 if ( !file.open( QFile::ReadOnly ) )
567 return false; // file is not accessible
569 QTextStream ts( &file );
576 QString separator = option( "separator" );
577 if ( separator.isNull() )
578 separator = QString( "=" );
580 QString comment = option( "comment" );
581 if ( comment.isNull() )
582 comment = QString( "#" );
586 data = ts.readLine();
592 data = data.trimmed();
593 if ( data.isEmpty() )
596 if ( data.startsWith( comment ) )
599 QRegExp rx( "^\\[([\\w\\s\\._]*)\\]$" );
600 if ( rx.indexIn( data ) != -1 )
602 section = rx.cap( 1 );
603 if ( section.isEmpty() )
606 qWarning() << "QtxResourceMgr: Empty section in line:" << line;
609 else if ( data.contains( separator ) && !section.isEmpty() )
611 int pos = data.indexOf( separator );
612 QString key = data.left( pos ).trimmed();
613 QString val = data.mid( pos + 1 ).trimmed();
614 secMap[section].insert( key, val );
616 else if ( section == "import" )
618 QString impFile = QDir::toNativeSeparators( Qtx::makeEnvVarSubst( data, Qtx::Always ) );
619 QFileInfo impFInfo( impFile );
620 if ( impFInfo.isRelative() )
621 impFInfo.setFile( aFinfo.absoluteDir(), impFile );
623 QMap<QString, Section> impMap;
624 if ( !load( impFInfo.absoluteFilePath(), impMap, importHistory ) )
626 qDebug() << "QtxResourceMgr: Error with importing file:" << data;
630 QMap<QString, Section>::const_iterator it = impMap.constBegin();
631 for ( ; it != impMap.constEnd() ; ++it )
633 if ( !secMap.contains( it.key() ) )
635 // insert full section
636 secMap.insert( it.key(), it.value() );
640 // insert all parameters from the section
641 Section::ConstIterator paramIt = it.value().begin();
642 for ( ; paramIt != it.value().end() ; ++paramIt )
644 if ( !secMap[it.key()].contains( paramIt.key() ) )
645 secMap[it.key()].insert( paramIt.key(), paramIt.value() );
654 if ( section.isEmpty() )
655 qWarning() << "QtxResourceMgr: Current section is empty";
657 qWarning() << "QtxResourceMgr: Error in line:" << line;
667 \brief Save resources to the ini-file.
668 \param fname resources file name
669 \param secMap resources map
670 \return \c true on success and \c false on error
672 bool QtxResourceMgr::IniFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
674 if ( !Qtx::mkDir( QFileInfo( fname ).absolutePath() ) )
678 if ( !file.open( QFile::WriteOnly ) )
681 QTextStream ts( &file );
683 ts << "# This file is automatically created by SALOME application." << endl;
684 ts << "# Changes made in this file can be lost!" << endl;
688 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end() && res; ++it )
690 QStringList data( QString( "[%1]" ).arg( it.key() ) );
691 for ( Section::ConstIterator iter = it.value().begin(); iter != it.value().end(); ++iter )
692 data.append( iter.key() + " = " + iter.value() );
695 for ( QStringList::ConstIterator itr = data.begin(); itr != data.end(); ++itr )
705 \class QtxResourceMgr::XmlFormat
707 \brief Reader/writer for .xml resources files.
710 class QtxResourceMgr::XmlFormat : public Format
717 virtual bool load( const QString&, QMap<QString, Section>& );
718 virtual bool save( const QString&, const QMap<QString, Section>& );
721 QString docTag() const;
722 QString sectionTag() const;
723 QString parameterTag() const;
724 QString importTag() const;
725 QString nameAttribute() const;
726 QString valueAttribute() const;
728 bool load( const QString&, QMap<QString, Section>&, QSet<QString>& );
734 QtxResourceMgr::XmlFormat::XmlFormat()
742 QtxResourceMgr::XmlFormat::~XmlFormat()
747 \brief Load resources from xml-file.
748 \param fname resources file name
749 \param secMap resources map to be filled in
750 \return \c true on success and \c false on error
752 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap )
754 QSet<QString> importHistory;
755 return load( fname, secMap, importHistory );
759 \brief Load resources from xml-file.
760 \param fname resources file name
761 \param secMap resources map to be filled in
762 \param importHistory list of already imported resources files (to prevent import loops)
763 \return \c true on success and \c false on error
765 bool QtxResourceMgr::XmlFormat::load( const QString& fname, QMap<QString, Section>& secMap, QSet<QString>& importHistory )
767 QString aFName = fname.trimmed();
768 if ( !QFileInfo( aFName ).exists() )
770 if ( QFileInfo( aFName + ".xml" ).exists() )
772 else if ( QFileInfo( aFName + ".XML" ).exists() )
775 return false; // file does not exist
777 QFileInfo aFinfo( aFName );
778 aFName = aFinfo.canonicalFilePath();
780 if ( !importHistory.contains( aFName ) )
781 importHistory.insert( aFName );
783 return true; // already imported (prevent import loops)
789 QFile file( aFName );
790 if ( !file.open( QFile::ReadOnly ) )
792 qDebug() << "QtxResourceMgr: File is not accessible:" << aFName;
798 res = doc.setContent( &file );
803 qDebug() << "QtxResourceMgr: File is empty:" << aFName;
807 QDomElement root = doc.documentElement();
808 if ( root.isNull() || root.tagName() != docTag() )
810 qDebug() << "QtxResourceMgr: Invalid root in file:" << aFName;
814 QDomNode sectNode = root.firstChild();
815 while ( res && !sectNode.isNull() )
817 res = sectNode.isElement();
820 QDomElement sectElem = sectNode.toElement();
821 if ( sectElem.tagName() == sectionTag() && sectElem.hasAttribute( nameAttribute() ) )
823 QString section = sectElem.attribute( nameAttribute() );
824 QDomNode paramNode = sectNode.firstChild();
825 while ( res && !paramNode.isNull() )
827 res = paramNode.isElement();
830 QDomElement paramElem = paramNode.toElement();
831 if ( paramElem.tagName() == parameterTag() &&
832 paramElem.hasAttribute( nameAttribute() ) && paramElem.hasAttribute( valueAttribute() ) )
834 QString paramName = paramElem.attribute( nameAttribute() );
835 QString paramValue = paramElem.attribute( valueAttribute() );
836 secMap[section].insert( paramName, paramValue );
840 qDebug() << "QtxResourceMgr: Invalid parameter element in file:" << aFName;
846 res = paramNode.isComment();
848 qDebug() << "QtxResourceMgr: Node is neither element nor comment in file:" << aFName;
851 paramNode = paramNode.nextSibling();
854 else if ( sectElem.tagName() == importTag() && sectElem.hasAttribute( nameAttribute() ) )
856 QString impFile = QDir::toNativeSeparators( Qtx::makeEnvVarSubst( sectElem.attribute( nameAttribute() ), Qtx::Always ) );
857 QFileInfo impFInfo( impFile );
858 if ( impFInfo.isRelative() )
859 impFInfo.setFile( aFinfo.absoluteDir(), impFile );
861 QMap<QString, Section> impMap;
862 if ( !load( impFInfo.absoluteFilePath(), impMap, importHistory ) )
864 qDebug() << "QtxResourceMgr: Error with importing file:" << sectElem.attribute( nameAttribute() );
868 QMap<QString, Section>::const_iterator it = impMap.constBegin();
869 for ( ; it != impMap.constEnd() ; ++it )
871 if ( !secMap.contains( it.key() ) )
873 // insert full section
874 secMap.insert( it.key(), it.value() );
878 // insert all parameters from the section
879 Section::ConstIterator paramIt = it.value().begin();
880 for ( ; paramIt != it.value().end() ; ++paramIt )
882 if ( !secMap[it.key()].contains( paramIt.key() ) )
883 secMap[it.key()].insert( paramIt.key(), paramIt.value() );
891 qDebug() << "QtxResourceMgr: Invalid section in file:" << aFName;
897 res = sectNode.isComment(); // if it's a comment -- let it be, pass it..
899 qDebug() << "QtxResourceMgr: Node is neither element nor comment in file:" << aFName;
902 sectNode = sectNode.nextSibling();
908 qDebug() << "QtxResourceMgr: File" << fname << "is loaded successfully";
913 \brief Save resources to the xml-file.
914 \param fname resources file name
915 \param secMap resources map
916 \return \c true on success and \c false on error
918 bool QtxResourceMgr::XmlFormat::save( const QString& fname, const QMap<QString, Section>& secMap )
924 if ( !Qtx::mkDir( QFileInfo( fname ).absolutePath() ) )
928 if ( !file.open( QFile::WriteOnly ) )
931 QDomDocument doc( docTag() );
932 QDomComment comment = doc.createComment( "\nThis file is automatically created by SALOME application.\nChanges made in this file can be lost!\n" );
933 doc.appendChild( comment );
934 QDomElement root = doc.createElement( docTag() );
935 doc.appendChild( root );
937 for ( QMap<QString, Section>::ConstIterator it = secMap.begin(); it != secMap.end(); ++it )
939 QDomElement sect = doc.createElement( sectionTag() );
940 sect.setAttribute( nameAttribute(), it.key() );
941 root.appendChild( sect );
942 for ( Section::ConstIterator iter = it.value().begin(); iter != it.value().end(); ++iter )
944 QDomElement val = doc.createElement( parameterTag() );
945 val.setAttribute( nameAttribute(), iter.key() );
946 val.setAttribute( valueAttribute(), iter.value() );
947 sect.appendChild( val );
951 QTextStream ts( &file );
952 QStringList docStr = doc.toString().split( "\n" );
953 for ( QStringList::ConstIterator itr = docStr.begin(); itr != docStr.end(); ++itr )
964 \brief Get document tag name
965 \return XML document tag name
967 QString QtxResourceMgr::XmlFormat::docTag() const
969 QString tag = option( "doc_tag" );
971 tag = QString( "document" );
976 \brief Get section tag name
977 \return XML section tag name
979 QString QtxResourceMgr::XmlFormat::sectionTag() const
981 QString tag = option( "section_tag" );
983 tag = QString( "section" );
988 \brief Get parameter tag name
989 \return XML parameter tag name
991 QString QtxResourceMgr::XmlFormat::parameterTag() const
993 QString tag = option( "parameter_tag" );
995 tag = QString( "parameter" );
1000 \brief Get import tag name
1001 \return XML import tag name
1003 QString QtxResourceMgr::XmlFormat::importTag() const
1005 QString tag = option( "import_tag" );
1006 if ( tag.isEmpty() )
1007 tag = QString( "import" );
1012 \brief Get parameter tag's "name" attribute name
1013 \return XML parameter tag's "name" attribute name
1015 QString QtxResourceMgr::XmlFormat::nameAttribute() const
1017 QString str = option( "name_attribute" );
1018 if ( str.isEmpty() )
1019 str = QString( "name" );
1024 \brief Get parameter tag's "value" attribute name
1025 \return XML parameter tag's "value" attribute name
1027 QString QtxResourceMgr::XmlFormat::valueAttribute() const
1029 QString str = option( "value_attribute" );
1030 if ( str.isEmpty() )
1031 str = QString( "value" );
1036 \class QtxResourceMgr::Format
1037 \brief Generic resources files reader/writer class.
1042 \param fmt format name (for example, "xml" or "ini")
1044 QtxResourceMgr::Format::Format( const QString& fmt )
1052 QtxResourceMgr::Format::~Format()
1057 \brief Get the format name.
1060 QString QtxResourceMgr::Format::format() const
1066 \brief Get options names.
1067 \return list of the format options
1069 QStringList QtxResourceMgr::Format::options() const
1071 return myOpt.keys();
1075 \brief Get the value of the option with specified name.
1077 If option doesn't exist then null QString is returned.
1079 \param opt option name
1080 \return option value
1082 QString QtxResourceMgr::Format::option( const QString& opt ) const
1085 if ( myOpt.contains( opt ) )
1091 \brief Set the value of the option with specified name.
1092 \param opt option name
1093 \param val option value
1095 void QtxResourceMgr::Format::setOption( const QString& opt, const QString& val )
1097 myOpt.insert( opt, val );
1101 \brief Load resources from the resource file.
1102 \param res resources object
1103 \return \c true on success and \c false on error
1105 bool QtxResourceMgr::Format::load( Resources* res )
1110 QMap<QString, Section> sections;
1111 bool status = load( res->myFileName, sections );
1113 res->mySections = sections;
1115 qDebug() << "QtxResourceMgr: Can't load resource file:" << res->myFileName;
1121 \brief Save resources to the resource file.
1122 \param res resources object
1123 \return \c true on success and \c false on error
1125 bool QtxResourceMgr::Format::save( Resources* res )
1130 Qtx::mkDir( Qtx::dir( res->myFileName ) );
1132 QtxResourceMgr* mgr = res->resMgr();
1133 QString name = mgr ? mgr->userFileName( mgr->appName(), false ) : res->myFileName;
1134 return save( name, res->mySections );
1138 \fn virtual bool QtxResourceMgr::Format::load( const QString& fname,
1139 QMap<QString, Section>& secMap )
1140 \brief Load resources from the specified resources file.
1142 Should be implemented in the successors.
1144 \param fname resources file name
1145 \param secMap resources map to be filled in
1146 \return \c true on success and \c false on error
1150 \fn virtual bool QtxResourceMgr::Format::save( const QString& fname,
1151 const QMap<QString, Section>& secMap )
1153 \brief Save resources to the specified resources file.
1155 Should be implemented in the successors.
1157 \param fname resources file name
1158 \param secMap resources map
1159 \return \c true on success and \c false on error
1163 \class QtxResourceMgr
1164 \brief Application resources manager.
1166 This class can be used to define settings, save/load settings and
1167 application preferences to the resource file(s), load translation files
1168 (internationalization mechanism), load pixmaps and other resources from
1169 external files, etc.
1171 Currently it supports .ini and .xml resources file formats. To implement
1172 own resources file format, inherit from the Format class and implement virtual
1173 Format::load() and Format::save() methods.
1175 Resources manager is initialized by the (symbolic) name of the application.
1176 The parameter \a resVarTemplate specifies the template for the environment
1177 variable which should point to the resource directory or list of directories.
1178 Environment variable name is calculated by substitution of "%1" substring in
1179 the \a resVarTemplate parameter (if it contains such substring) by the
1180 application name (\a appName).
1181 By default, \a resVarTemplate is set to "%1Resources". For example, if the application name
1182 is "MyApp", the environment variable "MyAppResources" will be inspected in this case.
1184 Resource manager can handle several global application configuration files and
1185 one user configuration file. Location of global configuration files is defined
1186 by the environment variable (see above) and these files are always read-only.
1187 The name of the global configuration files is retrieved by calling virtual method
1188 globalFileName() which can be redefined in the QtxResourceMgr class successors.
1189 User configuration file always situated in the user's home directory. It's name
1190 is defined by calling virtual method userFileName() which can be also redefined
1191 in the QtxResourceMgr class successors. This is the only file which the preferences
1192 changed by the user during the application session are written to (usually
1193 when the application closes).
1195 Resources environment variable should contain one or several resource directories
1196 (separated by ";" symbol on Windows and ":" or ";" on Linux). Each resource directory
1197 can contain application global configuration file. The user configuration file has
1198 the highest priority, for the global configuration files the priority is decreasing from
1199 left to right, i.e. the first directory in the directoris list, defined by the
1200 resources environment variable has higher priority. Priority has the meaning when
1201 searching requested resources (application preference, pixmap file name, translation
1204 When retrieving preferences, it is sometimes helpful to ignore values coming from the
1205 user preference file and take into account only global preferences.
1206 To do this, use setWorkingMode() method passing QtxResourceMgr::IgnoreUserValues enumerator
1209 Resources manager operates with such terms like options, sections and parameters.
1210 Parametets are named application resources, for example, application preferences like
1211 integer, double, boolean or string values, pictures, font and color definitions, etc.
1212 Parameters are organized inside the resources files into the named groups - sections.
1213 Options are special kind of resoures which allow customizing resource files interpreting.
1214 For example, by default language settings are defined in the resource file in the
1215 section "language". It is possible to change this section name by setting "language"
1216 option to another value (see setOption()).
1218 Retrieving preferences values can be done by using one of value() methods, each returns
1219 \c true if the corresponding preference is found. Another way is to use integerValue(),
1220 doubleValue(), etc methods, which allow specifying default value which is used if the
1221 specified preference is not found. Removing of preferences or sections can be done using
1222 remove(const QString& sect) or remove(const QString& sect, const QString& name) methods.
1223 To add the preference or to change exiting preference value use setValue() methods family.
1224 Methods hasSection() and hasValue() can be used to check existence of section or
1225 preference (in the specified section). List of all sections can be retrieved with the
1226 sections() method, and list of all settings names in some specified section can be
1227 obtained with parameters() method.
1229 Pixmaps can be loaded with the loadPixmap() methods. If the specified pixmap is not found,
1230 the default one is returned. Default pixmap can be set by setDefaultPixmap().
1232 One of the key feature of the resources manager is support of application
1233 internationalization mechanism. Translation files for the specified language can be loaded
1234 with loadLanguage() method.
1238 \brief Constructs the resource manager.
1239 \param appName application name
1240 \param resVarTemplate resource environment variable pattern
1242 QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate )
1243 : myAppName( appName ),
1244 myCheckExist( true ),
1246 myIsPixmapCached( true ),
1247 myHasUserValues( true ),
1248 myWorkingMode( AllowUserValues )
1250 QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" );
1251 if ( envVar.contains( "%1" ) )
1252 envVar = envVar.arg( appName );
1255 QString tmpValue = Qtx::getenv( envVar );
1256 if ( !tmpValue.isEmpty() )
1259 QString dirsep = ";"; // for Windows: ";" is used as directories separator
1261 QString dirsep = "[:|;]"; // for Linux: both ":" and ";" can be used
1263 setDirList( dirs.split( QRegExp( dirsep ), QString::SkipEmptyParts ) );
1265 installFormat( new XmlFormat() );
1266 installFormat( new IniFormat() );
1268 setOption( "translators", QString( "%P_msg_%L.qm|%P_images.qm" ) );
1274 Destroy the resource manager and free allocated memory.
1276 QtxResourceMgr::~QtxResourceMgr()
1278 QStringList prefList = myTranslator.keys();
1279 for ( QStringList::ConstIterator it = prefList.begin(); it != prefList.end(); ++it )
1280 removeTranslators( *it );
1282 qDeleteAll( myResources );
1283 qDeleteAll( myFormats );
1285 delete myDefaultPix;
1289 \brief Get the application name.
1290 \return application name
1292 QString QtxResourceMgr::appName() const
1298 \brief Get the "check existance" flag
1300 If this flag is \c true then preference can be set (with setValue() method)
1301 only if it doesn't exist or if the value is changed.
1303 \return \c true if "check existance" flag is set
1305 bool QtxResourceMgr::checkExisting() const
1307 return myCheckExist;
1311 \brief Set the "check existance" flag.
1312 \param on new flag value
1314 void QtxResourceMgr::setCheckExisting( const bool on )
1320 \brief Get the resource directories list.
1322 Home user directory (where the user application configuration file is situated)
1323 is not included. This is that directories list defined by the application
1324 resources environment variable.
1326 \return list of directories names
1328 QStringList QtxResourceMgr::dirList() const
1334 \brief Initialise resources manager.
1336 Prepare the resources containers and load resources (if \a autoLoad is \c true).
1338 \param autoLoad if \c true (default) then all resources are loaded
1340 void QtxResourceMgr::initialize( const bool autoLoad ) const
1342 if ( !myResources.isEmpty() )
1345 QtxResourceMgr* that = (QtxResourceMgr*)this;
1347 if ( !userFileName( appName() ).isEmpty() )
1348 that->myResources.append( new Resources( that, userFileName( appName() ) ) );
1350 that->myHasUserValues = myResources.count() > 0;
1352 for ( QStringList::ConstIterator it = myDirList.begin(); it != myDirList.end(); ++it )
1354 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
1355 that->myResources.append( new Resources( that, path ) );
1363 \brief Get "cached pixmaps" option value.
1365 Resources manager allows possibility to cache loaded pixmaps that allow to
1366 improve application performance. This feature is turned on by default - all
1367 loaded pixmaps are stored in the internal map. Switching of this feature on/off
1368 can be done by setIsPixmapCached() method.
1370 \return \c true if pixmap cache is turned on
1371 \sa setIsPixmapCached()
1373 bool QtxResourceMgr::isPixmapCached() const
1375 return myIsPixmapCached;
1379 \brief Switch "cached pixmaps" option on/off.
1380 \param on enable pixmap cache if \c true and disable it if \c false
1381 \sa isPixmapCached()
1383 void QtxResourceMgr::setIsPixmapCached( const bool on )
1385 myIsPixmapCached = on;
1389 \brief Remove all resources from the resources manager.
1391 void QtxResourceMgr::clear()
1393 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
1398 \brief Get current working mode.
1400 \return current working mode
1401 \sa setWorkingMode(), value(), hasValue(), hasSection(), setValue()
1403 QtxResourceMgr::WorkingMode QtxResourceMgr::workingMode() const
1405 return myWorkingMode;
1409 \brief Set resource manager's working mode.
1411 The resource manager can operate in the following working modes:
1412 * AllowUserValues : methods values(), hasValue(), hasSection() take into account user values (default)
1413 * IgnoreUserValues : methods values(), hasValue(), hasSection() do not take into account user values
1415 Note, that setValue() method always put the value to the user settings file.
1417 \param mode new working mode
1418 \return previous working mode
1419 \sa workingMode(), value(), hasValue(), hasSection(), setValue()
1421 QtxResourceMgr::WorkingMode QtxResourceMgr::setWorkingMode( WorkingMode mode )
1423 WorkingMode m = myWorkingMode;
1424 myWorkingMode = mode;
1429 \brief Get interger parameter value.
1430 \param sect section name
1431 \param name parameter name
1432 \param iVal parameter to return resulting integer value
1433 \return \c true if parameter is found and \c false if parameter is not found
1434 (in this case \a iVal value is undefined)
1436 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
1439 if ( !value( sect, name, val, true ) )
1443 iVal = val.toInt( &ok );
1449 \brief Get double parameter value.
1450 \param sect section name
1451 \param name parameter name
1452 \param dVal parameter to return resulting double value
1453 \return \c true if parameter is found and \c false if parameter is not found
1454 (in this case \a dVal value is undefined)
1456 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
1459 if ( !value( sect, name, val, true ) )
1463 dVal = val.toDouble( &ok );
1469 \brief Get boolean parameter value.
1470 \param sect section name
1471 \param name parameter name
1472 \param bVal parameter to return resulting boolean value
1473 \return \c true if parameter is found and \c false if parameter is not found
1474 (in this case \a bVal value is undefined)
1476 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
1479 if ( !value( sect, name, val, true ) )
1482 static QMap<QString, bool> boolMap;
1483 if ( boolMap.isEmpty() )
1485 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
1486 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
1489 val = val.toLower();
1490 bool res = boolMap.contains( val );
1492 bVal = boolMap[val];
1495 double num = val.toDouble( &res );
1504 \brief Get color parameter value.
1505 \param sect section name
1506 \param name parameter name
1507 \param cVal parameter to return resulting color value
1508 \return \c true if parameter is found and \c false if parameter is not found
1509 (in this case \a cVal value is undefined)
1511 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
1514 if ( !value( sect, name, val, true ) )
1517 return Qtx::stringToColor( val, cVal );
1521 \brief Get font parameter value.
1522 \param sect section name
1523 \param name parameter name
1524 \param fVal parameter to return resulting font value
1525 \return \c true if parameter is found and \c false if parameter is not found
1526 (in this case \a fVal value is undefined)
1528 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
1531 if ( !value( sect, name, val, true ) )
1534 QStringList fontDescr = val.split( ",", QString::SkipEmptyParts );
1536 if ( fontDescr.count() < 2 )
1539 QString family = fontDescr[0];
1540 if ( family.isEmpty() )
1543 fVal = QFont( family );
1545 for ( int i = 1; i < (int)fontDescr.count(); i++ )
1547 QString curval = fontDescr[i].trimmed().toLower();
1548 if ( curval == QString( "bold" ) )
1549 fVal.setBold( true );
1550 else if ( curval == QString( "italic" ) )
1551 fVal.setItalic( true );
1552 else if ( curval == QString( "underline" ) )
1553 fVal.setUnderline( true );
1554 else if ( curval == QString( "shadow" ) || curval == QString( "overline" ) )
1555 fVal.setOverline( true );
1559 int ps = curval.toInt( &isOk );
1561 fVal.setPointSize( ps );
1569 \brief Get byte array parameter value.
1570 \param sect section name
1571 \param name parameter name
1572 \param baVal parameter to return resulting byte array value
1573 \return \c true if parameter is found and \c false if parameter is not found
1574 (in this case \a baVal value is undefined)
1576 bool QtxResourceMgr::value( const QString& sect, const QString& name, QByteArray& baVal ) const
1579 if ( !value( sect, name, val, true ) )
1582 if ( val.startsWith( "@ByteArray(" ) && val.endsWith( ')' ) ) {
1583 baVal = QByteArray( val.midRef( 11, val.size() - 12 ).toLatin1() );
1586 if ( val.startsWith( "@ByteArray[" ) && val.endsWith( ']' ) ) {
1587 val = val.mid( 11, val.size() - 12 );
1590 QStringList lst = val.split( QRegExp( "[\\s|,]" ), QString::SkipEmptyParts );
1591 for ( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it )
1595 if ( str.startsWith( "#" ) )
1601 int num = str.toInt( &ok, base );
1602 if ( !ok || num < 0 || num > 255 )
1605 baVal.append( (char)num );
1608 return !baVal.isEmpty();
1612 \brief Get linear gradient parameter value.
1613 \param sect section name
1614 \param name parameter name
1615 \param gVal parameter to return resulting linear gradient value
1616 \return \c true if parameter is found and \c false if parameter is not found
1617 (in this case \a gVal value is undefined)
1619 bool QtxResourceMgr::value( const QString& sect, const QString& name, QLinearGradient& gVal ) const
1622 if ( !value( sect, name, val, true ) )
1625 return Qtx::stringToLinearGradient( val, gVal );
1629 \brief Get radial gradient parameter value.
1630 \param sect section name
1631 \param name parameter name
1632 \param gVal parameter to return resulting radial gradient value
1633 \return \c true if parameter is found and \c false if parameter is not found
1634 (in this case \a gVal value is undefined)
1636 bool QtxResourceMgr::value( const QString& sect, const QString& name, QRadialGradient& gVal ) const
1639 if ( !value( sect, name, val, true ) )
1642 return Qtx::stringToRadialGradient( val, gVal );
1646 \brief Get conical gradient parameter value.
1647 \param sect section name
1648 \param name parameter name
1649 \param gVal parameter to return resulting conical gradient value
1650 \return \c true if parameter is found and \c false if parameter is not found
1651 (in this case \a gVal value is undefined)
1653 bool QtxResourceMgr::value( const QString& sect, const QString& name, QConicalGradient& gVal ) const
1656 if ( !value( sect, name, val, true ) )
1659 return Qtx::stringToConicalGradient( val, gVal );
1663 \brief Get background parameter value.
1664 \param sect section name
1665 \param name parameter name
1666 \param bgVal parameter to return resulting background value
1667 \return \c true if parameter is found and \c false if parameter is not found
1668 (in this case \a bgVal value is undefined)
1670 bool QtxResourceMgr::value( const QString& sect, const QString& name, Qtx::BackgroundData& bgVal ) const
1673 if ( !value( sect, name, val, true ) )
1676 bgVal = Qtx::stringToBackground( val );
1677 return bgVal.isValid();
1681 \brief Get string parameter value (native format).
1682 \param sect section name
1683 \param name parameter name
1684 \param val parameter to return resulting byte array value
1685 \param subst if \c true perform environment variables substitution
1686 \return \c true if parameter is found and \c false if parameter is not found
1687 (in this case \a val value is undefined)
1689 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
1695 ResList::ConstIterator it = myResources.begin();
1696 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1699 for ( ; it != myResources.end() && !ok; ++it )
1701 ok = (*it)->hasValue( sect, name );
1703 val = (*it)->value( sect, name, subst, myConstants );
1710 \brief Get interger parameter value.
1712 If the specified parameter is not found or can not be converted to the integer value,
1713 the specified default value is returned instead.
1715 \param sect section name
1716 \param name parameter name
1717 \param def default value
1718 \return parameter value (or default value if parameter is not found)
1720 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
1723 if ( !value( sect, name, val ) )
1729 \brief Get double parameter value.
1731 If the specified parameter is not found or can not be converted to the double value,
1732 the specified default value is returned instead.
1734 \param sect section name
1735 \param name parameter name
1736 \param def default value
1737 \return parameter value (or default value if parameter is not found)
1739 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
1742 if ( !value( sect, name, val ) )
1748 \brief Get boolean parameter value.
1750 If the specified parameter is not found or can not be converted to the boolean value,
1751 the specified default value is returned instead.
1753 \param sect section name
1754 \param name parameter name
1755 \param def default value
1756 \return parameter value (or default value if parameter is not found)
1758 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
1761 if ( !value( sect, name, val ) )
1767 \brief Get font parameter value.
1769 If the specified parameter is not found or can not be converted to the font value,
1770 the specified default value is returned instead.
1772 \param sect section name
1773 \param name parameter name
1774 \param def default value
1775 \return parameter value (or default value if parameter is not found)
1777 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
1780 if( !value( sect, name, font ) )
1786 \brief Get color parameter value.
1788 If the specified parameter is not found or can not be converted to the color value,
1789 the specified default value is returned instead.
1791 \param sect section name
1792 \param name parameter name
1793 \param def default value
1794 \return parameter value (or default value if parameter is not found)
1796 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
1799 if ( !value( sect, name, val ) )
1805 \brief Get string parameter value.
1807 If the specified parameter is not found, the specified default value is returned instead.
1809 \param sect section name
1810 \param name parameter name
1811 \param def default value
1812 \return parameter value (or default value if parameter is not found)
1814 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def, const bool subst ) const
1817 if ( !value( sect, name, val, subst ) )
1823 \brief Get byte array parameter value.
1825 If the specified parameter is not found, the specified default value is returned instead.
1827 \param sect section name
1828 \param name parameter name
1829 \param def default value
1830 \return parameter value (or default value if parameter is not found)
1832 QByteArray QtxResourceMgr::byteArrayValue( const QString& sect, const QString& name, const QByteArray& def ) const
1835 if ( !value( sect, name, val ) )
1841 \brief Get linear gradient parameter value.
1843 If the specified parameter is not found, the specified default value is returned instead.
1845 \param sect section name
1846 \param name parameter name
1847 \param def default value
1848 \return parameter value (or default value if parameter is not found)
1850 QLinearGradient QtxResourceMgr::linearGradientValue( const QString& sect, const QString& name, const QLinearGradient& def ) const
1852 QLinearGradient val;
1853 if ( !value( sect, name, val ) )
1859 \brief Get radial gradient parameter value.
1861 If the specified parameter is not found, the specified default value is returned instead.
1863 \param sect section name
1864 \param name parameter name
1865 \param def default value
1866 \return parameter value (or default value if parameter is not found)
1868 QRadialGradient QtxResourceMgr::radialGradientValue( const QString& sect, const QString& name, const QRadialGradient& def ) const
1870 QRadialGradient val;
1871 if ( !value( sect, name, val ) )
1877 \brief Get conical gradient parameter value.
1879 If the specified parameter is not found, the specified default value is returned instead.
1881 \param sect section name
1882 \param name parameter name
1883 \param def default value
1884 \return parameter value (or default value if parameter is not found)
1886 QConicalGradient QtxResourceMgr::conicalGradientValue( const QString& sect, const QString& name, const QConicalGradient& def ) const
1888 QConicalGradient val;
1889 if ( !value( sect, name, val ) )
1895 \brief Get background parameter value.
1897 If the specified parameter is not found, the specified default value is returned instead.
1899 \param sect section name
1900 \param name parameter name
1901 \param def default value
1902 \return parameter value (or default value if parameter is not found)
1904 Qtx::BackgroundData QtxResourceMgr::backgroundValue( const QString& sect, const QString& name, const Qtx::BackgroundData& def ) const
1906 Qtx::BackgroundData val;
1907 if ( !value( sect, name, val ) )
1913 \brief Check parameter existence.
1914 \param sect section name
1915 \param name parameter name
1916 \return \c true if parameter exists in specified section
1918 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
1924 ResList::ConstIterator it = myResources.begin();
1925 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1928 for ( ; it != myResources.end() && !ok; ++it )
1929 ok = (*it)->hasValue( sect, name );
1935 \brief Check section existence.
1936 \param sect section name
1937 \return \c true if section exists
1939 bool QtxResourceMgr::hasSection( const QString& sect ) const
1945 ResList::ConstIterator it = myResources.begin();
1946 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1949 for ( ; it != myResources.end() && !ok; ++it )
1950 ok = (*it)->hasSection( sect );
1956 \brief Set integer parameter value.
1957 \param sect section name
1958 \param name parameter name
1959 \param val parameter value
1961 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1964 if ( checkExisting() && value( sect, name, res ) && res == val )
1967 setResource( sect, name, QString::number( val ) );
1971 \brief Set double parameter value.
1972 \param sect section name
1973 \param name parameter name
1974 \param val parameter value
1976 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1979 if ( checkExisting() && value( sect, name, res ) && res == val )
1982 setResource( sect, name, QString::number( val, 'g', 12 ) );
1986 \brief Set boolean parameter value.
1987 \param sect section name
1988 \param name parameter name
1989 \param val parameter value
1991 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1994 if ( checkExisting() && value( sect, name, res ) && res == val )
1997 setResource( sect, name, QString( val ? "true" : "false" ) );
2001 \brief Set color parameter value.
2002 \param sect section name
2003 \param name parameter name
2004 \param val parameter value
2006 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
2009 if ( checkExisting() && value( sect, name, res ) && res == val )
2012 setResource( sect, name, Qtx::colorToString( val ) );
2016 \brief Set font parameter value.
2017 \param sect section name
2018 \param name parameter name
2019 \param val parameter value
2021 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val )
2024 if ( checkExisting() && value( sect, name, res ) && res == val )
2027 QStringList fontDescr;
2028 fontDescr.append( val.family() );
2030 fontDescr.append( "Bold" );
2032 fontDescr.append( "Italic" );
2033 if ( val.underline() )
2034 fontDescr.append( "Underline" );
2035 if ( val.overline() )
2036 fontDescr.append( "Overline" );
2037 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
2039 setResource( sect, name, fontDescr.join( "," ) );
2043 \brief Set string parameter value.
2044 \param sect section name
2045 \param name parameter name
2046 \param val parameter value
2048 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
2051 if ( checkExisting() && value( sect, name, res ) && res == val )
2054 setResource( sect, name, val );
2058 \brief Set byte array parameter value.
2059 \param sect section name
2060 \param name parameter name
2061 \param val parameter value
2063 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QByteArray& val )
2066 if ( checkExisting() && value( sect, name, res ) && res == val )
2071 for ( int i = 0; i < val.size(); i++ )
2073 ::sprintf( buf, "#%02X", (unsigned char)val.at( i ) );
2074 lst.append( QString( buf ) );
2077 QString result = QString( "@ByteArray[%1]" ).arg( lst.join( " " ) );
2078 setResource( sect, name, result );
2082 \brief Set linear gradient parameter value.
2083 \param sect section name
2084 \param name parameter name
2085 \param val parameter value
2087 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QLinearGradient& val )
2089 QLinearGradient res;
2090 if ( checkExisting() && value( sect, name, res ) && res == val )
2093 setResource( sect, name, Qtx::gradientToString( val ) );
2097 \brief Set radial gradient parameter value.
2098 \param sect section name
2099 \param name parameter name
2100 \param val parameter value
2102 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QRadialGradient& val )
2104 QRadialGradient res;
2105 if ( checkExisting() && value( sect, name, res ) && res == val )
2108 setResource( sect, name, Qtx::gradientToString( val ) );
2112 \brief Set conical gradient parameter value.
2113 \param sect section name
2114 \param name parameter name
2115 \param val parameter value
2117 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QConicalGradient& val )
2119 QConicalGradient res;
2120 if ( checkExisting() && value( sect, name, res ) && res == val )
2123 setResource( sect, name, Qtx::gradientToString( val ) );
2127 \brief Set background parameter value.
2128 \param sect section name
2129 \param name parameter name
2130 \param val parameter value
2132 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const Qtx::BackgroundData& val )
2134 Qtx::BackgroundData res;
2135 if ( checkExisting() && value( sect, name, res ) && res == val )
2138 setResource( sect, name, Qtx::backgroundToString( val ) );
2142 \brief Remove resources section.
2143 \param sect section name
2145 void QtxResourceMgr::remove( const QString& sect )
2149 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2150 (*it)->removeSection( sect );
2154 \brief Remove the specified parameter.
2155 \param sect section name
2156 \param name parameter name
2158 void QtxResourceMgr::remove( const QString& sect, const QString& name )
2162 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2163 (*it)->removeValue( sect, name );
2167 \brief Get current configuration files format.
2168 \return configuration files format name
2170 QString QtxResourceMgr::currentFormat() const
2173 if ( !myFormats.isEmpty() )
2174 fmt = myFormats[0]->format();
2179 \brief Set current configuration files format.
2180 \param fmt configuration files format name
2182 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
2184 Format* form = format( fmt );
2188 myFormats.removeAll( form );
2189 myFormats.prepend( form );
2191 if ( myResources.isEmpty() )
2194 ResList::Iterator resIt = myResources.begin();
2195 if ( myResources.count() > myDirList.count() && resIt != myResources.end() )
2197 (*resIt)->setFile( userFileName( appName() ) );
2201 for ( QStringList::ConstIterator it = myDirList.begin(); it != myDirList.end() && resIt != myResources.end(); ++it, ++resIt )
2202 (*resIt)->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
2206 \brief Get configuration files format by specified format name.
2207 \param fmt configuration files format name
2208 \return format object or 0 if format is not defined
2210 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
2213 for ( FormatList::ConstIterator it = myFormats.begin(); it != myFormats.end() && !form; ++it )
2215 if ( (*it)->format() == fmt )
2223 \brief Install configuration files format.
2225 Added format becomes current.
2227 \param form format object to be installed
2229 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
2231 if ( !myFormats.contains( form ) )
2232 myFormats.prepend( form );
2236 \brief Remove configuration files format.
2237 \param form format object to be uninstalled
2239 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
2241 myFormats.removeAll( form );
2245 \brief Get resource format options names.
2246 \return list of options names
2248 QStringList QtxResourceMgr::options() const
2250 return myOptions.keys();
2254 \brief Get the string value of the specified resources format option.
2256 If option does not exist, null QString is returned.
2258 \param opt option name
2259 \return option value
2260 \sa setOption(), options()
2262 QString QtxResourceMgr::option( const QString& opt ) const
2265 if ( myOptions.contains( opt ) )
2266 val = myOptions[opt];
2271 \brief Set the string value of the specified resources format option.
2272 \param opt option name
2273 \param val option value
2274 \sa option(), options()
2276 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
2278 myOptions.insert( opt, val );
2282 \brief Get names of all known constants.
2283 \return list of constants names
2284 \sa constant(), setConstant
2286 QStringList QtxResourceMgr::constants() const
2288 return myConstants.keys();
2292 \brief Get the value of the known constant.
2294 If constant is not set, null QString is returned.
2296 \param name constant name
2297 \return constant value
2298 \sa setConstant(), constants()
2300 QString QtxResourceMgr::constant( const QString& name ) const
2302 return myConstants.value( name, QString() );
2306 \brief Set the value of the constant.
2307 \param name constant name
2308 \param value constant value
2309 \sa constants(), constants()
2311 void QtxResourceMgr::setConstant( const QString& name, const QString& value )
2313 if ( !name.isEmpty() )
2314 myConstants.insert( name, value );
2318 \brief Load all resources from all resource files (global and user).
2319 \return \c true on success and \c false on error
2322 bool QtxResourceMgr::load()
2324 initialize( false );
2326 Format* fmt = format( currentFormat() );
2331 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2332 res = fmt->load( *it ) && res;
2338 \brief Import resources from specified resource file.
2339 \param fname resources file name
2340 \return \c true on success and \c false on error
2342 bool QtxResourceMgr::import( const QString& fname )
2344 Format* fmt = format( currentFormat() );
2348 if ( myResources.isEmpty() || !myHasUserValues )
2351 Resources* r = myResources[0];
2355 QString old = r->file();
2356 r->setFile( fname );
2357 bool res = fmt->load( r );
2363 \brief Save all resources to the user resource files.
2364 \return \c true on success and \c false on error
2366 bool QtxResourceMgr::save()
2368 initialize( false );
2370 Format* fmt = format( currentFormat() );
2374 if ( myResources.isEmpty() || !myHasUserValues )
2377 bool result = fmt->save( myResources[0] );
2385 \brief Get all sections names.
2386 \return list of section names
2388 QStringList QtxResourceMgr::sections() const
2392 QMap<QString, int> map;
2394 ResList::ConstIterator it = myResources.begin();
2395 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2398 for ( ; it != myResources.end(); ++it )
2400 QStringList lst = (*it)->sections();
2401 for ( QStringList::ConstIterator itr = lst.begin(); itr != lst.end(); ++itr )
2402 map.insert( *itr, 0 );
2409 \brief Get all sections names matching specified regular expression.
2410 \param re searched regular expression
2411 \return list of sections names
2413 QStringList QtxResourceMgr::sections(const QRegExp& re) const
2415 return sections().filter( re );
2419 \brief Get all sections names with the prefix specified by passed
2420 list of parent sections names.
2422 Sub-sections are separated inside the section name by the sections
2423 separator token, for example "splash:color:label".
2425 \param names parent sub-sections names
2426 \return list of sections names
2428 QStringList QtxResourceMgr::sections(const QStringList& names) const
2430 QStringList nm = names;
2432 QRegExp re( QString( "^%1$" ).arg( nm.join( sectionsToken() ) ) );
2433 return sections( re );
2437 \brief Get list of sub-sections names for the specified parent section name.
2439 Sub-sections are separated inside the section name by the sections
2440 separator token, for example "splash:color:label".
2442 \param section parent sub-section name
2443 \param full if \c true return full names of child sub-sections, if \c false,
2444 return only top-level sub-sections names
2445 \return list of sub-sections names
2447 QStringList QtxResourceMgr::subSections(const QString& section, const bool full) const
2449 QStringList names = sections( QStringList() << section );
2450 QMutableListIterator<QString> it( names );
2451 while ( it.hasNext() ) {
2452 QString name = it.next().mid( section.size() + 1 ).trimmed();
2453 if ( name.isEmpty() ) {
2457 if ( !full ) name = name.split( sectionsToken() ).first();
2458 it.setValue( name );
2460 names.removeDuplicates();
2466 \brief Get all parameters name in specified section.
2467 \param sec section name
2468 \return list of settings names
2470 QStringList QtxResourceMgr::parameters( const QString& sec ) const
2474 #if defined(QTX_NO_INDEXED_MAP)
2475 typedef QMap<QString, int> PMap;
2477 typedef IMap<QString, int> PMap;
2481 Resources* ur = !myResources.isEmpty() && workingMode() == IgnoreUserValues ? myResources[0] : 0;
2483 QListIterator<Resources*> it( myResources );
2485 while ( it.hasPrevious() )
2487 Resources* r = it.previous();
2488 if ( r == ur ) break;
2489 QStringList lst = r->parameters( sec );
2490 for ( QStringList::ConstIterator itr = lst.begin(); itr != lst.end(); ++itr )
2491 #if defined(QTX_NO_INDEXED_MAP)
2492 if ( !pmap.contains( *itr ) ) pmap.insert( *itr, 0 );
2494 pmap.insert( *itr, 0, false );
2502 \brief Get all parameters name in specified
2503 list of sub-sections names.
2505 Sub-sections are separated inside the section name by the sections
2506 separator token, for example "splash:color:label".
2508 \param names parent sub-sections names
2509 \return list of settings names
2511 QStringList QtxResourceMgr::parameters( const QStringList& names ) const
2513 return parameters( names.join( sectionsToken() ) );
2517 \brief Get absolute path to the file which name is defined by the parameter.
2519 The file name is defined by \a name argument, while directory name is retrieved
2520 from resources parameter \a prefix of section \a sec. Both directory and file name
2521 can be relative. If the directory is relative, it is calculated from the initial
2522 resources file name. Directory parameter can contain environment
2523 variables, which are substituted automatically.
2525 \param sec section name
2526 \param prefix parameter containing directory name
2527 \param name file name
2528 \return absolute file path or null QString if file does not exist
2530 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
2534 ResList::ConstIterator it = myResources.begin();
2535 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2538 for ( ; it != myResources.end() && res.isEmpty(); ++it )
2539 res = (*it)->path( sect, prefix, name, myConstants );
2544 \brief Get application resources section name.
2546 By default, application resources section name is "resources" but
2547 it can be changed by setting the "res_section_name" resources manager option.
2549 \return section corresponding to the resources directories
2550 \sa option(), setOption()
2552 QString QtxResourceMgr::resSection() const
2554 QString res = option( "res_section_name" );
2555 if ( res.isEmpty() )
2556 res = QString( "resources" );
2561 \brief Get application language section name.
2563 By default, application language section name is "language" but
2564 it can be changed by setting the "lang_section_name" resources manager option.
2566 \return section corresponding to the application language settings
2567 \sa option(), setOption()
2569 QString QtxResourceMgr::langSection() const
2571 QString res = option( "lang_section_name" );
2572 if ( res.isEmpty() )
2573 res = QString( "language" );
2578 \brief Get sections separator token.
2580 By default, sections separator token is colon symbol ":" but
2581 it can be changed by setting the "section_token" resources manager option.
2583 \return string corresponding to the current section separator token
2584 \sa option(), setOption()
2586 QString QtxResourceMgr::sectionsToken() const
2588 QString res = option( "section_token" );
2589 if ( res.isEmpty() )
2590 res = QString( ":" );
2595 \brief Get default pixmap.
2597 Default pixmap is used when requested pixmap resource is not found.
2599 \return default pixmap
2600 \sa setDefaultPixmap(), loadPixmap()
2602 QPixmap QtxResourceMgr::defaultPixmap() const
2604 static QPixmap* defpx = 0;
2606 defpx = new QPixmap( pixmap_not_found_xpm );
2608 return myDefaultPix ? *myDefaultPix : *defpx;
2612 \brief Set default pixmap.
2614 Default pixmap is used when requested pixmap resource is not found.
2616 \param pix default pixmap
2617 \sa defaultPixmap(), loadPixmap()
2619 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
2621 delete myDefaultPix;
2625 myDefaultPix = new QPixmap( pix );
2629 \brief Load pixmap resource.
2630 \param prefix parameter which refers to the resources directory (directories)
2631 \param name pixmap file name
2632 \return pixmap loaded from the file
2633 \sa defaultPixmap(), setDefaultPixmap()
2635 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
2637 return loadPixmap( prefix, name, true );
2641 \brief Load pixmap resource.
2643 \param prefix parameter which refers to the resources directory (directories)
2644 \param name pixmap file name
2645 \param useDef if \c false, default pixmap is not returned if resource is not found,
2646 in this case null pixmap is returned instead
2647 \return pixmap loaded from the file
2648 \sa defaultPixmap(), setDefaultPixmap()
2650 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
2652 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
2656 \brief Load pixmap resource.
2658 \param prefix parameter which refers to the resources directory (directories)
2659 \param name pixmap file name
2660 \param defPix default which should be used if the resource file doesn't exist
2661 \return pixmap loaded from the file
2662 \sa defaultPixmap(), setDefaultPixmap()
2664 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
2670 ResList::ConstIterator it = myResources.begin();
2671 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2674 for ( ; it != myResources.end() && pix.isNull(); ++it )
2675 pix = (*it)->loadPixmap( resSection(), prefix, name, myConstants );
2682 \brief Specify default language for the application.
2684 QString QtxResourceMgr::defaultLanguage() const
2691 \brief Select language to be used.
2692 \param preferableLanguage preferable language name (if empty, default language is used)
2694 QString QtxResourceMgr::language( const QString& preferableLanguage ) const
2696 // first try to select preferable language probably specified via the parameter
2697 QString lang = preferableLanguage;
2699 // then try default language; selection of default language can be redefined in successors
2700 if ( lang.isEmpty() )
2701 lang = defaultLanguage();
2703 // then try language as defined in the preferences files
2704 if ( lang.isEmpty() )
2705 value( langSection(), "language", lang );
2707 // finally try strongly hardcoded Ennglish
2708 if ( lang.isEmpty() )
2710 lang = QString( "en" );
2711 qWarning() << "QtxResourceMgr: Language not specified. Assumed:" << lang;
2719 \brief Load translation files according to the specified language.
2721 Names of the translation files are calculated according to the pattern specified
2722 by the "translators" option (this option is read from the section "language" of resources files).
2723 By default, "%P_msg_%L.qm" pattern is used.
2724 Keywords \%A, \%P, \%L in the pattern are substituted by the application name, prefix and language name
2726 For example, for prefix "SUIT" and language "en", all translation files "SUIT_msg_en.qm" are searched and
2729 If prefix is empty or null string, all translation files specified in the "resources" section of resources
2730 files are loaded (actually, the section is retrieved from resSection() method).
2731 If language is not specified, it is retrieved from the langSection() method, and if the latest is also empty,
2732 by default "en" (English) language is used.
2733 By default, settings from the user preferences file are also loaded (if user resource file is valid,
2734 see userFileName()). To avoid loading user settings, pass \c false as first parameter.
2736 \param pref parameter which defines translation context (for example, package name)
2737 \param preferableLanguage language name
2739 \sa resSection(), langSection(), loadTranslators()
2741 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& preferableLanguage )
2745 QMap<QChar, QString> substMap;
2746 substMap.insert( 'A', appName() );
2748 QString lang = language( preferableLanguage );
2750 substMap.insert( 'L', lang );
2753 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
2755 QStringList translators = option( "translators" ).split( "|", QString::SkipEmptyParts );
2756 QStringList newTranslators = trs.split( "|", QString::SkipEmptyParts );
2757 for ( int i = 0; i < (int)newTranslators.count(); i++ )
2759 if ( translators.indexOf( newTranslators[i] ) < 0 )
2760 translators += newTranslators[i];
2762 setOption( "translators", translators.join( "|" ) );
2765 QStringList trList = option( "translators" ).split( "|", QString::SkipEmptyParts );
2766 if ( trList.isEmpty() )
2768 trList.append( "%P_msg_%L.qm" );
2769 qWarning() << "QtxResourceMgr: Translators not defined. Assumed:" << trList[0];
2772 QStringList prefixList;
2773 if ( !pref.isEmpty() )
2774 prefixList.append( pref );
2776 prefixList = parameters( resSection() );
2778 if ( pref.isEmpty() && lang != "en" ) {
2779 // load Qt resources
2780 QString qt_translations = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
2781 QString qt_dir_trpath = Qtx::qtDir( "translations" );
2782 QTranslator* trans = new QtxTranslator( 0 );
2783 if ( trans->load( QString("qt_%1").arg( lang ), qt_translations ) || trans->load( QString("qt_%1").arg( lang ), qt_dir_trpath ) ) {
2784 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2788 for ( QStringList::ConstIterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
2790 QString prefix = *iter;
2791 substMap.insert( 'P', prefix );
2794 for ( QStringList::ConstIterator it = trList.begin(); it != trList.end(); ++it )
2795 trs.append( substMacro( *it, substMap ).trimmed() );
2797 loadTranslators( prefix, trs );
2802 \brief Load translation files for the specified translation context.
2803 \param prefix parameter which defines translation context (for example, package name)
2804 \param translators list of translation files
2807 void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators )
2813 ResList::ConstIterator iter = myResources.begin();
2814 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2817 for ( ; iter != myResources.end(); ++iter )
2818 lst.prepend( *iter );
2820 QTranslator* trans = 0;
2822 for ( ResList::Iterator it = lst.begin(); it != lst.end(); ++it )
2824 for ( QStringList::ConstIterator itr = translators.begin(); itr != translators.end(); ++itr )
2826 trans = (*it)->loadTranslator( resSection(), prefix, *itr, myConstants );
2829 if ( !myTranslator[prefix].contains( trans ) )
2830 myTranslator[prefix].append( trans );
2831 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2838 \brief Load translation file.
2839 \param prefix parameter which defines translation context (for example, package name)
2840 \param name translator file name
2841 \sa loadLanguage(), loadTranslators()
2843 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
2847 QTranslator* trans = 0;
2849 Resources* ur = !myResources.isEmpty() && workingMode() == IgnoreUserValues ? myResources[0] : 0;
2851 QListIterator<Resources*> it( myResources );
2853 while ( it.hasPrevious() )
2855 Resources* r = it.previous();
2856 if ( r == ur ) break;
2858 trans = r->loadTranslator( resSection(), prefix, name, myConstants );
2861 if ( !myTranslator[prefix].contains( trans ) )
2862 myTranslator[prefix].append( trans );
2863 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2869 \brief Add custom translator.
2870 \param prefix parameter which defines translation context (for example, package name)
2871 \param translator translator being installed
2872 \sa loadLanguage(), loadTranslators()
2874 void QtxResourceMgr::addTranslator( const QString& prefix, QTranslator* translator )
2878 if ( !myTranslator[prefix].contains( translator ) ) {
2879 myTranslator[prefix].append( translator );
2880 if ( QApplication::instance() )
2881 QApplication::instance()->installTranslator( translator );
2887 \brief Remove all translators corresponding to the specified translation context.
2888 \param prefix parameter which defines translation context (for example, package name)
2890 void QtxResourceMgr::removeTranslators( const QString& prefix )
2892 if ( !myTranslator.contains( prefix ) )
2895 for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
2897 if ( QApplication::instance() ) QApplication::instance()->removeTranslator( *it );
2901 myTranslator.remove( prefix );
2905 \brief Move all translators corresponding to the specified translation context
2906 to the top of translators stack (increase their priority).
2907 \param prefix parameter which defines translation context (for example, package name)
2909 void QtxResourceMgr::raiseTranslators( const QString& prefix )
2911 if ( !myTranslator.contains( prefix ) )
2914 for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
2916 if ( QApplication::instance() ) {
2917 QApplication::instance()->removeTranslator( *it );
2918 QApplication::instance()->installTranslator( *it );
2924 \brief Copy all parameters to the user resources in order to
2925 saved them lately in the user home folder.
2927 void QtxResourceMgr::refresh()
2929 QStringList sl = sections();
2930 for ( QStringList::ConstIterator it = sl.begin(); it != sl.end(); ++it )
2932 QStringList pl = parameters( *it );
2933 for ( QStringList::ConstIterator itr = pl.begin(); itr != pl.end(); ++itr )
2934 setResource( *it, *itr, stringValue( *it, *itr ) );
2939 \brief Set the resource directories (where global confguration files are searched).
2941 This function also clears all currently set resources.
2943 \param dl directories list
2945 void QtxResourceMgr::setDirList( const QStringList& dl )
2948 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2951 myResources.clear();
2955 \brief Set parameter value.
2956 \param sect section name
2957 \param name parameter name
2958 \param val parameter value
2960 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
2964 if ( !myResources.isEmpty() && myHasUserValues )
2965 myResources.first()->setValue( sect, name, val );
2969 \brief Get user configuration file name.
2971 This method can be redefined in the successor class to customize the user configuration file name.
2972 User configuration file is always situated in the user's home directory. By default .<appName>rc
2973 file is used on Linux (e.g. .MyApprc) and <appName>.<format> under Windows (e.g. MyApp.xml).
2975 Parameter \a for_load (not used in default implementation) specifies the usage mode, i.e. if
2976 user configuration file is opened for reading or writing. This allows customizing a way of application
2977 resources initializing (for example, if the user configuraion file includes version number and there is
2978 no file corresponding to this version in the user's home directory, it could be good idea to try
2979 the configuration file from the previous versions of the application).
2981 \param appName application name
2982 \param for_load boolean flag indicating that file is opened for loading or saving (not used in default implementation)
2983 \return user configuration file name
2984 \sa globalFileName()
2986 QString QtxResourceMgr::userFileName( const QString& appName, const bool /*for_load*/ ) const
2989 QString pathName = QDir::homePath();
2990 QString cfgAppName = QApplication::applicationName();
2991 if ( !cfgAppName.isEmpty() )
2992 pathName = Qtx::addSlash( Qtx::addSlash( pathName ) + QString( ".config" ) ) + cfgAppName;
2995 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
2997 fileName = QString( "%1rc" ).arg( appName );
2998 // VSR 24/09/2012: issue 0021781: do not prepend filename with "."
2999 // when user file is stored in ~/.config/<appname> directory
3000 if ( cfgAppName.isEmpty() )
3001 fileName.prepend( "." );
3004 if ( !fileName.isEmpty() )
3005 pathName = Qtx::addSlash( pathName ) + fileName;
3011 \brief Get global configuration file name.
3013 This method can be redefined in the successor class to customize the global configuration file name.
3014 Global configuration files are searched in the directories specified by the application resources
3015 environment variable (e.g. MyAppResources). By default <appName>.<format> file name is used
3018 \param appName application name
3019 \return global configuration file name
3022 QString QtxResourceMgr::globalFileName( const QString& appName ) const
3024 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
3028 \brief This function is called after user configuration file is saved.
3029 Can be redefined in the successor classes, default implementation does nothing.
3031 void QtxResourceMgr::saved()
3036 \brief Perform substitution of the patterns like \%A, \%B, etc by values from the map.
3038 Used by loadLanguage().
3040 \param src sring to be processed
3041 \param substMap map of values for replacing
3042 \return processed string
3044 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
3048 QRegExp rx( "%[A-Za-z%]" );
3051 while ( ( idx = rx.indexIn( trg, idx ) ) >= 0 )
3053 QChar spec = trg.at( idx + 1 );
3057 else if ( substMap.contains( spec ) )
3058 subst = substMap[spec];
3060 if ( !subst.isEmpty() )
3062 trg.replace( idx, rx.matchedLength(), subst );
3063 idx += subst.length();
3066 idx += rx.matchedLength();