1 // File: XGUI_OperationMgr.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "XGUI_OperationMgr.h"
7 #include "ModuleBase_Operation.h"
8 #include <ModelAPI_Validator.h>
9 #include <ModelAPI_FeatureValidator.h>
11 #include <QMessageBox>
12 #include <QApplication>
15 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
18 // listen to Escape signal to stop the current operation
19 qApp->installEventFilter(this);
22 XGUI_OperationMgr::~XGUI_OperationMgr()
26 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
28 return myOperations.count() > 0 ? myOperations.last() : 0;
31 bool XGUI_OperationMgr::hasOperation() const
33 return (myOperations.count() > 0) && (myOperations.last() != NULL);
36 int XGUI_OperationMgr::operationsCount() const
38 return myOperations.count();
41 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
43 if (!canStartOperation(theOperation))
46 myOperations.append(theOperation);
48 connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
49 connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
50 connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
51 connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
52 SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
54 theOperation->start();
55 validateCurrentOperation();
59 bool XGUI_OperationMgr::abortOperation()
61 ModuleBase_Operation* aCurrentOp = currentOperation();
62 if (!aCurrentOp || !canStopOperation())
69 QStringList XGUI_OperationMgr::operationList()
72 foreach(ModuleBase_Operation* eachOperation, myOperations)
74 result << eachOperation->id();
79 void XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
81 //Get operation Id and feature to validate
82 QString anOperationId = theOperation->id();
83 FeaturePtr aFeature = theOperation->feature();
84 //Get validators for the Id
85 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
86 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
88 bool isValid = aFactory->validate(aFeature);
89 emit operationValidated(isValid);
92 void XGUI_OperationMgr::validateCurrentOperation()
96 ModuleBase_Operation* anOperation = currentOperation();
97 validateOperation(currentOperation());
100 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
102 if (theEvent->type() == QEvent::KeyRelease) {
103 QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
104 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
105 // TODO: this is Escape button processing when the property panel has empty content,
106 // but the operation should be stopped by the Enter has been clicked
107 onKeyReleased("", aKeyEvent);
111 return QObject::eventFilter(theObject, theEvent);
114 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
116 theOperation->resume();
119 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
121 bool aCanStart = true;
122 ModuleBase_Operation* aCurrentOp = currentOperation();
124 if (!theOperation->isGranted()) {
125 if (!aCurrentOp->isValid(theOperation)) {
126 if (canStopOperation()) {
137 bool XGUI_OperationMgr::canStopOperation()
139 ModuleBase_Operation* anOperation = currentOperation();
141 if (anOperation->isModified()) {
142 int anAnswer = QMessageBox::question(
143 qApp->activeWindow(), tr("Operation launch"),
144 tr("Previous operation is not finished and will be aborted"), QMessageBox::Ok,
145 QMessageBox::Cancel);
146 return anAnswer == QMessageBox::Ok;
152 void XGUI_OperationMgr::onCommitOperation()
154 ModuleBase_Operation* anOperation = currentOperation();
155 anOperation->onWidgetActivated(NULL);
157 anOperation->commit();
160 void XGUI_OperationMgr::onAbortOperation()
162 ModuleBase_Operation* anOperation = currentOperation();
163 if (anOperation && canAbortOperation())
164 anOperation->abort();
167 bool XGUI_OperationMgr::canAbortOperation()
169 ModuleBase_Operation* anOperation = currentOperation();
170 if (anOperation && anOperation->isModified()) {
171 int anAnswer = QMessageBox::question(
172 qApp->activeWindow(), tr("Cancel operation"),
173 tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()), QMessageBox::Yes,
175 return anAnswer == QMessageBox::Yes;
180 void XGUI_OperationMgr::onOperationStopped()
182 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
183 ModuleBase_Operation* anOperation = currentOperation();
184 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
187 myOperations.removeAll(anOperation);
188 anOperation->deleteLater();
190 emit operationStopped(anOperation);
192 // get last operation which can be resumed
193 ModuleBase_Operation* aResultOp = 0;
194 QListIterator<ModuleBase_Operation*> anIt(myOperations);
196 while (anIt.hasPrevious()) {
197 ModuleBase_Operation* anOp = anIt.previous();
204 resumeOperation(aResultOp);
205 validateCurrentOperation();
209 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
211 ModuleBase_Operation* anOperation = currentOperation();
213 anOperation->keyReleased(theName, theEvent);
216 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
218 ModuleBase_Operation* anOperation = currentOperation();
220 anOperation->onWidgetActivated(theWidget);