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 ModuleBase_Operation* anOperation = currentOperation();
113 if (anOperation->isModified()) {
114 int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Operation launch"),
115 tr("Previous operation is not finished and will be aborted"),
116 QMessageBox::Ok, QMessageBox::Cancel);
117 return anAnswer == QMessageBox::Ok;
123 void XGUI_OperationMgr::onCommitOperation()
125 ModuleBase_Operation* anOperation = currentOperation();
127 anOperation->commit();
130 void XGUI_OperationMgr::onAbortOperation()
132 ModuleBase_Operation* anOperation = currentOperation();
133 if (anOperation && canAbortOperation())
134 anOperation->abort();
137 bool XGUI_OperationMgr::canAbortOperation()
139 ModuleBase_Operation* anOperation = currentOperation();
140 if (anOperation && anOperation->isModified()) {
141 int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Cancel operation"),
142 tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()),
143 QMessageBox::Yes, QMessageBox::No);
144 return anAnswer == QMessageBox::Yes;
149 void XGUI_OperationMgr::onOperationStopped()
151 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
152 ModuleBase_Operation* anOperation = currentOperation();
153 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
156 myOperations.removeAll(anOperation);
157 anOperation->deleteLater();
159 emit operationStopped(anOperation);
161 // get last operation which can be resumed
162 ModuleBase_Operation* aResultOp = 0;
163 QListIterator<ModuleBase_Operation*> anIt(myOperations);
165 while(anIt.hasPrevious())
167 ModuleBase_Operation* anOp = anIt.previous();
174 resumeOperation(aResultOp);
177 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
179 ModuleBase_Operation* anOperation = currentOperation();
181 anOperation->keyReleased(theName, theEvent);
184 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
186 ModuleBase_Operation* anOperation = currentOperation();
188 anOperation->onWidgetActivated(theWidget);