1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_OperationMgr.h
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "XGUI_OperationMgr.h"
9 #include "ModuleBase_Operation.h"
11 #include <QMessageBox>
12 #include <QApplication>
15 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
16 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false)
20 XGUI_OperationMgr::~XGUI_OperationMgr()
24 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
26 return myOperations.count() > 0 ? myOperations.last() : 0;
29 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
33 return currentOperation() == theOperation;
36 bool XGUI_OperationMgr::hasOperation() const
38 return !myOperations.isEmpty() && (myOperations.last() != NULL);
41 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
43 foreach(ModuleBase_Operation* aOp, myOperations) {
44 if (aOp->id() == theId)
50 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
52 foreach(ModuleBase_Operation* aOp, myOperations) {
53 if (aOp->id() == theId)
60 int XGUI_OperationMgr::operationsCount() const
62 return myOperations.count();
65 QStringList XGUI_OperationMgr::operationList() const
68 foreach(ModuleBase_Operation* eachOperation, myOperations) {
69 FeaturePtr aFeature = eachOperation->feature();
71 result << QString::fromStdString(aFeature->getKind());
77 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
79 int idx = myOperations.lastIndexOf(theOperation);
80 if(idx == -1 || idx == 0) {
83 return myOperations.at(idx - 1);
86 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
88 if (theEvent->type() == QEvent::KeyRelease) {
89 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
91 return onKeyReleased(aKeyEvent);
94 return QObject::eventFilter(theObject, theEvent);
97 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
100 currentOperation()->postpone();
101 myOperations.append(theOperation);
103 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
104 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
105 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
106 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
107 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
108 connect(theOperation, SIGNAL(activatedByPreselection()),
109 SIGNAL(operationActivatedByPreselection()));
111 theOperation->start();
112 onValidateOperation();
116 bool XGUI_OperationMgr::abortAllOperations()
118 if(!hasOperation()) {
120 } else if (operationsCount() == 1) {
124 QString aMessage = tr("All active operations will be aborted.");
125 int anAnswer = QMessageBox::question(qApp->activeWindow(),
126 tr("Abort operation"),
128 QMessageBox::Ok | QMessageBox::Cancel,
129 QMessageBox::Cancel);
130 bool result = anAnswer == QMessageBox::Ok;
131 while(result && hasOperation()) {
132 currentOperation()->abort();
137 bool XGUI_OperationMgr::commitAllOperations()
139 while (hasOperation()) {
140 if (isApplyEnabled()) {
143 currentOperation()->abort();
149 void XGUI_OperationMgr::onValidateOperation()
153 ModuleBase_Operation* anOperation = currentOperation();
154 if(anOperation && (!myIsValidationLock)) {
155 setApplyEnabled(anOperation->isValid());
159 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
161 myIsApplyEnabled = theEnabled;
162 emit validationStateChanged(theEnabled);
165 bool XGUI_OperationMgr::isApplyEnabled() const
167 return myIsApplyEnabled;
170 bool XGUI_OperationMgr::canStopOperation()
172 ModuleBase_Operation* anOperation = currentOperation();
173 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
175 if (anOperation && anOperation->isModified()) {
176 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
177 int anAnswer = QMessageBox::question(qApp->activeWindow(),
178 tr("Abort operation"),
180 QMessageBox::Ok | QMessageBox::Cancel,
181 QMessageBox::Cancel);
182 return anAnswer == QMessageBox::Ok;
187 bool XGUI_OperationMgr::commitOperation()
189 if (hasOperation() && currentOperation()->isValid()) {
196 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
198 theOperation->resume();
201 bool XGUI_OperationMgr::canStartOperation(QString theId)
203 bool aCanStart = true;
204 ModuleBase_Operation* aCurrentOp = currentOperation();
206 if (!aCurrentOp->isGranted(theId)) {
207 if (canStopOperation()) {
208 if (myIsApplyEnabled)
209 aCurrentOp->commit();
221 void XGUI_OperationMgr::onCommitOperation()
223 ModuleBase_Operation* anOperation = currentOperation();
225 anOperation->commit();
228 void XGUI_OperationMgr::onAbortOperation()
230 if (hasOperation() && canStopOperation()) {
231 currentOperation()->abort();
235 void XGUI_OperationMgr::onOperationStarted()
237 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
239 bool isNestedOk = (myOperations.count() >= 1) &&
240 myOperations.at(0)->isValid();
241 emit nestedStateChanged(isNestedOk);
242 emit operationStarted(aSenderOperation);
245 void XGUI_OperationMgr::onOperationAborted()
247 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
248 emit operationAborted(aSenderOperation);
251 void XGUI_OperationMgr::onOperationCommitted()
253 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
254 emit nestedStateChanged(myOperations.count() >= 1);
255 emit operationCommitted(aSenderOperation);
258 void XGUI_OperationMgr::onOperationResumed()
260 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
261 emit operationResumed(aSenderOperation);
264 void XGUI_OperationMgr::onOperationStopped()
266 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
267 ModuleBase_Operation* anOperation = currentOperation();
268 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
271 myOperations.removeAll(anOperation);
272 anOperation->deleteLater();
274 emit operationStopped(anOperation);
276 // get last operation which can be resumed
277 ModuleBase_Operation* aResultOp = 0;
278 QListIterator<ModuleBase_Operation*> anIt(myOperations);
280 while (anIt.hasPrevious()) {
281 ModuleBase_Operation* anOp = anIt.previous();
288 resumeOperation(aResultOp);
289 onValidateOperation();
293 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
295 // Let the manager decide what to do with the given key combination.
296 ModuleBase_Operation* anOperation = currentOperation();
297 bool isAccepted = true;
298 switch (theEvent->key()) {
300 case Qt::Key_Enter: {
301 emit keyEnterReleased();
310 // anOperation->keyReleased(theEvent->key());