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)
18 XGUI_OperationMgr::~XGUI_OperationMgr()
22 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
24 return myOperations.count() > 0 ? myOperations.last() : 0;
27 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
31 return currentOperation() == theOperation;
34 bool XGUI_OperationMgr::hasOperation() const
36 return !myOperations.isEmpty() && (myOperations.last() != NULL);
39 int XGUI_OperationMgr::operationsCount() const
41 return myOperations.count();
44 QStringList XGUI_OperationMgr::operationList() const
47 foreach(ModuleBase_Operation* eachOperation, myOperations) {
48 FeaturePtr aFeature = eachOperation->feature();
50 result << QString::fromStdString(aFeature->getKind());
56 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
58 int idx = myOperations.lastIndexOf(theOperation);
59 if(idx == -1 || idx == 0) {
62 return myOperations.at(idx - 1);
65 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
67 if (theEvent->type() == QEvent::KeyRelease) {
68 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
70 return onKeyReleased(aKeyEvent);
73 return QObject::eventFilter(theObject, theEvent);
76 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
78 if (!canStartOperation(theOperation))
81 myOperations.append(theOperation);
83 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
84 connect(theOperation, SIGNAL(started()), this, SLOT(onOperationStarted()));
85 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
87 theOperation->start();
88 onValidateOperation();
92 bool XGUI_OperationMgr::abortAllOperations()
96 } else if (operationsCount() == 1) {
100 QString aMessage = tr("All active operations will be aborted.");
101 int anAnswer = QMessageBox::question(qApp->activeWindow(),
102 tr("Abort operation"),
104 QMessageBox::Ok | QMessageBox::Cancel,
105 QMessageBox::Cancel);
106 bool result = anAnswer == QMessageBox::Ok;
107 while(result && hasOperation()) {
108 currentOperation()->abort();
113 void XGUI_OperationMgr::onValidateOperation()
117 ModuleBase_Operation* anOperation = currentOperation();
119 bool isValid = anOperation->isValid();
120 emit operationValidated(isValid);
124 bool XGUI_OperationMgr::commitOperation()
126 if (hasOperation() && currentOperation()->isValid()) {
133 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
135 theOperation->resume();
138 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
140 bool aCanStart = true;
141 ModuleBase_Operation* aCurrentOp = currentOperation();
143 if (!aCurrentOp->isGranted(theOperation)) {
144 if (canAbortOperation()) {
155 void XGUI_OperationMgr::onCommitOperation()
157 ModuleBase_Operation* anOperation = currentOperation();
159 anOperation->commit();
162 void XGUI_OperationMgr::onAbortOperation()
164 if (hasOperation() && canAbortOperation()) {
165 currentOperation()->abort();
169 bool XGUI_OperationMgr::canAbortOperation()
171 ModuleBase_Operation* anOperation = currentOperation();
172 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
174 if (anOperation && anOperation->isModified()) {
175 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
176 int anAnswer = QMessageBox::question(qApp->activeWindow(),
177 tr("Abort operation"),
179 QMessageBox::Ok | QMessageBox::Cancel,
180 QMessageBox::Cancel);
181 return anAnswer == QMessageBox::Ok;
186 void XGUI_OperationMgr::onOperationStarted()
188 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
189 emit operationStarted(aSenderOperation);
192 void XGUI_OperationMgr::onOperationStopped()
194 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
195 ModuleBase_Operation* anOperation = currentOperation();
196 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
199 myOperations.removeAll(anOperation);
200 anOperation->deleteLater();
202 emit operationStopped(anOperation);
204 // get last operation which can be resumed
205 ModuleBase_Operation* aResultOp = 0;
206 QListIterator<ModuleBase_Operation*> anIt(myOperations);
208 while (anIt.hasPrevious()) {
209 ModuleBase_Operation* anOp = anIt.previous();
216 resumeOperation(aResultOp);
217 onValidateOperation();
221 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
223 // Let the manager decide what to do with the given key combination.
224 ModuleBase_Operation* anOperation = currentOperation();
225 bool isAccepted = true;
226 switch (theEvent->key()) {
228 case Qt::Key_Enter: {
237 anOperation->keyReleased(theEvent->key());