]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Avoid the ability to cancel the current sketch when saving.
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_OperationMgr.cpp
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "XGUI_OperationMgr.h"
8 #include "XGUI_ModuleConnector.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_ErrorMgr.h"
11 #include "XGUI_Tools.h"
12 #include "XGUI_ObjectsBrowser.h"
13 #include "XGUI_ContextMenuMgr.h"
14
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_ModelWidget.h>
17 #include "ModuleBase_Operation.h"
18 #include "ModuleBase_IWorkshop.h"
19 #include "ModuleBase_IModule.h"
20 #include <ModuleBase_IViewer.h>
21 #include "ModuleBase_OperationDescription.h"
22 #include "ModuleBase_OperationFeature.h"
23 #include "ModuleBase_Tools.h"
24
25 #include "ModelAPI_CompositeFeature.h"
26 #include "ModelAPI_Session.h"
27
28 #include <XGUI_PropertyPanel.h>
29 #include <QToolButton>
30 #include <QLineEdit>
31
32 #include <QMessageBox>
33 #include <QApplication>
34 #include <QKeyEvent>
35
36 //#define DEBUG_CURRENT_FEATURE
37
38 /// Processes "Delete" key event of application. This key is used by several application actions.
39 /// There is a logical order of the actions processing. So the key can not be set for actions
40 /// as a shortcut. The class listens the key event and call operation manager processor.
41 class XGUI_ShortCutListener : public QObject
42 {
43 public:
44   /// Constructor
45   /// \param theParent the parent to be deleted when the parent is deleted
46   /// \param theOperationMgr the class to perform deletion
47   XGUI_ShortCutListener(QObject* theParent, XGUI_OperationMgr* theOperationMgr)
48     : QObject(theParent), myOperationMgr(theOperationMgr)
49   {
50     qApp->installEventFilter(this);
51   }
52   ~XGUI_ShortCutListener() {}
53
54   /// Switch on short cut listener
55   void setActive(const bool theIsActive) { myIsActive = theIsActive; }
56
57   /// Redefinition of virtual function to process Delete key release
58   virtual bool eventFilter(QObject *theObject, QEvent *theEvent)
59   {
60     bool isAccepted = false;
61     if (myIsActive && theEvent->type() == QEvent::KeyRelease) {
62       QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
63       if(aKeyEvent) {
64         switch (aKeyEvent->key()) {
65           case Qt::Key_Delete: {
66             isAccepted = myOperationMgr->onProcessDelete(theObject);
67           }
68         }
69       }
70     }
71     if (!isAccepted)
72       isAccepted = QObject::eventFilter(theObject, theEvent);
73     return isAccepted;
74   }
75
76 private:
77   XGUI_OperationMgr* myOperationMgr; /// processor for key event
78   bool myIsActive; /// boolean state whether the event filter perform own signal processing
79 };
80
81 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent,
82                                      ModuleBase_IWorkshop* theWorkshop)
83 : QObject(theParent), myWorkshop(theWorkshop)
84 {
85   /// we need to install filter to the application in order to react to 'Delete' key button
86   /// this key can not be a short cut for a corresponded action because we need to set
87   /// the actions priority
88   myShortCutListener = new XGUI_ShortCutListener(theParent, this);
89 }
90
91 XGUI_OperationMgr::~XGUI_OperationMgr()
92 {
93 }
94
95 void XGUI_OperationMgr::activate()
96 {
97   myShortCutListener->setActive(true);
98 }
99
100 void XGUI_OperationMgr::deactivate()
101 {
102   myShortCutListener->setActive(false);
103 }
104
105 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
106 {
107   return myOperations.count() > 0 ? myOperations.last() : 0;
108 }
109
110 bool XGUI_OperationMgr::isCurrentOperation(ModuleBase_Operation* theOperation)
111 {
112   if(!hasOperation())
113     return false;
114   return currentOperation() == theOperation;
115 }
116
117 bool XGUI_OperationMgr::hasOperation() const
118 {
119   return !myOperations.isEmpty() && (myOperations.last() != NULL);
120 }
121
122 bool XGUI_OperationMgr::hasOperation(const QString& theId) const
123 {
124   foreach(ModuleBase_Operation* aOp, myOperations) {
125     if (aOp->id() == theId)
126       return true;
127   }
128   return false;
129 }
130
131 ModuleBase_Operation* XGUI_OperationMgr::findOperation(const QString& theId) const
132 {
133   QList<ModuleBase_Operation*>::const_iterator anIt = myOperations.end();
134   while (anIt != myOperations.begin()) {
135     --anIt;
136     ModuleBase_Operation* anOperation = *anIt;
137     if (anOperation->id() == theId)
138       return anOperation;
139   }
140   return 0;
141 }
142
143
144 int XGUI_OperationMgr::operationsCount() const
145 {
146   return myOperations.count();
147 }
148
149 QStringList XGUI_OperationMgr::operationList() const
150 {
151   QStringList result;
152   foreach(ModuleBase_Operation* eachOperation, myOperations) {
153     ModuleBase_OperationFeature* aFOperation =
154       dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
155     if (aFOperation) {
156       FeaturePtr aFeature = aFOperation->feature();
157       if(aFeature) {
158         result << QString::fromStdString(aFeature->getKind());
159       }
160     }
161   }
162   return result;
163 }
164
165 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
166 {
167   int idx = myOperations.lastIndexOf(theOperation);
168   if(idx == -1 || idx == 0) {
169     return NULL;
170   }
171   return myOperations.at(idx - 1);
172 }
173
174 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
175 {
176   bool isAccepted = false;
177   if (theEvent->type() == QEvent::KeyRelease) {
178     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
179     if(aKeyEvent)
180       isAccepted = onKeyReleased(theObject, aKeyEvent);
181   }
182   if (!isAccepted)
183     isAccepted = QObject::eventFilter(theObject, theEvent);
184
185   return isAccepted;
186 }
187
188 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
189 {
190   if (hasOperation())
191     currentOperation()->postpone();
192   myOperations.append(theOperation);
193
194   connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
195   connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
196   connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
197
198   connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
199   connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
200   connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
201
202   connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
203   connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
204
205   bool isStarted = theOperation->start();
206   if (isStarted)
207     onValidateOperation();
208   return isStarted;
209 }
210
211 void XGUI_OperationMgr::onAbortAllOperations()
212 {
213   abortAllOperations();
214 }
215
216 bool XGUI_OperationMgr::abortAllOperations(const XGUI_MessageKind& theMessageKind)
217 {
218   bool aResult = true;
219   if(!hasOperation())
220     return aResult;
221
222   if (operationsCount() == 1) {
223     ModuleBase_Operation* aCurrentOperation = currentOperation();
224     if (canStopOperation(aCurrentOperation, theMessageKind)) {
225       abortOperation(aCurrentOperation);
226     }
227     else
228       aResult = false;
229   }
230   else {
231     if (theMessageKind == XGUI_AbortOperationMessage) {
232       aResult = QMessageBox::question(qApp->activeWindow(),
233                                       tr("Abort operation"),
234                                       tr("All active operations will be aborted."),
235                                       QMessageBox::Ok | QMessageBox::Cancel,
236                                       QMessageBox::Cancel) == QMessageBox::Ok;
237     }
238     else if (theMessageKind == XGUI_InformationMessage) {
239       QString aMessage = tr("Please validate all your active operations before saving.");
240       QMessageBox::question(qApp->activeWindow(),
241                             tr("Validate operation"),
242                             aMessage,
243                             QMessageBox::Ok,
244                             QMessageBox::Ok);
245       aResult = false; // do not perform abort
246     }
247     while(aResult && hasOperation()) {
248       abortOperation(currentOperation());
249     }
250   }
251   return aResult;
252 }
253
254 bool XGUI_OperationMgr::commitAllOperations()
255 {
256   bool isCompositeCommitted = false, anOperationProcessed = false;
257   while (hasOperation()) {
258     ModuleBase_Operation* anOperation = currentOperation();
259     if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled()) {
260       anOperationProcessed = commitOperation();
261     } else {
262       abortOperation(anOperation);
263       anOperationProcessed = true;
264     }
265     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
266                                                                             (anOperation);
267     if (aFOperation) {
268       FeaturePtr aFeature = aFOperation->feature();
269       CompositeFeaturePtr aComposite =
270           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
271       isCompositeCommitted = aComposite.get();
272       if (isCompositeCommitted)
273         break;
274     }
275     // not processed[committed] operation might be used in composite feature,
276     // so the while will be stopped by the previous check.
277     // this code is not necessary, but logically should be done when the processing will not
278     // be done for not composite feature by some reasons
279     if (!anOperationProcessed)
280       break;
281   }
282   return true;
283 }
284
285 void XGUI_OperationMgr::onValidateOperation()
286 {
287   if (!hasOperation())
288     return;
289   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
290                                                                           (currentOperation());
291   if(aFOperation && aFOperation->feature().get())
292     XGUI_Tools::workshop(myWorkshop)->errorMgr()->updateActions(aFOperation->feature());
293 }
294
295 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
296 {
297   XGUI_ErrorMgr* anErrorMgr = XGUI_Tools::workshop(myWorkshop)->errorMgr();
298   if (theOperation) {
299     ModuleBase_OperationFeature* aFOperation =
300       dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
301     if (aFOperation)
302       anErrorMgr->updateAcceptAllAction(aFOperation->feature());
303   }
304   else {
305     foreach(ModuleBase_Operation* anOperation, myOperations) {
306       if (anOperation)
307         updateApplyOfOperations(anOperation);
308     }
309   }
310   // Apply button of the current operation should also be updated
311   onValidateOperation();
312 }
313
314 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation,
315                                          const XGUI_OperationMgr::XGUI_MessageKind& theMessageKind)
316 {
317   //in case of nested (sketch) operation no confirmation needed
318   if (isGrantedOperation(theOperation->id()))
319     return true;
320   if (theOperation && theOperation->isModified()) {
321     if (theMessageKind == XGUI_AbortOperationMessage) {
322       QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
323       int anAnswer = QMessageBox::question(qApp->activeWindow(),
324                                            tr("Abort operation"),
325                                            aMessage,
326                                            QMessageBox::Ok | QMessageBox::Cancel,
327                                            QMessageBox::Cancel);
328       return anAnswer == QMessageBox::Ok;
329     }
330     else if (theMessageKind == XGUI_InformationMessage) {
331       QString aMessage = tr("Please validate your %1 before saving.").arg(theOperation->id());
332       QMessageBox::question(qApp->activeWindow(),
333                             tr("Validate operation"),
334                             aMessage,
335                             QMessageBox::Ok,
336                             QMessageBox::Ok);
337       return false;
338     }
339   }
340   return true;
341 }
342
343 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
344 {
345   theOperation->resume();
346 }
347
348 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
349 {
350   bool isGranted = false;
351
352   QListIterator<ModuleBase_Operation*> anIt(myOperations);
353   anIt.toBack();
354   ModuleBase_Operation* aPreviousOperation = 0;
355   while (anIt.hasPrevious() && !isGranted) {
356     ModuleBase_Operation* anOp = anIt.previous();
357     if (anOp)
358       isGranted = anOp->isGranted(theId);
359   }
360   return isGranted;
361 }
362
363 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
364 {
365   SessionPtr aMgr = ModelAPI_Session::get();
366   DocumentPtr aDoc = aMgr->activeDocument();
367   bool aIsOp = aMgr->isOperation();
368   if (!aIsOp)
369     aMgr->startOperation(QString("Set current feature: %1")
370     .arg(theFeature->getKind().c_str()).toStdString());
371   aDoc->setCurrentFeature(theFeature, false);
372   if (!aIsOp)
373     aMgr->finishOperation();
374 }
375
376 bool XGUI_OperationMgr::canStartOperation(const QString& theId, bool& isCommitted)
377 {
378   bool aCanStart = true;
379   isCommitted = false;
380   ModuleBase_Operation* aCurrentOp = currentOperation();
381   if (aCurrentOp) {
382     bool aGranted = aCurrentOp->isGranted(theId);
383     // the started operation is granted for the current one,
384     // e.g. current - Sketch, started - Line
385     if (aGranted) {
386       aCanStart = true;
387     }
388     else {
389       if (!isGrantedOperation(theId)) {
390         // the operation is not granted in the current list of operations
391         // e.g. Edit Parameter when Sketch, Line in Sketch is active.
392         aCanStart = abortAllOperations();
393       }
394       else if (canStopOperation(aCurrentOp)) {
395         // the started operation is granted in the parrent operation,
396         // e.g. current - Line in Sketch, started Circle
397         stopOperation(aCurrentOp, isCommitted);
398       } else {
399         aCanStart = false;
400       }
401     }
402   }
403   return aCanStart;
404 }
405
406 void XGUI_OperationMgr::stopOperation(ModuleBase_Operation* theOperation, bool& isCommitted)
407 {
408   if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() && theOperation->isModified())
409     isCommitted = theOperation->commit();
410   else {
411     isCommitted = false;
412     abortOperation(theOperation);
413   }
414 }
415
416 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
417 {
418   ModuleBase_Operation* aCurrentOperation = currentOperation();
419   if (theOperation == aCurrentOperation)
420     theOperation->abort();
421   else {
422     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
423     // all operation from the current to triggered should also be aborted
424     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
425     while(hasOperation()) {
426       ModuleBase_Operation* aCurrentOperation = currentOperation();
427       aCurrentOperation->abort();
428       if(theOperation == aCurrentOperation)
429         break;
430     }
431   }
432 }
433
434 bool XGUI_OperationMgr::commitOperation()
435 {
436   bool isCommitted = false;
437   ModuleBase_Operation* anOperation = currentOperation();
438   if (anOperation && myWorkshop->module()->canCommitOperation())
439     isCommitted = anOperation->commit();
440   return isCommitted;
441 }
442
443 void XGUI_OperationMgr::onAbortOperation()
444 {
445   ModuleBase_Operation* aCurrentOperation = currentOperation();
446   if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
447     abortOperation(aCurrentOperation);
448   }
449 }
450
451 void XGUI_OperationMgr::onBeforeOperationStarted()
452 {
453   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
454   if (!aCurrentOperation)
455     return;
456
457   /// Set current feature and remeber old current feature
458   ModuleBase_OperationFeature* aFOperation =
459     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
460   if (aFOperation) {
461     SessionPtr aMgr = ModelAPI_Session::get();
462     DocumentPtr aDoc = aMgr->activeDocument();
463     // the parameter of current feature should be false, we should use all feature, not only
464     // visible in order to correctly save the previous feature of the nested operation, where the
465     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
466     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
467     // When sketch entity operation started, the sketch should be cashed here as the current.
468     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
469     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
470     // As a result the sketch disappears from the viewer.
471     // However after commit it is displayed back.
472     aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
473
474 #ifdef DEBUG_CURRENT_FEATURE
475     FeaturePtr aFeature = aFOperation->feature();
476     QString aKind = aFeature ? aFeature->getKind().c_str() : "";
477     qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
478             .arg(aFOperation->isEditOperation())
479             .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
480
481     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
482             ModuleBase_Tools::objectInfo(
483             ModelAPI_Session::get()->activeDocument()->currentFeature(false)))
484             .toStdString().c_str());
485 #endif
486
487     if (aFOperation->isEditOperation()) {// it should be performed by the feature edit only
488       // in create operation, the current feature is changed by addFeature()
489       aDoc->setCurrentFeature(aFOperation->feature(), false);
490       // this is the only place where flushes must be called after setCurrentFeature for the
491       // current moment: after this the opertion is not finished, so, the ObjectBrowser
492       // state may be corrupted (issue #1457)
493       static Events_Loop* aLoop = Events_Loop::loop();
494       static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
495       aLoop->flush(aCreateEvent);
496       static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
497       aLoop->flush(aDeleteEvent);
498     }
499
500 #ifdef DEBUG_CURRENT_FEATURE
501     qDebug("\tdocument->setCurrentFeature");
502     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
503             ModuleBase_Tools::objectInfo(
504             ModelAPI_Session::get()->activeDocument()->currentFeature(false)))
505             .toStdString().c_str());
506 #endif
507   }
508 }
509
510 void XGUI_OperationMgr::onOperationStarted()
511 {
512   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
513   updateApplyOfOperations(aSenderOperation);
514   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
515   aWorkshop->operationStarted(aSenderOperation);
516 }
517
518 void XGUI_OperationMgr::onBeforeOperationAborted()
519 {
520   onBeforeOperationCommitted();
521 }
522
523 void XGUI_OperationMgr::onOperationAborted()
524 {
525   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
526   emit operationAborted(aSenderOperation);
527 }
528
529 void XGUI_OperationMgr::onBeforeOperationCommitted()
530 {
531   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
532   if (!aCurrentOperation)
533     return;
534
535   /// Restore the previous current feature
536   ModuleBase_OperationFeature* aFOperation =
537     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
538   if (aFOperation) {
539 #ifdef DEBUG_CURRENT_FEATURE
540     QString aKind = aFOperation->feature()->getKind().c_str();
541     qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
542             .arg(aFOperation->isEditOperation())
543             .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
544
545     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
546             ModuleBase_Tools::objectInfo(
547             ModelAPI_Session::get()->activeDocument()->currentFeature(false)))
548             .toStdString().c_str());
549 #endif
550
551     if (aFOperation->isEditOperation()) {
552       /// Restore the previous current feature
553       setCurrentFeature(aFOperation->previousCurrentFeature());
554     }
555     else { // create operation
556       // the Top created feature should stays the current. In nested operations,
557       // like Line in the Sketch or
558       // Sketch in ExtrusionCut, a previous feature should be restored on commit.
559       // It is performed here
560       // in order to perform it in the current transaction without opening a new one.
561       if (myOperations.front() != aFOperation)
562         setCurrentFeature(aFOperation->previousCurrentFeature());
563     }
564 #ifdef DEBUG_CURRENT_FEATURE
565     qDebug("\tdocument->setCurrentFeature");
566     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
567            ModuleBase_Tools::objectInfo(
568            ModelAPI_Session::get()->activeDocument()->currentFeature(false)))
569            .toStdString().c_str());
570 #endif
571     ModuleBase_IModule* aModule = myWorkshop->module();
572     if (aModule)
573       aModule->beforeOperationStopped(aFOperation);
574   }
575 }
576
577 void XGUI_OperationMgr::onOperationCommitted()
578 {
579   // apply state for all features from the stack of operations should be updated
580   updateApplyOfOperations();
581
582   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
583   emit operationCommitted(aSenderOperation);
584 }
585
586 void XGUI_OperationMgr::onOperationResumed()
587 {
588   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
589   emit operationResumed(aSenderOperation);
590 }
591
592 void XGUI_OperationMgr::onOperationStopped()
593 {
594   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
595   ModuleBase_Operation* aCurrentOperation = currentOperation();
596   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
597     return;
598
599   myOperations.removeAll(aCurrentOperation);
600   aCurrentOperation->deleteLater();
601
602   emit operationStopped(aCurrentOperation);
603
604   // get last operation which can be resumed
605   ModuleBase_Operation* aResultOp = 0;
606   QListIterator<ModuleBase_Operation*> anIt(myOperations);
607   anIt.toBack();
608   while (anIt.hasPrevious()) {
609     ModuleBase_Operation* anOp = anIt.previous();
610     if (anOp) {
611       aResultOp = anOp;
612       break;
613     }
614   }
615   if (aResultOp) {
616     bool isModified = aCurrentOperation->isModified();
617     aResultOp->setIsModified(aResultOp->isModified() || isModified);
618     resumeOperation(aResultOp);
619     onValidateOperation();
620   }
621 }
622
623 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
624 {
625   // Let the manager decide what to do with the given key combination.
626   ModuleBase_Operation* anOperation = currentOperation();
627   bool isAccepted = false;
628   switch (theEvent->key()) {
629     case Qt::Key_Return:
630     case Qt::Key_Enter: {
631       isAccepted = onProcessEnter(theObject);
632     }
633     break;
634     case Qt::Key_N:
635     case Qt::Key_P: {
636       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
637       if (noModifiers) {
638         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
639         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
640         if (!aContext.IsNull()) {
641           Handle(V3d_View) aView = aViewer->activeView();
642           if ((theEvent->key() == Qt::Key_N))
643             aContext->HilightNextDetected(aView);
644           else if ((theEvent->key() == Qt::Key_P))
645             aContext->HilightPreviousDetected(aView);
646         }
647       }
648     }
649     break;
650     break;
651     default:
652       isAccepted = false;
653       break;
654   }
655   //if(anOperation) {
656   //  anOperation->keyReleased(theEvent->key());
657   //}
658   return isAccepted;
659 }
660
661 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
662 {
663   bool isAccepted = false;
664   ModuleBase_Operation* aOperation = currentOperation();
665   // to avoid enter processing when operation has not been started yet
666   if (!aOperation)
667     return isAccepted;
668   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
669   // only property panel enter is processed in order to do not process enter in application dialogs
670   bool isPPChild = isChildObject(theObject, aPanel);
671   if (!isPPChild)
672     return isAccepted;
673
674   ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
675   bool isAborted = false;
676   if (!anActiveWgt) {
677     QWidget* aFocusWidget = aPanel->focusWidget();
678     QToolButton* aCancelBtn =
679       dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
680     if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
681       abortOperation(aOperation);
682       isAccepted = true;
683       isAborted = true;
684     }
685   }
686   if (!isAborted) {
687     isAccepted = anActiveWgt && anActiveWgt->processEnter();
688     if (!isAccepted) {
689       isAccepted =
690         myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
691       if (!isAccepted) {
692         /// functionality is similar to Apply click
693         ModuleBase_OperationFeature* aFOperation =
694           dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
695         if (!aFOperation ||
696             myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
697           // key released is emitted to apply the current value to the model
698           // if it was modified in PP
699           emit keyEnterReleased();
700           commitOperation();
701           isAccepted = true;
702         }
703         else
704           isAccepted = false;
705       }
706     }
707   }
708   return isAccepted;
709 }
710
711 bool editorControl(QObject* theObject)
712 {
713   QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>(theObject);
714   return aLineEdit;
715 }
716
717 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
718 {
719   bool isAccepted = false;
720   ModuleBase_Operation* aOperation = currentOperation();
721   ModuleBase_ModelWidget* anActiveWgt = 0;
722   // firstly the widget should process Delete action
723   ModuleBase_IPropertyPanel* aPanel;
724   bool isPPChildObject = false;
725   if (aOperation) {
726     aPanel = aOperation->propertyPanel();
727     if (aPanel) {
728       isPPChildObject = isChildObject(theObject, aPanel);
729       // process delete in active widget only if delete sender is child of property panel
730       // it is necessary for the case when OB is shown, user perform selection and click Delete
731       if (isPPChildObject) {
732         anActiveWgt = aPanel->activeWidget();
733         if (anActiveWgt) {
734           isAccepted = anActiveWgt->processDelete();
735         }
736       }
737     }
738   }
739   if (!isAccepted) {
740     // after widget, object browser and viewer should process delete
741     /// other widgets such as line edit controls should not lead to
742     /// processing delete by workshop
743     XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
744     QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
745     bool isToDeleteObject = false;
746     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
747     XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
748     if (theObject == aBrowser->treeView()) {
749       aContextMenuMgr->updateObjectBrowserMenu();
750       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
751     }
752     else if (isChildObject(theObject, aViewPort)) {
753       aContextMenuMgr->updateViewerMenu();
754       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
755     }
756     else if (isPPChildObject) {
757       // property panel child object is processed to process delete performed on Apply button of PP
758       isToDeleteObject = true;
759     }
760     else if (editorControl(theObject)) {
761       isToDeleteObject = false; /// Line Edit of Rename operation in ObjectBrowser
762       isAccepted = true;
763     }
764
765     if (isToDeleteObject) {
766       aWorkshop->deleteObjects();
767       isAccepted = true;
768     }
769   }
770
771   return isAccepted;
772 }
773
774 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
775 {
776   bool isPPChild = false;
777   if (theParent && theObject) {
778     QObject* aParent = (QObject*)theObject;
779     while (aParent ) {
780       isPPChild = aParent == theParent;
781       if (isPPChild)
782         break;
783       aParent = aParent->parent();
784     }
785   }
786   return isPPChild;
787 }