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