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_IViewer.h>
13 #include "ModuleBase_OperationDescription.h"
14 #include "ModuleBase_OperationFeature.h"
16 #include "ModelAPI_CompositeFeature.h"
17 #include "ModelAPI_Session.h"
19 #include <QMessageBox>
20 #include <QApplication>
23 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
24 ModuleBase_IWorkshop* theWorkshop)
25 : QObject(theParent), myIsValidationLock(false), myIsApplyEnabled(false),
26 myWorkshop(theWorkshop)
30 XGUI_OperationMgr::~XGUI_OperationMgr()
34 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
36 return myOperations.count() > 0 ? myOperations.last() : 0;
39 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
43 return currentOperation() == theOperation;
46 bool XGUI_OperationMgr::hasOperation() const
48 return !myOperations.isEmpty() && (myOperations.last() != NULL);
51 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
53 foreach(ModuleBase_Operation* aOp, myOperations) {
54 if (aOp->id() == theId)
60 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
62 foreach(ModuleBase_Operation* aOp, myOperations) {
63 if (aOp->id() == theId)
70 int XGUI_OperationMgr::operationsCount() const
72 return myOperations.count();
75 QStringList XGUI_OperationMgr::operationList() const
78 foreach(ModuleBase_Operation* eachOperation, myOperations) {
79 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
81 FeaturePtr aFeature = aFOperation->feature();
83 result << QString::fromStdString(aFeature->getKind());
90 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
92 int idx = myOperations.lastIndexOf(theOperation);
93 if(idx == -1 || idx == 0) {
96 return myOperations.at(idx - 1);
99 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
101 if (theEvent->type() == QEvent::KeyRelease) {
102 QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
104 return onKeyReleased(aKeyEvent);
107 return QObject::eventFilter(theObject, theEvent);
110 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
113 currentOperation()->postpone();
114 myOperations.append(theOperation);
116 connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
117 connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
118 connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
119 connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
120 connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
121 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
124 connect(aFOperation, SIGNAL(activatedByPreselection()),
125 SIGNAL(operationActivatedByPreselection()));
127 theOperation->start();
128 onValidateOperation();
132 bool XGUI_OperationMgr::abortAllOperations()
138 if (operationsCount() == 1) {
139 ModuleBase_Operation* aCurrentOperation = currentOperation();
140 if (canStopOperation(aCurrentOperation)) {
141 abortOperation(aCurrentOperation);
147 aResult = QMessageBox::question(qApp->activeWindow(),
148 tr("Abort operation"),
149 tr("All active operations will be aborted."),
150 QMessageBox::Ok | QMessageBox::Cancel,
151 QMessageBox::Cancel) == QMessageBox::Ok;
152 while(aResult && hasOperation()) {
153 abortOperation(currentOperation());
159 bool XGUI_OperationMgr::commitAllOperations()
161 bool isCompositeCommitted = false;
162 while (hasOperation()) {
163 ModuleBase_Operation* anOperation = currentOperation();
164 if (isApplyEnabled()) {
167 abortOperation(anOperation);
169 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
172 FeaturePtr aFeature = aFOperation->feature();
173 CompositeFeaturePtr aComposite =
174 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
175 isCompositeCommitted = aComposite.get();
176 if (isCompositeCommitted)
183 void XGUI_OperationMgr::onValidateOperation()
187 ModuleBase_Operation* anOperation = currentOperation();
189 bool aCanCommit = myWorkshop->module()->canCommitOperation();
190 setApplyEnabled(!myIsValidationLock && aCanCommit && anOperation->isValid());
194 void XGUI_OperationMgr::setLockValidating(bool toLock)
196 myIsValidationLock = toLock;
197 onValidateOperation();
200 void XGUI_OperationMgr::setApplyEnabled(const bool theEnabled)
202 myIsApplyEnabled = theEnabled;
203 emit validationStateChanged(theEnabled);
206 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
209 emit nestedStateChanged(theOperation->getDescription()->operationId().toStdString(),
210 theOperation->isValid());
212 foreach(ModuleBase_Operation* anOperation, myOperations) {
213 emit nestedStateChanged(anOperation->getDescription()->operationId().toStdString(),
214 anOperation->isValid());
219 bool XGUI_OperationMgr::isApplyEnabled() const
221 return myIsApplyEnabled;
224 bool XGUI_OperationMgr::isParentOperationValid() const
226 bool isValid = false;
227 // the enable state of the parent operation of the nested one is defined by the rules that
228 // firstly there are nested operations and secondly the parent operation is valid
229 ModuleBase_Operation* aPrevOp = 0;
230 Operations::const_iterator anIt = myOperations.end();
231 if (anIt != myOperations.begin()) { // there are items in the operations list
233 aPrevOp = *anIt; // the last top operation, the operation which is started
234 if (anIt != myOperations.begin()) { // find the operation where the started operation is nested
239 return aPrevOp && aPrevOp->isValid();
242 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
244 //in case of nested (sketch) operation no confirmation needed
245 if (isGrantedOperation(theOperation))
247 if (theOperation && theOperation->isModified()) {
248 QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
249 int anAnswer = QMessageBox::question(qApp->activeWindow(),
250 tr("Abort operation"),
252 QMessageBox::Ok | QMessageBox::Cancel,
253 QMessageBox::Cancel);
254 return anAnswer == QMessageBox::Ok;
259 bool XGUI_OperationMgr::commitOperation()
261 if (hasOperation() && currentOperation()->isValid()) {
268 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
270 theOperation->resume();
273 bool XGUI_OperationMgr::isGrantedOperation(ModuleBase_Operation* theOperation)
275 bool isGranted = false;
277 QListIterator<ModuleBase_Operation*> anIt(myOperations);
279 ModuleBase_Operation* aPreviousOperation = 0;
280 while (anIt.hasPrevious()) {
281 ModuleBase_Operation* anOp = anIt.previous();
282 if (anOp == theOperation) {
283 if (anIt.hasPrevious())
284 aPreviousOperation = anIt.previous();
288 if (aPreviousOperation)
289 isGranted = aPreviousOperation->isGranted(theOperation->id());
294 bool XGUI_OperationMgr::canStartOperation(const QString& theId, const bool isAdditionallyGranted)
296 bool aCanStart = true;
297 ModuleBase_Operation* aCurrentOp = currentOperation();
299 bool aGranted = aCurrentOp->isGranted(theId) || isAdditionallyGranted;
301 if (canStopOperation(aCurrentOp)) {
302 if (myIsApplyEnabled && aCurrentOp->isModified())
303 aCurrentOp->commit();
305 abortOperation(aCurrentOp);
314 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
316 ModuleBase_Operation* aCurrentOperation = currentOperation();
317 if (theOperation == aCurrentOperation)
318 theOperation->abort();
320 // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
321 // all operation from the current to triggered should also be aborted
322 // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
323 while(hasOperation()) {
324 ModuleBase_Operation* aCurrentOperation = currentOperation();
325 aCurrentOperation->abort();
326 if(theOperation == aCurrentOperation)
332 void XGUI_OperationMgr::onCommitOperation()
334 ModuleBase_Operation* anOperation = currentOperation();
336 anOperation->commit();
339 void XGUI_OperationMgr::onAbortOperation()
341 ModuleBase_Operation* aCurrentOperation = currentOperation();
342 if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
343 abortOperation(aCurrentOperation);
347 void XGUI_OperationMgr::onOperationStarted()
349 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
350 updateApplyOfOperations(aSenderOperation);
351 emit operationStarted(aSenderOperation);
354 void XGUI_OperationMgr::onOperationAborted()
356 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
357 emit operationAborted(aSenderOperation);
360 void XGUI_OperationMgr::onOperationCommitted()
362 updateApplyOfOperations();
364 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
365 emit operationCommitted(aSenderOperation);
368 void XGUI_OperationMgr::onOperationResumed()
370 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
371 emit operationResumed(aSenderOperation);
374 void XGUI_OperationMgr::onOperationStopped()
376 ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
377 ModuleBase_Operation* aCurrentOperation = currentOperation();
378 if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
381 myOperations.removeAll(aCurrentOperation);
382 aCurrentOperation->deleteLater();
384 emit operationStopped(aCurrentOperation);
386 // get last operation which can be resumed
387 ModuleBase_Operation* aResultOp = 0;
388 QListIterator<ModuleBase_Operation*> anIt(myOperations);
390 while (anIt.hasPrevious()) {
391 ModuleBase_Operation* anOp = anIt.previous();
398 bool isModified = aCurrentOperation->isModified();
399 aResultOp->setIsModified(aResultOp->isModified() || isModified);
400 resumeOperation(aResultOp);
401 onValidateOperation();
405 bool XGUI_OperationMgr::onKeyReleased(QKeyEvent* theEvent)
407 // Let the manager decide what to do with the given key combination.
408 ModuleBase_Operation* anOperation = currentOperation();
409 bool isAccepted = true;
410 switch (theEvent->key()) {
412 case Qt::Key_Enter: {
413 emit keyEnterReleased();
418 bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
420 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
421 Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
422 Handle(V3d_View) aView = aViewer->activeView();
423 if ((theEvent->key() == Qt::Key_N))
424 aContext->HilightNextDetected(aView);
425 else if ((theEvent->key() == Qt::Key_P))
426 aContext->HilightPreviousDetected(aView);
436 // anOperation->keyReleased(theEvent->key());