From: vsr Date: Wed, 7 Sep 2016 10:42:07 +0000 (+0300) Subject: Introduce possibility to specify constants in the Resources manager. X-Git-Tag: V8_1_0rc1~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=3aac643b57ae1acf9961017906a67c57b83627f8;p=modules%2Fgui.git Introduce possibility to specify constants in the Resources manager. + Manage Python-like substitutions when performing variable substitution. --- diff --git a/src/Qtx/Qtx.cxx b/src/Qtx/Qtx.cxx index c60b75b56..fd7622eec 100755 --- a/src/Qtx/Qtx.cxx +++ b/src/Qtx/Qtx.cxx @@ -651,11 +651,17 @@ QCompleter* Qtx::pathCompleter( const PathType type, const QString& filter ) /*! \brief Parse given string to retrieve environment variable. - Looks through the string for the patterns: ${name} or $(name) or %name%. + Looks through the string for the environment variable patterns. If string contains variable satisfying any pattern, the variable name is returned, start index of the variable is returned in the \a start parameter, and length of the variable is returned in the \a len parameter. + Supported environment variables definitions: + - ${name} or $name : Linux shell variable + - $(name) : GNU make substitution + - %name% : Windows shell variable + - %(name)s : Python substitutions: + \param str string being processed \param start if variable is found, this parameter contains its starting position in the \a str @@ -667,30 +673,22 @@ QString Qtx::findEnvVar( const QString& str, int& start, int& len ) QString varName; len = 0; - QRegExp rx( "(^\\$\\{|[^\\$]\\$\\{)([a-zA-Z]+[a-zA-Z0-9_]*)(\\})|(^\\$\\(|[^\\$]\\$\\()([a-zA-Z]+[a-zA-Z0-9_]*)(\\))|(^\\$|[^\\$]\\$)([a-zA-Z]+[a-zA-Z0-9_]*)|(^%|[^%]%)([a-zA-Z]+[a-zA-Z0-9_]*)(%[^%]|%$)" ); - - int pos = rx.indexIn( str, start ); - if ( pos != -1 ) + QStringList rxList; + rxList << "\\$\\{([a-zA-Z][a-zA-Z_0-9]*)\\}"; // ${name} + rxList << "\\$([a-zA-Z][a-zA-Z_0-9]*)"; // $name + rxList << "\\$\\(([a-zA-Z][a-zA-Z_0-9]*)\\)"; // $(name) + rxList << "%([a-zA-Z][a-zA-Z0-9_]*)%"; // %name% + rxList << "%\\(([a-zA-Z][a-zA-Z_0-9]*)\\)s"; // %(name)s + + for ( int i = 0; i < rxList.count() && varName.isEmpty(); ++i ) { - int i = 1; - while ( i <= rx.captureCount() && varName.isEmpty() ) - { - QString capStr = rx.cap( i ); - if ( !capStr.contains( "%" ) && !capStr.contains( "$" ) ) - varName = capStr; - i++; - } - - if ( !varName.isEmpty() ) + QRegExp rx(rxList[i]); + int pos = rx.indexIn( str, start ); + if ( pos != -1 ) { - int capIdx = i - 1; - start = rx.pos( capIdx ); - int end = start + varName.length(); - if ( capIdx > 1 && rx.cap( capIdx - 1 ).contains( QRegExp( "\\$|%" ) ) ) - start = rx.pos( capIdx - 1 ) + rx.cap( capIdx - 1 ).indexOf( QRegExp( "\\$|%" ) ); - if ( capIdx < rx.captureCount() && !rx.cap( capIdx - 1 ).isEmpty() ) - end++; - len = end - start; + varName = rx.cap( 1 ); + start = pos; + len = rx.matchedLength(); } } return varName; diff --git a/src/Qtx/QtxResourceMgr.cxx b/src/Qtx/QtxResourceMgr.cxx index 022fb88a8..4df9c1ff8 100644 --- a/src/Qtx/QtxResourceMgr.cxx +++ b/src/Qtx/QtxResourceMgr.cxx @@ -74,6 +74,10 @@ static const char* pixmap_not_found_xpm[] = { class QtxResourceMgr::Resources { +private: + typedef QMap SectionMap; + typedef QMap OptionsMap; + public: Resources( QtxResourceMgr*, const QString& ); virtual ~Resources(); @@ -81,7 +85,7 @@ public: 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; @@ -90,17 +94,15 @@ public: 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; @@ -109,10 +111,9 @@ private: 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 SectionMap; + QString fileName( const QString&, const QString&, const QString&, const OptionsMap& ) const; private: QtxResourceMgr* myMgr; //!< resources manager @@ -172,7 +173,7 @@ void QtxResourceMgr::Resources::setFile( const QString& fn ) \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; @@ -180,7 +181,7 @@ QString QtxResourceMgr::Resources::value( const QString& sect, const QString& na { val = section( sect )[name]; if ( subst ) - val = makeSubstitution( val, sect, name ); + val = makeSubstitution( val, sect, name, constants ); } return val; } @@ -291,9 +292,9 @@ QStringList QtxResourceMgr::Resources::parameters( const QString& sec ) const \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() ) @@ -354,7 +355,7 @@ const QtxResourceMgr::Section QtxResourceMgr::Resources::section( const QString& 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() ) @@ -365,7 +366,7 @@ QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& { if ( hasValue( sect, prefix ) ) { - path = value( sect, prefix, true ); + path = value( sect, prefix, true, constants ); if ( !path.isEmpty() ) { if ( QFileInfo( path ).isRelative() ) @@ -397,9 +398,9 @@ QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& \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 ) ) @@ -420,10 +421,10 @@ QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QStrin \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; @@ -443,7 +444,7 @@ QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, con \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; @@ -457,11 +458,14 @@ QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const Q 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 ) ) { @@ -470,7 +474,7 @@ QString QtxResourceMgr::Resources::makeSubstitution( const QString& str, const Q } if ( hasValue( sect, envName ) ) - newStr = value( sect, envName, false ); + newStr = value( sect, envName, false, constants ); ignoreMap.insert( envName, 0 ); } res.replace( start, len, newStr ); @@ -1694,7 +1698,7 @@ bool QtxResourceMgr::value( const QString& sect, const QString& name, QString& v { ok = (*it)->hasValue( sect, name ); if ( ok ) - val = (*it)->value( sect, name, subst ); + val = (*it)->value( sect, name, subst, myConstants ); } return ok; @@ -1805,10 +1809,10 @@ QColor QtxResourceMgr::colorValue( const QString& sect, const QString& name, con \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; } @@ -2272,6 +2276,42 @@ void QtxResourceMgr::setOption( const QString& opt, const QString& 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 @@ -2494,7 +2534,7 @@ QString QtxResourceMgr::path( const QString& sect, const QString& prefix, const ++it; for ( ; it != myResources.end() && res.isEmpty(); ++it ) - res = (*it)->path( sect, prefix, name ); + res = (*it)->path( sect, prefix, name, myConstants ); return res; } @@ -2630,7 +2670,7 @@ QPixmap QtxResourceMgr::loadPixmap( const QString& prefix, const QString& name, ++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; @@ -2752,7 +2792,7 @@ void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList& { 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 ) ) @@ -2784,7 +2824,7 @@ void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name 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 ) ) diff --git a/src/Qtx/QtxResourceMgr.h b/src/Qtx/QtxResourceMgr.h index 80f306da6..a7df2f268 100644 --- a/src/Qtx/QtxResourceMgr.h +++ b/src/Qtx/QtxResourceMgr.h @@ -105,7 +105,7 @@ public: bool booleanValue( const QString&, const QString&, const bool = false ) const; QFont fontValue( const QString&, const QString&, const QFont& = QFont() ) const; QColor colorValue( const QString&, const QString&, const QColor& = QColor() ) const; - QString stringValue( const QString&, const QString&, const QString& = QString() ) const; + QString stringValue( const QString&, const QString&, const QString& = QString(), const bool = true ) const; QByteArray byteArrayValue( const QString&, const QString&, const QByteArray& = QByteArray() ) const; QLinearGradient linearGradientValue( const QString&, const QString&, const QLinearGradient& = QLinearGradient() ) const; QRadialGradient radialGradientValue( const QString&, const QString&, const QRadialGradient& = QRadialGradient() ) const; @@ -141,6 +141,10 @@ public: QString option( const QString& ) const; void setOption( const QString&, const QString& ); + QStringList constants() const; + QString constant( const QString& ) const; + void setConstant( const QString&, const QString& ); + QPixmap defaultPixmap() const; virtual void setDefaultPixmap( const QPixmap& ); @@ -199,6 +203,7 @@ private: QStringList myDirList; //!< list of resources directories FormatList myFormats; //!< list of formats OptionsMap myOptions; //!< options map + OptionsMap myConstants; //!< constants map ResList myResources; //!< resources list bool myCheckExist; //!< "check existance" flag TransListMap myTranslator; //!< map of loaded translators diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx index 71ddbfa0d..0d91d941d 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx @@ -1010,6 +1010,67 @@ QString SalomePyQt::getSetting( const QString& name ) 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 @@ -1285,11 +1346,13 @@ bool SalomePyQt::boolSetting( const QString& section, const QString& name, const /*! \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 */ @@ -1300,20 +1363,21 @@ public: 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 ) ); } /*! diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h index 60d7be45d..f508b046f 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h @@ -246,6 +246,8 @@ public: static QAction* action( const int ); static int actionId( const QAction* ); + static QString constant ( const QString& ); + static void setConstant ( const QString&, const QString& ); static void addSetting ( const QString&, const QString&, const double ); static void addSetting ( const QString&, const QString&, const int ); static void addSetting ( const QString&, const QString&, const bool, const int ); @@ -255,7 +257,7 @@ public: static int integerSetting( const QString&, const QString&, const int = 0 ); static double doubleSetting ( const QString&, const QString&, const double = 0 ); static bool boolSetting ( const QString&, const QString&, const bool = 0 ); - static QString stringSetting ( const QString&, const QString&, const QString& = QString("") ); + static QString stringSetting ( const QString&, const QString&, const QString& = QString(""), const bool = true ); static QColor colorSetting ( const QString&, const QString&, const QColor& = QColor() ); static QByteArray byteArraySetting( const QString&, const QString&, const QByteArray& = QByteArray() ); static void removeSetting ( const QString&, const QString& ); diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip index 212f3725c..662aa4858 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip @@ -350,6 +350,8 @@ public: static QAction* action( const int ) /ReleaseGIL/ ; static int actionId( const QAction* ) /ReleaseGIL/ ; + static QString constant ( const QString& ) /ReleaseGIL/ ; + static void setConstant ( const QString&, const QString& ) /ReleaseGIL/ ; static void addSetting ( const QString&, const QString&, const double ) /ReleaseGIL/ ; static void addSetting ( const QString&, const QString&, const int /Constrained/ ) /ReleaseGIL/ ; static void addSetting ( const QString&, const QString&, const bool, const int ) /ReleaseGIL/ ; @@ -359,7 +361,7 @@ public: static int integerSetting( const QString&, const QString&, const int = 0 ) /ReleaseGIL/ ; static double doubleSetting ( const QString&, const QString&, const double = 0 ) /ReleaseGIL/ ; static bool boolSetting ( const QString&, const QString&, const bool = false ) /ReleaseGIL/ ; - static QString stringSetting ( const QString&, const QString&, const QString& = QString("") ) /ReleaseGIL/ ; + static QString stringSetting ( const QString&, const QString&, const QString& = QString(""), const bool = true ) /ReleaseGIL/ ; static QColor colorSetting ( const QString&, const QString&, const QColor& = QColor() ) /ReleaseGIL/ ; static QByteArray byteArraySetting( const QString&, const QString&, const QByteArray& = QByteArray() ) /ReleaseGIL/ ; static void removeSetting ( const QString&, const QString& ) /ReleaseGIL/ ;