]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Feature validator created
authorvsv <vitaly.smetannikov@opencascade.com>
Mon, 4 Aug 2014 11:05:09 +0000 (15:05 +0400)
committervsv <vitaly.smetannikov@opencascade.com>
Mon, 4 Aug 2014 11:05:09 +0000 (15:05 +0400)
19 files changed:
src/ModuleBase/CMakeLists.txt
src/ModuleBase/ModuleBase_FeatureValidator.h [new file with mode: 0644]
src/ModuleBase/ModuleBase_IOperation.cpp
src/ModuleBase/ModuleBase_IOperation.h
src/ModuleBase/ModuleBase_Operation.cpp
src/ModuleBase/ModuleBase_Operation.h
src/PartSet/PartSet_Module.cpp
src/PartSet/PartSet_OperationFeatureCreate.cpp
src/PartSet/PartSet_OperationFeatureCreate.h
src/PartSet/PartSet_OperationFeatureEdit.cpp
src/PartSet/PartSet_OperationFeatureEditMulti.cpp
src/PartSet/PartSet_Validators.cpp
src/PartSet/PartSet_Validators.h
src/SketchPlugin/CMakeLists.txt
src/SketchPlugin/SketchPlugin_Plugin.cpp
src/SketchPlugin/SketchPlugin_Validators.cpp [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_Validators.h [new file with mode: 0644]
src/SketchPlugin/plugin-Sketch.xml
src/XGUI/XGUI_Workshop.cpp

index 1d8531d4d7b53455d9f8c9120a74c98dbf3717bb..4c3f20caa061d34b52ef2da5d2f053d3561eeab4 100644 (file)
@@ -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 (file)
index 0000000..ccbcca6
--- /dev/null
@@ -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 <ModelAPI_Feature.h>
+#include <ModelAPI_Validator.h>
+
+
+class ModuleBase_FeatureValidator: public ModelAPI_Validator
+{
+public:
+  virtual bool isValid(const FeaturePtr theFeature) const = 0;
+};
+
+#endif
index cc398e5b03df0b227fb47b3b153740eb501fe097..cee8fbc2b1d531cc41fa5c3bab5d656d7f1481d2 100644 (file)
@@ -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)
index 0d304c0a2a7bebc1d684914ef6bba0b4bf48dc2b..c9510192f683b1e6cbfd719259ca28f6b0418669 100644 (file)
@@ -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<ModelAPI_Document> document() const;
 
index f3a657548a6fcf25bf613f9cb9b4662d02f2074b..ba44d45817af3591dde5e221d6893195b15b0e5d 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "ModuleBase_OperationDescription.h"
 #include "ModuleBase_ModelWidget.h"
+#include "ModuleBase_FeatureValidator.h"
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_Document.h>
@@ -17,6 +18,7 @@
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Result.h>
+#include <ModelAPI_Validator.h>
 
 #include <Events_Loop.h>
 
@@ -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<const ModuleBase_FeatureValidator*>(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));
index ae2250624db9ad0c7fa94040b767abf69f65bfe8..c9693d3f60f120414451d29994dece2f69df4932 100644 (file)
@@ -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
 };
index 611d2a5fd41bb913259a5f8319073d4d0eacf0c6..160f63f2e91fd12c40f3f45dcf8dd07492b03eb3 100644 (file)
@@ -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);
index 0b390fe761550717492b4a7ddab488e9f220f177..816153a00a7c58d06e7474913222d7a355bdca1d 100644 (file)
@@ -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<ModuleBase_ViewerPrs>& theSelected,
                                                 const std::list<ModuleBase_ViewerPrs>& /*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();
       }
     }
index b0d9918875df89341a0c951229e6f666fa0d6fd9..5ba2e7428ee1d632159471e0a30db14a5d29a2f2 100644 (file)
@@ -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
index 1a6fb55aaed07271807856e7702683e7aa41fa3b..32f0a86e2f2de97dd6fc01cbb00c7a1e8ee3657d 100644 (file)
@@ -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<ObjectPtr> aSelected;
-      std::list<ModuleBase_ViewerPrs>::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<ObjectPtr> aSelected;
+        std::list<ModuleBase_ViewerPrs>::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);
+      }
     }
   }
 }
index 1768f0c94d82706699c8443b917d050919f0b793..30796c5fbc75228ce98eba176ad58da85e03a3d1 100644 (file)
@@ -131,14 +131,15 @@ void PartSet_OperationFeatureEditMulti::mouseReleased(QMouseEvent* theEvent, Han
                                               const std::list<ModuleBase_ViewerPrs>& /*theSelected*/,
                                               const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
 {
-  std::list<ModuleBase_ViewerPrs> aFeatures = myFeatures;
-  commit();
-  std::list<ModuleBase_ViewerPrs>::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<ModuleBase_ViewerPrs> aFeatures = myFeatures;
+    std::list<ModuleBase_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
+    for (; anIt != aLast; anIt++) {
+      ObjectPtr aFeature = (*anIt).object();
+      if (aFeature) {
+        emit featureConstructed(aFeature, FM_Deactivation);
+           }
+    }
   }
 }
 
