class QtxResourceMgr::Resources
{
+private:
+ typedef QMap<QString, Section> SectionMap;
+ typedef QMap<QString, QString> OptionsMap;
+
public:
Resources( QtxResourceMgr*, const QString& );
virtual ~Resources();
QString file() const;
void setFile( const QString& );
- QString value( const QString&, const QString&, const bool ) const;
+ QString value( const QString&, const QString&, const bool, const OptionsMap& ) const;
void setValue( const QString&, const QString&, const QString& );
bool hasSection( const QString& ) const;
void removeSection( const QString& );
void removeValue( const QString&, const QString& );
- QPixmap loadPixmap( const QString&, const QString&, const QString& ) const;
- QTranslator* loadTranslator( const QString&, const QString&, const QString& ) const;
-
- QString makeSubstitution( const QString&, const QString&, const QString& ) const;
+ QPixmap loadPixmap( const QString&, const QString&, const QString&, const OptionsMap& ) const;
+ QTranslator* loadTranslator( const QString&, const QString&, const QString&, const OptionsMap& ) const;
void clear();
QStringList sections() const;
QStringList parameters( const QString& ) const;
- QString path( const QString&, const QString&, const QString& ) const;
+ QString path( const QString&, const QString&, const QString&, const OptionsMap& ) const;
protected:
QtxResourceMgr* resMgr() const;
Section section( const QString& );
const Section section( const QString& ) const;
- QString fileName( const QString&, const QString&, const QString& ) const;
+ QString makeSubstitution( const QString&, const QString&, const QString&, const OptionsMap& ) const;
-private:
- typedef QMap<QString, Section> SectionMap;
+ QString fileName( const QString&, const QString&, const QString&, const OptionsMap& ) const;
private:
QtxResourceMgr* myMgr; //!< resources manager
\return parameter value or null QString if there is no such parameter
\sa setValue(), makeSubstitution()
*/
-QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst ) const
+QString QtxResourceMgr::Resources::value( const QString& sect, const QString& name, const bool subst, const OptionsMap& constants ) const
{
QString val;
{
val = section( sect )[name];
if ( subst )
- val = makeSubstitution( val, sect, name );
+ val = makeSubstitution( val, sect, name, constants );
}
return val;
}
\return absolute file path or null QString if file does not exist
\sa fileName(), file(), makeSubstitution()
*/
-QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name ) const
+QString QtxResourceMgr::Resources::path( const QString& sec, const QString& prefix, const QString& name, const OptionsMap& constants ) const
{
- QString filePath = fileName( sec, prefix, name );
+ QString filePath = fileName( sec, prefix, name, constants );
if ( !filePath.isEmpty() )
{
if ( !QFileInfo( filePath ).exists() )
does not exist in section \sec
\sa path(), file(), makeSubstitution()
*/
-QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
+QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name, const OptionsMap& constants ) const
{
QString path;
if ( !QFileInfo( name ).isRelative() )
{
if ( hasValue( sect, prefix ) )
{
- path = value( sect, prefix, true );
+ path = value( sect, prefix, true, constants );
if ( !path.isEmpty() )
{
if ( QFileInfo( path ).isRelative() )
\param name pixmap file name
\return pixmap loaded from file
*/
-QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
+QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name, const OptionsMap& constants ) const
{
- QString fname = fileName( sect, prefix, name );
+ QString fname = fileName( sect, prefix, name, constants );
bool toCache = resMgr() ? resMgr()->isPixmapCached() : false;
QPixmap p;
if( toCache && myPixmapCache.contains( fname ) )
\param name translation file name
\return just created and loaded translator or 0 in case of error
*/
-QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const
+QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name, const OptionsMap& constants ) const
{
QTranslator* trans = new QtxTranslator( 0 );
- QString fname = QDir::toNativeSeparators( fileName( sect, prefix, name ) );
+ QString fname = QDir::toNativeSeparators( fileName( sect, prefix, name, constants ) );
if ( !trans->load( Qtx::file( fname, false ), Qtx::dir( fname ) ) )
{
delete trans;
\param name name of variable which must be ignored during substitution
\return processed string (with all substitutions made)
*/
-QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name ) const
+QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const QString& sect, const QString& name, const OptionsMap& constants ) const
{
QString res = str;
if ( envName.isNull() )
break;
- QString newStr;
- if ( ::getenv( envName.toLatin1() ) )
+ // First we look in the constants map
+ QString newStr = constants.value( envName, QString() );
+
+ // Then we check for environment variable
+ if ( newStr.isEmpty() && ::getenv( envName.toLatin1() ) )
newStr = QString( ::getenv( envName.toLatin1() ) );
- if ( newStr.isNull() )
+ if ( newStr.isEmpty() )
{
if ( ignoreMap.contains( envName ) )
{
}
if ( hasValue( sect, envName ) )
- newStr = value( sect, envName, false );
+ newStr = value( sect, envName, false, constants );
ignoreMap.insert( envName, 0 );
}
res.replace( start, len, newStr );
{
ok = (*it)->hasValue( sect, name );
if ( ok )
- val = (*it)->value( sect, name, subst );
+ val = (*it)->value( sect, name, subst, myConstants );
}
return ok;
\param def default value
\return parameter value (or default value if parameter is not found)
*/
-QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def ) const
+QString QtxResourceMgr::stringValue( const QString& sect, const QString& name, const QString& def, const bool subst ) const
{
QString val;
- if ( !value( sect, name, val ) )
+ if ( !value( sect, name, val, subst ) )
val = def;
return val;
}
myOptions.insert( opt, val );
}
+/*!
+ \brief Get names of all known constants.
+ \return list of constants names
+ \sa constant(), setConstant
+*/
+QStringList QtxResourceMgr::constants() const
+{
+ return myConstants.keys();
+}
+
+/*!
+ \brief Get the value of the known constant.
+
+ If constant is not set, null QString is returned.
+
+ \param name constant name
+ \return constant value
+ \sa setConstant(), constants()
+*/
+QString QtxResourceMgr::constant( const QString& name ) const
+{
+ return myConstants.value( name, QString() );
+}
+
+/*!
+ \brief Set the value of the constant.
+ \param name constant name
+ \param value constant value
+ \sa constants(), constants()
+*/
+void QtxResourceMgr::setConstant( const QString& name, const QString& value )
+{
+ if ( !name.isEmpty() )
+ myConstants.insert( name, value );
+}
+
/*!
\brief Load all resources from all resource files (global and user).
\return \c true on success and \c false on error
++it;
for ( ; it != myResources.end() && res.isEmpty(); ++it )
- res = (*it)->path( sect, prefix, name );
+ res = (*it)->path( sect, prefix, name, myConstants );
return res;
}
++it;
for ( ; it != myResources.end() && pix.isNull(); ++it )
- pix = (*it)->loadPixmap( resSection(), prefix, name );
+ pix = (*it)->loadPixmap( resSection(), prefix, name, myConstants );
if ( pix.isNull() )
pix = defPix;
return pix;
{
for ( QStringList::ConstIterator itr = translators.begin(); itr != translators.end(); ++itr )
{
- trans = (*it)->loadTranslator( resSection(), prefix, *itr );
+ trans = (*it)->loadTranslator( resSection(), prefix, *itr, myConstants );
if ( trans )
{
if ( !myTranslator[prefix].contains( trans ) )
Resources* r = it.previous();
if ( r == ur ) break;
- trans = r->loadTranslator( resSection(), prefix, name );
+ trans = r->loadTranslator( resSection(), prefix, name, myConstants );
if ( trans )
{
if ( !myTranslator[prefix].contains( trans ) )
return ProcessEvent( new TGetSettingEvent( name ) );
}
+/*!
+ \fn QString SalomePyQt::constant( const QString& name );
+ \brief Get constant's value from application's resource manager.
+
+ \param name name of the constant
+ \return value of the constant
+
+ \sa setConstant()
+*/
+
+class TGetConstantEvent: public SALOME_Event
+{
+public:
+ typedef QString TResult;
+ TResult myResult;
+ QString myName;
+ TGetConstantEvent( const QString& name ) : myName( name ) {}
+ virtual void Execute()
+ {
+ if ( SUIT_Session::session() )
+ myResult = SUIT_Session::session()->resourceMgr()->constant( myName );
+ }
+};
+QString SalomePyQt::constant( const QString& name )
+{
+ return ProcessEvent( new TGetConstantEvent( name ) );
+}
+
+/*!
+ \brief Add constant to the application's resource manager.
+
+ This function is useful to specify programmatically specific
+ variables that are referenced in the resource setting.
+
+ For example, some resource value can be set as "$(myroot)/data/files".
+ Then, "mypath" constant can be set programmatically by the application
+ depending on run-time requirements.
+
+ \param section resources file section name
+ \param name name of the constant
+ \param value value of the constant
+
+ \sa constant()
+*/
+void SalomePyQt::setConstant( const QString& name, const QString& value )
+{
+ class TEvent: public SALOME_Event
+ {
+ QString myName, myValue;
+ public:
+ TEvent( const QString& name, const QString& value )
+ : myName( name ), myValue( value ) {}
+ virtual void Execute()
+ {
+ if ( SUIT_Session::session() )
+ SUIT_Session::session()->resourceMgr()->setConstant( myName, myValue );
+ }
+ };
+ ProcessVoidEvent( new TEvent( name, value ) );
+}
+
/*!
\brief Add double setting to the application preferences.
\param section resources file section name
/*!
\fn QString SalomePyQt::stringSetting( const QString& section,
const QString& name,
- const QString& def );
+ const QString& def,
+ const bool subst );
\brief Get string setting from the application preferences.
\param section resources file section name
\param name setting name
\param def default value which is returned if the setting is not found
+ \param subst \c true to make substitution, \c false to get "raw" value
\return setting value
*/
TResult myResult;
QString mySection;
QString myName;
+ bool mySubst;
TResult myDefault;
- TGetStrSettingEvent( const QString& section, const QString& name, const QString& def )
- : mySection( section ), myName( name ), myDefault( def ) {}
+ TGetStrSettingEvent( const QString& section, const QString& name, const QString& def, const bool subst )
+ : mySection( section ), myName( name ), myDefault( def ), mySubst( subst ) {}
virtual void Execute()
{
if ( SUIT_Session::session() ) {
SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
- myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->stringValue( mySection, myName, myDefault ) : myDefault;
+ myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->stringValue( mySection, myName, myDefault, mySubst ) : myDefault;
}
}
};
-QString SalomePyQt::stringSetting( const QString& section, const QString& name, const QString& def )
+QString SalomePyQt::stringSetting( const QString& section, const QString& name, const QString& def, const bool subst )
{
- return ProcessEvent( new TGetStrSettingEvent( section, name, def ) );
+ return ProcessEvent( new TGetStrSettingEvent( section, name, def, subst ) );
}
/*!