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 ModuleBase_Operation* aCurrentOperation = currentOperation();
139 if (canStopOperation(aCurrentOperation)) {
140 abortOperation(aCurrentOperation);
146 aResult = QMessageBox::question(qApp->activeWindow(),
147 tr("Abort operation"),
148 tr("All active operations will be aborted."),
149 QMessageBox::Ok | QMessageBox::Cancel,
150 QMessageBox::Cancel) == QMessageBox::Ok;
151 while(aResult && hasOperation()) {
152 abortOperation(currentOperation());
158 bool XGUI_OperationMgr::commitAllOperations()
160 bool isCompositeCommitted = false;
161 while (hasOperation()) {
162 ModuleBase_Operation* anOperation = currentOperation();
163 if (isApplyEnabled()) {
166 abortOperation(anOperation);
168 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
171 FeaturePtr aFeature = aFOperation->feature();
172 CompositeFeaturePtr aComposite =
173 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
174 isCompositeCommitted = aComposite.get();
175 if (isCompositeCommitted)
182 void XGUI_OperationMgr::onValidateOperation()
186 ModuleBase_Operation* anOperation = currentOperation();
188 bool aCanCommit = myWorkshop->module()->canCommitOperation();
189 setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
193 void XGUI_OperationMgr::setLockValidating(bool toLock)
195 myIsValidationLock = toLock;
196 onValidateOperation();
199 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
201 myIsApplyEnabled = theEnabled;
202 emit validationStateChanged(theEnabled);
205 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
208 emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
209 theOperation->isValid());
211 foreach(ModuleBase_Operation* anOperation, myOperations) {
212 emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
213 anOperation->isValid());
218 bool XGUI_OperationMgr::isApplyEnabled() const
220 return myIsApplyEnabled;
223 bool XGUI_OperationMgr::isParentOperationValid() const
225 bool isValid = false;
226 // the enable state of the parent operation of the nested one is defined by the rules that
227 // firstly there are nested operations and secondly the parent operation is valid
228 ModuleBase_Operation* aPrevOp = 0;
229 Operations::const_iterator anIt = myOperations.end();
230 if (anIt != myOperations.begin()) { // there are items in the operations list
232 aPrevOp = *anIt; // the last top operation, the operation which is started
233 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
238 return aPrevOp && aPrevOp->isValid();
241 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
243 //in case of nested (sketch) operation no confirmation needed
244 if (isGrantedOperation(theOperation))
246 if (theOperation && theOperation->isModified()) {
247 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
248 int anAnswer = QMessageBox::question(qApp->activeWindow(),
249 tr("Abort operation"),
251 QMessageBox::Ok | QMessageBox::Cancel,
252 QMessageBox::Cancel);
253 return anAnswer == QMessageBox::Ok;
258 bool XGUI_OperationMgr::commitOperation()
260 if (hasOperation() && currentOperation()->isValid()) {
267 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
269 theOperation->resume();
272 bool XGUI_OperationMgr::isGrantedOperation(ModuleBase_Operation* theOperation)
274 bool isGranted = false;
276 QListIterator<ModuleBase_Operation*> anIt(myOperations);
278 ModuleBase_Operation* aPreviousOperation = 0;
279 while (anIt.hasPrevious()) {
280 ModuleBase_Operation* anOp = anIt.previous();
281 if (anOp == theOperation) {
282 if (anIt.hasPrevious())
283 aPreviousOperation = anIt.previous();
287 if (aPreviousOperation)
288 isGranted = aPreviousOperation->isGranted(theOperation->id());
293 bool XGUI_OperationMgr::canStartOperation(const QString& theId, const bool isAdditionallyGranted)
295 bool aCanStart = true;
296 ModuleBase_Operation* aCurrentOp = currentOperation();
298 bool aGranted = aCurrentOp->isGranted(theId) || isAdditionallyGranted;
300 if (canStopOperation(aCurrentOp)) {
301 if (myIsApplyEnabled && aCurrentOp->isModified())
302 aCurrentOp->commit();
304 abortOperation(aCurrentOp);
313 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
315 ModuleBase_Operation* aCurrentOperation = currentOperation();
316 if (theOperation == aCurrentOperation)
317 theOperation->abort();
319 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
320 // all operation from the current to triggered should also be aborted
321 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
322 while(hasOperation()) {
323 ModuleBase_Operation* aCurrentOperation = currentOperation();
324 aCurrentOperation->abort();
325 if(theOperation == aCurrentOperation)
331 void XGUI_OperationMgr::onCommitOperation()
333 ModuleBase_Operation* anOperation = currentOperation();
335 anOperation->commit();
338 void XGUI_OperationMgr::onAbortOperation()
340 ModuleBase_Operation* aCurrentOperation = currentOperation();
341 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
342 abortOperation(aCurrentOperation);
346 void XGUI_OperationMgr::onOperationStarted()
348 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
349 updateApplyOfOperations(aSenderOperation);
350 emit operationStarted(aSenderOperation);
353 void XGUI_OperationMgr::onOperationAborted()
355 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
356 emit operationAborted(aSenderOperation);
359 void XGUI_OperationMgr::onOperationCommitted()
361 updateApplyOfOperations();
363 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
364 emit operationCommitted(aSenderOperation);
367 void XGUI_OperationMgr::onOperationResumed()
369 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
370 emit operationResumed(aSenderOperation);
373 void XGUI_OperationMgr::onOperationStopped()
375 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
376 ModuleBase_Operation* aCurrentOperation = currentOperation();
377 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
380 myOperations.removeAll(aCurrentOperation);
381 aCurrentOperation->deleteLater();
383 emit operationStopped(aCurrentOperation);
385 // get last operation which can be resumed
386 ModuleBase_Operation* aResultOp = 0;
387 QListIterator<ModuleBase_Operation*> anIt(myOperations);
389 while (anIt.hasPrevious()) {
390 ModuleBase_Operation* anOp = anIt.previous();
397 bool isModified = aCurrentOperation->isModified();
398 aResultOp->setIsModified(aResultOp->isModified() || isModified);
399 resumeOperation(aResultOp);
400 onValidateOperation();
404 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
406 // Let the manager decide what to do with the given key combination.
407 ModuleBase_Operation* anOperation = currentOperation();
408 bool isAccepted = true;
409 switch (theEvent->key()) {
411 case Qt::Key_Enter: {
412 emit keyEnterReleased();
421 // anOperation->keyReleased(theEvent->key());