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_IPropertyPanel.h>
13 #include <ModuleBase_ModelWidget.h>
14 #include "ModuleBase_Operation.h"
15 #include "ModuleBase_IWorkshop.h"
16 #include "ModuleBase_IModule.h"
17 #include <ModuleBase_IViewer.h>
18 #include "ModuleBase_OperationDescription.h"
19 #include "ModuleBase_OperationFeature.h"
21 #include "ModelAPI_CompositeFeature.h"
22 #include "ModelAPI_Session.h"
24 #include <QMessageBox>
25 #include <QApplication>
28 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
29 ModuleBase_IWorkshop* theWorkshop)
30 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
31 myWorkshop(theWorkshop)
35 XGUI_OperationMgr::~XGUI_OperationMgr()
39 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
41 return myOperations.count() > 0 ? myOperations.last() : 0;
44 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
48 return currentOperation() == theOperation;
51 bool XGUI_OperationMgr::hasOperation() const
53 return !myOperations.isEmpty() && (myOperations.last() != NULL);
56 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
58 foreach(ModuleBase_Operation* aOp, myOperations) {
59 if (aOp->id() == theId)
65 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
67 foreach(ModuleBase_Operation* aOp, myOperations) {
68 if (aOp->id() == theId)
75 int XGUI_OperationMgr::operationsCount() const
77 return myOperations.count();
80 QStringList XGUI_OperationMgr::operationList() const
83 foreach(ModuleBase_Operation* eachOperation, myOperations) {
84 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
86 FeaturePtr aFeature = aFOperation->feature();
88 result << QString::fromStdString(aFeature->getKind());
95 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
97 int idx = myOperations.lastIndexOf(theOperation);
98 if(idx == -1 || idx == 0) {
101 return myOperations.at(idx - 1);
104 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
106 if (theEvent->type() == QEvent::KeyRelease) {
107 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
109 return onKeyReleased(aKeyEvent);
112 return QObject::eventFilter(theObject, theEvent);
115 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
118 currentOperation()->postpone();
119 myOperations.append(theOperation);
121 connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
122 connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
123 connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
125 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
126 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
127 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
129 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
130 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
131 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
134 connect(aFOperation, SIGNAL(activatedByPreselection()),
135 SIGNAL(operationActivatedByPreselection()));
137 theOperation->start();
138 onValidateOperation();
142 bool XGUI_OperationMgr::abortAllOperations()
148 if (operationsCount() == 1) {
149 ModuleBase_Operation* aCurrentOperation = currentOperation();
150 if (canStopOperation(aCurrentOperation)) {
151 abortOperation(aCurrentOperation);
157 aResult = QMessageBox::question(qApp->activeWindow(),
158 tr("Abort operation"),
159 tr("All active operations will be aborted."),
160 QMessageBox::Ok | QMessageBox::Cancel,
161 QMessageBox::Cancel) == QMessageBox::Ok;
162 while(aResult && hasOperation()) {
163 abortOperation(currentOperation());
169 bool XGUI_OperationMgr::commitAllOperations()
171 bool isCompositeCommitted = false;
172 while (hasOperation()) {
173 ModuleBase_Operation* anOperation = currentOperation();
174 if (isApplyEnabled()) {
177 abortOperation(anOperation);
179 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
182 FeaturePtr aFeature = aFOperation->feature();
183 CompositeFeaturePtr aComposite =
184 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
185 isCompositeCommitted = aComposite.get();
186 if (isCompositeCommitted)
193 void XGUI_OperationMgr::onValidateOperation()
197 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
198 (currentOperation());
199 if(aFOperation && aFOperation->feature().get())
200 setApplyEnabled(myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty());
203 void XGUI_OperationMgr::setLockValidating(bool toLock)
205 myIsValidationLock = toLock;
206 onValidateOperation();
209 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
211 myIsApplyEnabled = theEnabled;
212 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
213 (currentOperation());
215 workshop()->errorMgr()->updateActions(aFOperation->feature());
217 //emit validationStateChanged(theEnabled);
220 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
222 XGUI_ErrorMgr* anErrorMgr = workshop()->errorMgr();
224 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
226 anErrorMgr->updateAcceptAllAction(aFOperation->feature());
227 //emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
228 // theOperation->isValid());
231 foreach(ModuleBase_Operation* anOperation, myOperations) {
233 updateApplyOfOperations(anOperation);
234 //emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
235 // anOperation->isValid());
240 bool XGUI_OperationMgr::isApplyEnabled() const
242 return myIsApplyEnabled;
245 bool XGUI_OperationMgr::isParentOperationValid() const
247 bool isValid = false;
248 // the enable state of the parent operation of the nested one is defined by the rules that
249 // firstly there are nested operations and secondly the parent operation is valid
250 ModuleBase_Operation* aPrevOp = 0;
251 Operations::const_iterator anIt = myOperations.end();
252 if (anIt != myOperations.begin()) { // there are items in the operations list
254 aPrevOp = *anIt; // the last top operation, the operation which is started
255 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
260 return aPrevOp && aPrevOp->isValid();
263 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
265 //in case of nested (sketch) operation no confirmation needed
266 if (isGrantedOperation(theOperation->id()))
268 if (theOperation && theOperation->isModified()) {
269 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
270 int anAnswer = QMessageBox::question(qApp->activeWindow(),
271 tr("Abort operation"),
273 QMessageBox::Ok | QMessageBox::Cancel,
274 QMessageBox::Cancel);
275 return anAnswer == QMessageBox::Ok;
280 bool XGUI_OperationMgr::commitOperation()
282 if (hasOperation() && currentOperation()->isValid()) {
289 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
291 theOperation->resume();
294 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
296 bool isGranted = false;
298 QListIterator<ModuleBase_Operation*> anIt(myOperations);
300 ModuleBase_Operation* aPreviousOperation = 0;
301 while (anIt.hasPrevious()) {
302 ModuleBase_Operation* anOp = anIt.previous();
304 isGranted = anOp->isGranted(theId);
309 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
311 SessionPtr aMgr = ModelAPI_Session::get();
312 DocumentPtr aDoc = aMgr->activeDocument();
313 bool aIsOp = aMgr->isOperation();
315 aMgr->startOperation();
316 aDoc->setCurrentFeature(theFeature, false);
318 aMgr->finishOperation();
321 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
323 bool aCanStart = true;
324 ModuleBase_Operation* aCurrentOp = currentOperation();
326 bool aGranted = aCurrentOp->isGranted(theId);
327 // the started operation is granted for the current one,
328 // e.g. current - Sketch, started - Line
333 if (!isGrantedOperation(theId)) {
334 // the operation is not granted in the current list of operations
335 // e.g. Edit Parameter when Sketch, Line in Sketch is active.
336 aCanStart = abortAllOperations();
338 else if (canStopOperation(aCurrentOp)) {
339 // the started operation is granted in the parrent operation,
340 // e.g. current - Line in Sketch, started Circle
341 if (myIsApplyEnabled && aCurrentOp->isModified())
342 aCurrentOp->commit();
344 abortOperation(aCurrentOp);
353 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
355 ModuleBase_Operation* aCurrentOperation = currentOperation();
356 if (theOperation == aCurrentOperation)
357 theOperation->abort();
359 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
360 // all operation from the current to triggered should also be aborted
361 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
362 while(hasOperation()) {
363 ModuleBase_Operation* aCurrentOperation = currentOperation();
364 aCurrentOperation->abort();
365 if(theOperation == aCurrentOperation)
371 void XGUI_OperationMgr::onCommitOperation()
373 ModuleBase_Operation* anOperation = currentOperation();
375 anOperation->commit();
378 void XGUI_OperationMgr::onAbortOperation()
380 ModuleBase_Operation* aCurrentOperation = currentOperation();
381 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
382 abortOperation(aCurrentOperation);
386 void XGUI_OperationMgr::onBeforeOperationStarted()
388 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
389 if (!aCurrentOperation)
392 /// Set current feature and remeber old current feature
393 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
395 SessionPtr aMgr = ModelAPI_Session::get();
396 DocumentPtr aDoc = aMgr->activeDocument();
397 // the parameter of current feature should be false, we should use all feature, not only visible
398 // in order to correctly save the previous feature of the nested operation, where the
399 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
400 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
401 // When sketch entity operation started, the sketch should be cashed here as the current.
402 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
403 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
404 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
405 aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
406 if (aFOperation->isEditOperation()) // it should be performed by the feature edit only
407 // in create operation, the current feature is changed by addFeature()
408 aDoc->setCurrentFeature(aFOperation->feature(), false);
412 void XGUI_OperationMgr::onOperationStarted()
414 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
415 updateApplyOfOperations(aSenderOperation);
416 emit operationStarted(aSenderOperation);
419 void XGUI_OperationMgr::onBeforeOperationAborted()
421 onBeforeOperationCommitted();
424 void XGUI_OperationMgr::onOperationAborted()
426 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
427 emit operationAborted(aSenderOperation);
430 void XGUI_OperationMgr::onBeforeOperationCommitted()
432 ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
433 if (!aCurrentOperation)
436 /// Restore the previous current feature
437 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
439 if (aFOperation->isEditOperation()) {
440 /// Restore the previous current feature
441 setCurrentFeature(aFOperation->previousCurrentFeature());
443 else { // create operation
444 // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
445 // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
446 // in order to perform it in the current transaction without opening a new one.
447 if (myOperations.front() != aFOperation)
448 setCurrentFeature(aFOperation->previousCurrentFeature());
453 void XGUI_OperationMgr::onOperationCommitted()
455 // apply state for all features from the stack of operations should be updated
456 updateApplyOfOperations();
458 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
459 emit operationCommitted(aSenderOperation);
462 void XGUI_OperationMgr::onOperationResumed()
464 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
465 emit operationResumed(aSenderOperation);
468 void XGUI_OperationMgr::onOperationStopped()
470 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
471 ModuleBase_Operation* aCurrentOperation = currentOperation();
472 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
475 myOperations.removeAll(aCurrentOperation);
476 aCurrentOperation->deleteLater();
478 emit operationStopped(aCurrentOperation);
480 // get last operation which can be resumed
481 ModuleBase_Operation* aResultOp = 0;
482 QListIterator<ModuleBase_Operation*> anIt(myOperations);
484 while (anIt.hasPrevious()) {
485 ModuleBase_Operation* anOp = anIt.previous();
492 bool isModified = aCurrentOperation->isModified();
493 aResultOp->setIsModified(aResultOp->isModified() || isModified);
494 resumeOperation(aResultOp);
495 onValidateOperation();
499 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
501 QObject* aSender = sender();
503 // Let the manager decide what to do with the given key combination.
504 ModuleBase_Operation* anOperation = currentOperation();
505 bool isAccepted = true;
506 switch (theEvent->key()) {
508 case Qt::Key_Enter: {
509 ModuleBase_Operation* aOperation = currentOperation();
510 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
511 ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
512 if (!aActiveWgt || !aActiveWgt->processEnter()) {
513 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
514 if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
515 emit keyEnterReleased();
525 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
527 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
528 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
529 if (!aContext.IsNull()) {
530 Handle(V3d_View) aView = aViewer->activeView();
531 if ((theEvent->key() == Qt::Key_N))
532 aContext->HilightNextDetected(aView);
533 else if ((theEvent->key() == Qt::Key_P))
534 aContext->HilightPreviousDetected(aView);
545 // anOperation->keyReleased(theEvent->key());
550 XGUI_Workshop* XGUI_OperationMgr::workshop() const
552 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
553 return aConnector->workshop();