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"
8 #include "XGUI_ModuleConnector.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_ErrorMgr.h"
12 #include "ModuleBase_Operation.h"
13 #include "ModuleBase_IWorkshop.h"
14 #include "ModuleBase_IModule.h"
15 #include <ModuleBase_IViewer.h>
16 #include "ModuleBase_OperationDescription.h"
17 #include "ModuleBase_OperationFeature.h"
19 #include "ModelAPI_CompositeFeature.h"
20 #include "ModelAPI_Session.h"
22 #include <QMessageBox>
23 #include <QApplication>
26 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
27 ModuleBase_IWorkshop* theWorkshop)
28 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
29 myWorkshop(theWorkshop)
33 XGUI_OperationMgr::~XGUI_OperationMgr()
37 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
39 return myOperations.count() > 0 ? myOperations.last() : 0;
42 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
46 return currentOperation() == theOperation;
49 bool XGUI_OperationMgr::hasOperation() const
51 return !myOperations.isEmpty() && (myOperations.last() != NULL);
54 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
56 foreach(ModuleBase_Operation* aOp, myOperations) {
57 if (aOp->id() == theId)
63 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
65 foreach(ModuleBase_Operation* aOp, myOperations) {
66 if (aOp->id() == theId)
73 int XGUI_OperationMgr::operationsCount() const
75 return myOperations.count();
78 QStringList XGUI_OperationMgr::operationList() const
81 foreach(ModuleBase_Operation* eachOperation, myOperations) {
82 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
84 FeaturePtr aFeature = aFOperation->feature();
86 result << QString::fromStdString(aFeature->getKind());
93 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
95 int idx = myOperations.lastIndexOf(theOperation);
96 if(idx == -1 || idx == 0) {
99 return myOperations.at(idx - 1);
102 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
104 if (theEvent->type() == QEvent::KeyRelease) {
105 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
107 return onKeyReleased(aKeyEvent);
110 return QObject::eventFilter(theObject, theEvent);
113 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
116 currentOperation()->postpone();
117 myOperations.append(theOperation);
119 connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
120 connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
121 connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
123 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
124 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
125 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
127 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
128 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
129 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
132 connect(aFOperation, SIGNAL(activatedByPreselection()),
133 SIGNAL(operationActivatedByPreselection()));
135 theOperation->start();
136 onValidateOperation();
140 bool XGUI_OperationMgr::abortAllOperations()
146 if (operationsCount() == 1) {
147 ModuleBase_Operation* aCurrentOperation = currentOperation();
148 if (canStopOperation(aCurrentOperation)) {
149 abortOperation(aCurrentOperation);
155 aResult = QMessageBox::question(qApp->activeWindow(),
156 tr("Abort operation"),
157 tr("All active operations will be aborted."),
158 QMessageBox::Ok | QMessageBox::Cancel,
159 QMessageBox::Cancel) == QMessageBox::Ok;
160 while(aResult && hasOperation()) {
161 abortOperation(currentOperation());
167 bool XGUI_OperationMgr::commitAllOperations()
169 bool isCompositeCommitted = false;
170 while (hasOperation()) {
171 ModuleBase_Operation* anOperation = currentOperation();
172 if (isApplyEnabled()) {
175 abortOperation(anOperation);
177 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
180 FeaturePtr aFeature = aFOperation->feature();
181 CompositeFeaturePtr aComposite =
182 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
183 isCompositeCommitted = aComposite.get();
184 if (isCompositeCommitted)
191 void XGUI_OperationMgr::onValidateOperation()
195 //ModuleBase_Operation* anOperation = currentOperation();
196 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
197 (currentOperation());
198 if(aFOperation && aFOperation->feature().get()) {
199 //bool aCanCommit = myWorkshop->module()->canCommitOperation();
200 //setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
201 setApplyEnabled(myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty());
205 void XGUI_OperationMgr::setLockValidating(bool toLock)
207 myIsValidationLock = toLock;
208 onValidateOperation();
211 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
213 myIsApplyEnabled = theEnabled;
214 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
215 (currentOperation());
217 workshop()->errorMgr()->updateActions(aFOperation->feature());
219 //emit validationStateChanged(theEnabled);
222 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
224 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
226 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
228 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
229 //emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
230 // theOperation->isValid());
233 foreach(ModuleBase_Operation* anOperation, myOperations) {
235 updateApplyOfOperations(anOperation);
236 //emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
237 // anOperation->isValid());
242 bool XGUI_OperationMgr::isApplyEnabled() const
244 return myIsApplyEnabled;
247 bool XGUI_OperationMgr::isParentOperationValid() const
249 bool isValid = false;
250 // the enable state of the parent operation of the nested one is defined by the rules that
251 // firstly there are nested operations and secondly the parent operation is valid
252 ModuleBase_Operation* aPrevOp = 0;
253 Operations::const_iterator anIt = myOperations.end();
254 if (anIt != myOperations.begin()) { // there are items in the operations list
256 aPrevOp = *anIt; // the last top operation, the operation which is started
257 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
262 return aPrevOp && aPrevOp->isValid();
265 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
267 //in case of nested (sketch) operation no confirmation needed
268 if (isGrantedOperation(theOperation->id()))
270 if (theOperation && theOperation->isModified()) {
271 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
272 int anAnswer = QMessageBox::question(qApp->activeWindow(),
273 tr("Abort operation"),
275 QMessageBox::Ok | QMessageBox::Cancel,
276 QMessageBox::Cancel);
277 return anAnswer == QMessageBox::Ok;
282 bool XGUI_OperationMgr::commitOperation()
284 if (hasOperation() && currentOperation()->isValid()) {
291 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
293 theOperation->resume();
296 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
298 bool isGranted = false;
300 QListIterator<ModuleBase_Operation*> anIt(myOperations);
302 ModuleBase_Operation* aPreviousOperation = 0;
303 while (anIt.hasPrevious()) {
304 ModuleBase_Operation* anOp = anIt.previous();
305 if (anOp->id() == theId) {
306 if (anIt.hasPrevious())
307 aPreviousOperation = anIt.previous();
311 if (aPreviousOperation)
312 isGranted = aPreviousOperation->isGranted(theId);
317 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
319 SessionPtr aMgr = ModelAPI_Session::get();
320 DocumentPtr aDoc = aMgr->activeDocument();
321 bool aIsOp = aMgr->isOperation();
323 aMgr->startOperation();
324 aDoc->setCurrentFeature(theFeature, false);
326 aMgr->finishOperation();
329 bool XGUI_OperationMgr::canStartOperation(const QString& theId, const bool isAdditionallyGranted)
331 bool aCanStart = true;
332 ModuleBase_Operation* aCurrentOp = currentOperation();
334 bool aGranted = aCurrentOp->isGranted(theId) || isAdditionallyGranted;
335 // the started operation is granted for the current one,
336 // e.g. current - Sketch, started - Line
341 if (!isGrantedOperation(theId)) {
342 // the operation is not granted in the current list of operations
343 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
344 aCanStart = abortAllOperations();
346 else if (canStopOperation(aCurrentOp)) {
347 // the started operation is granted in the parrent operation,
348 // e.g. current - Line in Sketch, started Circle
349 if (myIsApplyEnabled && aCurrentOp->isModified())
350 aCurrentOp->commit();
352 abortOperation(aCurrentOp);
361 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
363 ModuleBase_Operation* aCurrentOperation = currentOperation();
364 if (theOperation == aCurrentOperation)
365 theOperation->abort();
367 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
368 // all operation from the current to triggered should also be aborted
369 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
370 while(hasOperation()) {
371 ModuleBase_Operation* aCurrentOperation = currentOperation();
372 aCurrentOperation->abort();
373 if(theOperation == aCurrentOperation)
379 void XGUI_OperationMgr::onCommitOperation()
381 ModuleBase_Operation* anOperation = currentOperation();
383 anOperation->commit();
386 void XGUI_OperationMgr::onAbortOperation()
388 ModuleBase_Operation* aCurrentOperation = currentOperation();
389 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
390 abortOperation(aCurrentOperation);
394 void XGUI_OperationMgr::onBeforeOperationStarted()
396 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
397 if (!aCurrentOperation)
400 /// Set current feature and remeber old current feature
401 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
403 SessionPtr aMgr = ModelAPI_Session::get();
404 DocumentPtr aDoc = aMgr->activeDocument();
405 // the parameter of current feature should be false, we should use all feature, not only visible
406 // in order to correctly save the previous feature of the nested operation, where the
407 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
408 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
409 // When sketch entity operation started, the sketch should be cashed here as the current.
410 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
411 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
412 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
413 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
414 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
415 // in create operation, the current feature is changed by addFeature()
416 aDoc->setCurrentFeature(aFOperation->feature(), false);
420 void XGUI_OperationMgr::onOperationStarted()
422 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
423 updateApplyOfOperations(aSenderOperation);
424 emit operationStarted(aSenderOperation);
427 void XGUI_OperationMgr::onBeforeOperationAborted()
429 onBeforeOperationCommitted();
432 void XGUI_OperationMgr::onOperationAborted()
434 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
435 emit operationAborted(aSenderOperation);
438 void XGUI_OperationMgr::onBeforeOperationCommitted()
440 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
441 if (!aCurrentOperation)
444 /// Restore the previous current feature
445 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
447 if (aFOperation->isEditOperation()) {
448 /// Restore the previous current feature
449 setCurrentFeature(aFOperation->previousCurrentFeature());
451 else { // create operation
452 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
453 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
454 // in order to perform it in the current transaction without opening a new one.
455 if (myOperations.front() != aFOperation)
456 setCurrentFeature(aFOperation->previousCurrentFeature());
461 void XGUI_OperationMgr::onOperationCommitted()
463 // apply state for all features from the stack of operations should be updated
464 updateApplyOfOperations();
466 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
467 emit operationCommitted(aSenderOperation);
470 void XGUI_OperationMgr::onOperationResumed()
472 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
473 emit operationResumed(aSenderOperation);
476 void XGUI_OperationMgr::onOperationStopped()
478 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
479 ModuleBase_Operation* aCurrentOperation = currentOperation();
480 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
483 myOperations.removeAll(aCurrentOperation);
484 aCurrentOperation->deleteLater();
486 emit operationStopped(aCurrentOperation);
488 // get last operation which can be resumed
489 ModuleBase_Operation* aResultOp = 0;
490 QListIterator<ModuleBase_Operation*> anIt(myOperations);
492 while (anIt.hasPrevious()) {
493 ModuleBase_Operation* anOp = anIt.previous();
500 bool isModified = aCurrentOperation->isModified();
501 aResultOp->setIsModified(aResultOp->isModified() || isModified);
502 resumeOperation(aResultOp);
503 onValidateOperation();
507 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
509 // Let the manager decide what to do with the given key combination.
510 ModuleBase_Operation* anOperation = currentOperation();
511 bool isAccepted = true;
512 switch (theEvent->key()) {
514 case Qt::Key_Enter: {
515 emit keyEnterReleased();
520 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
522 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
523 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
524 if (!aContext.IsNull()) {
525 Handle(V3d_View) aView = aViewer->activeView();
526 if ((theEvent->key() == Qt::Key_N))
527 aContext->HilightNextDetected(aView);
528 else if ((theEvent->key() == Qt::Key_P))
529 aContext->HilightPreviousDetected(aView);
540 // anOperation->keyReleased(theEvent->key());
545 XGUI_Workshop* XGUI_OperationMgr::workshop() const
547 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
548 return aConnector->workshop();