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();
136 if (aCurrentOp && !theOperation->isGranted(aCurrentOp)) {
137 if (canStopOperation()) {
146 bool XGUI_OperationMgr::canStopOperation()
148 ModuleBase_Operation* anOperation = currentOperation();
150 if (anOperation->isModified()) {
151 int anAnswer = QMessageBox::question(
152 qApp->activeWindow(), tr("Operation launch"),
153 tr("Previous operation is not finished and will be aborted"), QMessageBox::Ok,
154 QMessageBox::Cancel);
155 return anAnswer == QMessageBox::Ok;
161 void XGUI_OperationMgr::onCommitOperation()
163 ModuleBase_Operation* anOperation = currentOperation();
165 anOperation->commit();
168 void XGUI_OperationMgr::onAbortOperation()
170 ModuleBase_Operation* anOperation = currentOperation();
171 if (anOperation && canAbortOperation())
172 anOperation->abort();
175 bool XGUI_OperationMgr::canAbortOperation()
177 ModuleBase_Operation* anOperation = currentOperation();
178 if (anOperation && anOperation->isModified()) {
179 int anAnswer = QMessageBox::question(
180 qApp->activeWindow(), tr("Cancel operation"),
181 tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()), QMessageBox::Yes,
183 return anAnswer == QMessageBox::Yes;
188 void XGUI_OperationMgr::onOperationStopped()
190 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
191 ModuleBase_Operation* anOperation = currentOperation();
192 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
195 myOperations.removeAll(anOperation);
196 anOperation->deleteLater();
198 emit operationStopped(anOperation);
200 // get last operation which can be resumed
201 ModuleBase_Operation* aResultOp = 0;
202 QListIterator<ModuleBase_Operation*> anIt(myOperations);
204 while (anIt.hasPrevious()) {
205 ModuleBase_Operation* anOp = anIt.previous();
212 resumeOperation(aResultOp);
213 validateCurrentOperation();
217 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
219 ModuleBase_Operation* anOperation = currentOperation();
221 anOperation->keyReleased(theName, theEvent);
224 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
226 ModuleBase_Operation* anOperation = currentOperation();
228 anOperation->onWidgetActivated(theWidget);