Salome HOME
No automatic rebuild: only on preview. Update of visualization behavior due to the...
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
index 4a93b9d47c770272bb5eb4a3c35c3e06e9369460..58260b79c6cd24bce86d4a53a138de985531c0a4 100644 (file)
@@ -38,6 +38,39 @@ int XGUI_OperationMgr::operationsCount() const
   return myOperations.count();
 }
 
+QStringList XGUI_OperationMgr::operationList() const
+{
+  QStringList result;
+  foreach(ModuleBase_Operation* eachOperation, myOperations) {
+    FeaturePtr aFeature = eachOperation->feature();
+    if(aFeature) {
+      result << QString::fromStdString(aFeature->getKind());
+    }
+  }
+  return result;
+}
+
+ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
+{
+  int idx = myOperations.lastIndexOf(theOperation);
+  if(idx == -1 || idx == 0) {
+    return NULL;
+  }
+  return myOperations.at(idx - 1);
+}
+
+bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
+{
+  if (theEvent->type() == QEvent::KeyRelease) {
+    QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
+    if(aKeyEvent) {
+      onKeyReleased(aKeyEvent);
+      return true;
+    }
+  }
+  return QObject::eventFilter(theObject, theEvent);
+}
+
 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
 {
   if (!canStartOperation(theOperation))
@@ -52,57 +85,46 @@ bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
           SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
 
   theOperation->start();
-  validateCurrentOperation();
-  return true;
-}
-
-bool XGUI_OperationMgr::abortOperation()
-{
-  ModuleBase_Operation* aCurrentOp = currentOperation();
-  if (!aCurrentOp || !canStopOperation())
-    return false;
-
-  aCurrentOp->abort();
+  onValidateOperation();
   return true;
 }
 
