Salome HOME
1. CanStopOperation knows which operation is stopped. Scenario: Unpress Sketch in...
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
index 315be91780a0db0180dafdbfb6879b70d77ce351..7de4b0ee5e0e5912510d5d0aae8bd9e452728fe9 100644 (file)
@@ -7,6 +7,7 @@
 #include "ModuleBase_Operation.h"
 #include "ModuleBase_ISelection.h"
 #include "ModuleBase_OperationDescription.h"
+#include "ModuleBase_OperationFeature.h"
 
 #include <Events_Loop.h>
 
@@ -38,17 +39,19 @@ ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
 }
 
-
 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
 {
   if (!myWorkshop->canStartOperation(theCmdId))
     return;
 
-  ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
-  ModuleBase_ISelection* aSelection = myWorkshop->selection();
-  // Initialise operation with preliminary selection
-  anOperation->initSelection(aSelection, myWorkshop->viewer());
-  sendOperation(anOperation);
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                             (createOperation(theCmdId.toStdString()));
+  if (aFOperation) {
+    ModuleBase_ISelection* aSelection = myWorkshop->selection();
+    // Initialise operation with preliminary selection
+    aFOperation->initSelection(aSelection, myWorkshop->viewer());
+    sendOperation(aFOperation);
+  }
 }
 
 
@@ -63,22 +66,23 @@ void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
 
 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
 {
-  return new ModuleBase_Operation(theFeatureId.c_str(), this);
+  return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
 }
 
 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
 {
-  ModuleBase_Operation* anOperation = getNewOperation(theFeatureId);
-
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                          (getNewOperation(theFeatureId));
   // If the operation is launched as sub-operation of another then we have to initialise
   // parent feature
-  ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
+  ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                         (myWorkshop->currentOperation());
   if (aCurOperation) {
     FeaturePtr aFeature = aCurOperation->feature();
     CompositeFeaturePtr aCompFeature =
         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
     if (aCompFeature) {
-      anOperation->setParentFeature(aCompFeature);
+      aFOperation->setParentFeature(aCompFeature);
     }
   }
 
@@ -88,10 +92,10 @@ ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& the
   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
 
-  anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
-  anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
+  aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
+  aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
 
-  return anOperation;
+  return aFOperation;
 }
 
 void ModuleBase_IModule::createFeatures()
@@ -113,8 +117,7 @@ void ModuleBase_IModule::actionCreated(QAction* theFeature)
 
 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
 {
-  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
-  return anOperation && anOperation->hasObject(theObject);
+  return true;
 }
 
 bool ModuleBase_IModule::canUndo() const
@@ -129,23 +132,51 @@ bool ModuleBase_IModule::canRedo() const
   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
 }
 
+bool ModuleBase_IModule::canCommitOperation() const
+{
+  return true;
+}
+
 void ModuleBase_IModule::onFeatureTriggered()
 {
   QAction* aCmd = dynamic_cast<QAction*>(sender());
   //Do nothing on uncheck
-  if (aCmd->isCheckable() && !aCmd->isChecked())
-    return;
-  launchOperation(aCmd->data().toString());
+  if (aCmd->isCheckable() && !aCmd->isChecked()) {
+    ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
+    if (myWorkshop->canStopOperation(anOperation))
+      myWorkshop->abortOperation(anOperation);
+    else {
+      aCmd->setChecked(true);
+    }
+  }
+  else {
+    launchOperation(aCmd->data().toString());
+    emit operationLaunched();
+  }
 }
 
-
 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
 {
   std::string aFeatureId = theFeature->getKind();
   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
     return;
 
-  ModuleBase_Operation* anOperation = createOperation(aFeatureId);
-  anOperation->setFeature(theFeature);
-  sendOperation(anOperation);
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                         (createOperation(aFeatureId));
+  if (aFOperation) {
+    aFOperation->setFeature(theFeature);
+    sendOperation(aFOperation);
+  }
+}
+
+bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
+{
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                     (myWorkshop->currentOperation());
+  return !aFOperation || !aFOperation->hasObject(theObject);
+}
+
+void ModuleBase_IModule::onOperationResumed(ModuleBase_Operation* theOperation) 
+{
+  emit operationResumed(theOperation);
 }