index 1d6250afe4c3245bffd2b1be651f7af52b089a3b..7a3d43fe7f17274dfd84289076a41a4f10802552 100644 (file)
@@ -99,3 +99,4 @@ bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection)
   }
   return (aCount > 0) && (aCount < 2);
 }
+
index 90487fe4f722de683f0cdee5caab1d4b69e4df83..94bab7756d5a7dc1b50a99dbb3fff9fbbde95a36 100644 (file)
 #include <ModuleBase_SelectionValidator.h>
 #include <ModuleBase_ISelection.h>
 
+/*
+* 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
index 0b2870fa8530ac4afca51b5e5f4842ee49d1eb7b..c731f49ea53f801f43036abb251fe724fe5b8f33 100644 (file)
@@ -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
index 9c0da3f592d8e5b530c74bf82dc45f1acdd841c1..687e2cd94d0f8055ddaa4ed18a31a0d0ee90c281 100644 (file)
 #include "SketchPlugin_ConstraintParallel.h"
 #include "SketchPlugin_ConstraintPerpendicular.h"
 #include "SketchPlugin_ConstraintRadius.h"
+#include "SketchPlugin_Validators.h"
 #include <ModelAPI_PluginManager.h>
 #include <ModelAPI_Document.h>
+#include <ModelAPI_Validator.h>
 
 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 (file)
index 0000000..7f44ef5
--- /dev/null
@@ -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 <ModelAPI_Data.h>
+
+
+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<ModelAPI_Data> aData = theFeature->data();
+
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefA =
+          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+          aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefB =
+          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+          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 (file)
index 0000000..2941485
--- /dev/null
@@ -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 <ModuleBase_FeatureValidator.h>
+
+
+//! 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
index ec1425964287f379225d733d4f3bf5ae5582431a..96ccaefce297951e05f809ba48798859bdb558f6 100644 (file)
 
       <feature id="SketchConstraintDistance" title="Distance" tooltip="Create constraint for the distance from a point to an object">
         <label title="Select point and another feature (point or point on line) between which to calculate distance" tooltip="Select point and another feature (point or point on line) between which to calculate distance"/>
-        <feature_or_attribute_selector id="ConstraintEntityA" label="First point" tooltip="Select an point in the viewer">
+        <feature_or_attribute_selector id="ConstraintEntityA" label="First object" tooltip="Select an point in the viewer">
           <validator id="ModuleBase_ResultPointValidator"/>
         </feature_or_attribute_selector>
-        <feature_or_attribute_selector id="ConstraintEntityB" label="Last point" tooltip="Select an point in the viewer">
+        <feature_or_attribute_selector id="ConstraintEntityB" label="Last object" tooltip="Select an point in the viewer">
           <validator id="ModuleBase_ResultPointValidator"/>
         </feature_or_attribute_selector>
         <point_selector id="ConstraintFlyoutValuePnt" internal="1"/>
         <doublevalue_editor label="Value" tooltip="Constraint value" id="ConstraintValue"/>
-        <validator id="PartSet_DistanceValidator"/> 
+        <validator id="PartSet_DistanceValidator"/>
+        <validator id="SketchPlugin_DistanceFeatureValidator"/> 
       </feature>
       
       <feature id="SketchConstraintLength" title="Length" tooltip="Create constraint for the given length of a line segment">
index a8d23dd5212af78cc510beffafac2f6a03ed8269..05910dd9573712ed3e9e89fa153a0ba7dcf37907 100644 (file)
@@ -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;