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) {
45 FeaturePtr aFeature = eachOperation->feature();
47 result << QString::fromStdString(aFeature->getKind());
53 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
55 if (theEvent->type() == QEvent::KeyRelease) {
56 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
58 onKeyReleased(aKeyEvent);
62 return QObject::eventFilter(theObject, theEvent);
65 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
67 if (!canStartOperation(theOperation))
70 myOperations.append(theOperation);
72 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
73 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
74 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
75 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
76 SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
78 theOperation->start();
79 onValidateOperation();
83 bool XGUI_OperationMgr::abortAllOperations()
87 } else if (operationsCount() == 1) {
91 QString aMessage = tr("All active operations will be aborted.");
92 int anAnswer = QMessageBox::question(qApp->activeWindow(),
93 tr("Abort operation"),
95 QMessageBox::Ok | QMessageBox::Cancel,
97 bool result = anAnswer == QMessageBox::Ok;
98 while(result && hasOperation()) {
99 currentOperation()->abort();
104 bool XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
106 //Get operation feature to validate
107 FeaturePtr aFeature = theOperation->feature();
108 //Get validators for the Id
109 SessionPtr aMgr = ModelAPI_Session::get();
110 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
112 bool isValid = aFactory->validate(aFeature);
113 emit operationValidated(isValid);
117 void XGUI_OperationMgr::onValidateOperation()
121 ModuleBase_Operation* anOperation = currentOperation();
122 validateOperation(currentOperation());
125 bool XGUI_OperationMgr::commitOperation()
127 if (validateOperation(currentOperation())) {
134 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
136 theOperation->resume();
139 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
141 bool aCanStart = true;
142 ModuleBase_Operation* aCurrentOp = currentOperation();
144 if (!theOperation->isGranted()) {
145 if (!aCurrentOp->isValid(theOperation)) {
146 if (canAbortOperation()) {
158 void XGUI_OperationMgr::onCommitOperation()
160 ModuleBase_Operation* anOperation = currentOperation();
162 anOperation->commit();
165 void XGUI_OperationMgr::onAbortOperation()
167 if (hasOperation() && canAbortOperation()) {
168 currentOperation()->abort();
172 bool XGUI_OperationMgr::canAbortOperation()
174 ModuleBase_Operation* anOperation = currentOperation();
175 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
177 if (anOperation && anOperation->isModified()) {
178 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
179 int anAnswer = QMessageBox::question(qApp->activeWindow(),
180 tr("Abort operation"),
182 QMessageBox::Ok | QMessageBox::Cancel,
183 QMessageBox::Cancel);
184 return anAnswer == QMessageBox::Ok;
189 void XGUI_OperationMgr::onOperationStopped()
191 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
192 ModuleBase_Operation* anOperation = currentOperation();
193 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
196 myOperations.removeAll(anOperation);
197 anOperation->deleteLater();
199 emit operationStopped(anOperation);
201 // get last operation which can be resumed
202 ModuleBase_Operation* aResultOp = 0;
203 QListIterator<ModuleBase_Operation*> anIt(myOperations);
205 while (anIt.hasPrevious()) {
206 ModuleBase_Operation* anOp = anIt.previous();
213 resumeOperation(aResultOp);
214 onValidateOperation();
218 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
220 // Let the manager decide what to do with the given key combination.
221 ModuleBase_Operation* anOperation = currentOperation();
222 bool isRestart = false;
223 switch (theEvent->key()) {
224 case Qt::Key_Escape: {
229 case Qt::Key_Enter: {
231 anOperation->activateNextToCurrentWidget();
240 anOperation->keyReleased(theEvent->key());
243 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
245 ModuleBase_Operation* anOperation = currentOperation();
247 anOperation->onWidgetActivated(theWidget);