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*)),
52 this, 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) {
73 result << eachOperation->id();
78 void XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
80 //Get operation Id and feature to validate
81 QString anOperationId = theOperation->id();
82 FeaturePtr aFeature = theOperation->feature();
83 //Get validators for the Id
84 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
85 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
86 std::list<ModelAPI_Validator*> aValidators;
87 aFactory->validators(anOperationId.toStdString(), aValidators);
89 std::list<ModelAPI_Validator*>::iterator it = aValidators.begin();
91 for(; it != aValidators.end(); it++) {
92 const ModelAPI_FeatureValidator* aFeatureValidator =
93 dynamic_cast<const ModelAPI_FeatureValidator*>(*it);
94 if (!aFeatureValidator) continue;
95 if (!aFeatureValidator->isValid(aFeature)) {
100 emit operationValidated(isValid);
103 void XGUI_OperationMgr::validateCurrentOperation()
107 ModuleBase_Operation* anOperation = currentOperation();
108 validateOperation(currentOperation());
111 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
113 if (theEvent->type() == QEvent::KeyRelease) {
114 QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
115 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
116 // TODO: this is Escape button processing when the property panel has empty content,
117 // but the operation should be stopped by the Enter has been clicked
118 onKeyReleased("", aKeyEvent);
122 return QObject::eventFilter(theObject, theEvent);
125 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
127 theOperation->resume();
130 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
132 bool aCanStart = true;
133 ModuleBase_Operation* aCurrentOp = currentOperation();
134 if (aCurrentOp && !theOperation->isGranted(aCurrentOp))
136 if (canStopOperation()) {
145 bool XGUI_OperationMgr::canStopOperation()
147 ModuleBase_Operation* anOperation = currentOperation();
149 if (anOperation->isModified()) {
150 int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Operation launch"),
151 tr("Previous operation is not finished and will be aborted"),
152 QMessageBox::Ok, QMessageBox::Cancel);
153 return anAnswer == QMessageBox::Ok;
159 void XGUI_OperationMgr::onCommitOperation()
161 ModuleBase_Operation* anOperation = currentOperation();
163 anOperation->commit();
166 void XGUI_OperationMgr::onAbortOperation()
168 ModuleBase_Operation* anOperation = currentOperation();
169 if (anOperation && canAbortOperation())
170 anOperation->abort();
173 bool XGUI_OperationMgr::canAbortOperation()
175 ModuleBase_Operation* anOperation = currentOperation();
176 if (anOperation && anOperation->isModified()) {
177 int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Cancel operation"),
178 tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()),
179 QMessageBox::Yes, QMessageBox::No);
180 return anAnswer == QMessageBox::Yes;
185 void XGUI_OperationMgr::onOperationStopped()
187 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
188 ModuleBase_Operation* anOperation = currentOperation();
189 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation )
192 myOperations.removeAll(anOperation);
193 anOperation->deleteLater();
195 emit operationStopped(anOperation);
197 // get last operation which can be resumed
198 ModuleBase_Operation* aResultOp = 0;
199 QListIterator<ModuleBase_Operation*> anIt(myOperations);
201 while(anIt.hasPrevious())
203 ModuleBase_Operation* anOp = anIt.previous();
210 resumeOperation(aResultOp);
213 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
215 ModuleBase_Operation* anOperation = currentOperation();
217 anOperation->keyReleased(theName, theEvent);
220 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
222 ModuleBase_Operation* anOperation = currentOperation();
224 anOperation->onWidgetActivated(theWidget);