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