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 QStringList XGUI_OperationMgr::operationList() const
42 foreach(ModuleBase_Operation* eachOperation, myOperations) {
43 FeaturePtr aFeature = eachOperation->feature();
45 result << QString::fromStdString(aFeature->getKind());
51 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
53 int idx = myOperations.lastIndexOf(theOperation);
54 if(idx == -1 || idx == 0) {
57 return myOperations.at(idx - 1);
60 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
62 if (theEvent->type() == QEvent::KeyRelease) {
63 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
65 onKeyReleased(aKeyEvent);
69 return QObject::eventFilter(theObject, theEvent);
72 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
74 if (!canStartOperation(theOperation))
77 myOperations.append(theOperation);
79 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
80 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
81 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
82 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
83 SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
85 theOperation->start();
86 onValidateOperation();
90 bool XGUI_OperationMgr::abortAllOperations()
94 } else if (operationsCount() == 1) {
98 QString aMessage = tr("All active operations will be aborted.");
99 int anAnswer = QMessageBox::question(qApp->activeWindow(),
100 tr("Abort operation"),
102 QMessageBox::Ok | QMessageBox::Cancel,
103 QMessageBox::Cancel);
104 bool result = anAnswer == QMessageBox::Ok;
105 while(result && hasOperation()) {
106 currentOperation()->abort();
111 void XGUI_OperationMgr::onValidateOperation()
115 ModuleBase_Operation* anOperation = currentOperation();
117 bool isValid = anOperation->isValid();
118 emit operationValidated(isValid);
122 bool XGUI_OperationMgr::commitOperation()
124 if (hasOperation() && currentOperation()->isValid()) {
131 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
133 theOperation->resume();
136 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
138 bool aCanStart = true;
139 ModuleBase_Operation* aCurrentOp = currentOperation();
141 if (!aCurrentOp->isGranted(theOperation)) {
142 if (canAbortOperation()) {
153 void XGUI_OperationMgr::onCommitOperation()
155 ModuleBase_Operation* anOperation = currentOperation();
157 anOperation->commit();
160 void XGUI_OperationMgr::onAbortOperation()
162 if (hasOperation() && canAbortOperation()) {
163 currentOperation()->abort();
167 bool XGUI_OperationMgr::canAbortOperation()
169 ModuleBase_Operation* anOperation = currentOperation();
170 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
172 if (anOperation && anOperation->isModified()) {
173 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
174 int anAnswer = QMessageBox::question(qApp->activeWindow(),
175 tr("Abort operation"),
177 QMessageBox::Ok | QMessageBox::Cancel,
178 QMessageBox::Cancel);
179 return anAnswer == QMessageBox::Ok;
184 void XGUI_OperationMgr::onOperationStopped()
186 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
187 ModuleBase_Operation* anOperation = currentOperation();
188 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
191 myOperations.removeAll(anOperation);
192 anOperation->deleteLater();
194 emit operationStopped(anOperation);
196 // get last operation which can be resumed
197 ModuleBase_Operation* aResultOp = 0;
198 QListIterator<ModuleBase_Operation*> anIt(myOperations);
200 while (anIt.hasPrevious()) {
201 ModuleBase_Operation* anOp = anIt.previous();
208 resumeOperation(aResultOp);
209 onValidateOperation();
213 void XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
215 // Let the manager decide what to do with the given key combination.
216 ModuleBase_Operation* anOperation = currentOperation();
217 bool isRestart = false;
218 switch (theEvent->key()) {
219 case Qt::Key_Escape: {
224 case Qt::Key_Enter: {
226 anOperation->activateNextToCurrentWidget();
235 anOperation->keyReleased(theEvent->key());
238 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
240 ModuleBase_Operation* anOperation = currentOperation();
242 anOperation->onWidgetActivated(theWidget);