From 1ae46761293cceffc4e65b851da944b0de81ef8e Mon Sep 17 00:00:00 2001 From: vsv Date: Tue, 8 Jul 2014 16:36:31 +0400 Subject: [PATCH] Define Selection validator interface. --- src/Model/Model_Validator.cpp | 4 +-- src/Model/Model_Validator.h | 4 +-- src/ModelAPI/CMakeLists.txt | 1 + src/ModelAPI/ModelAPI_FeatureValidator.h | 22 ++++++++++++ src/ModelAPI/ModelAPI_Validator.h | 11 +++--- src/ModuleBase/CMakeLists.txt | 1 + src/ModuleBase/ModuleBase_IModule.h | 2 +- .../ModuleBase_ViewSelectionValidator.h | 18 ++++++++++ src/PartSet/PartSet_Module.cpp | 4 +-- src/PartSet/PartSet_Module.h | 2 +- src/XGUI/XGUI_Workshop.cpp | 34 ++++++++++++++++--- src/XGUI/XGUI_Workshop.h | 5 ++- 12 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 src/ModelAPI/ModelAPI_FeatureValidator.h create mode 100644 src/ModuleBase/ModuleBase_ViewSelectionValidator.h diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index fe7110924..3f6e1c6ca 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -77,7 +77,7 @@ const ModelAPI_Validator* Model_ValidatorsFactory::validator(const string& theFe return NULL; // not found } -bool Model_ValidatorsFactory::validate( +/*bool Model_ValidatorsFactory::validate( const boost::shared_ptr& theFeature, const string& theAttrID ) const { map > > >::const_iterator @@ -87,7 +87,7 @@ bool Model_ValidatorsFactory::validate( anAttr = aFeature->second.find(theAttrID); if (anAttr == aFeature->second.cend()) return true; // attribute is not found return anAttr->second.first->validate(theFeature, theAttrID, anAttr->second.second); -} +}*/ Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory() { diff --git a/src/Model/Model_Validator.h b/src/Model/Model_Validator.h index 9d04e2901..a917911d9 100644 --- a/src/Model/Model_Validator.h +++ b/src/Model/Model_Validator.h @@ -46,8 +46,8 @@ public: /// Returns the result of "validate" method for attribute of validator. /// If validator is not exists, returns true: everything is valid by default. - MODEL_EXPORT virtual bool validate( - const boost::shared_ptr& theFeature, const std::string& theAttrID) const; + //MODEL_EXPORT virtual bool validate( + // const boost::shared_ptr& theFeature, const std::string& theAttrID) const; protected: /// Get instance from PluginManager diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 5a13d37a6..8b834dd72 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -19,6 +19,7 @@ SET(PROJECT_HEADERS ModelAPI_AttributeBoolean.h ModelAPI_Events.h ModelAPI_Validator.h + ModelAPI_FeatureValidator.h ) SET(PROJECT_SOURCES diff --git a/src/ModelAPI/ModelAPI_FeatureValidator.h b/src/ModelAPI/ModelAPI_FeatureValidator.h new file mode 100644 index 000000000..2023a66fc --- /dev/null +++ b/src/ModelAPI/ModelAPI_FeatureValidator.h @@ -0,0 +1,22 @@ +// File: ModelAPI_FeatureValidator.h +// Created: 8 Jul 2014 +// Author: Vitaly SMETANNIKOV + +#ifndef ModelAPI_FeatureValidator_H +#define ModelAPI_FeatureValidator_H + +#include "ModelAPI.h" +#include "ModelAPI_Validator.h" + +class MODELAPI_EXPORT ModelAPI_FeatureValidator: public ModelAPI_Validator +{ +public: + /// Returns true if feature and/or attributes are valid + /// \param theFeature the validated feature + /// \param theAttr the validated attribute ID, empty string of feature is validated + /// \param theArguments list of string, feature attribute names: dependent attributes + virtual bool validate(const boost::shared_ptr& theFeature, + const std::string theAttr, std::list theArguments) const = 0; +}; + +#endif diff --git a/src/ModelAPI/ModelAPI_Validator.h b/src/ModelAPI/ModelAPI_Validator.h index 4a0243cd0..4c807763a 100644 --- a/src/ModelAPI/ModelAPI_Validator.h +++ b/src/ModelAPI/ModelAPI_Validator.h @@ -25,12 +25,15 @@ class ModelAPI_Feature; class MODELAPI_EXPORT ModelAPI_Validator { public: + // Make virtual destructor in order to make the class polymorphic + virtual ~ModelAPI_Validator() {}; + /// Returns true if feature and/or attributes are valid /// \param theFeature the validated feature /// \param theAttr the validated attribute ID, empty string of feature is validated /// \param theArguments list of string, feature attribute names: dependent attributes - virtual bool validate(const boost::shared_ptr& theFeature, - const std::string theAttr, std::list theArguments) const = 0; + //virtual bool validate(const boost::shared_ptr& theFeature, + // const std::string theAttr, std::list theArguments) const = 0; }; typedef boost::shared_ptr ValidatorPtr; @@ -66,8 +69,8 @@ public: /// Returns the result of "validate" method for attribute of validator. /// If validator is not exists, returns true: everything is valid by default. - virtual bool validate( - const boost::shared_ptr& theFeature, const std::string& theAttrID) const = 0; + //virtual bool validate( + // const boost::shared_ptr& theFeature, const std::string& theAttrID) const = 0; protected: /// Get instance from PluginManager diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index 6b3847b29..4f34faab8 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -22,6 +22,7 @@ SET(PROJECT_HEADERS ModuleBase_WidgetValue.h ModuleBase_WidgetValueFeature.h ModuleBase_Definitions.h + ModuleBase_ViewSelectionValidator.h ) SET(PROJECT_SOURCES diff --git a/src/ModuleBase/ModuleBase_IModule.h b/src/ModuleBase/ModuleBase_IModule.h index b5245c37c..ae652617e 100644 --- a/src/ModuleBase/ModuleBase_IModule.h +++ b/src/ModuleBase/ModuleBase_IModule.h @@ -26,7 +26,7 @@ public: virtual void launchOperation(const QString& theCmdId) = 0; /// Called when it is necessary to update a command state (enable or disable it) - virtual bool isFeatureEnabled(const QString& theCmdId) const = 0; + //virtual bool isFeatureEnabled(const QString& theCmdId) const = 0; /// Creates custom widgets for property panel virtual QWidget* createWidgetByType(const std::string& theType, QWidget* theParent, diff --git a/src/ModuleBase/ModuleBase_ViewSelectionValidator.h b/src/ModuleBase/ModuleBase_ViewSelectionValidator.h new file mode 100644 index 000000000..9dcc910c0 --- /dev/null +++ b/src/ModuleBase/ModuleBase_ViewSelectionValidator.h @@ -0,0 +1,18 @@ +// File: ModuleBase_ViewSelectionValidator.h +// Created: 8 Jul 2014 +// Author: Vitaly SMETANNIKOV + +#ifndef ModuleBase_ViewSelectionValidator_H +#define ModuleBase_ViewSelectionValidator_H + +#include "ModuleBase.h" + +#include + +class MODULEBASE_EXPORT ModuleBase_ViewSelectionValidator: public ModelAPI_Validator +{ +public: + virtual bool isValid(const Handle(AIS_InteractiveContext)& theContext) const = 0; +}; + +#endif diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 9af38c4dc..8d40a07f7 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -522,7 +522,7 @@ void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& th aPoint->x(), aPoint->y()); } -bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const +/*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const { XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr(); XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr(); @@ -540,7 +540,7 @@ bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const } QStringList aList = aActMgr->nestedCommands(aOperation->id()); return aList.contains(theCmdId); -} +}*/ QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent, Config_WidgetAPI* theWidgetApi, QList& theModelWidgets) diff --git a/src/PartSet/PartSet_Module.h b/src/PartSet/PartSet_Module.h index 7dbbc587b..627d088b1 100644 --- a/src/PartSet/PartSet_Module.h +++ b/src/PartSet/PartSet_Module.h @@ -49,7 +49,7 @@ public: virtual void launchOperation(const QString& theCmdId); /// Called when it is necessary to update a command state (enable or disable it) - virtual bool isFeatureEnabled(const QString& theCmdId) const; + //virtual bool isFeatureEnabled(const QString& theCmdId) const; /// Displays or erase the current operation preview, if it has it. /// \param theFeature the feature instance to be displayed diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 7be906691..c6b5f55bd 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -28,14 +28,18 @@ #include #include #include +#include #include #include #include + #include #include #include +#include + #include #include #include @@ -85,7 +89,7 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector) myDisplayer = new XGUI_Displayer(this); mySelector = new XGUI_SelectionMgr(this); - connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands())); + //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands())); myOperationMgr = new XGUI_OperationMgr(this); myActionsMgr = new XGUI_ActionsMgr(this); @@ -95,6 +99,7 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector) this, SLOT(onContextMenuCommand(const QString&, bool))); myViewerProxy = new XGUI_ViewerProxy(this); + connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection())); myModuleConnector = new XGUI_ModuleConnector(this); @@ -712,7 +717,7 @@ void XGUI_Workshop::updateCommandStatus() } //****************************************************** -void XGUI_Workshop::updateModuleCommands() +QList XGUI_Workshop::getModuleCommands() const { QList aCommands; if (isSalomeMode()) { // update commands in SALOME mode @@ -726,9 +731,7 @@ void XGUI_Workshop::updateModuleCommands() } } } - foreach(QAction* aCmd, aCommands) { - aCmd->setEnabled(myModule->isFeatureEnabled(aCmd->data().toString())); - } + return aCommands; } //****************************************************** @@ -955,3 +958,24 @@ void XGUI_Workshop::showFeatures(QFeatureList theList, bool isVisible) } myDisplayer->updateViewer(); } + +//************************************************************** +void XGUI_Workshop::updateCommandsOnViewSelection() +{ + PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); + ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + Handle(AIS_InteractiveContext) aContext = viewer()->AISContext(); + + QList aActions = getModuleCommands(); + foreach(QAction* aAction, aActions) { + QString aId = aAction->data().toString(); + const ModelAPI_Validator* aValidator = aFactory->validator(aId.toStdString()); + if (aValidator) { + const ModuleBase_ViewSelectionValidator* aSelValidator = + dynamic_cast(aValidator); + if (aSelValidator) { + aAction->setEnabled(aSelValidator->isValid(aContext)); + } + } + } +} diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index 1172bc7de..960c4dd89 100644 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -38,6 +38,7 @@ class QWidget; class QDockWidget; class Model_FeatureUpdatedMessage; +class QAction; /**\class XGUI_Workshop * \ingroup GUI @@ -125,7 +126,7 @@ signals: public slots: void updateCommandStatus(); - void updateModuleCommands(); + void updateCommandsOnViewSelection(); void onNew(); void onOpen(); @@ -155,6 +156,8 @@ protected: void onFeatureCreatedMsg(const Model_FeatureUpdatedMessage* theMsg); void onFeatureRedisplayMsg(const Model_FeatureUpdatedMessage* theMsg); + QList getModuleCommands() const; + protected slots: /// SLOT, that is called after the operation is started. Update workshop state according to /// the started operation, e.g. visualizes the property panel and connect to it. -- 2.39.2