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();
87 std::list<ModelAPI_Validator*> aValidators;
88 aFactory->validators(anOperationId.toStdString(), aValidators);
90 std::list<ModelAPI_Validator*>::iterator it = aValidators.begin();
92 for (; it != aValidators.end(); it++) {
93 const ModelAPI_FeatureValidator* aFeatureValidator =
94 dynamic_cast<const ModelAPI_FeatureValidator*>(*it);
95 if (!aFeatureValidator)
97 if (!aFeatureValidator->isValid(aFeature)) {
102 emit operationValidated(isValid);
105 void XGUI_OperationMgr::validateCurrentOperation()
109 ModuleBase_Operation* anOperation = currentOperation();
110 validateOperation(currentOperation());
113 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
115 if (theEvent->type() == QEvent::KeyRelease) {
116 QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
117 if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
118 // TODO: this is Escape button processing when the property panel has empty content,
119 // but the operation should be stopped by the Enter has been clicked
120 onKeyReleased("", aKeyEvent);
124 return QObject::eventFilter(theObject, theEvent);
127 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
129 theOperation->resume();
132 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
134 bool aCanStart = true;
135 ModuleBase_Operation* aCurrentOp = currentOperation();
137 if (!theOperation->isGranted()) {
138 if (!aCurrentOp->isValid(theOperation)) {
139 if (canStopOperation()) {
150 bool XGUI_OperationMgr::canStopOperation()
152 ModuleBase_Operation* anOperation = currentOperation();
154 if (anOperation->isModified()) {
155 int anAnswer = QMessageBox::question(
156 qApp->activeWindow(), tr("Operation launch"),
157 tr("Previous operation is not finished and will be aborted"), QMessageBox::Ok,
158 QMessageBox::Cancel);
159 return anAnswer == QMessageBox::Ok;
165 void XGUI_OperationMgr::onCommitOperation()
167 ModuleBase_Operation* anOperation = currentOperation();
169 anOperation->commit();
172 void XGUI_OperationMgr::onAbortOperation()
174 ModuleBase_Operation* anOperation = currentOperation();
175 if (anOperation && canAbortOperation())
176 anOperation->abort();
179 bool XGUI_OperationMgr::canAbortOperation()
181 ModuleBase_Operation* anOperation = currentOperation();
182 if (anOperation && anOperation->isModified()) {
183 int anAnswer = QMessageBox::question(
184 qApp->activeWindow(), tr("Cancel operation"),
185 tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()), QMessageBox::Yes,
187 return anAnswer == QMessageBox::Yes;
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 validateCurrentOperation();
221 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
223 ModuleBase_Operation* anOperation = currentOperation();
225 anOperation->keyReleased(theName, theEvent);
228 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
230 ModuleBase_Operation* anOperation = currentOperation();
232 anOperation->onWidgetActivated(theWidget);