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);
71 if(aKeyEvent && onKeyReleased(aKeyEvent)) {
75 return QObject::eventFilter(theObject, theEvent);
78 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
80 if (!canStartOperation(theOperation))
83 myOperations.append(theOperation);
85 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
86 connect(theOperation, SIGNAL(started()), this, SLOT(onOperationStarted()));
87 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
89 theOperation->start();
90 onValidateOperation();
94 bool XGUI_OperationMgr::abortAllOperations()
98 } else if (operationsCount() == 1) {
102 QString aMessage = tr("All active operations will be aborted.");
103 int anAnswer = QMessageBox::question(qApp->activeWindow(),
104 tr("Abort operation"),
106 QMessageBox::Ok | QMessageBox::Cancel,
107 QMessageBox::Cancel);
108 bool result = anAnswer == QMessageBox::Ok;
109 while(result && hasOperation()) {
110 currentOperation()->abort();
115 void XGUI_OperationMgr::onValidateOperation()
119 ModuleBase_Operation* anOperation = currentOperation();
121 bool isValid = anOperation->isValid();
122 emit operationValidated(isValid);
126 bool XGUI_OperationMgr::commitOperation()
128 if (hasOperation() && currentOperation()->isValid()) {
135 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
137 theOperation->resume();
140 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
142 bool aCanStart = true;
143 ModuleBase_Operation* aCurrentOp = currentOperation();
145 if (!aCurrentOp->isGranted(theOperation)) {
146 if (canAbortOperation()) {
157 void XGUI_OperationMgr::onCommitOperation()
159 ModuleBase_Operation* anOperation = currentOperation();
161 anOperation->commit();
164 void XGUI_OperationMgr::onAbortOperation()
166 if (hasOperation() && canAbortOperation()) {
167 currentOperation()->abort();
171 bool XGUI_OperationMgr::canAbortOperation()
173 ModuleBase_Operation* anOperation = currentOperation();
174 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
176 if (anOperation && anOperation->isModified()) {
177 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
178 int anAnswer = QMessageBox::question(qApp->activeWindow(),
179 tr("Abort operation"),
181 QMessageBox::Ok | QMessageBox::Cancel,
182 QMessageBox::Cancel);
183 return anAnswer == QMessageBox::Ok;
188 void XGUI_OperationMgr::onOperationStarted()
190 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
191 emit operationStarted(aSenderOperation);
194 void XGUI_OperationMgr::onOperationStopped()
196 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
197 ModuleBase_Operation* anOperation = currentOperation();
198 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
201 myOperations.removeAll(anOperation);
202 anOperation->deleteLater();
204 emit operationStopped(anOperation);
206 // get last operation which can be resumed
207 ModuleBase_Operation* aResultOp = 0;
208 QListIterator<ModuleBase_Operation*> anIt(myOperations);
210 while (anIt.hasPrevious()) {
211 ModuleBase_Operation* anOp = anIt.previous();
218 resumeOperation(aResultOp);
219 onValidateOperation();
223 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
225 // Let the manager decide what to do with the given key combination.
226 ModuleBase_Operation* anOperation = currentOperation();
227 bool isAccepted = true;
228 switch (theEvent->key()) {
229 case Qt::Key_Escape: {
234 case Qt::Key_Enter: {
243 anOperation->keyReleased(theEvent->key());