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, SIGNAL(operationStarted()));
87 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
88 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
89 SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
91 theOperation->start();
92 onValidateOperation();
96 bool XGUI_OperationMgr::abortAllOperations()
100 } else if (operationsCount() == 1) {
104 QString aMessage = tr("All active operations will be aborted.");
105 int anAnswer = QMessageBox::question(qApp->activeWindow(),
106 tr("Abort operation"),
108 QMessageBox::Ok | QMessageBox::Cancel,
109 QMessageBox::Cancel);
110 bool result = anAnswer == QMessageBox::Ok;
111 while(result && hasOperation()) {
112 currentOperation()->abort();
117 void XGUI_OperationMgr::onValidateOperation()
121 ModuleBase_Operation* anOperation = currentOperation();
123 bool isValid = anOperation->isValid();
124 emit operationValidated(isValid);
128 bool XGUI_OperationMgr::commitOperation()
130 if (hasOperation() && currentOperation()->isValid()) {
137 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
139 theOperation->resume();
142 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
144 bool aCanStart = true;
145 ModuleBase_Operation* aCurrentOp = currentOperation();
147 if (!aCurrentOp->isGranted(theOperation)) {
148 if (canAbortOperation()) {
159 void XGUI_OperationMgr::onCommitOperation()
161 ModuleBase_Operation* anOperation = currentOperation();
163 anOperation->commit();
166 void XGUI_OperationMgr::onAbortOperation()
168 if (hasOperation() && canAbortOperation()) {
169 currentOperation()->abort();
173 bool XGUI_OperationMgr::canAbortOperation()
175 ModuleBase_Operation* anOperation = currentOperation();
176 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
178 if (anOperation && anOperation->isModified()) {
179 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
180 int anAnswer = QMessageBox::question(qApp->activeWindow(),
181 tr("Abort operation"),
183 QMessageBox::Ok | QMessageBox::Cancel,
184 QMessageBox::Cancel);
185 return anAnswer == QMessageBox::Ok;
190 void XGUI_OperationMgr::onOperationStopped()
192 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
193 ModuleBase_Operation* anOperation = currentOperation();
194 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
197 myOperations.removeAll(anOperation);
198 anOperation->deleteLater();
200 emit operationStopped(anOperation);
202 // get last operation which can be resumed
203 ModuleBase_Operation* aResultOp = 0;
204 QListIterator<ModuleBase_Operation*> anIt(myOperations);
206 while (anIt.hasPrevious()) {
207 ModuleBase_Operation* anOp = anIt.previous();
214 resumeOperation(aResultOp);
215 onValidateOperation();
219 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
221 // Let the manager decide what to do with the given key combination.
222 ModuleBase_Operation* anOperation = currentOperation();
223 bool isAccepted = true;
224 switch (theEvent->key()) {
225 case Qt::Key_Escape: {
230 case Qt::Key_Enter: {
239 anOperation->keyReleased(theEvent->key());
244 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
246 ModuleBase_Operation* anOperation = currentOperation();
248 anOperation->onWidgetActivated(theWidget);