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()));
49 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
50 this, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
52 theOperation->start();
56 bool XGUI_OperationMgr::abortOperation()
58 ModuleBase_Operation* aCurrentOp = currentOperation();
59 if (!aCurrentOp || !canStopOperation())
66 QStringList XGUI_OperationMgr::operationList()
69 foreach(ModuleBase_Operation* eachOperation, myOperations) {
70 result << eachOperation->id();
75 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
77 if (theEvent->type() == QEvent::KeyRelease) {
78 QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
79 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
80 // TODO: this is Escape button processing when the property panel has empty content,
81 // but the operation should be stopped by the Enter has been clicked
82 onKeyReleased("", aKeyEvent);
86 return QObject::eventFilter(theObject, theEvent);
89 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
91 theOperation->resume();
94 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
96 bool aCanStart = true;
97 ModuleBase_Operation* aCurrentOp = currentOperation();
98 if (aCurrentOp && !theOperation->isGranted(aCurrentOp))
100 if (canStopOperation()) {
109 bool XGUI_OperationMgr::canStopOperation()
111 int anAnswer = QMessageBox::question(0, tr("Operation launch"),
112 tr("Previous operation is not finished and will be aborted"),
113 QMessageBox::Ok, QMessageBox::Cancel);
114 return anAnswer == QMessageBox::Ok;
117 void XGUI_OperationMgr::onCommitOperation()
119 ModuleBase_Operation* anOperation = currentOperation();
121 anOperation->commit();
124 void XGUI_OperationMgr::onAbortOperation()
126 ModuleBase_Operation* anOperation = currentOperation();
128 anOperation->abort();
131 void XGUI_OperationMgr::onOperationStopped()
133 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
134 ModuleBase_Operation* anOperation = currentOperation();
135 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
138 myOperations.removeAll(anOperation);
139 anOperation->deleteLater();
141 emit operationStopped(anOperation);
143 // get last operation which can be resumed
144 ModuleBase_Operation* aResultOp = 0;
145 QListIterator<ModuleBase_Operation*> anIt(myOperations);
147 while(anIt.hasPrevious())
149 ModuleBase_Operation* anOp = anIt.previous();
156 resumeOperation(aResultOp);
159 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
161 ModuleBase_Operation* anOperation = currentOperation();
163 anOperation->keyReleased(theName, theEvent);
166 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
168 ModuleBase_Operation* anOperation = currentOperation();
170 anOperation->onWidgetActivated(theWidget);