Salome HOME
Merge branch 'master' of salome:modules/shaper
[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 = dynamic_cast<ModuleBase_OperationFeature*>(eachOperation);
154     if (aFOperation) {
155       FeaturePtr aFeature = aFOperation->feature();
156       if(aFeature) {
157         result << QString::fromStdString(aFeature->getKind());
158       }
159     }
160   }
161   return result;
162 }
163
164 ModuleBase_Operation* XGUI_OperationMgr::previousOperation(ModuleBase_Operation* theOperation) const
165 {
166   int idx = myOperations.lastIndexOf(theOperation);
167   if(idx == -1 || idx == 0) {
168     return NULL;
169   }
170   return myOperations.at(idx - 1);
171 }
172
173 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
174 {
175   bool isAccepted = false;
176   if (theEvent->type() == QEvent::KeyRelease) {
177     QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
178     if(aKeyEvent)
179       isAccepted = onKeyReleased(theObject, aKeyEvent);
180   }
181   if (!isAccepted)
182     isAccepted = QObject::eventFilter(theObject, theEvent);
183
184   return isAccepted;
185 }
186
187 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
188 {
189   if (hasOperation())
190     currentOperation()->postpone();
191   myOperations.append(theOperation);
192
193   connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
194   connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
195   connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
196
197   connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
198   connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
199   connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
200
201   connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
202   connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
203
204   bool isStarted = theOperation->start();
205   if (isStarted)
206     onValidateOperation();
207   return isStarted;
208 }
209
210 bool XGUI_OperationMgr::abortAllOperations()
211 {
212   bool aResult = true;
213   if(!hasOperation())
214     return aResult;
215
216   if (operationsCount() == 1) {
217     ModuleBase_Operation* aCurrentOperation = currentOperation();
218     if (canStopOperation(aCurrentOperation)) {
219       abortOperation(aCurrentOperation);
220     }
221     else
222       aResult = false;
223   }
224   else {
225     aResult = QMessageBox::question(qApp->activeWindow(),
226                                     tr("Abort operation"),
227                                     tr("All active operations will be aborted."),
228                                     QMessageBox::Ok | QMessageBox::Cancel,
229                                     QMessageBox::Cancel) == QMessageBox::Ok;
230     while(aResult && hasOperation()) {
231       abortOperation(currentOperation());
232     }
233   }
234   return aResult;
235 }
236
237 bool XGUI_OperationMgr::commitAllOperations()
238 {
239   bool isCompositeCommitted = false, anOperationProcessed = false;
240   while (hasOperation()) {
241     ModuleBase_Operation* anOperation = currentOperation();
242     if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled()) {
243       anOperationProcessed = onCommitOperation();
244     } else {
245       abortOperation(anOperation);
246       anOperationProcessed = true;
247     }
248     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
249                                                                             (anOperation);
250     if (aFOperation) {
251       FeaturePtr aFeature = aFOperation->feature();
252       CompositeFeaturePtr aComposite = 
253           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
254       isCompositeCommitted = aComposite.get();
255       if (isCompositeCommitted)
256         break;
257     }
258     // not processed[committed] operation might be used in composite feature,
259     // so the while will be stopped by the previous check.
260     // this code is not necessary, but logically should be done when the processing will not
261     // be done for not composite feature by some reasons
262     if (!anOperationProcessed)
263       break;
264   }
265   return true;
266 }
267
268 void XGUI_OperationMgr::onValidateOperation()
269 {
270   if (!hasOperation())
271     return;
272   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
273                                                                           (currentOperation());
274   if(aFOperation && aFOperation->feature().get())
275     XGUI_Tools::workshop(myWorkshop)->errorMgr()->updateActions(aFOperation->feature());
276 }
277
278 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
279 {
280   XGUI_ErrorMgr* anErrorMgr = XGUI_Tools::workshop(myWorkshop)->errorMgr();
281   if (theOperation) {
282     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
283     if (aFOperation)
284       anErrorMgr->updateAcceptAllAction(aFOperation->feature());
285   }
286   else {
287     foreach(ModuleBase_Operation* anOperation, myOperations) {
288       if (anOperation)
289         updateApplyOfOperations(anOperation);
290     }
291   }
292   // Apply button of the current operation should also be updated
293   onValidateOperation();
294 }
295
296 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation)
297 {
298   //in case of nested (sketch) operation no confirmation needed
299   if (isGrantedOperation(theOperation->id()))
300     return true;
301   if (theOperation && theOperation->isModified()) {
302     QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
303     int anAnswer = QMessageBox::question(qApp->activeWindow(),
304                                          tr("Abort operation"),
305                                          aMessage,
306                                          QMessageBox::Ok | QMessageBox::Cancel,
307                                          QMessageBox::Cancel);
308     return anAnswer == QMessageBox::Ok;
309   }
310   return true;
311 }
312
313 bool XGUI_OperationMgr::commitOperation()
314 {
315   //if (hasOperation() && currentOperation()->isValid()) {
316   //  onCommitOperation();
317   //  return true;
318   //}
319   //return false;
320   return onCommitOperation();
321 }
322
323 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
324 {
325   theOperation->resume();
326 }
327
328 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
329 {
330   bool isGranted = false;
331
332   QListIterator<ModuleBase_Operation*> anIt(myOperations);
333   anIt.toBack();
334   ModuleBase_Operation* aPreviousOperation = 0;
335   while (anIt.hasPrevious() && !isGranted) {
336     ModuleBase_Operation* anOp = anIt.previous();
337     if (anOp)
338       isGranted = anOp->isGranted(theId);
339   }
340   return isGranted;
341 }
342
343 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
344 {
345   SessionPtr aMgr = ModelAPI_Session::get();
346   DocumentPtr aDoc = aMgr->activeDocument();
347   bool aIsOp = aMgr->isOperation();
348   if (!aIsOp)
349     aMgr->startOperation(QString("Set current feature: %1").arg(theFeature->getKind().c_str()).toStdString());
350   aDoc->setCurrentFeature(theFeature, false);
351   if (!aIsOp)
352     aMgr->finishOperation();
353 }
354
355 bool XGUI_OperationMgr::canStartOperation(const QString& theId)
356 {
357   bool aCanStart = true;
358   ModuleBase_Operation* aCurrentOp = currentOperation();
359   if (aCurrentOp) {
360     bool aGranted = aCurrentOp->isGranted(theId);
361     // the started operation is granted for the current one,
362     // e.g. current - Sketch, started - Line
363     if (aGranted) {
364       aCanStart = true;
365     }
366     else {
367       if (!isGrantedOperation(theId)) {
368         // the operation is not granted in the current list of operations
369         // e.g. Edit Parameter when Sketch, Line in Sketch is active.
370         aCanStart = abortAllOperations();
371       }
372       else if (canStopOperation(aCurrentOp)) {
373         // the started operation is granted in the parrent operation,
374         // e.g. current - Line in Sketch, started Circle 
375         stopOperation(aCurrentOp);
376       } else {
377         aCanStart = false;
378       }
379     }
380   }
381   return aCanStart;
382 }
383
384 void XGUI_OperationMgr::stopOperation(ModuleBase_Operation* theOperation)
385 {
386   if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() && theOperation->isModified())
387     theOperation->commit();
388   else
389     abortOperation(theOperation);
390 }
391
392 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
393 {
394   ModuleBase_Operation* aCurrentOperation = currentOperation();
395   if (theOperation == aCurrentOperation)
396     theOperation->abort();
397   else {
398     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
399     // all operation from the current to triggered should also be aborted
400     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
401     while(hasOperation()) {
402       ModuleBase_Operation* aCurrentOperation = currentOperation();
403       aCurrentOperation->abort();
404       if(theOperation == aCurrentOperation)
405         break;
406     }
407   }
408 }
409
410 bool XGUI_OperationMgr::onCommitOperation()
411 {
412   bool isCommitted = false;
413   ModuleBase_Operation* anOperation = currentOperation();
414   if (anOperation && myWorkshop->module()->canCommitOperation())
415     isCommitted = anOperation->commit();
416   return isCommitted;
417 }
418
419 void XGUI_OperationMgr::onAbortOperation()
420 {
421   ModuleBase_Operation* aCurrentOperation = currentOperation();
422   if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
423     abortOperation(aCurrentOperation);
424   }
425 }
426
427 void XGUI_OperationMgr::onBeforeOperationStarted()
428 {
429   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
430   if (!aCurrentOperation)
431     return;
432
433   /// Set current feature and remeber old current feature
434   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
435   if (aFOperation) {
436     SessionPtr aMgr = ModelAPI_Session::get();
437     DocumentPtr aDoc = aMgr->activeDocument();
438     // the parameter of current feature should be false, we should use all feature, not only visible
439     // in order to correctly save the previous feature of the nested operation, where the
440     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
441     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
442     // When sketch entity operation started, the sketch should be cashed here as the current.
443     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
444     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
445     // As a result the sketch disappears from the viewer. However after commit it is displayed back.
446     aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
447
448 #ifdef DEBUG_CURRENT_FEATURE
449     FeaturePtr aFeature = aFOperation->feature();
450     QString aKind = aFeature ? aFeature->getKind().c_str() : "";
451     qDebug(QString("onBeforeOperationStarted(), edit operation = %1, feature = %2")
452             .arg(aFOperation->isEditOperation())
453             .arg(ModuleBase_Tools::objectInfo(aFeature)).toStdString().c_str());
454
455     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
456             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
457 #endif
458
459     if (aFOperation->isEditOperation()) {// it should be performed by the feature edit only
460       // in create operation, the current feature is changed by addFeature()
461       aDoc->setCurrentFeature(aFOperation->feature(), false);
462       // this is the only place where flushes must be called after setCurrentFeature for the current
463       // moment: after this the opertion is not finished, so, the ObjectBrowser state may be corrupted
464       // (issue #1457)
465       static Events_Loop* aLoop = Events_Loop::loop();
466       static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
467       aLoop->flush(aCreateEvent);
468       static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
469       aLoop->flush(aDeleteEvent);
470     }
471
472 #ifdef DEBUG_CURRENT_FEATURE
473     qDebug("\tdocument->setCurrentFeature");
474     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
475             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
476 #endif
477   }
478 }
479
480 void XGUI_OperationMgr::onOperationStarted()
481 {
482   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
483   updateApplyOfOperations(aSenderOperation);
484   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
485   aWorkshop->operationStarted(aSenderOperation);
486 }
487
488 void XGUI_OperationMgr::onBeforeOperationAborted()
489 {
490   onBeforeOperationCommitted();
491 }
492
493 void XGUI_OperationMgr::onOperationAborted()
494 {
495   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
496   emit operationAborted(aSenderOperation);
497 }
498
499 void XGUI_OperationMgr::onBeforeOperationCommitted()
500 {
501   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
502   if (!aCurrentOperation)
503     return;
504
505   /// Restore the previous current feature
506   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
507   if (aFOperation) {
508 #ifdef DEBUG_CURRENT_FEATURE
509     QString aKind = aFOperation->feature()->getKind().c_str();
510     qDebug(QString("onBeforeOperationCommitted(), edit operation = %1, feature = %2")
511             .arg(aFOperation->isEditOperation())
512             .arg(ModuleBase_Tools::objectInfo(aFOperation->feature())).toStdString().c_str());
513
514     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
515             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
516 #endif
517
518     if (aFOperation->isEditOperation()) {
519       /// Restore the previous current feature
520       setCurrentFeature(aFOperation->previousCurrentFeature());
521     }
522     else { // create operation
523       // the Top created feature should stays the current. In nested operations, like Line in the Sketch or
524       // Sketch in ExtrusionCut, a previous feature should be restored on commit. It is performed here
525       // in order to perform it in the current transaction without opening a new one.
526       if (myOperations.front() != aFOperation)
527         setCurrentFeature(aFOperation->previousCurrentFeature());
528     }
529 #ifdef DEBUG_CURRENT_FEATURE
530     qDebug("\tdocument->setCurrentFeature");
531     qDebug(QString("\tdocument->currentFeature(false) = %1").arg(
532             ModuleBase_Tools::objectInfo(ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
533 #endif
534     ModuleBase_IModule* aModule = myWorkshop->module();
535     if (aModule)
536       aModule->beforeOperationStopped(aFOperation);
537   }
538 }
539
540 void XGUI_OperationMgr::onOperationCommitted()
541 {
542   // apply state for all features from the stack of operations should be updated
543   updateApplyOfOperations();
544
545   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
546   emit operationCommitted(aSenderOperation);
547 }
548
549 void XGUI_OperationMgr::onOperationResumed()
550 {
551   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
552   emit operationResumed(aSenderOperation);
553 }
554
555 void XGUI_OperationMgr::onOperationStopped()
556 {
557   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
558   ModuleBase_Operation* aCurrentOperation = currentOperation();
559   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
560     return;
561
562   myOperations.removeAll(aCurrentOperation);
563   aCurrentOperation->deleteLater();
564
565   emit operationStopped(aCurrentOperation);
566
567   // get last operation which can be resumed
568   ModuleBase_Operation* aResultOp = 0;
569   QListIterator<ModuleBase_Operation*> anIt(myOperations);
570   anIt.toBack();
571   while (anIt.hasPrevious()) {
572     ModuleBase_Operation* anOp = anIt.previous();
573     if (anOp) {
574       aResultOp = anOp;
575       break;
576     }
577   }
578   if (aResultOp) {
579     bool isModified = aCurrentOperation->isModified();
580     aResultOp->setIsModified(aResultOp->isModified() || isModified);
581     resumeOperation(aResultOp);
582     onValidateOperation();
583   }
584 }
585
586 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
587 {
588   // Let the manager decide what to do with the given key combination.
589   ModuleBase_Operation* anOperation = currentOperation();
590   bool isAccepted = false;
591   switch (theEvent->key()) {
592     case Qt::Key_Return:
593     case Qt::Key_Enter: {
594       isAccepted = onProcessEnter(theObject);
595     }
596     break;
597     case Qt::Key_N:
598     case Qt::Key_P: {
599       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
600       if (noModifiers) {
601         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
602         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
603         if (!aContext.IsNull()) {
604           Handle(V3d_View) aView = aViewer->activeView();
605           if ((theEvent->key() == Qt::Key_N))
606             aContext->HilightNextDetected(aView);
607           else if ((theEvent->key() == Qt::Key_P))
608             aContext->HilightPreviousDetected(aView);
609         }
610       }
611     }
612     break;
613     break;
614     default:
615       isAccepted = false;
616       break;
617   }
618   //if(anOperation) {
619   //  anOperation->keyReleased(theEvent->key());
620   //}
621   return isAccepted;
622 }
623
624 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
625 {
626   bool isAccepted = false;
627   ModuleBase_Operation* aOperation = currentOperation();
628   // to avoid enter processing when operation has not been started yet
629   if (!aOperation)
630     return isAccepted;
631   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
632   // only property panel enter is processed in order to do not process enter in application dialogs
633   bool isPPChild = isChildObject(theObject, aPanel);
634   if (!isPPChild)
635     return isAccepted;
636
637   ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
638   bool isAborted = false;
639   if (!anActiveWgt) {
640     QWidget* aFocusWidget = aPanel->focusWidget();
641     QToolButton* aCancelBtn = dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
642     if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
643       abortOperation(aOperation);
644       isAccepted = true;
645       isAborted = true;
646     }
647   }
648   if (!isAborted) {
649     isAccepted = anActiveWgt && anActiveWgt->processEnter();
650     if (!isAccepted) {
651       isAccepted = myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
652       if (!isAccepted) {
653         /// functionality is similar to Apply click
654         ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
655         if (!aFOperation || myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
656           // key released is emitted to apply the current value to the model if it was modified in PP
657           emit keyEnterReleased();
658           commitOperation();
659           isAccepted = true;
660         }
661         else
662           isAccepted = false;
663       }
664     }
665   }
666   return isAccepted;
667 }
668
669 bool editorControl(QObject* theObject)
670 {
671   QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>(theObject);
672   return aLineEdit;
673 }
674
675 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
676 {
677   bool isAccepted = false;
678   ModuleBase_Operation* aOperation = currentOperation();
679   ModuleBase_ModelWidget* anActiveWgt = 0;
680   // firstly the widget should process Delete action
681   ModuleBase_IPropertyPanel* aPanel;
682   bool isPPChildObject = false;
683   if (aOperation) {
684     aPanel = aOperation->propertyPanel();
685     if (aPanel) {
686       isPPChildObject = isChildObject(theObject, aPanel);
687       // process delete in active widget only if delete sender is child of property panel
688       // it is necessary for the case when OB is shown, user perform selection and click Delete
689       if (isPPChildObject) {
690         anActiveWgt = aPanel->activeWidget();
691         if (anActiveWgt) {
692           isAccepted = anActiveWgt->processDelete();
693         }
694       }
695     }
696   }
697   if (!isAccepted) {
698     // after widget, object browser and viewer should process delete
699     /// other widgets such as line edit controls should not lead to
700     /// processing delete by workshop
701     XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
702     QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
703     bool isToDeleteObject = false;
704     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
705     XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
706     if (theObject == aBrowser->treeView()) {
707       aContextMenuMgr->updateObjectBrowserMenu();
708       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
709     }
710     else if (isChildObject(theObject, aViewPort)) {
711       aContextMenuMgr->updateViewerMenu();
712       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
713     }
714     else if (isPPChildObject) {
715       // property panel child object is processed to process delete performed on Apply button of PP
716       isToDeleteObject = true;
717     }
718     else if (editorControl(theObject)) {
719       isToDeleteObject = false; /// Line Edit of Rename operation in ObjectBrowser
720       isAccepted = true;
721     }
722
723     if (isToDeleteObject) {
724       aWorkshop->deleteObjects();
725       isAccepted = true;
726     }
727   }
728
729   return isAccepted;
730 }
731
732 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
733 {
734   bool isPPChild = false;
735   if (theParent && theObject) {
736     QObject* aParent = (QObject*)theObject;
737     while (aParent ) {
738       isPPChild = aParent == theParent;
739       if (isPPChild)
740         break;
741       aParent = aParent->parent();
742     }
743   }
744   return isPPChild;
745 }