+// File: XGUI_OperationMgr.h
+// Created: 20 Apr 2014
+// Author: Natalia ERMOLAEVA
+
#include "XGUI_OperationMgr.h"
#include "ModuleBase_Operation.h"
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_FeatureValidator.h>
#include <QMessageBox>
+#include <QApplication>
+#include <QKeyEvent>
-/*!
- \brief Constructor
- */
XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
-: QObject(theParent)
+ : QObject(theParent)
{
+ // listen to Escape signal to stop the current operation
+ qApp->installEventFilter(this);
}
-/*!
- \brief Destructor
- */
XGUI_OperationMgr::~XGUI_OperationMgr()
{
}
-/*!
- \brief Returns the current operation or NULL
- * \return the current operation
- */
ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
{
return myOperations.count() > 0 ? myOperations.last() : 0;
}
-/*!
- \brief Sets the current operation or NULL
- * \return the current operation
- */
+bool XGUI_OperationMgr::hasOperation() const
+{
+ return (myOperations.count() > 0) && (myOperations.last() != NULL);
+}
+
+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))
return false;
myOperations.append(theOperation);
- emit beforeOperationStart();
connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
- theOperation->start();
+ 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*)));
- emit afterOperationStart();
+ theOperation->start();
+ onValidateOperation();
return true;
}
+bool XGUI_OperationMgr::abortAllOperations()
+{
+ 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;
+}
+
+bool XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
+{
+ //Get operation feature to validate
+ FeaturePtr aFeature = theOperation->feature();
+ if (!aFeature) return true; // rename operation
+ //Get validators for the Id
+ SessionPtr aMgr = ModelAPI_Session::get();
+ ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+
+ bool isValid = aFactory->validate(aFeature);
+ emit operationValidated(isValid);
+ return isValid;
+}
+
+void XGUI_OperationMgr::onValidateOperation()
+{
+ if (!hasOperation())
+ return;
+ ModuleBase_Operation* anOperation = currentOperation();
+ validateOperation(currentOperation());
+}
+
+bool XGUI_OperationMgr::commitOperation()
+{
+ if (validateOperation(currentOperation())) {
+ onCommitOperation();
+ return true;
+ }
+ return false;
+}
+
+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())
- {
- 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)
- aCurrentOp->abort();
- else
- aCanStart = false;
+ if (aCurrentOp) {
+ if (!theOperation->isGranted()) {
+ if (!aCurrentOp->isValid(theOperation)) {
+ if (canAbortOperation()) {
+ aCurrentOp->abort();
+ } else {
+ aCanStart = false;
+ }
+ }
+ }
}
return aCanStart;
}
-void XGUI_OperationMgr::commitCurrentOperation()
+
+void XGUI_OperationMgr::onCommitOperation()
{
ModuleBase_Operation* anOperation = currentOperation();
- if (!anOperation)
- return;
+ if (anOperation)
+ anOperation->commit();
+}
- anOperation->commit();
+void XGUI_OperationMgr::onAbortOperation()
+{
+ 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()) {
+ 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;
}
void XGUI_OperationMgr::onOperationStopped()
{
ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
ModuleBase_Operation* anOperation = currentOperation();
- if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
+ if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
return;
myOperations.removeAll(anOperation);
+ anOperation->deleteLater();
+
+ emit operationStopped(anOperation);
// get last operation which can be resumed
ModuleBase_Operation* aResultOp = 0;
QListIterator<ModuleBase_Operation*> anIt(myOperations);
anIt.toBack();
- while(anIt.hasPrevious())
- {
+ while (anIt.hasPrevious()) {
ModuleBase_Operation* anOp = anIt.previous();
if (anOp) {
aResultOp = anOp;
break;
}
}
- if (aResultOp)
- startOperation(aResultOp);
+ if (aResultOp) {
+ resumeOperation(aResultOp);
+ onValidateOperation();
+ }
+}
+
+void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
+{
+ // Let the manager decide what to do with the given key combination.
+ ModuleBase_Operation* anOperation = currentOperation();
+ 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)
+{
+ ModuleBase_Operation* anOperation = currentOperation();
+ if (anOperation)
+ anOperation->onWidgetActivated(theWidget);
}