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"
13 #include "ModuleBase_OperationFeature.h"
15 #include "ModelAPI_CompositeFeature.h"
16 #include "ModelAPI_Session.h"
18 #include <QMessageBox>
19 #include <QApplication>
22 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
23 ModuleBase_IWorkshop* theWorkshop)
24 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
25 myWorkshop(theWorkshop)
29 XGUI_OperationMgr::~XGUI_OperationMgr()
33 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
35 return myOperations.count() > 0 ? myOperations.last() : 0;
38 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
42 return currentOperation() == theOperation;
45 bool XGUI_OperationMgr::hasOperation() const
47 return !myOperations.isEmpty() && (myOperations.last() != NULL);
50 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
52 foreach(ModuleBase_Operation* aOp, myOperations) {
53 if (aOp->id() == theId)
59 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
61 foreach(ModuleBase_Operation* aOp, myOperations) {
62 if (aOp->id() == theId)
69 int XGUI_OperationMgr::operationsCount() const
71 return myOperations.count();
74 QStringList XGUI_OperationMgr::operationList() const
77 foreach(ModuleBase_Operation* eachOperation, myOperations) {
78 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
80 FeaturePtr aFeature = aFOperation->feature();
82 result << QString::fromStdString(aFeature->getKind());
89 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
91 int idx = myOperations.lastIndexOf(theOperation);
92 if(idx == -1 || idx == 0) {
95 return myOperations.at(idx - 1);
98 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
100 if (theEvent->type() == QEvent::KeyRelease) {
101 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
103 return onKeyReleased(aKeyEvent);
106 return QObject::eventFilter(theObject, theEvent);
109 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
112 currentOperation()->postpone();
113 myOperations.append(theOperation);
115 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
116 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
117 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
118 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
119 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
120 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
123 connect(aFOperation, SIGNAL(activatedByPreselection()),
124 SIGNAL(operationActivatedByPreselection()));
126 theOperation->start();
127 onValidateOperation();
131 bool XGUI_OperationMgr::abortAllOperations()
137 if (operationsCount() == 1) {
138 if (canStopOperation()) {
139 abortOperation(currentOperation());
145 aResult = QMessageBox::question(qApp->activeWindow(),
146 tr("Abort operation"),
147 tr("All active operations will be aborted."),
148 QMessageBox::Ok | QMessageBox::Cancel,
149 QMessageBox::Cancel) == QMessageBox::Ok;
150 while(aResult && hasOperation()) {
151 abortOperation(currentOperation());
157 bool XGUI_OperationMgr::commitAllOperations()
159 bool isCompositeCommitted = false;
160 while (hasOperation()) {
161 ModuleBase_Operation* anOperation = currentOperation();
162 if (isApplyEnabled()) {
165 abortOperation(anOperation);
167 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
170 FeaturePtr aFeature = aFOperation->feature();
171 CompositeFeaturePtr aComposite =
172 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
173 isCompositeCommitted = aComposite.get();
174 if (isCompositeCommitted)
181 void XGUI_OperationMgr::onValidateOperation()
185 ModuleBase_Operation* anOperation = currentOperation();
187 bool aCanCommit = myWorkshop->module()->canCommitOperation();
188 setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
192 void XGUI_OperationMgr::setLockValidating(bool toLock)
194 myIsValidationLock = toLock;
195 onValidateOperation();
198 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
200 myIsApplyEnabled = theEnabled;
201 emit validationStateChanged(theEnabled);
204 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
207 emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
208 theOperation->isValid());
210 foreach(ModuleBase_Operation* anOperation, myOperations) {
211 emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
212 anOperation->isValid());
217 bool XGUI_OperationMgr::isApplyEnabled() const
219 return myIsApplyEnabled;
222 bool XGUI_OperationMgr::isParentOperationValid() const
224 bool isValid = false;
225 // the enable state of the parent operation of the nested one is defined by the rules that
226 // firstly there are nested operations and secondly the parent operation is valid
227 ModuleBase_Operation* aPrevOp = 0;
228 Operations::const_iterator anIt = myOperations.end();
229 if (anIt != myOperations.begin()) { // there are items in the operations list
231 aPrevOp = *anIt; // the last top operation, the operation which is started
232 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
237 return aPrevOp && aPrevOp->isValid();
240 bool XGUI_OperationMgr::canStopOperation()
242 ModuleBase_Operation* anOperation = currentOperation();
243 if(operationsCount() > 1) //in case of nested (sketch) operation no confirmation needed
245 if (anOperation && anOperation->isModified()) {
246 QString aMessage = tr("%1 operation will be aborted.").arg(anOperation->id());
247 int anAnswer = QMessageBox::question(qApp->activeWindow(),
248 tr("Abort operation"),
250 QMessageBox::Ok | QMessageBox::Cancel,
251 QMessageBox::Cancel);
252 return anAnswer == QMessageBox::Ok;
257 bool XGUI_OperationMgr::commitOperation()
259 if (hasOperation() && currentOperation()->isValid()) {
266 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
268 theOperation->resume();
271 bool XGUI_OperationMgr::canStartOperation(QString theId)
273 bool aCanStart = true;
274 ModuleBase_Operation* aCurrentOp = currentOperation();
276 if (!aCurrentOp->isGranted(theId)) {
277 if (canStopOperation()) {
278 if (myIsApplyEnabled && aCurrentOp->isModified())
279 aCurrentOp->commit();
281 abortOperation(aCurrentOp);
290 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
292 ModuleBase_Operation* aCurrentOperation = currentOperation();
293 if (theOperation == aCurrentOperation)
294 theOperation->abort();
296 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
297 // all operation from the current to triggered should also be aborted
298 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
299 while(hasOperation()) {
300 ModuleBase_Operation* aCurrentOperation = currentOperation();
301 aCurrentOperation->abort();
302 if(theOperation == aCurrentOperation)
308 void XGUI_OperationMgr::onCommitOperation()
310 ModuleBase_Operation* anOperation = currentOperation();
312 anOperation->commit();
315 void XGUI_OperationMgr::onAbortOperation()
317 if (hasOperation() && canStopOperation()) {
318 abortOperation(currentOperation());
322 void XGUI_OperationMgr::onOperationStarted()
324 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
325 updateApplyOfOperations(aSenderOperation);
326 emit operationStarted(aSenderOperation);
329 void XGUI_OperationMgr::onOperationAborted()
331 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
332 emit operationAborted(aSenderOperation);
335 void XGUI_OperationMgr::onOperationCommitted()
337 updateApplyOfOperations();
339 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
340 emit operationCommitted(aSenderOperation);
343 void XGUI_OperationMgr::onOperationResumed()
345 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
346 emit operationResumed(aSenderOperation);
349 void XGUI_OperationMgr::onOperationStopped()
351 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
352 ModuleBase_Operation* aCurrentOperation = currentOperation();
353 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
356 myOperations.removeAll(aCurrentOperation);
357 aCurrentOperation->deleteLater();
359 emit operationStopped(aCurrentOperation);
361 // get last operation which can be resumed
362 ModuleBase_Operation* aResultOp = 0;
363 QListIterator<ModuleBase_Operation*> anIt(myOperations);
365 while (anIt.hasPrevious()) {
366 ModuleBase_Operation* anOp = anIt.previous();
373 bool isModified = aCurrentOperation->isModified();
374 aResultOp->setIsModified(aResultOp->isModified() || isModified);
375 resumeOperation(aResultOp);
376 onValidateOperation();
380 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
382 // Let the manager decide what to do with the given key combination.
383 ModuleBase_Operation* anOperation = currentOperation();
384 bool isAccepted = true;
385 switch (theEvent->key()) {
387 case Qt::Key_Enter: {
388 emit keyEnterReleased();
397 // anOperation->keyReleased(theEvent->key());