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