1 // Copyright (C) 2007-2022 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 QString userFile = userFileName( appName() );
1348 if ( !userFile.isEmpty() )
1349 that->myResources.append( new Resources( that, userFile ) );
1351 that->myHasUserValues = myResources.count() > 0;
1353 for ( QStringList::ConstIterator it = myDirList.begin(); it != myDirList.end(); ++it )
1355 QString path = Qtx::addSlash( *it ) + globalFileName( appName() );
1356 that->myResources.append( new Resources( that, path ) );
1364 \brief Get "cached pixmaps" option value.
1366 Resources manager allows possibility to cache loaded pixmaps that allow to
1367 improve application performance. This feature is turned on by default - all
1368 loaded pixmaps are stored in the internal map. Switching of this feature on/off
1369 can be done by setIsPixmapCached() method.
1371 \return \c true if pixmap cache is turned on
1372 \sa setIsPixmapCached()
1374 bool QtxResourceMgr::isPixmapCached() const
1376 return myIsPixmapCached;
1380 \brief Switch "cached pixmaps" option on/off.
1381 \param on enable pixmap cache if \c true and disable it if \c false
1382 \sa isPixmapCached()
1384 void QtxResourceMgr::setIsPixmapCached( const bool on )
1386 myIsPixmapCached = on;
1390 \brief Remove all resources from the resources manager.
1392 void QtxResourceMgr::clear()
1394 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
1399 \brief Get current working mode.
1401 \return current working mode
1402 \sa setWorkingMode(), value(), hasValue(), hasSection(), setValue()
1404 QtxResourceMgr::WorkingMode QtxResourceMgr::workingMode() const
1406 return myWorkingMode;
1410 \brief Set resource manager's working mode.
1412 The resource manager can operate in the following working modes:
1413 * AllowUserValues : methods values(), hasValue(), hasSection() take into account user values (default)
1414 * IgnoreUserValues : methods values(), hasValue(), hasSection() do not take into account user values
1416 Note, that setValue() method always put the value to the user settings file.
1418 \param mode new working mode
1419 \return previous working mode
1420 \sa workingMode(), value(), hasValue(), hasSection(), setValue()
1422 QtxResourceMgr::WorkingMode QtxResourceMgr::setWorkingMode( WorkingMode mode )
1424 WorkingMode m = myWorkingMode;
1425 myWorkingMode = mode;
1430 \brief Get interger parameter value.
1431 \param sect section name
1432 \param name parameter name
1433 \param iVal parameter to return resulting integer value
1434 \return \c true if parameter is found and \c false if parameter is not found
1435 (in this case \a iVal value is undefined)
1437 bool QtxResourceMgr::value( const QString& sect, const QString& name, int& iVal ) const
1440 if ( !value( sect, name, val, true ) )
1444 iVal = val.toInt( &ok );
1450 \brief Get double parameter value.
1451 \param sect section name
1452 \param name parameter name
1453 \param dVal parameter to return resulting double value
1454 \return \c true if parameter is found and \c false if parameter is not found
1455 (in this case \a dVal value is undefined)
1457 bool QtxResourceMgr::value( const QString& sect, const QString& name, double& dVal ) const
1460 if ( !value( sect, name, val, true ) )
1464 dVal = val.toDouble( &ok );
1470 \brief Get boolean parameter value.
1471 \param sect section name
1472 \param name parameter name
1473 \param bVal parameter to return resulting boolean value
1474 \return \c true if parameter is found and \c false if parameter is not found
1475 (in this case \a bVal value is undefined)
1477 bool QtxResourceMgr::value( const QString& sect, const QString& name, bool& bVal ) const
1480 if ( !value( sect, name, val, true ) )
1483 static QMap<QString, bool> boolMap;
1484 if ( boolMap.isEmpty() )
1486 boolMap["true"] = boolMap["yes"] = boolMap["on"] = true;
1487 boolMap["false"] = boolMap["no"] = boolMap["off"] = false;
1490 val = val.toLower();
1491 bool res = boolMap.contains( val );
1493 bVal = boolMap[val];
1496 double num = val.toDouble( &res );
1505 \brief Get color parameter value.
1506 \param sect section name
1507 \param name parameter name
1508 \param cVal parameter to return resulting color value
1509 \return \c true if parameter is found and \c false if parameter is not found
1510 (in this case \a cVal value is undefined)
1512 bool QtxResourceMgr::value( const QString& sect, const QString& name, QColor& cVal ) const
1515 if ( !value( sect, name, val, true ) )
1518 return Qtx::stringToColor( val, cVal );
1522 \brief Get font parameter value.
1523 \param sect section name
1524 \param name parameter name
1525 \param fVal parameter to return resulting font value
1526 \return \c true if parameter is found and \c false if parameter is not found
1527 (in this case \a fVal value is undefined)
1529 bool QtxResourceMgr::value( const QString& sect, const QString& name, QFont& fVal ) const
1532 if ( !value( sect, name, val, true ) )
1535 QStringList fontDescr = val.split( ",", QString::SkipEmptyParts );
1537 if ( fontDescr.count() < 2 )
1540 QString family = fontDescr[0];
1541 if ( family.isEmpty() )
1544 fVal = QFont( family );
1546 for ( int i = 1; i < (int)fontDescr.count(); i++ )
1548 QString curval = fontDescr[i].trimmed().toLower();
1549 if ( curval == QString( "bold" ) )
1550 fVal.setBold( true );
1551 else if ( curval == QString( "italic" ) )
1552 fVal.setItalic( true );
1553 else if ( curval == QString( "underline" ) )
1554 fVal.setUnderline( true );
1555 else if ( curval == QString( "shadow" ) || curval == QString( "overline" ) )
1556 fVal.setOverline( true );
1560 int ps = curval.toInt( &isOk );
1562 fVal.setPointSize( ps );
1570 \brief Get byte array parameter value.
1571 \param sect section name
1572 \param name parameter name
1573 \param baVal parameter to return resulting byte array value
1574 \return \c true if parameter is found and \c false if parameter is not found
1575 (in this case \a baVal value is undefined)
1577 bool QtxResourceMgr::value( const QString& sect, const QString& name, QByteArray& baVal ) const
1580 if ( !value( sect, name, val, true ) )
1583 if ( val.startsWith( "@ByteArray(" ) && val.endsWith( ')' ) ) {
1584 baVal = QByteArray( val.midRef( 11, val.size() - 12 ).toLatin1() );
1587 if ( val.startsWith( "@ByteArray[" ) && val.endsWith( ']' ) ) {
1588 val = val.mid( 11, val.size() - 12 );
1591 QStringList lst = val.split( QRegExp( "[\\s|,]" ), QString::SkipEmptyParts );
1592 for ( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it )
1596 if ( str.startsWith( "#" ) )
1602 int num = str.toInt( &ok, base );
1603 if ( !ok || num < 0 || num > 255 )
1606 baVal.append( (char)num );
1609 return !baVal.isEmpty();
1613 \brief Get linear gradient parameter value.
1614 \param sect section name
1615 \param name parameter name
1616 \param gVal parameter to return resulting linear gradient value
1617 \return \c true if parameter is found and \c false if parameter is not found
1618 (in this case \a gVal value is undefined)
1620 bool QtxResourceMgr::value( const QString& sect, const QString& name, QLinearGradient& gVal ) const
1623 if ( !value( sect, name, val, true ) )
1626 return Qtx::stringToLinearGradient( val, gVal );
1630 \brief Get radial gradient parameter value.
1631 \param sect section name
1632 \param name parameter name
1633 \param gVal parameter to return resulting radial gradient value
1634 \return \c true if parameter is found and \c false if parameter is not found
1635 (in this case \a gVal value is undefined)
1637 bool QtxResourceMgr::value( const QString& sect, const QString& name, QRadialGradient& gVal ) const
1640 if ( !value( sect, name, val, true ) )
1643 return Qtx::stringToRadialGradient( val, gVal );
1647 \brief Get conical gradient parameter value.
1648 \param sect section name
1649 \param name parameter name
1650 \param gVal parameter to return resulting conical gradient value
1651 \return \c true if parameter is found and \c false if parameter is not found
1652 (in this case \a gVal value is undefined)
1654 bool QtxResourceMgr::value( const QString& sect, const QString& name, QConicalGradient& gVal ) const
1657 if ( !value( sect, name, val, true ) )
1660 return Qtx::stringToConicalGradient( val, gVal );
1664 \brief Get background parameter value.
1665 \param sect section name
1666 \param name parameter name
1667 \param bgVal parameter to return resulting background value
1668 \return \c true if parameter is found and \c false if parameter is not found
1669 (in this case \a bgVal value is undefined)
1671 bool QtxResourceMgr::value( const QString& sect, const QString& name, Qtx::BackgroundData& bgVal ) const
1674 if ( !value( sect, name, val, true ) )
1677 bgVal = Qtx::stringToBackground( val );
1678 return bgVal.isValid();
1682 \brief Get string parameter value (native format).
1683 \param sect section name
1684 \param name parameter name
1685 \param val parameter to return resulting byte array value
1686 \param subst if \c true perform environment variables substitution
1687 \return \c true if parameter is found and \c false if parameter is not found
1688 (in this case \a val value is undefined)
1690 bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& val, const bool subst ) const
1696 ResList::ConstIterator it = myResources.begin();
1697 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1700 for ( ; it != myResources.end() && !ok; ++it )
1702 ok = (*it)->hasValue( sect, name );
1704 val = (*it)->value( sect, name, subst, myConstants );
1711 \brief Get interger parameter value.
1713 If the specified parameter is not found or can not be converted to the integer value,
1714 the specified default value is returned instead.
1716 \param sect section name
1717 \param name parameter name
1718 \param def default value
1719 \return parameter value (or default value if parameter is not found)
1721 int QtxResourceMgr::integerValue( const QString& sect, const QString& name, const int def ) const
1724 if ( !value( sect, name, val ) )
1730 \brief Get double parameter value.
1732 If the specified parameter is not found or can not be converted to the double value,
1733 the specified default value is returned instead.
1735 \param sect section name
1736 \param name parameter name
1737 \param def default value
1738 \return parameter value (or default value if parameter is not found)
1740 double QtxResourceMgr::doubleValue( const QString& sect, const QString& name, const double def ) const
1743 if ( !value( sect, name, val ) )
1749 \brief Get boolean parameter value.
1751 If the specified parameter is not found or can not be converted to the boolean value,
1752 the specified default value is returned instead.
1754 \param sect section name
1755 \param name parameter name
1756 \param def default value
1757 \return parameter value (or default value if parameter is not found)
1759 bool QtxResourceMgr::booleanValue( const QString& sect, const QString& name, const bool def ) const
1762 if ( !value( sect, name, val ) )
1768 \brief Get font parameter value.
1770 If the specified parameter is not found or can not be converted to the font value,
1771 the specified default value is returned instead.
1773 \param sect section name
1774 \param name parameter name
1775 \param def default value
1776 \return parameter value (or default value if parameter is not found)
1778 QFont QtxResourceMgr::fontValue( const QString& sect, const QString& name, const QFont& def ) const
1781 if( !value( sect, name, font ) )
1787 \brief Get color parameter value.
1789 If the specified parameter is not found or can not be converted to the color value,
1790 the specified default value is returned instead.
1792 \param sect section name
1793 \param name parameter name
1794 \param def default value
1795 \return parameter value (or default value if parameter is not found)
1797 QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, const QColor& def ) const
1800 if ( !value( sect, name, val ) )
1806 \brief Get string parameter value.
1808 If the specified parameter is not found, the specified default value is returned instead.
1810 \param sect section name
1811 \param name parameter name
1812 \param def default value
1813 \return parameter value (or default value if parameter is not found)
1815 QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def, const bool subst ) const
1818 if ( !value( sect, name, val, subst ) )
1824 \brief Get byte array parameter value.
1826 If the specified parameter is not found, the specified default value is returned instead.
1828 \param sect section name
1829 \param name parameter name
1830 \param def default value
1831 \return parameter value (or default value if parameter is not found)
1833 QByteArray QtxResourceMgr::byteArrayValue( const QString& sect, const QString& name, const QByteArray& def ) const
1836 if ( !value( sect, name, val ) )
1842 \brief Get linear gradient parameter value.
1844 If the specified parameter is not found, the specified default value is returned instead.
1846 \param sect section name
1847 \param name parameter name
1848 \param def default value
1849 \return parameter value (or default value if parameter is not found)
1851 QLinearGradient QtxResourceMgr::linearGradientValue( const QString& sect, const QString& name, const QLinearGradient& def ) const
1853 QLinearGradient val;
1854 if ( !value( sect, name, val ) )
1860 \brief Get radial gradient parameter value.
1862 If the specified parameter is not found, the specified default value is returned instead.
1864 \param sect section name
1865 \param name parameter name
1866 \param def default value
1867 \return parameter value (or default value if parameter is not found)
1869 QRadialGradient QtxResourceMgr::radialGradientValue( const QString& sect, const QString& name, const QRadialGradient& def ) const
1871 QRadialGradient val;
1872 if ( !value( sect, name, val ) )
1878 \brief Get conical gradient parameter value.
1880 If the specified parameter is not found, the specified default value is returned instead.
1882 \param sect section name
1883 \param name parameter name
1884 \param def default value
1885 \return parameter value (or default value if parameter is not found)
1887 QConicalGradient QtxResourceMgr::conicalGradientValue( const QString& sect, const QString& name, const QConicalGradient& def ) const
1889 QConicalGradient val;
1890 if ( !value( sect, name, val ) )
1896 \brief Get background parameter value.
1898 If the specified parameter is not found, the specified default value is returned instead.
1900 \param sect section name
1901 \param name parameter name
1902 \param def default value
1903 \return parameter value (or default value if parameter is not found)
1905 Qtx::BackgroundData QtxResourceMgr::backgroundValue( const QString& sect, const QString& name, const Qtx::BackgroundData& def ) const
1907 Qtx::BackgroundData val;
1908 if ( !value( sect, name, val ) )
1914 \brief Check parameter existence.
1915 \param sect section name
1916 \param name parameter name
1917 \return \c true if parameter exists in specified section
1919 bool QtxResourceMgr::hasValue( const QString& sect, const QString& name ) const
1925 ResList::ConstIterator it = myResources.begin();
1926 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1929 for ( ; it != myResources.end() && !ok; ++it )
1930 ok = (*it)->hasValue( sect, name );
1936 \brief Check section existence.
1937 \param sect section name
1938 \return \c true if section exists
1940 bool QtxResourceMgr::hasSection( const QString& sect ) const
1946 ResList::ConstIterator it = myResources.begin();
1947 if ( myHasUserValues && workingMode() == IgnoreUserValues )
1950 for ( ; it != myResources.end() && !ok; ++it )
1951 ok = (*it)->hasSection( sect );
1957 \brief Set integer parameter value.
1958 \param sect section name
1959 \param name parameter name
1960 \param val parameter value
1962 void QtxResourceMgr::setValue( const QString& sect, const QString& name, int val )
1965 if ( checkExisting() && value( sect, name, res ) && res == val )
1968 setResource( sect, name, QString::number( val ) );
1972 \brief Set double parameter value.
1973 \param sect section name
1974 \param name parameter name
1975 \param val parameter value
1977 void QtxResourceMgr::setValue( const QString& sect, const QString& name, double val )
1980 if ( checkExisting() && value( sect, name, res ) && res == val )
1983 setResource( sect, name, QString::number( val, 'g', 12 ) );
1987 \brief Set boolean parameter value.
1988 \param sect section name
1989 \param name parameter name
1990 \param val parameter value
1992 void QtxResourceMgr::setValue( const QString& sect, const QString& name, bool val )
1995 if ( checkExisting() && value( sect, name, res ) && res == val )
1998 setResource( sect, name, QString( val ? "true" : "false" ) );
2002 \brief Set color parameter value.
2003 \param sect section name
2004 \param name parameter name
2005 \param val parameter value
2007 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QColor& val )
2010 if ( checkExisting() && value( sect, name, res ) && res == val )
2013 setResource( sect, name, Qtx::colorToString( val ) );
2017 \brief Set font parameter value.
2018 \param sect section name
2019 \param name parameter name
2020 \param val parameter value
2022 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QFont& val )
2025 if ( checkExisting() && value( sect, name, res ) && res == val )
2028 QStringList fontDescr;
2029 fontDescr.append( val.family() );
2031 fontDescr.append( "Bold" );
2033 fontDescr.append( "Italic" );
2034 if ( val.underline() )
2035 fontDescr.append( "Underline" );
2036 if ( val.overline() )
2037 fontDescr.append( "Overline" );
2038 fontDescr.append( QString( "%1" ).arg( val.pointSize() ) );
2040 setResource( sect, name, fontDescr.join( "," ) );
2044 \brief Set string parameter value.
2045 \param sect section name
2046 \param name parameter name
2047 \param val parameter value
2049 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QString& val )
2052 if ( checkExisting() && value( sect, name, res ) && res == val )
2055 setResource( sect, name, val );
2059 \brief Set byte array parameter value.
2060 \param sect section name
2061 \param name parameter name
2062 \param val parameter value
2064 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QByteArray& val )
2067 if ( checkExisting() && value( sect, name, res ) && res == val )
2072 for ( int i = 0; i < val.size(); i++ )
2074 ::sprintf( buf, "#%02X", (unsigned char)val.at( i ) );
2075 lst.append( QString( buf ) );
2078 QString result = QString( "@ByteArray[%1]" ).arg( lst.join( " " ) );
2079 setResource( sect, name, result );
2083 \brief Set linear gradient parameter value.
2084 \param sect section name
2085 \param name parameter name
2086 \param val parameter value
2088 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QLinearGradient& val )
2090 QLinearGradient res;
2091 if ( checkExisting() && value( sect, name, res ) && res == val )
2094 setResource( sect, name, Qtx::gradientToString( val ) );
2098 \brief Set radial gradient parameter value.
2099 \param sect section name
2100 \param name parameter name
2101 \param val parameter value
2103 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QRadialGradient& val )
2105 QRadialGradient res;
2106 if ( checkExisting() && value( sect, name, res ) && res == val )
2109 setResource( sect, name, Qtx::gradientToString( val ) );
2113 \brief Set conical gradient parameter value.
2114 \param sect section name
2115 \param name parameter name
2116 \param val parameter value
2118 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const QConicalGradient& val )
2120 QConicalGradient res;
2121 if ( checkExisting() && value( sect, name, res ) && res == val )
2124 setResource( sect, name, Qtx::gradientToString( val ) );
2128 \brief Set background parameter value.
2129 \param sect section name
2130 \param name parameter name
2131 \param val parameter value
2133 void QtxResourceMgr::setValue( const QString& sect, const QString& name, const Qtx::BackgroundData& val )
2135 Qtx::BackgroundData res;
2136 if ( checkExisting() && value( sect, name, res ) && res == val )
2139 setResource( sect, name, Qtx::backgroundToString( val ) );
2143 \brief Remove resources section.
2144 \param sect section name
2146 void QtxResourceMgr::remove( const QString& sect )
2150 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2151 (*it)->removeSection( sect );
2155 \brief Remove the specified parameter.
2156 \param sect section name
2157 \param name parameter name
2159 void QtxResourceMgr::remove( const QString& sect, const QString& name )
2163 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2164 (*it)->removeValue( sect, name );
2168 \brief Get current configuration files format.
2169 \return configuration files format name
2171 QString QtxResourceMgr::currentFormat() const
2174 if ( !myFormats.isEmpty() )
2175 fmt = myFormats[0]->format();
2180 \brief Set current configuration files format.
2181 \param fmt configuration files format name
2183 void QtxResourceMgr::setCurrentFormat( const QString& fmt )
2185 Format* form = format( fmt );
2189 myFormats.removeAll( form );
2190 myFormats.prepend( form );
2192 if ( myResources.isEmpty() )
2195 ResList::Iterator resIt = myResources.begin();
2196 if ( myResources.count() > myDirList.count() && resIt != myResources.end() )
2198 (*resIt)->setFile( userFileName( appName() ) );
2202 for ( QStringList::ConstIterator it = myDirList.begin(); it != myDirList.end() && resIt != myResources.end(); ++it, ++resIt )
2203 (*resIt)->setFile( Qtx::addSlash( *it ) + globalFileName( appName() ) );
2207 \brief Get configuration files format by specified format name.
2208 \param fmt configuration files format name
2209 \return format object or 0 if format is not defined
2211 QtxResourceMgr::Format* QtxResourceMgr::format( const QString& fmt ) const
2214 for ( FormatList::ConstIterator it = myFormats.begin(); it != myFormats.end() && !form; ++it )
2216 if ( (*it)->format() == fmt )
2224 \brief Install configuration files format.
2226 Added format becomes current.
2228 \param form format object to be installed
2230 void QtxResourceMgr::installFormat( QtxResourceMgr::Format* form )
2232 if ( !myFormats.contains( form ) )
2233 myFormats.prepend( form );
2237 \brief Remove configuration files format.
2238 \param form format object to be uninstalled
2240 void QtxResourceMgr::removeFormat( QtxResourceMgr::Format* form )
2242 myFormats.removeAll( form );
2246 \brief Get resource format options names.
2247 \return list of options names
2249 QStringList QtxResourceMgr::options() const
2251 return myOptions.keys();
2255 \brief Get the string value of the specified resources format option.
2257 If option does not exist, null QString is returned.
2259 \param opt option name
2260 \return option value
2261 \sa setOption(), options()
2263 QString QtxResourceMgr::option( const QString& opt ) const
2266 if ( myOptions.contains( opt ) )
2267 val = myOptions[opt];
2272 \brief Set the string value of the specified resources format option.
2273 \param opt option name
2274 \param val option value
2275 \sa option(), options()
2277 void QtxResourceMgr::setOption( const QString& opt, const QString& val )
2279 myOptions.insert( opt, val );
2283 \brief Get names of all known constants.
2284 \return list of constants names
2285 \sa constant(), setConstant
2287 QStringList QtxResourceMgr::constants() const
2289 return myConstants.keys();
2293 \brief Get the value of the known constant.
2295 If constant is not set, null QString is returned.
2297 \param name constant name
2298 \return constant value
2299 \sa setConstant(), constants()
2301 QString QtxResourceMgr::constant( const QString& name ) const
2303 return myConstants.value( name, QString() );
2307 \brief Set the value of the constant.
2308 \param name constant name
2309 \param value constant value
2310 \sa constants(), constants()
2312 void QtxResourceMgr::setConstant( const QString& name, const QString& value )
2314 if ( !name.isEmpty() )
2315 myConstants.insert( name, value );
2319 \brief Load all resources from all resource files (global and user).
2320 \return \c true on success and \c false on error
2323 bool QtxResourceMgr::load()
2325 initialize( false );
2327 Format* fmt = format( currentFormat() );
2332 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2333 res = fmt->load( *it ) && res;
2339 \brief Import resources from specified resource file.
2340 \param fname resources file name
2341 \return \c true on success and \c false on error
2343 bool QtxResourceMgr::import( const QString& fname )
2345 Format* fmt = format( currentFormat() );
2349 if ( myResources.isEmpty() || !myHasUserValues )
2352 Resources* r = myResources[0];
2356 QString old = r->file();
2357 r->setFile( fname );
2358 bool res = fmt->load( r );
2364 \brief Save all resources to the user resource files.
2365 \return \c true on success and \c false on error
2367 bool QtxResourceMgr::save()
2369 initialize( false );
2371 Format* fmt = format( currentFormat() );
2375 if ( myResources.isEmpty() || !myHasUserValues )
2378 bool result = fmt->save( myResources[0] );
2386 \brief Get all sections names.
2387 \return list of section names
2389 QStringList QtxResourceMgr::sections() const
2393 QMap<QString, int> map;
2395 ResList::ConstIterator it = myResources.begin();
2396 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2399 for ( ; it != myResources.end(); ++it )
2401 QStringList lst = (*it)->sections();
2402 for ( QStringList::ConstIterator itr = lst.begin(); itr != lst.end(); ++itr )
2403 map.insert( *itr, 0 );
2410 \brief Get all sections names matching specified regular expression.
2411 \param re searched regular expression
2412 \return list of sections names
2414 QStringList QtxResourceMgr::sections(const QRegExp& re) const
2416 return sections().filter( re );
2420 \brief Get all sections names with the prefix specified by passed
2421 list of parent sections names.
2423 Sub-sections are separated inside the section name by the sections
2424 separator token, for example "splash:color:label".
2426 \param names parent sub-sections names
2427 \return list of sections names
2429 QStringList QtxResourceMgr::sections(const QStringList& names) const
2431 QStringList nm = names;
2433 QRegExp re( QString( "^%1$" ).arg( nm.join( sectionsToken() ) ) );
2434 return sections( re );
2438 \brief Get list of sub-sections names for the specified parent section name.
2440 Sub-sections are separated inside the section name by the sections
2441 separator token, for example "splash:color:label".
2443 \param section parent sub-section name
2444 \param full if \c true return full names of child sub-sections, if \c false,
2445 return only top-level sub-sections names
2446 \return list of sub-sections names
2448 QStringList QtxResourceMgr::subSections(const QString& section, const bool full) const
2450 QStringList names = sections( QStringList() << section );
2451 QMutableListIterator<QString> it( names );
2452 while ( it.hasNext() ) {
2453 QString name = it.next().mid( section.size() + 1 ).trimmed();
2454 if ( name.isEmpty() ) {
2458 if ( !full ) name = name.split( sectionsToken() ).first();
2459 it.setValue( name );
2461 names.removeDuplicates();
2467 \brief Get all parameters name in specified section.
2468 \param sec section name
2469 \return list of settings names
2471 QStringList QtxResourceMgr::parameters( const QString& sec ) const
2475 #if defined(QTX_NO_INDEXED_MAP)
2476 typedef QMap<QString, int> PMap;
2478 typedef IMap<QString, int> PMap;
2482 Resources* ur = !myResources.isEmpty() && workingMode() == IgnoreUserValues ? myResources[0] : 0;
2484 QListIterator<Resources*> it( myResources );
2486 while ( it.hasPrevious() )
2488 Resources* r = it.previous();
2489 if ( r == ur ) break;
2490 QStringList lst = r->parameters( sec );
2491 for ( QStringList::ConstIterator itr = lst.begin(); itr != lst.end(); ++itr )
2492 #if defined(QTX_NO_INDEXED_MAP)
2493 if ( !pmap.contains( *itr ) ) pmap.insert( *itr, 0 );
2495 pmap.insert( *itr, 0, false );
2503 \brief Get all parameters name in specified
2504 list of sub-sections names.
2506 Sub-sections are separated inside the section name by the sections
2507 separator token, for example "splash:color:label".
2509 \param names parent sub-sections names
2510 \return list of settings names
2512 QStringList QtxResourceMgr::parameters( const QStringList& names ) const
2514 return parameters( names.join( sectionsToken() ) );
2518 \brief Get absolute path to the file which name is defined by the parameter.
2520 The file name is defined by \a name argument, while directory name is retrieved
2521 from resources parameter \a prefix of section \a sec. Both directory and file name
2522 can be relative. If the directory is relative, it is calculated from the initial
2523 resources file name. Directory parameter can contain environment
2524 variables, which are substituted automatically.
2526 \param sec section name
2527 \param prefix parameter containing directory name
2528 \param name file name
2529 \return absolute file path or null QString if file does not exist
2531 QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const QString& name ) const
2535 ResList::ConstIterator it = myResources.begin();
2536 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2539 for ( ; it != myResources.end() && res.isEmpty(); ++it )
2540 res = (*it)->path( sect, prefix, name, myConstants );
2545 \brief Get application resources section name.
2547 By default, application resources section name is "resources" but
2548 it can be changed by setting the "res_section_name" resources manager option.
2550 \return section corresponding to the resources directories
2551 \sa option(), setOption()
2553 QString QtxResourceMgr::resSection() const
2555 QString res = option( "res_section_name" );
2556 if ( res.isEmpty() )
2557 res = QString( "resources" );
2562 \brief Get application language section name.
2564 By default, application language section name is "language" but
2565 it can be changed by setting the "lang_section_name" resources manager option.
2567 \return section corresponding to the application language settings
2568 \sa option(), setOption()
2570 QString QtxResourceMgr::langSection() const
2572 QString res = option( "lang_section_name" );
2573 if ( res.isEmpty() )
2574 res = QString( "language" );
2579 \brief Get sections separator token.
2581 By default, sections separator token is colon symbol ":" but
2582 it can be changed by setting the "section_token" resources manager option.
2584 \return string corresponding to the current section separator token
2585 \sa option(), setOption()
2587 QString QtxResourceMgr::sectionsToken() const
2589 QString res = option( "section_token" );
2590 if ( res.isEmpty() )
2591 res = QString( ":" );
2596 \brief Get default pixmap.
2598 Default pixmap is used when requested pixmap resource is not found.
2600 \return default pixmap
2601 \sa setDefaultPixmap(), loadPixmap()
2603 QPixmap QtxResourceMgr::defaultPixmap() const
2605 static QPixmap* defpx = 0;
2607 defpx = new QPixmap( pixmap_not_found_xpm );
2609 return myDefaultPix ? *myDefaultPix : *defpx;
2613 \brief Set default pixmap.
2615 Default pixmap is used when requested pixmap resource is not found.
2617 \param pix default pixmap
2618 \sa defaultPixmap(), loadPixmap()
2620 void QtxResourceMgr::setDefaultPixmap( const QPixmap& pix )
2622 delete myDefaultPix;
2626 myDefaultPix = new QPixmap( pix );
2630 \brief Load pixmap resource.
2631 \param prefix parameter which refers to the resources directory (directories)
2632 \param name pixmap file name
2633 \return pixmap loaded from the file
2634 \sa defaultPixmap(), setDefaultPixmap()
2636 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name ) const
2638 return loadPixmap( prefix, name, true );
2642 \brief Load pixmap resource.
2644 \param prefix parameter which refers to the resources directory (directories)
2645 \param name pixmap file name
2646 \param useDef if \c false, default pixmap is not returned if resource is not found,
2647 in this case null pixmap is returned instead
2648 \return pixmap loaded from the file
2649 \sa defaultPixmap(), setDefaultPixmap()
2651 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const bool useDef ) const
2653 return loadPixmap( prefix, name, useDef ? defaultPixmap() : QPixmap() );
2657 \brief Load pixmap resource.
2659 \param prefix parameter which refers to the resources directory (directories)
2660 \param name pixmap file name
2661 \param defPix default which should be used if the resource file doesn't exist
2662 \return pixmap loaded from the file
2663 \sa defaultPixmap(), setDefaultPixmap()
2665 QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, const QPixmap& defPix ) const
2671 ResList::ConstIterator it = myResources.begin();
2672 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2675 for ( ; it != myResources.end() && pix.isNull(); ++it )
2676 pix = (*it)->loadPixmap( resSection(), prefix, name, myConstants );
2683 \brief Specify default language for the application.
2685 QString QtxResourceMgr::defaultLanguage() const
2692 \brief Select language to be used.
2693 \param preferableLanguage preferable language name (if empty, default language is used)
2695 QString QtxResourceMgr::language( const QString& preferableLanguage ) const
2697 // first try to select preferable language probably specified via the parameter
2698 QString lang = preferableLanguage;
2700 // then try default language; selection of default language can be redefined in successors
2701 if ( lang.isEmpty() )
2702 lang = defaultLanguage();
2704 // then try language as defined in the preferences files
2705 if ( lang.isEmpty() )
2706 value( langSection(), "language", lang );
2708 // finally try strongly hardcoded Ennglish
2709 if ( lang.isEmpty() )
2711 lang = QString( "en" );
2712 qWarning() << "QtxResourceMgr: Language not specified. Assumed:" << lang;
2720 \brief Load translation files according to the specified language.
2722 Names of the translation files are calculated according to the pattern specified
2723 by the "translators" option (this option is read from the section "language" of resources files).
2724 By default, "%P_msg_%L.qm" pattern is used.
2725 Keywords \%A, \%P, \%L in the pattern are substituted by the application name, prefix and language name
2727 For example, for prefix "SUIT" and language "en", all translation files "SUIT_msg_en.qm" are searched and
2730 If prefix is empty or null string, all translation files specified in the "resources" section of resources
2731 files are loaded (actually, the section is retrieved from resSection() method).
2732 If language is not specified, it is retrieved from the langSection() method, and if the latest is also empty,
2733 by default "en" (English) language is used.
2734 By default, settings from the user preferences file are also loaded (if user resource file is valid,
2735 see userFileName()). To avoid loading user settings, pass \c false as first parameter.
2737 \param pref parameter which defines translation context (for example, package name)
2738 \param preferableLanguage language name
2740 \sa resSection(), langSection(), loadTranslators()
2742 void QtxResourceMgr::loadLanguage( const QString& pref, const QString& preferableLanguage )
2746 QMap<QChar, QString> substMap;
2747 substMap.insert( 'A', appName() );
2749 QString lang = language( preferableLanguage );
2751 substMap.insert( 'L', lang );
2754 if ( value( langSection(), "translators", trs, false ) && !trs.isEmpty() )
2756 QStringList translators = option( "translators" ).split( "|", QString::SkipEmptyParts );
2757 QStringList newTranslators = trs.split( "|", QString::SkipEmptyParts );
2758 for ( int i = 0; i < (int)newTranslators.count(); i++ )
2760 if ( translators.indexOf( newTranslators[i] ) < 0 )
2761 translators += newTranslators[i];
2763 setOption( "translators", translators.join( "|" ) );
2766 QStringList trList = option( "translators" ).split( "|", QString::SkipEmptyParts );
2767 if ( trList.isEmpty() )
2769 trList.append( "%P_msg_%L.qm" );
2770 qWarning() << "QtxResourceMgr: Translators not defined. Assumed:" << trList[0];
2773 QStringList prefixList;
2774 if ( !pref.isEmpty() )
2775 prefixList.append( pref );
2777 prefixList = parameters( resSection() );
2779 if ( pref.isEmpty() && lang != "en" ) {
2780 // load Qt resources
2781 QString qt_translations = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
2782 QString qt_dir_trpath = Qtx::qtDir( "translations" );
2783 QTranslator* trans = new QtxTranslator( 0 );
2784 if ( trans->load( QString("qt_%1").arg( lang ), qt_translations ) || trans->load( QString("qt_%1").arg( lang ), qt_dir_trpath ) ) {
2785 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2789 for ( QStringList::ConstIterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
2791 QString prefix = *iter;
2792 substMap.insert( 'P', prefix );
2795 for ( QStringList::ConstIterator it = trList.begin(); it != trList.end(); ++it )
2796 trs.append( substMacro( *it, substMap ).trimmed() );
2798 loadTranslators( prefix, trs );
2803 \brief Load translation files for the specified translation context.
2804 \param prefix parameter which defines translation context (for example, package name)
2805 \param translators list of translation files
2808 void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& translators )
2814 ResList::ConstIterator iter = myResources.begin();
2815 if ( myHasUserValues && workingMode() == IgnoreUserValues )
2818 for ( ; iter != myResources.end(); ++iter )
2819 lst.prepend( *iter );
2821 QTranslator* trans = 0;
2823 for ( ResList::Iterator it = lst.begin(); it != lst.end(); ++it )
2825 for ( QStringList::ConstIterator itr = translators.begin(); itr != translators.end(); ++itr )
2827 trans = (*it)->loadTranslator( resSection(), prefix, *itr, myConstants );
2830 if ( !myTranslator[prefix].contains( trans ) )
2831 myTranslator[prefix].append( trans );
2832 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2839 \brief Load translation file.
2840 \param prefix parameter which defines translation context (for example, package name)
2841 \param name translator file name
2842 \sa loadLanguage(), loadTranslators()
2844 void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name )
2848 QTranslator* trans = 0;
2850 Resources* ur = !myResources.isEmpty() && workingMode() == IgnoreUserValues ? myResources[0] : 0;
2852 QListIterator<Resources*> it( myResources );
2854 while ( it.hasPrevious() )
2856 Resources* r = it.previous();
2857 if ( r == ur ) break;
2859 trans = r->loadTranslator( resSection(), prefix, name, myConstants );
2862 if ( !myTranslator[prefix].contains( trans ) )
2863 myTranslator[prefix].append( trans );
2864 if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
2870 \brief Add custom translator.
2871 \param prefix parameter which defines translation context (for example, package name)
2872 \param translator translator being installed
2873 \sa loadLanguage(), loadTranslators()
2875 void QtxResourceMgr::addTranslator( const QString& prefix, QTranslator* translator )
2879 if ( !myTranslator[prefix].contains( translator ) ) {
2880 myTranslator[prefix].append( translator );
2881 if ( QApplication::instance() )
2882 QApplication::instance()->installTranslator( translator );
2888 \brief Remove all translators corresponding to the specified translation context.
2889 \param prefix parameter which defines translation context (for example, package name)
2891 void QtxResourceMgr::removeTranslators( const QString& prefix )
2893 if ( !myTranslator.contains( prefix ) )
2896 for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
2898 if ( QApplication::instance() ) QApplication::instance()->removeTranslator( *it );
2902 myTranslator.remove( prefix );
2906 \brief Move all translators corresponding to the specified translation context
2907 to the top of translators stack (increase their priority).
2908 \param prefix parameter which defines translation context (for example, package name)
2910 void QtxResourceMgr::raiseTranslators( const QString& prefix )
2912 if ( !myTranslator.contains( prefix ) )
2915 for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
2917 if ( QApplication::instance() ) {
2918 QApplication::instance()->removeTranslator( *it );
2919 QApplication::instance()->installTranslator( *it );
2925 \brief Copy all parameters to the user resources in order to
2926 saved them lately in the user home folder.
2928 void QtxResourceMgr::refresh()
2930 QStringList sl = sections();
2931 for ( QStringList::ConstIterator it = sl.begin(); it != sl.end(); ++it )
2933 QStringList pl = parameters( *it );
2934 for ( QStringList::ConstIterator itr = pl.begin(); itr != pl.end(); ++itr )
2935 setResource( *it, *itr, stringValue( *it, *itr ) );
2940 \brief Set the resource directories (where global confguration files are searched).
2942 This function also clears all currently set resources.
2944 \param dl directories list
2946 void QtxResourceMgr::setDirList( const QStringList& dl )
2949 for ( ResList::Iterator it = myResources.begin(); it != myResources.end(); ++it )
2952 myResources.clear();
2956 \brief Set parameter value.
2957 \param sect section name
2958 \param name parameter name
2959 \param val parameter value
2961 void QtxResourceMgr::setResource( const QString& sect, const QString& name, const QString& val )
2965 if ( !myResources.isEmpty() && myHasUserValues )
2966 myResources.first()->setValue( sect, name, val );
2970 \brief Get user configuration file name.
2972 This method can be redefined in the successor class to customize the user configuration file name.
2973 User configuration file is always situated in the user's home directory. By default .<appName>rc
2974 file is used on Linux (e.g. .MyApprc) and <appName>.<format> under Windows (e.g. MyApp.xml).
2976 Parameter \a for_load (not used in default implementation) specifies the usage mode, i.e. if
2977 user configuration file is opened for reading or writing. This allows customizing a way of application
2978 resources initializing (for example, if the user configuraion file includes version number and there is
2979 no file corresponding to this version in the user's home directory, it could be good idea to try
2980 the configuration file from the previous versions of the application).
2982 \param appName application name
2983 \param for_load boolean flag indicating that file is opened for loading or saving (not used in default implementation)
2984 \return user configuration file name
2985 \sa globalFileName()
2987 QString QtxResourceMgr::userFileName( const QString& appName, const bool /*for_load*/ ) const
2990 QString pathName = QDir::homePath();
2991 QString cfgAppName = QApplication::organizationName();
2992 if ( !cfgAppName.isEmpty() )
2993 pathName = Qtx::addSlash( Qtx::addSlash( pathName ) + QString( ".config" ) ) + cfgAppName;
2996 fileName = QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
2998 fileName = QString( "%1rc" ).arg( appName );
2999 // VSR 24/09/2012: issue 0021781: do not prepend filename with "."
3000 // when user file is stored in ~/.config/<appname> directory
3001 if ( cfgAppName.isEmpty() )
3002 fileName.prepend( "." );
3005 if ( !fileName.isEmpty() )
3006 pathName = Qtx::addSlash( pathName ) + fileName;
3012 \brief Get global configuration file name.
3014 This method can be redefined in the successor class to customize the global configuration file name.
3015 Global configuration files are searched in the directories specified by the application resources
3016 environment variable (e.g. MyAppResources). By default <appName>.<format> file name is used
3019 \param appName application name
3020 \return global configuration file name
3023 QString QtxResourceMgr::globalFileName( const QString& appName ) const
3025 return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
3029 \brief This function is called after user configuration file is saved.
3030 Can be redefined in the successor classes, default implementation does nothing.
3032 void QtxResourceMgr::saved()
3037 \brief Perform substitution of the patterns like \%A, \%B, etc by values from the map.
3039 Used by loadLanguage().
3041 \param src sring to be processed
3042 \param substMap map of values for replacing
3043 \return processed string
3045 QString QtxResourceMgr::substMacro( const QString& src, const QMap<QChar, QString>& substMap ) const
3049 QRegExp rx( "%[A-Za-z%]" );
3052 while ( ( idx = rx.indexIn( trg, idx ) ) >= 0 )
3054 QChar spec = trg.at( idx + 1 );
3058 else if ( substMap.contains( spec ) )
3059 subst = substMap[spec];
3061 if ( !subst.isEmpty() )
3063 trg.replace( idx, rx.matchedLength(), subst );
3064 idx += subst.length();
3067 idx += rx.matchedLength();