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