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() const
44 foreach(ModuleBase_Operation* eachOperation, myOperations) {
45 FeaturePtr aFeature = eachOperation->feature();
47 result << QString::fromStdString(aFeature->getKind());
53 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
55 int idx = myOperations.lastIndexOf(theOperation);
56 if(idx == -1 || idx == 0) {
59 return myOperations.at(idx - 1);
62 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
64 if (theEvent->type() == QEvent::KeyRelease) {
65 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
67 onKeyReleased(aKeyEvent);
71 return QObject::eventFilter(theObject, theEvent);
74 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
76 if (!canStartOperation(theOperation))
79 myOperations.append(theOperation);
81 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
82 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
83 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
84 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
85 SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
87 theOperation->start();
88 onValidateOperation();
92 bool XGUI_OperationMgr::abortAllOperations()
96 } else if (operationsCount() == 1) {
100 QString aMessage = tr("All active operations will be aborted.");
101 int anAnswer = QMessageBox::question(qApp->activeWindow(),
102 tr("Abort operation"),
104 QMessageBox::Ok | QMessageBox::Cancel,
105 QMessageBox::Cancel);
106 bool result = anAnswer == QMessageBox::Ok;
107 while(result && hasOperation()) {
108 currentOperation()->abort();
113 bool XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
115 //Get operation feature to validate
116 FeaturePtr aFeature = theOperation->feature();
117 if (!aFeature) return true; // rename operation
118 //Get validators for the Id
119 SessionPtr aMgr = ModelAPI_Session::get();
120 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
122 bool isValid = aFactory->validate(aFeature);
123 emit operationValidated(isValid);
127 void XGUI_OperationMgr::onValidateOperation()
131 ModuleBase_Operation* anOperation = currentOperation();
132 validateOperation(currentOperation());
135 bool XGUI_OperationMgr::commitOperation()
137 if (validateOperation(currentOperation())) {
144 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
146 theOperation->resume();
149 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
151 bool aCanStart = true;
152 ModuleBase_Operation* aCurrentOp = currentOperation();
154 if (!theOperation->isGranted()) {
155 if (!aCurrentOp->isValid(theOperation)) {
156 if (canAbortOperation()) {
168 void XGUI_OperationMgr::onCommitOperation()
170 ModuleBase_Operation* anOperation = currentOperation();
172 anOperation->commit();
175 void XGUI_OperationMgr::onAbortOperation()
177 if (hasOperation() && canAbortOperation()) {
178 currentOperation()->abort();
182 bool XGUI_OperationMgr::canAbortOperation()
184 ModuleBase_Operation* anOperation = currentOperation();
185 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
187 if (anOperation && anOperation->isModified()) {
188 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
189 int anAnswer = QMessageBox::question(qApp->activeWindow(),
190 tr("Abort operation"),
192 QMessageBox::Ok | QMessageBox::Cancel,
193 QMessageBox::Cancel);
194 return anAnswer == QMessageBox::Ok;
199 void XGUI_OperationMgr::onOperationStopped()
201 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
202 ModuleBase_Operation* anOperation = currentOperation();
203 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
206 myOperations.removeAll(anOperation);
207 anOperation->deleteLater();
209 emit operationStopped(anOperation);
211 // get last operation which can be resumed
212 ModuleBase_Operation* aResultOp = 0;
213 QListIterator<ModuleBase_Operation*> anIt(myOperations);
215 while (anIt.hasPrevious()) {
216 ModuleBase_Operation* anOp = anIt.previous();
223 resumeOperation(aResultOp);
224 onValidateOperation();
228 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
230 // Let the manager decide what to do with the given key combination.
231 ModuleBase_Operation* anOperation = currentOperation();
232 bool isRestart = false;
233 switch (theEvent->key()) {
234 case Qt::Key_Escape: {
239 case Qt::Key_Enter: {
241 anOperation->activateNextToCurrentWidget();
250 anOperation->keyReleased(theEvent->key());
253 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
255 ModuleBase_Operation* anOperation = currentOperation();
257 anOperation->onWidgetActivated(theWidget);