#include "XGUI_OperationMgr.h"
#include "ModuleBase_Operation.h"
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_FeatureValidator.h>
#include <QMessageBox>
+#include <QApplication>
+#include <QKeyEvent>
XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
-: QObject(theParent)
+ : QObject(theParent)
{
+ // listen to Escape signal to stop the current operation
+ qApp->installEventFilter(this);
}
XGUI_OperationMgr::~XGUI_OperationMgr()
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))
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();
+ onValidateOperation();
return true;
}
-bool XGUI_OperationMgr::abortOperation()
+bool XGUI_OperationMgr::abortAllOperations()
{
- ModuleBase_Operation* aCurrentOp = currentOperation();
- if (!aCurrentOp || !canStopOperation())
- return false;
+ 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;
+}
- aCurrentOp->abort();
- return true;
+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::resumeOperation(ModuleBase_Operation* theOperation)
+void XGUI_OperationMgr::onValidateOperation()
{
- connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
- connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
+ 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 aCanStart = true;
ModuleBase_Operation* aCurrentOp = currentOperation();
- if (aCurrentOp && !theOperation->isGranted())
- {
- 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()
-{
- int anAnswer = QMessageBox::question(0, tr("Operation launch"),
- tr("Previous operation is not finished and will be aborted"),
- QMessageBox::Ok, QMessageBox::Cancel);
- return anAnswer == QMessageBox::Ok;
-}
void XGUI_OperationMgr::onCommitOperation()
{
}
void XGUI_OperationMgr::onAbortOperation()
+{
+ if (hasOperation() && canAbortOperation()) {
+ currentOperation()->abort();
+ }
+}
+
+bool XGUI_OperationMgr::canAbortOperation()
{
ModuleBase_Operation* anOperation = currentOperation();
- if (anOperation)
- anOperation->abort();
+ 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;
- 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);
anIt.toBack();
- while(anIt.hasPrevious())
- {
+ while (anIt.hasPrevious()) {
ModuleBase_Operation* anOp = anIt.previous();
if (anOp) {
aResultOp = anOp;
break;
}
}
- if (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);
}