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