Salome HOME
Avoid redisplay of sketcher while it is not finished
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
index bca019e31da01b8eea4ad02e56cd8d7a0e266416..8a3b36e12ca8755668b625de5c2f3df25eb958ca 100644 (file)
@@ -1,12 +1,20 @@
+// File:        XGUI_OperationMgr.h
+// Created:     20 Apr 2014
+// Author:      Natalia ERMOLAEVA
+
 #include "XGUI_OperationMgr.h"
 
 #include "ModuleBase_Operation.h"
 
 #include <QMessageBox>
+#include <QApplication>
+#include <QKeyEvent>
 
 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
 : QObject(theParent)
 {
+  // listen to Escape signal to stop the current operation
+  qApp->installEventFilter(this);
 }
 
 XGUI_OperationMgr::~XGUI_OperationMgr()
@@ -18,6 +26,16 @@ ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
   return myOperations.count() > 0 ? myOperations.last() : 0;
 }
 
+bool XGUI_OperationMgr::hasOperation() const
+{
+  return (myOperations.count() > 0) && (myOperations.last() != NULL);
+}
+
+int XGUI_OperationMgr::operationsCount() const
+{
+  return myOperations.count();
+}
+
 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
 {
   if (!canStartOperation(theOperation))
@@ -27,35 +45,105 @@ bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
 
   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
+  connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
+  connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
+          this, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
 
   theOperation->start();
   return true;
 }
 
+bool XGUI_OperationMgr::abortOperation()
+{
+  ModuleBase_Operation* aCurrentOp = currentOperation();
+  if (!aCurrentOp || !canStopOperation())
+    return false; 
+
+  aCurrentOp->abort();
+  return true;
+}
+
+QStringList XGUI_OperationMgr::operationList()
+{
+  QStringList result;
+  foreach(ModuleBase_Operation* eachOperation, myOperations) {
+    result << eachOperation->id();
+  }
+  return result;
+}
+
+bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
+{
+  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;
+    }
+  }
+  return QObject::eventFilter(theObject, theEvent);
+}
+
+void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
+{
+  theOperation->resume();
+}
+
 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
 {
   bool aCanStart = true;
   ModuleBase_Operation* aCurrentOp = currentOperation();
-  if (aCurrentOp && !theOperation->isGranted())
+  if (aCurrentOp && !theOperation->isGranted(aCurrentOp))
   {
-    int anAnswer = QMessageBox::question(0, tr("Operation launch"),
-                                tr("Previous operation is not finished and will be aborted"),
-                                QMessageBox::Ok, QMessageBox::Cancel);
-    if (anAnswer == QMessageBox::Ok)
+    if (canStopOperation()) {
       aCurrentOp->abort();
-    else
+    } else {
       aCanStart = false;
+    }
   }
   return aCanStart;
 }
 
-void XGUI_OperationMgr::commitCurrentOperation()
+bool XGUI_OperationMgr::canStopOperation()
 {
   ModuleBase_Operation* anOperation = currentOperation();
-  if (!anOperation)
-    return;
+  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()
+{
+  ModuleBase_Operation* anOperation = currentOperation();
+  if (anOperation)
+    anOperation->commit();
+}
 
-  anOperation->commit();
+void XGUI_OperationMgr::onAbortOperation()
+{
+  ModuleBase_Operation* anOperation = currentOperation();
+  if (anOperation && canAbortOperation())
+    anOperation->abort();
+}
+
+bool XGUI_OperationMgr::canAbortOperation()
+{
+  ModuleBase_Operation* anOperation = currentOperation();
+  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;
+  }
+  return true;
 }
 
 void XGUI_OperationMgr::onOperationStopped()
@@ -65,11 +153,11 @@ void XGUI_OperationMgr::onOperationStopped()
   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
     return;
 
-  emit operationStopped(anOperation);
-
   myOperations.removeAll(anOperation);
   anOperation->deleteLater();
 
+  emit operationStopped(anOperation);
+
   // get last operation which can be resumed
   ModuleBase_Operation* aResultOp = 0;
   QListIterator<ModuleBase_Operation*> anIt(myOperations);
@@ -83,5 +171,19 @@ void XGUI_OperationMgr::onOperationStopped()
     }
   }
   if (aResultOp)
-    startOperation(aResultOp);
+    resumeOperation(aResultOp);
+}
+
+void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
+{
+  ModuleBase_Operation* anOperation = currentOperation();
+  if (anOperation)
+    anOperation->keyReleased(theName, theEvent);
+}
+
+void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
+{
+  ModuleBase_Operation* anOperation = currentOperation();
+  if (anOperation)
+    anOperation->onWidgetActivated(theWidget);
 }