From 21b8032b53c1384132fac9d56db1d375f505c51d Mon Sep 17 00:00:00 2001 From: vsr Date: Mon, 22 May 2017 08:59:44 +0300 Subject: [PATCH] SalomePyQt: allow inserting custom widget as a Preferences item --- src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx | 120 +++++++++++++++++++++- src/SALOME_PYQT/SalomePyQt/SalomePyQt.h | 18 ++++ src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip | 29 ++++++ 3 files changed, 166 insertions(+), 1 deletion(-) diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx index 4748bde69..699ce585f 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx @@ -67,8 +67,10 @@ #include #include #include +#include #include + namespace { /*! @@ -329,6 +331,90 @@ void SALOME_Selection::ClearFilters() ProcessVoidEvent( new TEvent( mySelMgr ) ); } +/*! + \class UserDefinedContent + \brief The class represents base class for user defined widget that + can be inserted to the Preferences dialog. +*/ + +/*! + \brief Constructor +*/ +UserDefinedContent::UserDefinedContent() + : QWidget() +{ +} + +/*! + \brief Called from Preferences dialog to store settings to the resource file. +*/ +void UserDefinedContent::store() +{ +} + +/*! + \brief Called from Preferences dialog to restore settings from the resource file. +*/ +void UserDefinedContent::retrieve() +{ +} + +/*! + \class SgPyQtUserDefinedContent + \brief A Wrapper for UserDefinedContent class. + \internal +*/ +class SgPyQtUserDefinedContent: public QtxUserDefinedContent +{ +public: + SgPyQtUserDefinedContent(UserDefinedContent*); + virtual ~SgPyQtUserDefinedContent(); + + void store( QtxResourceMgr*, QtxPreferenceMgr* ); + void retrieve( QtxResourceMgr*, QtxPreferenceMgr* ); + +private: + UserDefinedContent* myContent; +}; + +/*! + \brief Create custom item for Preferences dialog wrapping widget passed from Python. + \internal +*/ +SgPyQtUserDefinedContent::SgPyQtUserDefinedContent(UserDefinedContent* content) + : QtxUserDefinedContent( 0 ), myContent( content ) +{ + QVBoxLayout* l = new QVBoxLayout( this ); + l->setContentsMargins( 0, 0, 0, 0 ); + l->addWidget( myContent ); +} + +/*! + \brief Destructor. + \internal +*/ +SgPyQtUserDefinedContent::~SgPyQtUserDefinedContent() +{ +} + +/*! + \brief Called from Preferences dialog to store settings to the resource file. + \internal +*/ +void SgPyQtUserDefinedContent::store( QtxResourceMgr*, QtxPreferenceMgr* ) +{ + myContent->store(); +} + +/*! + \brief Called from Preferences dialog to restore settings from the resource file. + \internal +*/ +void SgPyQtUserDefinedContent::retrieve( QtxResourceMgr*, QtxPreferenceMgr* ) +{ + myContent->retrieve(); +} + /*! \class SalomePyQt \brief The class provides utility functions which can be used in the Python @@ -2440,7 +2526,39 @@ void SalomePyQt::setPreferenceProperty( const int id, } } }; - ProcessVoidEvent( new TEvent( id, prop, var) ); + ProcessVoidEvent( new TEvent( id, prop, var ) ); +} + +/*! + \brief Set specific widget as a custom preferences item. + \param id preferences identifier + \param prop preferences property name + \param widget custom widget +*/ +void SalomePyQt::setPreferencePropertyWg( const int id, + const QString& prop, + UserDefinedContent* widget ) +{ + class TEvent: public SALOME_Event + { + int myId; + QString myProp; + UserDefinedContent* myWidget; + public: + TEvent( const int id, const QString& prop, UserDefinedContent* widget ) + : myId( id ), myProp( prop ), myWidget( widget ) {} + virtual void Execute() + { + LightApp_Module* module = getActiveModule(); + if ( module ) { + LightApp_Preferences* pref = module->getApp()->preferences(); + if ( pref ) { + pref->setItemProperty( myProp, (qint64) new SgPyQtUserDefinedContent( myWidget ), myId ); + } + } + } + }; + ProcessVoidEvent( new TEvent( id, prop, widget ) ); } /*! diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h index d31a8600f..a64b01e89 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.h @@ -114,6 +114,23 @@ enum { PT_Font = LightApp_Preferences::Font, PT_DirList = LightApp_Preferences::DirList, PT_File = LightApp_Preferences::File, + PT_Slider = LightApp_Preferences::Slider, + PT_Shortcut = LightApp_Preferences::Shortcut, + PT_ShortcutTree = LightApp_Preferences::ShortcutTree, + PT_BiColor = LightApp_Preferences::BiColor, + PT_Background = LightApp_Preferences::Background, + PT_UserDefined = LightApp_Preferences::UserDefined, +}; + +class UserDefinedContent: public QWidget +{ + Q_OBJECT + +public: + explicit UserDefinedContent(); + + virtual void store(); + virtual void retrieve(); }; //! Orientation @@ -277,6 +294,7 @@ public: const QString& = QString() ); static QVariant preferenceProperty( const int, const QString& ); static void setPreferenceProperty( const int, const QString&, const QVariant& ); + static void setPreferencePropertyWg( const int, const QString&, UserDefinedContent* ); static void addPreferenceProperty( const int, const QString&, const int, const QVariant& ); static void message( const QString&, bool = true ); diff --git a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip index 59309bcfd..ca70e94c5 100644 --- a/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip +++ b/src/SALOME_PYQT/SalomePyQt/SalomePyQt.sip @@ -102,6 +102,12 @@ enum PrefType { PT_Font, PT_DirList, PT_File, + PT_Slider, + PT_Shortcut, + PT_ShortcutTree, + PT_BiColor, + PT_Background, + PT_UserDefined }; enum Orientation { @@ -237,6 +243,26 @@ private: QtxTreeView( const QtxTreeView& ); }; +class UserDefinedContent : public QWidget +{ +%TypeHeaderCode +#include +%End + +%ConvertToSubClassCode + if ( qobject_cast( sipCpp ) ) + sipClass = sipClass_UserDefinedContent; + else + sipClass = NULL; +%End + +public: + explicit UserDefinedContent(); + + virtual void store(); + virtual void retrieve(); +}; + enum VisibilityState { ShownState, @@ -391,6 +417,9 @@ public: static void setPreferenceProperty( const int, const QString&, const QVariant& ) /ReleaseGIL/ ; + static void setPreferencePropertyWg( const int, + const QString&, + UserDefinedContent* ) /ReleaseGIL/ ; static void addPreferenceProperty( const int, const QString&, const int, -- 2.39.2