-QStringList XGUI_OperationMgr::operationList()
+bool XGUI_OperationMgr::abortAllOperations()
 {
-  QStringList result;
-  foreach(ModuleBase_Operation* eachOperation, myOperations)
-  {
-    result << eachOperation->id();
+  if(!hasOperation()) {
+    return true;
+  } else if (operationsCount() == 1) {
+    onAbortOperation();
+    return true;
+  }
+  QString aMessage = tr("All active operations will be aborted.");
+  int anAnswer = QMessageBox::question(qApp->activeWindow(),
+                                       tr("Abort operation"),
+                                       aMessage,
+                                       QMessageBox::Ok | QMessageBox::Cancel,
+                                       QMessageBox::Cancel);
+  bool result = anAnswer == QMessageBox::Ok;
+  while(result && hasOperation()) {
+    currentOperation()->abort();
   }
   return result;
 }
 
-void XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
+bool XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
 {
-  //Get operation Id and feature to validate
-  QString anOperationId = theOperation->id();
+  //Get operation feature to validate
   FeaturePtr aFeature = theOperation->feature();
+  if (!aFeature) return true; // rename operation
   //Get validators for the Id
-  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+  SessionPtr aMgr = ModelAPI_Session::get();
   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-  std::list<ModelAPI_Validator*> aValidators;
-  aFactory->validators(anOperationId.toStdString(), aValidators);
-  //
-  std::list<ModelAPI_Validator*>::iterator it = aValidators.begin();
-  bool isValid = true;
-  for (; it != aValidators.end(); it++) {
-    const ModelAPI_FeatureValidator* aFeatureValidator =
-        dynamic_cast<const ModelAPI_FeatureValidator*>(*it);
-    if (!aFeatureValidator)
-      continue;
-    if (!aFeatureValidator->isValid(aFeature)) {
-      isValid = false;
-      break;
-    }
-  }
+
+  bool isValid = aFactory->validate(aFeature);
   emit operationValidated(isValid);
+  return isValid;
 }
 
-void XGUI_OperationMgr::validateCurrentOperation()
+void XGUI_OperationMgr::onValidateOperation()
 {
   if (!hasOperation())
     return;
@@ -110,18 +132,13 @@ void XGUI_OperationMgr::validateCurrentOperation()
   validateOperation(currentOperation());
 }
 
-bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
+bool XGUI_OperationMgr::commitOperation()
 {
-  if (theEvent->type() == QEvent::KeyRelease) {
-    QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
-    if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
-      // TODO: this is Escape button processing when the property panel has empty content,
-      // but the operation should be stopped by the Enter has been clicked
-      onKeyReleased("", aKeyEvent);
-      return true;
-    }
+  if (validateOperation(currentOperation())) {
+    onCommitOperation();
+    return true;
   }
-  return QObject::eventFilter(theObject, theEvent);
+  return false;
 }
 
 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
@@ -133,30 +150,20 @@ bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
 {
   bool aCanStart = true;
   ModuleBase_Operation* aCurrentOp = currentOperation();
-  if (aCurrentOp && !theOperation->isGranted(aCurrentOp)) {
-    if (canStopOperation()) {
-      aCurrentOp->abort();
-    } else {
-      aCanStart = false;
+  if (aCurrentOp) {
+    if (!theOperation->isGranted()) {
+      if (!aCurrentOp->isValid(theOperation)) {
+        if (canAbortOperation()) {
+          aCurrentOp->abort();
+        } else {
+          aCanStart = false;
+        }
+      }
     }
   }
   return aCanStart;
 }
 
-bool XGUI_OperationMgr::canStopOperation()
-{
-  ModuleBase_Operation* anOperation = currentOperation();
-  if (anOperation) {
-    if (anOperation->isModified()) {
-      int anAnswer = QMessageBox::question(
-          qApp->activeWindow(), tr("Operation launch"),
-          tr("Previous operation is not finished and will be aborted"), QMessageBox::Ok,
-          QMessageBox::Cancel);
-      return anAnswer == QMessageBox::Ok;
-    }
-  }
-  return true;
-}
 
 void XGUI_OperationMgr::onCommitOperation()
 {
@@ -167,20 +174,24 @@ void XGUI_OperationMgr::onCommitOperation()
 
 void XGUI_OperationMgr::onAbortOperation()
 {
-  ModuleBase_Operation* anOperation = currentOperation();
-  if (anOperation && canAbortOperation())
-    anOperation->abort();
+  if (hasOperation() && canAbortOperation()) {
+    currentOperation()->abort();
+  }
 }
 
 bool XGUI_OperationMgr::canAbortOperation()
 {
   ModuleBase_Operation* anOperation = currentOperation();
+  if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
+    return true;
   if (anOperation && anOperation->isModified()) {
-    int anAnswer = QMessageBox::question(
-        qApp->activeWindow(), tr("Cancel operation"),
-        tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()), QMessageBox::Yes,
-        QMessageBox::No);
-    return anAnswer == QMessageBox::Yes;
+    QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
+    int anAnswer = QMessageBox::question(qApp->activeWindow(),
+                                         tr("Abort operation"),
+                                         aMessage,
+                                         QMessageBox::Ok | QMessageBox::Cancel,
+                                         QMessageBox::Cancel);
+    return anAnswer == QMessageBox::Ok;
   }
   return true;
 }
@@ -210,15 +221,33 @@ void XGUI_OperationMgr::onOperationStopped()
   }
   if (aResultOp) {
     resumeOperation(aResultOp);
-    validateCurrentOperation();
+    onValidateOperation();
   }
 }
 
-void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
+void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
 {
+  // Let the manager decide what to do with the given key combination.
   ModuleBase_Operation* anOperation = currentOperation();
-  if (anOperation)
-    anOperation->keyReleased(theName, theEvent);
+  bool isRestart = false;
+  switch (theEvent->key()) {
+    case Qt::Key_Escape: {
+      onAbortOperation();
+    }
+      break;
+    case Qt::Key_Return:
+    case Qt::Key_Enter: {
+      if(anOperation) {
+         anOperation->activateNextToCurrentWidget();
+      }
+      commitOperation();
+    }
+      break;
+    default:
+      break;
+  }
+  if(anOperation)
+    anOperation->keyReleased(theEvent->key());
 }
 
 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)