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"
10 #include "ModuleBase_IWorkshop.h"
11 #include "ModuleBase_IModule.h"
13 #include <QMessageBox>
14 #include <QApplication>
17 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
18 ModuleBase_IWorkshop* theWorkshop)
19 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
20 myWorkshop(theWorkshop)
24 XGUI_OperationMgr::~XGUI_OperationMgr()
28 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
30 return myOperations.count() > 0 ? myOperations.last() : 0;
33 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
37 return currentOperation() == theOperation;
40 bool XGUI_OperationMgr::hasOperation() const
42 return !myOperations.isEmpty() && (myOperations.last() != NULL);
45 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
47 foreach(ModuleBase_Operation* aOp, myOperations) {
48 if (aOp->id() == theId)
54 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
56 foreach(ModuleBase_Operation* aOp, myOperations) {
57 if (aOp->id() == theId)
64 int XGUI_OperationMgr::operationsCount() const
66 return myOperations.count();
69 QStringList XGUI_OperationMgr::operationList() const
72 foreach(ModuleBase_Operation* eachOperation, myOperations) {
73 FeaturePtr aFeature = eachOperation->feature();
75 result << QString::fromStdString(aFeature->getKind());
81 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
83 int idx = myOperations.lastIndexOf(theOperation);
84 if(idx == -1 || idx == 0) {
87 return myOperations.at(idx - 1);
90 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
92 if (theEvent->type() == QEvent::KeyRelease) {
93 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
95 return onKeyReleased(aKeyEvent);
98 return QObject::eventFilter(theObject, theEvent);
101 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
104 currentOperation()->postpone();
105 myOperations.append(theOperation);
107 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
108 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
109 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
110 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
111 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
112 connect(theOperation, SIGNAL(triggered(bool)), SLOT(onOperationTriggered(bool)));
113 connect(theOperation, SIGNAL(activatedByPreselection()),
114 SIGNAL(operationActivatedByPreselection()));
116 theOperation->start();
117 onValidateOperation();
121 bool XGUI_OperationMgr::abortAllOperations()
123 if(!hasOperation()) {
125 } else if (operationsCount() == 1) {
129 QString aMessage = tr("All active operations will be aborted.");
130 int anAnswer = QMessageBox::question(qApp->activeWindow(),
131 tr("Abort operation"),
133 QMessageBox::Ok | QMessageBox::Cancel,
134 QMessageBox::Cancel);
135 bool result = anAnswer == QMessageBox::Ok;
136 while(result && hasOperation()) {
137 currentOperation()->abort();
142 bool XGUI_OperationMgr::commitAllOperations()
144 while (hasOperation()) {
145 if (isApplyEnabled()) {
148 currentOperation()->abort();
154 void XGUI_OperationMgr::onValidateOperation()
158 ModuleBase_Operation* anOperation = currentOperation();
160 bool aCanCommit = myWorkshop->module()->canCommitOperation();
161 setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
165 void XGUI_OperationMgr::setLockValidating(bool toLock)
167 myIsValidationLock = toLock;
168 onValidateOperation();
171 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
173 myIsApplyEnabled = theEnabled;
174 emit validationStateChanged(theEnabled);
177 bool XGUI_OperationMgr::isApplyEnabled() const
179 return myIsApplyEnabled;
182 bool XGUI_OperationMgr::canStopOperation()
184 ModuleBase_Operation* anOperation = currentOperation();
185 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
187 if (anOperation && anOperation->isModified()) {
188 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
189 int anAnswer = QMessageBox::question(qApp->activeWindow(),
190 tr("Abort operation"),
192 QMessageBox::Ok | QMessageBox::Cancel,
193 QMessageBox::Cancel);
194 return anAnswer == QMessageBox::Ok;
199 bool XGUI_OperationMgr::commitOperation()
201 if (hasOperation() && currentOperation()->isValid()) {
208 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
210 theOperation->resume();
213 bool XGUI_OperationMgr::canStartOperation(QString theId)
215 bool aCanStart = true;
216 ModuleBase_Operation* aCurrentOp = currentOperation();
218 if (!aCurrentOp->isGranted(theId)) {
219 if (canStopOperation()) {
220 if (myIsApplyEnabled)
221 aCurrentOp->commit();
233 void XGUI_OperationMgr::onCommitOperation()
235 ModuleBase_Operation* anOperation = currentOperation();
237 anOperation->commit();
240 void XGUI_OperationMgr::onAbortOperation()
242 if (hasOperation() && canStopOperation()) {
243 currentOperation()->abort();
247 void XGUI_OperationMgr::onOperationStarted()
249 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
251 bool isNestedOk = (myOperations.count() >= 1) &&
252 myOperations.at(0)->isValid();
253 emit nestedStateChanged(isNestedOk);
254 emit operationStarted(aSenderOperation);
257 void XGUI_OperationMgr::onOperationAborted()
259 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
260 emit operationAborted(aSenderOperation);
263 void XGUI_OperationMgr::onOperationCommitted()
265 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
266 emit nestedStateChanged(myOperations.count() >= 1);
267 emit operationCommitted(aSenderOperation);
270 void XGUI_OperationMgr::onOperationResumed()
272 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
273 emit operationResumed(aSenderOperation);
276 void XGUI_OperationMgr::onOperationStopped()
278 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
279 ModuleBase_Operation* anOperation = currentOperation();
280 if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
283 myOperations.removeAll(anOperation);
284 anOperation->deleteLater();
286 emit operationStopped(anOperation);
288 // get last operation which can be resumed
289 ModuleBase_Operation* aResultOp = 0;
290 QListIterator<ModuleBase_Operation*> anIt(myOperations);
292 while (anIt.hasPrevious()) {
293 ModuleBase_Operation* anOp = anIt.previous();
300 resumeOperation(aResultOp);
301 onValidateOperation();
305 void XGUI_OperationMgr::onOperationTriggered(bool theState)
307 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
308 if (aSenderOperation && !theState) {
309 ModuleBase_Operation* aCurrentOperation = currentOperation();
310 if (aSenderOperation == aCurrentOperation)
311 aCurrentOperation->abort();
313 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
314 // all operation from the current to triggered should also be aborted
315 while(hasOperation()) {
316 ModuleBase_Operation* aCurrentOperation = currentOperation();
317 aCurrentOperation->abort();
318 if(aSenderOperation == aCurrentOperation)
325 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
327 // Let the manager decide what to do with the given key combination.
328 ModuleBase_Operation* anOperation = currentOperation();
329 bool isAccepted = true;
330 switch (theEvent->key()) {
332 case Qt::Key_Enter: {
333 emit keyEnterReleased();
342 // anOperation->keyReleased(theEvent->key());