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();
156 anOperation->commit();
159 void XGUI_OperationMgr::onAbortOperation()
161 ModuleBase_Operation* anOperation = currentOperation();
162 if (anOperation && canAbortOperation())
163 anOperation->abort();
166 bool XGUI_OperationMgr::canAbortOperation()
168 ModuleBase_Operation* anOperation = currentOperation();
169 if (anOperation && anOperation->isModified()) {
170 int anAnswer = QMessageBox::question(
171 qApp->activeWindow(), tr("Cancel operation"),
172 tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()), QMessageBox::Yes,
174 return anAnswer == QMessageBox::Yes;
179 void XGUI_OperationMgr::onOperationStopped()
181 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
182 ModuleBase_Operation* anOperation = currentOperation();
183 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
186 myOperations.removeAll(anOperation);
187 anOperation->deleteLater();
189 emit operationStopped(anOperation);
191 // get last operation which can be resumed
192 ModuleBase_Operation* aResultOp = 0;
193 QListIterator<ModuleBase_Operation*> anIt(myOperations);
195 while (anIt.hasPrevious()) {
196 ModuleBase_Operation* anOp = anIt.previous();
203 resumeOperation(aResultOp);
204 validateCurrentOperation();
208 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
210 ModuleBase_Operation* anOperation = currentOperation();
212 anOperation->keyReleased(theName, theEvent);
215 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
217 ModuleBase_Operation* anOperation = currentOperation();
219 anOperation->onWidgetActivated(theWidget);