1 // File: XGUI_OperationMgr.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "XGUI_OperationMgr.h"
7 #include "ModuleBase_Operation.h"
10 #include <QApplication>
13 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
16 // listen to Escape signal to stop the current operation
17 qApp->installEventFilter(this);
20 XGUI_OperationMgr::~XGUI_OperationMgr()
24 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
26 return myOperations.count() > 0 ? myOperations.last() : 0;
29 bool XGUI_OperationMgr::hasOperation() const
31 return (myOperations.count() > 0) && (myOperations.last() != NULL);
34 int XGUI_OperationMgr::operationsCount() const
36 return myOperations.count();
39 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
41 if (!canStartOperation(theOperation))
44 myOperations.append(theOperation);
46 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
47 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
48 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
50 theOperation->start();
54 bool XGUI_OperationMgr::abortOperation()
56 ModuleBase_Operation* aCurrentOp = currentOperation();
57 if (!aCurrentOp || !canStopOperation())
64 QStringList XGUI_OperationMgr::operationList()
67 foreach(ModuleBase_Operation* eachOperation, myOperations) {
68 result << eachOperation->id();
73 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
75 if (theEvent->type() == QEvent::KeyRelease) {
76 QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
77 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
78 // TODO: this is Escape button processing when the property panel has empty content,
79 // but the operation should be stopped by the Enter has been clicked
80 onKeyReleased("", aKeyEvent);
84 return QObject::eventFilter(theObject, theEvent);
87 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
89 theOperation->resume();
92 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
94 bool aCanStart = true;
95 ModuleBase_Operation* aCurrentOp = currentOperation();
96 if (aCurrentOp && !theOperation->isGranted(aCurrentOp))
98 if (canStopOperation()) {
107 bool XGUI_OperationMgr::canStopOperation()
109 int anAnswer = QMessageBox::question(0, tr("Operation launch"),
110 tr("Previous operation is not finished and will be aborted"),
111 QMessageBox::Ok, QMessageBox::Cancel);
112 return anAnswer == QMessageBox::Ok;
115 void XGUI_OperationMgr::onCommitOperation()
117 ModuleBase_Operation* anOperation = currentOperation();
119 anOperation->commit();
122 void XGUI_OperationMgr::onAbortOperation()
124 ModuleBase_Operation* anOperation = currentOperation();
126 anOperation->abort();
129 void XGUI_OperationMgr::onOperationStopped()
131 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
132 ModuleBase_Operation* anOperation = currentOperation();
133 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
136 myOperations.removeAll(anOperation);
137 anOperation->deleteLater();
139 emit operationStopped(anOperation);
141 // get last operation which can be resumed
142 ModuleBase_Operation* aResultOp = 0;
143 QListIterator<ModuleBase_Operation*> anIt(myOperations);
145 while(anIt.hasPrevious())
147 ModuleBase_Operation* anOp = anIt.previous();
154 resumeOperation(aResultOp);
157 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
159 ModuleBase_Operation* anOperation = currentOperation();
161 anOperation->keyReleased(theName, theEvent);