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