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::isCurrentOperation(ModuleBase_Operation* theOperation)
33 return currentOperation() == theOperation;
36 bool XGUI_OperationMgr::hasOperation() const
38 return !myOperations.isEmpty() && (myOperations.last() != NULL);
41 int XGUI_OperationMgr::operationsCount() const
43 return myOperations.count();
46 QStringList XGUI_OperationMgr::operationList() const
49 foreach(ModuleBase_Operation* eachOperation, myOperations) {
50 FeaturePtr aFeature = eachOperation->feature();
52 result << QString::fromStdString(aFeature->getKind());
58 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
60 int idx = myOperations.lastIndexOf(theOperation);
61 if(idx == -1 || idx == 0) {
64 return myOperations.at(idx - 1);
67 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
69 if (theEvent->type() == QEvent::KeyRelease) {
70 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
72 onKeyReleased(aKeyEvent);
76 return QObject::eventFilter(theObject, theEvent);
79 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
81 if (!canStartOperation(theOperation))
84 myOperations.append(theOperation);
86 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
87 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
88 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
89 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
90 SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
92 theOperation->start();
93 onValidateOperation();
97 bool XGUI_OperationMgr::abortAllOperations()
101 } else if (operationsCount() == 1) {
105 QString aMessage = tr("All active operations will be aborted.");
106 int anAnswer = QMessageBox::question(qApp->activeWindow(),
107 tr("Abort operation"),
109 QMessageBox::Ok | QMessageBox::Cancel,
110 QMessageBox::Cancel);
111 bool result = anAnswer == QMessageBox::Ok;
112 while(result && hasOperation()) {
113 currentOperation()->abort();
118 void XGUI_OperationMgr::onValidateOperation()
122 ModuleBase_Operation* anOperation = currentOperation();
124 bool isValid = anOperation->isValid();
125 emit operationValidated(isValid);
129 bool XGUI_OperationMgr::commitOperation()
131 if (hasOperation() && currentOperation()->isValid()) {
138 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
140 theOperation->resume();
143 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
145 bool aCanStart = true;
146 ModuleBase_Operation* aCurrentOp = currentOperation();
148 if (!aCurrentOp->isGranted(theOperation)) {
149 if (canAbortOperation()) {
160 void XGUI_OperationMgr::onCommitOperation()
162 ModuleBase_Operation* anOperation = currentOperation();
164 anOperation->commit();
167 void XGUI_OperationMgr::onAbortOperation()
169 if (hasOperation() && canAbortOperation()) {
170 currentOperation()->abort();
174 bool XGUI_OperationMgr::canAbortOperation()
176 ModuleBase_Operation* anOperation = currentOperation();
177 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
179 if (anOperation && anOperation->isModified()) {
180 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
181 int anAnswer = QMessageBox::question(qApp->activeWindow(),
182 tr("Abort operation"),
184 QMessageBox::Ok | QMessageBox::Cancel,
185 QMessageBox::Cancel);
186 return anAnswer == QMessageBox::Ok;
191 void XGUI_OperationMgr::onOperationStopped()
193 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
194 ModuleBase_Operation* anOperation = currentOperation();
195 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
198 myOperations.removeAll(anOperation);
199 anOperation->deleteLater();
201 emit operationStopped(anOperation);
203 // get last operation which can be resumed
204 ModuleBase_Operation* aResultOp = 0;
205 QListIterator<ModuleBase_Operation*> anIt(myOperations);
207 while (anIt.hasPrevious()) {
208 ModuleBase_Operation* anOp = anIt.previous();
215 resumeOperation(aResultOp);
216 onValidateOperation();
220 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
222 // Let the manager decide what to do with the given key combination.
223 ModuleBase_Operation* anOperation = currentOperation();
224 bool isRestart = false;
225 switch (theEvent->key()) {
226 case Qt::Key_Escape: {
231 case Qt::Key_Enter: {
233 anOperation->activateNextToCurrentWidget();
242 anOperation->keyReleased(theEvent->key());
245 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
247 ModuleBase_Operation* anOperation = currentOperation();
249 anOperation->onWidgetActivated(theWidget);