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