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