From 7f39940037e138a8d4d6fb09290e5ccf65c29139 Mon Sep 17 00:00:00 2001 From: vsv Date: Mon, 4 Aug 2014 15:05:09 +0400 Subject: [PATCH] Feature validator created --- src/ModuleBase/CMakeLists.txt | 1 + src/ModuleBase/ModuleBase_FeatureValidator.h | 20 +++++++ src/ModuleBase/ModuleBase_IOperation.cpp | 18 +++--- src/ModuleBase/ModuleBase_IOperation.h | 14 +++-- src/ModuleBase/ModuleBase_Operation.cpp | 23 ++++++++ src/ModuleBase/ModuleBase_Operation.h | 4 ++ src/PartSet/PartSet_Module.cpp | 2 +- .../PartSet_OperationFeatureCreate.cpp | 22 +++----- src/PartSet/PartSet_OperationFeatureCreate.h | 8 +-- src/PartSet/PartSet_OperationFeatureEdit.cpp | 37 ++++++------ .../PartSet_OperationFeatureEditMulti.cpp | 17 +++--- src/PartSet/PartSet_Validators.cpp | 1 + src/PartSet/PartSet_Validators.h | 5 ++ src/SketchPlugin/CMakeLists.txt | 2 + src/SketchPlugin/SketchPlugin_Plugin.cpp | 6 ++ src/SketchPlugin/SketchPlugin_Validators.cpp | 56 +++++++++++++++++++ src/SketchPlugin/SketchPlugin_Validators.h | 19 +++++++ src/SketchPlugin/plugin-Sketch.xml | 7 ++- src/XGUI/XGUI_Workshop.cpp | 4 +- 19 files changed, 203 insertions(+), 63 deletions(-) create mode 100644 src/ModuleBase/ModuleBase_FeatureValidator.h create mode 100644 src/SketchPlugin/SketchPlugin_Validators.cpp create mode 100644 src/SketchPlugin/SketchPlugin_Validators.h diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index 1d8531d4d..4c3f20caa 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -27,6 +27,7 @@ SET(PROJECT_HEADERS ModuleBase_ViewerPrs.h ModuleBase_Tools.h ModuleBase_ResultValidators.h + ModuleBase_FeatureValidator.h ) SET(PROJECT_SOURCES diff --git a/src/ModuleBase/ModuleBase_FeatureValidator.h b/src/ModuleBase/ModuleBase_FeatureValidator.h new file mode 100644 index 000000000..ccbcca6e5 --- /dev/null +++ b/src/ModuleBase/ModuleBase_FeatureValidator.h @@ -0,0 +1,20 @@ +// File: ModuleBase_FeatureValidator.h +// Created: 8 Jul 2014 +// Author: Vitaly SMETANNIKOV + +#ifndef ModuleBase_FeatureValidator_H +#define ModuleBase_FeatureValidator_H + +#include "ModuleBase.h" + +#include +#include + + +class ModuleBase_FeatureValidator: public ModelAPI_Validator +{ +public: + virtual bool isValid(const FeaturePtr theFeature) const = 0; +}; + +#endif diff --git a/src/ModuleBase/ModuleBase_IOperation.cpp b/src/ModuleBase/ModuleBase_IOperation.cpp index cc398e5b0..cee8fbc2b 100644 --- a/src/ModuleBase/ModuleBase_IOperation.cpp +++ b/src/ModuleBase/ModuleBase_IOperation.cpp @@ -81,17 +81,21 @@ void ModuleBase_IOperation::abort() emit stopped(); } -void ModuleBase_IOperation::commit() +bool ModuleBase_IOperation::commit() { - commitOperation(); - emit committed(); + if (canBeCommitted()) { + commitOperation(); + emit committed(); - stopOperation(); + stopOperation(); - document()->finishOperation(); - emit stopped(); + document()->finishOperation(); + emit stopped(); - afterCommitOperation(); + afterCommitOperation(); + return true; + } + return false; } void ModuleBase_IOperation::setRunning(bool theState) diff --git a/src/ModuleBase/ModuleBase_IOperation.h b/src/ModuleBase/ModuleBase_IOperation.h index 0d304c0a2..c9510192f 100644 --- a/src/ModuleBase/ModuleBase_IOperation.h +++ b/src/ModuleBase/ModuleBase_IOperation.h @@ -57,10 +57,6 @@ public: /// /returns the instance of the description class ModuleBase_OperationDescription* getDescription() const; - /// Verifies whether this operator can be commited. - /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled - virtual bool canBeCommitted() const; - /// Verifies whether this operator can be always started above any already running one /// \return Returns TRUE if current operation must not be checked for ActiveOperation->IsValid( this ) /// This method must be redefined in derived operation if operation of derived class @@ -111,7 +107,7 @@ public slots: /// Commits operation /// Public slot. Commits operation. This slot is not virtual and cannot be redefined. /// Redefine commitOperation method to change behavior of operation instead - void commit(); + bool commit(); /// Alias for start/abort slots /// Public slot. Aborts operation if false, else does nothing. @@ -127,16 +123,24 @@ protected: /// Virtual method called when operation started (see start() method for more description) /// Default impl calls corresponding slot and commits immediately. virtual void startOperation() = 0; + /// Virtual method called when operation stopped - committed or aborted. virtual void stopOperation() = 0; + /// Virtual method called when operation aborted (see abort() method for more description) virtual void abortOperation() = 0; + /// Virtual method called when operation committed (see commit() method for more description) virtual void commitOperation() = 0; + /// Virtual method called after operation committed (see commit() method for more description) /// it is important that the method is called after the stop() signal is emitted virtual void afterCommitOperation() = 0; + /// Verifies whether this operator can be commited. + /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled + virtual bool canBeCommitted() const; + /// Returns pointer to the root document. boost::shared_ptr document() const; diff --git a/src/ModuleBase/ModuleBase_Operation.cpp b/src/ModuleBase/ModuleBase_Operation.cpp index f3a657548..ba44d4581 100644 --- a/src/ModuleBase/ModuleBase_Operation.cpp +++ b/src/ModuleBase/ModuleBase_Operation.cpp @@ -9,6 +9,7 @@ #include "ModuleBase_OperationDescription.h" #include "ModuleBase_ModelWidget.h" +#include "ModuleBase_FeatureValidator.h" #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include @@ -92,6 +94,27 @@ void ModuleBase_Operation::afterCommitOperation() { } +bool ModuleBase_Operation::canBeCommitted() const +{ + if (ModuleBase_IOperation::canBeCommitted()) { + FeaturePtr aFeature = feature(); + std::string aId = aFeature->getKind(); + + PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); + ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + const ModelAPI_Validator* aValidator = aFactory->validator(aId); + if (aValidator) { + const ModuleBase_FeatureValidator* aFValidator = + dynamic_cast(aValidator); + if (aFValidator) { + return aFValidator->isValid(aFeature); + } + } + return true; + } + return false; +} + void ModuleBase_Operation::flushUpdated() { Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); diff --git a/src/ModuleBase/ModuleBase_Operation.h b/src/ModuleBase/ModuleBase_Operation.h index ae2250624..c9693d3f6 100644 --- a/src/ModuleBase/ModuleBase_Operation.h +++ b/src/ModuleBase/ModuleBase_Operation.h @@ -112,6 +112,10 @@ protected: /// Sets the operation feature void setFeature(FeaturePtr theFeature); + /// Verifies whether this operator can be commited. + /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled + virtual bool canBeCommitted() const; + protected: FeaturePtr myFeature; /// the operation feature to be handled }; diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 611d2a5fd..160f63f2e 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -108,7 +108,7 @@ XGUI_Workshop* PartSet_Module::workshop() const void PartSet_Module::createFeatures() { - //!! Test registering of validators + //Registering of validators PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator); diff --git a/src/PartSet/PartSet_OperationFeatureCreate.cpp b/src/PartSet/PartSet_OperationFeatureCreate.cpp index 0b390fe76..816153a00 100644 --- a/src/PartSet/PartSet_OperationFeatureCreate.cpp +++ b/src/PartSet/PartSet_OperationFeatureCreate.cpp @@ -67,7 +67,9 @@ bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId) bool PartSet_OperationFeatureCreate::canBeCommitted() const { - return !myActiveWidget; + if (PartSet_OperationSketchBase::canBeCommitted()) + return !myActiveWidget; + return false; } bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const @@ -104,11 +106,9 @@ void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle const std::list& theSelected, const std::list& /*theHighlighted*/) { - if (canBeCommitted()) - { + if (commit()) { // if the point creation is finished, the next mouse release should commit the modification // the next release can happens by double click in the viewer - commit(); restartOperation(feature()->getKind(), feature()); return; } @@ -161,8 +161,7 @@ void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView) { - if (canBeCommitted()) { - commit(); + if (commit()) { restartOperation(feature()->getKind(), feature()); } else { @@ -210,21 +209,14 @@ void PartSet_OperationFeatureCreate::keyReleased(const int theKey) { switch (theKey) { case Qt::Key_Return: { - if (canBeCommitted()) - { - commit(); + if (commit()) { // it start a new line creation at a free point restartOperation(feature()->getKind(), FeaturePtr()); } } break; case Qt::Key_Escape: { - if (canBeCommitted()) - { - commit(); - } - else - { + if (!commit()) { abort(); } } diff --git a/src/PartSet/PartSet_OperationFeatureCreate.h b/src/PartSet/PartSet_OperationFeatureCreate.h index b0d991887..5ba2e7428 100644 --- a/src/PartSet/PartSet_OperationFeatureCreate.h +++ b/src/PartSet/PartSet_OperationFeatureCreate.h @@ -40,10 +40,6 @@ public: /// Destructor virtual ~PartSet_OperationFeatureCreate(); - /// Verifies whether this operator can be commited. - /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled - virtual bool canBeCommitted() const; - /// Returns that this operator can be started above already running one. /// The runned operation should be the sketch feature modified operation /// \param theOperation the previous running operation @@ -114,6 +110,10 @@ protected: /// \returns the created feature virtual FeaturePtr createFeature(const bool theFlushMessage = true); + /// Verifies whether this operator can be commited. + /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled + virtual bool canBeCommitted() const; + protected: /// Set value to the active widget /// \param theFeature the feature diff --git a/src/PartSet/PartSet_OperationFeatureEdit.cpp b/src/PartSet/PartSet_OperationFeatureEdit.cpp index 1a6fb55aa..32f0a86e2 100644 --- a/src/PartSet/PartSet_OperationFeatureEdit.cpp +++ b/src/PartSet/PartSet_OperationFeatureEdit.cpp @@ -82,25 +82,26 @@ void PartSet_OperationFeatureEdit::mousePressed(QMouseEvent* theEvent, Handle(V3 FeaturePtr aFeature = ModuleBase_Tools::feature(aObject); if (!aFeature || aFeature != feature()) { - commit(); - emit featureConstructed(feature(), FM_Deactivation); - - bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier); - if(aHasShift && !theHighlighted.empty()) { - QList aSelected; - std::list::const_iterator aIt; - for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt) - aSelected.append((*aIt).object()); - /*for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) { - if (!aSelected.contains((*aIt).object())) + if (commit()) { + emit featureConstructed(feature(), FM_Deactivation); + + bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier); + if(aHasShift && !theHighlighted.empty()) { + QList aSelected; + std::list::const_iterator aIt; + for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt) aSelected.append((*aIt).object()); - }*/ - //aSelected.push_back(feature()); - //aSelected.push_back(theHighlighted.front().object()); - emit setSelection(aSelected); - } - else if (aFeature) { - restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature); + /*for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) { + if (!aSelected.contains((*aIt).object())) + aSelected.append((*aIt).object()); + }*/ + //aSelected.push_back(feature()); + //aSelected.push_back(theHighlighted.front().object()); + emit setSelection(aSelected); + } + else if (aFeature) { + restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature); + } } } } diff --git a/src/PartSet/PartSet_OperationFeatureEditMulti.cpp b/src/PartSet/PartSet_OperationFeatureEditMulti.cpp index 1768f0c94..30796c5fb 100644 --- a/src/PartSet/PartSet_OperationFeatureEditMulti.cpp +++ b/src/PartSet/PartSet_OperationFeatureEditMulti.cpp @@ -131,14 +131,15 @@ void PartSet_OperationFeatureEditMulti::mouseReleased(QMouseEvent* theEvent, Han const std::list& /*theSelected*/, const std::list& /*theHighlighted*/) { - std::list aFeatures = myFeatures; - commit(); - std::list::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end(); - for (; anIt != aLast; anIt++) { - ObjectPtr aFeature = (*anIt).object(); - if (aFeature) { - emit featureConstructed(aFeature, FM_Deactivation); - } + if (commit()) { + std::list aFeatures = myFeatures; + std::list::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end(); + for (; anIt != aLast; anIt++) { + ObjectPtr aFeature = (*anIt).object(); + if (aFeature) { + emit featureConstructed(aFeature, FM_Deactivation); + } + } } } diff --git a/src/PartSet/PartSet_Validators.cpp b/src/PartSet/PartSet_Validators.cpp index 1d6250afe..7a3d43fe7 100644 --- a/src/PartSet/PartSet_Validators.cpp +++ b/src/PartSet/PartSet_Validators.cpp @@ -99,3 +99,4 @@ bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) } return (aCount > 0) && (aCount < 2); } + diff --git a/src/PartSet/PartSet_Validators.h b/src/PartSet/PartSet_Validators.h index 90487fe4f..94bab7756 100644 --- a/src/PartSet/PartSet_Validators.h +++ b/src/PartSet/PartSet_Validators.h @@ -10,6 +10,10 @@ #include #include +/* +* Selector validators +*/ + //! A class to validate a selection for Distance constraint operation class PartSet_DistanceValidator: public ModuleBase_SelectionValidator { @@ -45,4 +49,5 @@ public: PARTSET_EXPORT virtual bool isValid(const ModuleBase_ISelection* theSelection) const; }; + #endif diff --git a/src/SketchPlugin/CMakeLists.txt b/src/SketchPlugin/CMakeLists.txt index 0b2870fa8..c731f49ea 100644 --- a/src/SketchPlugin/CMakeLists.txt +++ b/src/SketchPlugin/CMakeLists.txt @@ -17,6 +17,7 @@ SET(PROJECT_HEADERS SketchPlugin_ConstraintParallel.h SketchPlugin_ConstraintPerpendicular.h SketchPlugin_ConstraintRadius.h + SketchPlugin_Validators.h ) SET(PROJECT_SOURCES @@ -34,6 +35,7 @@ SET(PROJECT_SOURCES SketchPlugin_ConstraintParallel.cpp SketchPlugin_ConstraintPerpendicular.cpp SketchPlugin_ConstraintRadius.cpp + SketchPlugin_Validators.cpp ) SET(PROJECT_LIBRARIES diff --git a/src/SketchPlugin/SketchPlugin_Plugin.cpp b/src/SketchPlugin/SketchPlugin_Plugin.cpp index 9c0da3f59..687e2cd94 100644 --- a/src/SketchPlugin/SketchPlugin_Plugin.cpp +++ b/src/SketchPlugin/SketchPlugin_Plugin.cpp @@ -10,8 +10,10 @@ #include "SketchPlugin_ConstraintParallel.h" #include "SketchPlugin_ConstraintPerpendicular.h" #include "SketchPlugin_ConstraintRadius.h" +#include "SketchPlugin_Validators.h" #include #include +#include using namespace std; @@ -20,6 +22,10 @@ static SketchPlugin_Plugin* MY_INSTANCE = new SketchPlugin_Plugin(); SketchPlugin_Plugin::SketchPlugin_Plugin() { + PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); + ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + aFactory->registerValidator("SketchPlugin_DistanceFeatureValidator", new SketchPlugin_DistanceFeatureValidator); + // register this plugin ModelAPI_PluginManager::get()->registerPlugin(this); } diff --git a/src/SketchPlugin/SketchPlugin_Validators.cpp b/src/SketchPlugin/SketchPlugin_Validators.cpp new file mode 100644 index 000000000..7f44ef5b1 --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_Validators.cpp @@ -0,0 +1,56 @@ +// File: SketchPlugin_Validators.cpp +// Created: 01 Aug 2014 +// Author: Vitaly SMETANNIKOV + +#include "SketchPlugin_Validators.h" +#include "SketchPlugin_Constraint.h" +#include "SketchPlugin_Sketch.h" +#include "SketchPlugin_Point.h" +#include "SketchPlugin_Line.h" +#include "SketchPlugin_Circle.h" +#include "SketchPlugin_Arc.h" +#include + + +bool isValidType(const std::string& theType) +{ + return (theType == SketchPlugin_Point::ID()) || + (theType == SketchPlugin_Circle::ID()) || + (theType == SketchPlugin_Arc::ID()); +} + +bool SketchPlugin_DistanceFeatureValidator::isValid(const FeaturePtr theFeature) const +{ + if (!theFeature) + return false; + if (!theFeature->data() || !theFeature->data()->isValid()) + return false; + boost::shared_ptr aData = theFeature->data(); + + boost::shared_ptr aRefA = + boost::dynamic_pointer_cast( + aData->attribute(SketchPlugin_Constraint::ENTITY_A())); + + boost::shared_ptr aRefB = + boost::dynamic_pointer_cast( + aData->attribute(SketchPlugin_Constraint::ENTITY_B())); + + if (!aRefA || !aRefB) + return false; + + FeaturePtr aFetureA = SketchPlugin_Sketch::getFeature(aRefA->object()); + FeaturePtr aFetureB = SketchPlugin_Sketch::getFeature(aRefB->object()); + if (!aFetureA || !aFetureB) + return false; + + std::string aTypeA = aFetureA->getKind(); + std::string aTypeB = aFetureB->getKind(); + + if (aTypeA == SketchPlugin_Line::ID()) { + return isValidType(aTypeB); + } else if (aTypeB == SketchPlugin_Line::ID()) { + return isValidType(aTypeA); + } else + return isValidType(aTypeA) && isValidType(aTypeB); + return false; +} diff --git a/src/SketchPlugin/SketchPlugin_Validators.h b/src/SketchPlugin/SketchPlugin_Validators.h new file mode 100644 index 000000000..2941485cf --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_Validators.h @@ -0,0 +1,19 @@ +// File: SketchPlugin_Validators.h +// Created: 01 Aug 2014 +// Author: Vitaly SMETANNIKOV + +#ifndef SketchPlugin_Validators_H +#define SketchPlugin_Validators_H + +#include "SketchPlugin.h" +#include + + +//! A class to validate a selection for Perpendicular constraint operation +class SketchPlugin_DistanceFeatureValidator: public ModuleBase_FeatureValidator +{ +public: + SKETCHPLUGIN_EXPORT virtual bool isValid(const FeaturePtr theFeature) const; +}; + +#endif \ No newline at end of file diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index ec1425964..96ccaefce 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -28,15 +28,16 @@ diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index a8d23dd52..05910dd95 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -282,8 +282,8 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage) if (myOperationMgr->startOperation(anOperation)) { myPropertyPanel->updateContentWidget(anOperation->feature()); if (!anOperation->getDescription()->hasXmlRepresentation()) { - anOperation->commit(); - updateCommandStatus(); + if (anOperation->commit()) + updateCommandStatus(); } } return; -- 2.39.2