1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_OperationMgr.cpp
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"
12 #include "ModuleBase_OperationDescription.h"
14 #include "ModelAPI_CompositeFeature.h"
15 #include "ModelAPI_Session.h"
17 #include <QMessageBox>
18 #include <QApplication>
21 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
22 ModuleBase_IWorkshop* theWorkshop)
23 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
24 myWorkshop(theWorkshop)
28 XGUI_OperationMgr::~XGUI_OperationMgr()
32 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
34 return myOperations.count() > 0 ? myOperations.last() : 0;
37 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
41 return currentOperation() == theOperation;
44 bool XGUI_OperationMgr::hasOperation() const
46 return !myOperations.isEmpty() && (myOperations.last() != NULL);
49 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
51 foreach(ModuleBase_Operation* aOp, myOperations) {
52 if (aOp->id() == theId)
58 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
60 foreach(ModuleBase_Operation* aOp, myOperations) {
61 if (aOp->id() == theId)
68 int XGUI_OperationMgr::operationsCount() const
70 return myOperations.count();
73 QStringList XGUI_OperationMgr::operationList() const
76 foreach(ModuleBase_Operation* eachOperation, myOperations) {
77 FeaturePtr aFeature = eachOperation->feature();
79 result << QString::fromStdString(aFeature->getKind());
85 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
87 int idx = myOperations.lastIndexOf(theOperation);
88 if(idx == -1 || idx == 0) {
91 return myOperations.at(idx - 1);
94 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
96 if (theEvent->type() == QEvent::KeyRelease) {
97 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
99 return onKeyReleased(aKeyEvent);
102 return QObject::eventFilter(theObject, theEvent);
105 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
108 currentOperation()->postpone();
109 myOperations.append(theOperation);
111 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
112 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
113 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
114 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
115 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
116 connect(theOperation, SIGNAL(triggered(bool)), SLOT(onOperationTriggered(bool)));
117 connect(theOperation, SIGNAL(activatedByPreselection()),
118 SIGNAL(operationActivatedByPreselection()));
120 theOperation->start();
121 onValidateOperation();
125 bool XGUI_OperationMgr::abortAllOperations()
131 if (operationsCount() == 1) {
132 if (canStopOperation()) {
133 abortOperation(currentOperation());
139 aResult = QMessageBox::question(qApp->activeWindow(),
140 tr("Abort operation"),
141 tr("All active operations will be aborted."),
142 QMessageBox::Ok | QMessageBox::Cancel,
143 QMessageBox::Cancel) == QMessageBox::Ok;
144 while(aResult && hasOperation()) {
145 abortOperation(currentOperation());
151 bool XGUI_OperationMgr::commitAllOperations()
153 bool isCompositeCommitted = false;
154 while (hasOperation()) {
155 ModuleBase_Operation* anOperation = currentOperation();
156 if (isApplyEnabled()) {
159 abortOperation(anOperation);
161 FeaturePtr aFeature = anOperation->feature();
162 CompositeFeaturePtr aComposite =
163 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
164 isCompositeCommitted = aComposite.get();
165 if (isCompositeCommitted)
171 void XGUI_OperationMgr::onValidateOperation()
175 ModuleBase_Operation* anOperation = currentOperation();
177 bool aCanCommit = myWorkshop->module()->canCommitOperation();
178 setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
182 void XGUI_OperationMgr::setLockValidating(bool toLock)
184 myIsValidationLock = toLock;
185 onValidateOperation();
188 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
190 myIsApplyEnabled = theEnabled;
191 emit validationStateChanged(theEnabled);
194 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
197 emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
198 theOperation->isValid());
200 foreach(ModuleBase_Operation* anOperation, myOperations) {
201 emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
202 anOperation->isValid());
207 bool XGUI_OperationMgr::isApplyEnabled() const
209 return myIsApplyEnabled;
212 bool XGUI_OperationMgr::isParentOperationValid() const
214 bool isValid = false;
215 // the enable state of the parent operation of the nested one is defined by the rules that
216 // firstly there are nested operations and secondly the parent operation is valid
217 ModuleBase_Operation* aPrevOp = 0;
218 Operations::const_iterator anIt = myOperations.end();
219 if (anIt != myOperations.begin()) { // there are items in the operations list
221 aPrevOp = *anIt; // the last top operation, the operation which is started
222 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
227 return aPrevOp && aPrevOp->isValid();
230 bool XGUI_OperationMgr::canStopOperation()
232 ModuleBase_Operation* anOperation = currentOperation();
233 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
235 if (anOperation && anOperation->isModified()) {
236 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
237 int anAnswer = QMessageBox::question(qApp->activeWindow(),
238 tr("Abort operation"),
240 QMessageBox::Ok | QMessageBox::Cancel,
241 QMessageBox::Cancel);
242 return anAnswer == QMessageBox::Ok;
247 bool XGUI_OperationMgr::commitOperation()
249 if (hasOperation() && currentOperation()->isValid()) {
256 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
258 theOperation->resume();
261 bool XGUI_OperationMgr::canStartOperation(QString theId)
263 bool aCanStart = true;
264 ModuleBase_Operation* aCurrentOp = currentOperation();
266 if (!aCurrentOp->isGranted(theId)) {
267 if (canStopOperation()) {
268 if (myIsApplyEnabled)
269 aCurrentOp->commit();
271 abortOperation(aCurrentOp);
280 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
282 ModuleBase_Operation* aCurrentOperation = currentOperation();
283 if (theOperation == aCurrentOperation)
284 theOperation->abort();
286 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
287 // all operation from the current to triggered should also be aborted
288 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
289 while(hasOperation()) {
290 ModuleBase_Operation* aCurrentOperation = currentOperation();
291 aCurrentOperation->abort();
292 if(theOperation == aCurrentOperation)
298 void XGUI_OperationMgr::onCommitOperation()
300 ModuleBase_Operation* anOperation = currentOperation();
302 anOperation->commit();
305 void XGUI_OperationMgr::onAbortOperation()
307 if (hasOperation() && canStopOperation()) {
308 abortOperation(currentOperation());
312 void XGUI_OperationMgr::onOperationStarted()
314 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
315 updateApplyOfOperations(aSenderOperation);
316 emit operationStarted(aSenderOperation);
319 void XGUI_OperationMgr::onOperationAborted()
321 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
322 emit operationAborted(aSenderOperation);
325 void XGUI_OperationMgr::onOperationCommitted()
327 updateApplyOfOperations();
329 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
330 emit operationCommitted(aSenderOperation);
333 void XGUI_OperationMgr::onOperationResumed()
335 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
336 emit operationResumed(aSenderOperation);
339 void XGUI_OperationMgr::onOperationStopped()
341 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
342 ModuleBase_Operation* aCurrentOperation = currentOperation();
343 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
346 myOperations.removeAll(aCurrentOperation);
347 aCurrentOperation->deleteLater();
349 emit operationStopped(aCurrentOperation);
351 // get last operation which can be resumed
352 ModuleBase_Operation* aResultOp = 0;
353 QListIterator<ModuleBase_Operation*> anIt(myOperations);
355 while (anIt.hasPrevious()) {
356 ModuleBase_Operation* anOp = anIt.previous();
363 bool isModified = aCurrentOperation->isModified();
364 aResultOp->setIsModified(aResultOp->isModified() || isModified);
365 resumeOperation(aResultOp);
366 onValidateOperation();
370 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
372 // Let the manager decide what to do with the given key combination.
373 ModuleBase_Operation* anOperation = currentOperation();
374 bool isAccepted = true;
375 switch (theEvent->key()) {
377 case Qt::Key_Enter: {
378 emit keyEnterReleased();
387 // anOperation->keyReleased(theEvent->key());