1 // File: XGUI_OperationMgr.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "XGUI_OperationMgr.h"
7 #include "ModuleBase_Operation.h"
8 #include <ModelAPI_Validator.h>
9 #include <ModelAPI_FeatureValidator.h>
11 #include <QMessageBox>
12 #include <QApplication>
15 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
18 // listen to Escape signal to stop the current operation
19 qApp->installEventFilter(this);
22 XGUI_OperationMgr::~XGUI_OperationMgr()
26 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
28 return myOperations.count() > 0 ? myOperations.last() : 0;
31 bool XGUI_OperationMgr::hasOperation() const
33 return (myOperations.count() > 0) && (myOperations.last() != NULL);
36 int XGUI_OperationMgr::operationsCount() const
38 return myOperations.count();
41 QStringList XGUI_OperationMgr::operationList()
44 foreach(ModuleBase_Operation* eachOperation, myOperations)
46 result << eachOperation->id();
51 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
53 if (!canStartOperation(theOperation))
56 myOperations.append(theOperation);
58 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
59 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
60 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
61 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
62 SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
64 theOperation->start();
65 onValidateOperation();
69 bool XGUI_OperationMgr::abortAllOperations()
71 if (operationsCount() == 1) {
75 QString aMessage = tr("All active operations will be aborted.");
76 int anAnswer = QMessageBox::question(qApp->activeWindow(),
77 tr("Abort operation"),
79 QMessageBox::Ok | QMessageBox::Cancel,
81 bool result = anAnswer == QMessageBox::Ok;
82 while(result && hasOperation()) {
83 currentOperation()->abort();
88 bool XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
90 //Get operation feature to validate
91 FeaturePtr aFeature = theOperation->feature();
92 //Get validators for the Id
93 SessionPtr aMgr = ModelAPI_Session::get();
94 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
96 bool isValid = aFactory->validate(aFeature);
97 emit operationValidated(isValid);
101 void XGUI_OperationMgr::onValidateOperation()
105 ModuleBase_Operation* anOperation = currentOperation();
106 validateOperation(currentOperation());
109 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
111 if (theEvent->type() == QEvent::KeyRelease) {
112 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
113 // TODO: this is Escape button processing when the property panel has empty content,
114 // but the operation should be stopped by the Enter has been clicked
116 onKeyReleased(aKeyEvent);
120 return QObject::eventFilter(theObject, theEvent);
123 void XGUI_OperationMgr::commitOperation()
125 if (validateOperation(currentOperation())) {
130 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
132 theOperation->resume();
135 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
137 bool aCanStart = true;
138 ModuleBase_Operation* aCurrentOp = currentOperation();
140 if (!theOperation->isGranted()) {
141 if (!aCurrentOp->isValid(theOperation)) {
142 if (canAbortOperation()) {
154 void XGUI_OperationMgr::onCommitOperation()
156 ModuleBase_Operation* anOperation = currentOperation();
157 anOperation->onWidgetActivated(NULL);
159 anOperation->commit();
162 void XGUI_OperationMgr::onAbortOperation()
164 if (hasOperation() && canAbortOperation()) {
165 currentOperation()->abort();
169 bool XGUI_OperationMgr::canAbortOperation()
171 ModuleBase_Operation* anOperation = currentOperation();
172 if (anOperation && anOperation->isModified()) {
173 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
174 int anAnswer = QMessageBox::question(qApp->activeWindow(),
175 tr("Abort operation"),
177 QMessageBox::Ok | QMessageBox::Cancel,
178 QMessageBox::Cancel);
179 return anAnswer == QMessageBox::Ok;
184 void XGUI_OperationMgr::onOperationStopped()
186 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
187 ModuleBase_Operation* anOperation = currentOperation();
188 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
191 myOperations.removeAll(anOperation);
192 anOperation->deleteLater();
194 emit operationStopped(anOperation);
196 // get last operation which can be resumed
197 ModuleBase_Operation* aResultOp = 0;
198 QListIterator<ModuleBase_Operation*> anIt(myOperations);
200 while (anIt.hasPrevious()) {
201 ModuleBase_Operation* anOp = anIt.previous();
208 resumeOperation(aResultOp);
209 onValidateOperation();
213 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
215 ModuleBase_Operation* anOperation = currentOperation();
217 bool isFinished = anOperation->keyReleased(theEvent->key());
221 // Let the manager decide what to do with the given key combination.
222 switch (theEvent->key()) {
235 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
237 ModuleBase_Operation* anOperation = currentOperation();
239 anOperation->onWidgetActivated(theWidget);