Salome HOME
Prevent SIGSEGV in Save in case dump study failed.
[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             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), myActiveMessageBox(0), mySHIFTPressed(false)
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 && aOp->feature())
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   while (anIt.hasPrevious() && !isGranted) {
400     ModuleBase_Operation* anOp = anIt.previous();
401     if (anOp)
402       isGranted = anOp->isGranted(theId);
403   }
404   return isGranted;
405 }
406
407 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
408 {
409   SessionPtr aMgr = ModelAPI_Session::get();
410   DocumentPtr aDoc = aMgr->activeDocument();
411   bool aIsOp = aMgr->isOperation();
412   if (!aIsOp)
413     aMgr->startOperation(QString("Set current feature: %1")
414     .arg(theFeature->getKind().c_str()).toStdString());
415   aDoc->setCurrentFeature(theFeature, false);
416 #ifdef DEBUG_CURRENT_FEATURE
417   qDebug(QString("   document->setCurrentFeature(false) = %1    SET").arg(
418          ModuleBase_Tools::objectName(
419          ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
420 #endif
421
422   if (!aIsOp)
423     aMgr->finishOperation();
424 }
425
426 bool XGUI_OperationMgr::canStartOperation(const QString& theId, bool& isCommitted)
427 {
428   bool aCanStart = true;
429   isCommitted = false;
430   ModuleBase_Operation* aCurrentOp = currentOperation();
431   if (aCurrentOp) {
432     bool aGranted = aCurrentOp->isGranted(theId);
433     // the started operation is granted for the current one,
434     // e.g. current - Sketch, started - Line
435     if (aGranted) {
436       aCanStart = true;
437     }
438     else {
439       if (!isGrantedOperation(theId)) {
440         // the operation is not granted in the current list of operations
441         // e.g. Edit Parameter when Sketch, Line in Sketch is active.
442         aCanStart = abortAllOperations();
443       }
444       else if (canStopOperation(aCurrentOp)) {
445         // the started operation is granted in the parrent operation,
446         // e.g. current - Line in Sketch, started Circle
447         stopOperation(aCurrentOp, isCommitted);
448       } else {
449         aCanStart = false;
450       }
451     }
452   }
453   return aCanStart;
454 }
455
456 void XGUI_OperationMgr::stopOperation(ModuleBase_Operation* theOperation, bool& isCommitted)
457 {
458   if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() &&
459       theOperation->isModified()) {
460     isCommitted = theOperation->commit();
461   } else {
462     isCommitted = false;
463     abortOperation(theOperation);
464   }
465 }
466
467 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
468 {
469   ModuleBase_Operation* aCurrentOperation = currentOperation();
470   if (theOperation && (theOperation == aCurrentOperation))
471     theOperation->abort();
472   else {
473     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
474     // all operation from the current to triggered should also be aborted
475     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
476     while(hasOperation()) {
477       ModuleBase_Operation* aCurOperation = currentOperation();
478       aCurOperation->abort();
479       if(theOperation == aCurOperation)
480         break;
481     }
482   }
483 }
484
485 bool XGUI_OperationMgr::commitOperation()
486 {
487   bool isCommitted = false;
488   ModuleBase_Operation* anOperation = currentOperation();
489   if (anOperation && myWorkshop->module()->canCommitOperation())
490     isCommitted = anOperation->commit();
491   return isCommitted;
492 }
493
494 void XGUI_OperationMgr::onAbortOperation()
495 {
496   ModuleBase_Operation* aCurrentOperation = currentOperation();
497   if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
498     abortOperation(aCurrentOperation);
499   }
500 }
501
502 void XGUI_OperationMgr::onAbortAllOperation()
503 {
504   abortAllOperations();
505 }
506
507 void XGUI_OperationMgr::onBeforeOperationStarted()
508 {
509   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
510   if (!aCurrentOperation)
511     return;
512
513   /// Set current feature and remeber old current feature
514   ModuleBase_OperationFeature* aFOperation =
515     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
516   if (aFOperation) {
517     SessionPtr aMgr = ModelAPI_Session::get();
518     DocumentPtr aDoc = aMgr->activeDocument();
519     // the parameter of current feature should be false, we should use all feature, not only
520     // visible in order to correctly save the previous feature of the nested operation, where the
521     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
522     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
523     // When sketch entity operation started, the sketch should be cashed here as the current.
524     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
525     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
526     // As a result the sketch disappears from the viewer.
527     // However after commit it is displayed back.
528     aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
529
530 #ifdef DEBUG_CURRENT_FEATURE
531     FeaturePtr aFeature = aFOperation->feature();
532     QString aKind = aFeature ? aFeature->getKind().c_str() : "";
533     qDebug("");
534     qDebug(QString("onBeforeOperationStarted() isEditOperation = %1, feature = %2")
535             .arg(aFOperation->isEditOperation())
536             .arg(ModuleBase_Tools::objectName(aFeature)).toStdString().c_str());
537     qDebug(QString("   document->currentFeature(false) = %1 : DO: setPreviousCurrentFeature").arg(
538             ModuleBase_Tools::objectName(aDoc->currentFeature(false))).toStdString().c_str());
539 #endif
540
541     if (aFOperation->isEditOperation()) {// it should be performed by the feature edit only
542       // in create operation, the current feature is changed by addFeature()
543       aDoc->setCurrentFeature(aFOperation->feature(), false);
544 #ifdef DEBUG_CURRENT_FEATURE
545       qDebug(QString("   document->setCurrentFeature(false) = %1").arg(
546              ModuleBase_Tools::objectName(aDoc->currentFeature(false))).toStdString().c_str());
547 #endif
548       // this is the only place where flushes must be called after setCurrentFeature for the
549       // current moment: after this the opertion is not finished, so, the ObjectBrowser
550       // state may be corrupted (issue #1457)
551       static Events_Loop* aLoop = Events_Loop::loop();
552       static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
553       aLoop->flush(aCreateEvent);
554       static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
555       aLoop->flush(aDeleteEvent);
556     }
557   }
558 }
559
560 void XGUI_OperationMgr::onOperationStarted()
561 {
562   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
563   updateApplyOfOperations(aSenderOperation);
564   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
565   aWorkshop->operationStarted(aSenderOperation);
566 }
567
568 void XGUI_OperationMgr::onBeforeOperationAborted()
569 {
570   onBeforeOperationCommitted();
571 }
572
573 void XGUI_OperationMgr::onOperationAborted()
574 {
575   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
576   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
577   aWorkshop->setStatusBarMessage("");
578   emit operationAborted(aSenderOperation);
579 }
580
581 void XGUI_OperationMgr::onBeforeOperationCommitted()
582 {
583   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
584   if (!aCurrentOperation)
585     return;
586
587   /// Restore the previous current feature
588   ModuleBase_OperationFeature* aFOperation =
589     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
590   if (aFOperation) {
591 #ifdef DEBUG_CURRENT_FEATURE
592     QString aKind = aFOperation->feature()->getKind().c_str();
593     qDebug(QString("onBeforeOperationCommitted() isEditOperation = %1, feature = %2")
594             .arg(aFOperation->isEditOperation())
595             .arg(ModuleBase_Tools::objectName(aFOperation->feature())).toStdString().c_str());
596     qDebug(QString("   document->currentFeature(false) = %1").arg(
597             ModuleBase_Tools::objectName(
598             ModelAPI_Session::get()->activeDocument()->currentFeature(false)))
599             .toStdString().c_str());
600 #endif
601
602     if (aFOperation->isEditOperation()) {
603       /// Restore the previous current feature
604       setCurrentFeature(aFOperation->previousCurrentFeature());
605     }
606     else { // create operation
607       // the Top created feature should stays the current. In nested operations,
608       // like Line in the Sketch or
609       // Sketch in ExtrusionCut, a previous feature should be restored on commit.
610       // It is performed here
611       // in order to perform it in the current transaction without opening a new one.
612       if (myOperations.front() != aFOperation)
613         setCurrentFeature(aFOperation->previousCurrentFeature());
614     }
615     ModuleBase_IModule* aModule = myWorkshop->module();
616     if (aModule)
617       aModule->beforeOperationStopped(aFOperation);
618   }
619 }
620
621 void XGUI_OperationMgr::onOperationCommitted()
622 {
623   // apply state for all features from the stack of operations should be updated
624   updateApplyOfOperations();
625
626   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
627   emit operationCommitted(aSenderOperation);
628 }
629
630 void XGUI_OperationMgr::onOperationResumed()
631 {
632   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
633   emit operationResumed(aSenderOperation);
634 }
635
636 void XGUI_OperationMgr::onOperationStopped()
637 {
638   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
639   ModuleBase_Operation* aCurrentOperation = currentOperation();
640   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
641     return;
642
643   myOperations.removeAll(aCurrentOperation);
644   aCurrentOperation->deleteLater();
645
646   emit operationStopped(aCurrentOperation);
647
648   // get last operation which can be resumed
649   ModuleBase_Operation* aResultOp = 0;
650   QListIterator<ModuleBase_Operation*> anIt(myOperations);
651   anIt.toBack();
652   while (anIt.hasPrevious()) {
653     ModuleBase_Operation* anOp = anIt.previous();
654     if (anOp) {
655       aResultOp = anOp;
656       break;
657     }
658   }
659   if (aResultOp) {
660     //bool isModified = aCurrentOperation->isModified();
661     //aResultOp->setIsModified(aResultOp->isModified() || isModified);
662     resumeOperation(aResultOp);
663     onValidateOperation();
664   }
665 }
666
667 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
668 {
669   bool isAccepted = false;
670
671   // Let the manager decide what to do with the given key combination.
672   ModuleBase_Operation* anOperation = currentOperation();
673   switch (theEvent->key()) {
674     case Qt::Key_Tab:
675     case Qt::Key_Backtab:
676     {
677       ModuleBase_Operation* aOperation = currentOperation();
678       if (aOperation) {
679         ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
680         if (aPanel) {
681           QWidget* aFocusedWidget = qApp->focusWidget();
682           bool isPPChildObject = aFocusedWidget && isChildObject(aFocusedWidget, aPanel);
683           if (!isPPChildObject) {
684             // check for case when the operation is started but property panel is not filled
685             XGUI_PropertyPanel* aPP = dynamic_cast<XGUI_PropertyPanel*>(aPanel);
686             aPP->setFocusNextPrevChild(theEvent->key() == Qt::Key_Tab);
687             isAccepted = true;
688           }
689         }
690       }
691     }
692     break;
693     case Qt::Key_Return:
694     case Qt::Key_Enter: {
695       isAccepted = onProcessEnter(theObject);
696     }
697     break;
698     case Qt::Key_N:
699     case Qt::Key_P: {
700       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
701       if (noModifiers) {
702         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
703         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
704         if (!aContext.IsNull()) {
705           Handle(V3d_View) aView = aViewer->activeView();
706           if ((theEvent->key() == Qt::Key_N))
707             aContext->HilightNextDetected(aView);
708           else if ((theEvent->key() == Qt::Key_P))
709             aContext->HilightPreviousDetected(aView);
710           aViewer->updateHighlight();
711           isAccepted = true;
712         }
713       }
714       }
715       break;
716    case Qt::Key_H:
717      if ((theEvent->modifiers() == Qt::NoModifier))
718       myWorkshop->viewer()->hideSelectionHighlight();
719      break;
720    default:
721       isAccepted = false;
722       break;
723   }
724   //if(anOperation) {
725   //  anOperation->keyReleased(theEvent->key());
726   //}
727   return isAccepted;
728 }
729
730 bool XGUI_OperationMgr::onKeyPressed(QObject *theObject, QKeyEvent* theEvent)
731 {
732   // Let the manager decide what to do with the given key combination.
733   bool isAccepted = false;
734   switch (theEvent->key()) {
735     case Qt::Key_Escape: {
736       // processing in message box
737       if (myActiveMessageBox)
738       {
739         myActiveMessageBox->reject();
740         isAccepted = true;
741       }
742       // processing in the active widget
743       ModuleBase_Operation* aOperation = currentOperation();
744       if (!isAccepted && aOperation) {
745         ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
746         if (aPanel) {
747           ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
748           if (anActiveWgt)
749           {
750             isAccepted = anActiveWgt && anActiveWgt->processAction(ActionEscape);
751             if (isAccepted) {
752               ModuleBase_OperationFeature* aFOperation =
753                 dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
754               if (aFOperation)
755                 aFOperation->setNeedToBeAborted(true);
756             }
757           }
758         }
759       }
760       if (!isAccepted)
761       {
762         XGUI_ActiveControlSelector* anActiveSelector =
763           XGUI_Tools::workshop(myWorkshop)->activeControlMgr()->activeSelector();
764         if (anActiveSelector && anActiveSelector->getType() == XGUI_FacesPanelSelector::Type())
765           isAccepted = XGUI_Tools::workshop(myWorkshop)->facesPanel()->processAction(ActionEscape);
766       }
767       // default Escape button functionality
768       if (!isAccepted && aOperation) {
769         onAbortOperation();
770         isAccepted = true;
771       }
772     }
773     break;
774     case Qt::Key_H:
775       if ((theEvent->modifiers() == Qt::NoModifier))
776         myWorkshop->viewer()->showSelectionHighlight();
777       break;
778   }
779   return isAccepted;
780 }
781
782 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
783 {
784   bool isAccepted = false;
785   ModuleBase_Operation* aOperation = currentOperation();
786   // to avoid enter processing when operation has not been started yet
787   if (!aOperation)
788     return isAccepted;
789   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
790   if (!aPanel)
791     return isAccepted;
792   // the next code is obsolete as we want to process Enter in property panel always
793   // only property panel enter is processed in order to do not process enter in application dialogs
794   //bool isPPChild = isChildObject(theObject, aPanel);
795   //if (!isPPChild)
796   //  return isAccepted;
797
798   ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
799   bool isAborted = false;
800   if (!anActiveWgt) {
801     QWidget* aFocusWidget = aPanel->focusWidget();
802     QToolButton* aCancelBtn =
803       dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
804     if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
805       abortOperation(aOperation);
806       isAccepted = true;
807       isAborted = true;
808     }
809   }
810   if (!isAborted) {
811     isAccepted = anActiveWgt && anActiveWgt->processAction(ActionEnter);
812     if (!isAccepted) {
813       isAccepted =
814         myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
815       if (!isAccepted) {
816         /// functionality is similar to Apply click
817         ModuleBase_OperationFeature* aFOperation =
818           dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
819         if (!aFOperation ||
820             myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
821           // key released is emitted to apply the current value to the model
822           // if it was modified in PP
823           emit keyEnterReleased();
824           commitOperation();
825           isAccepted = true;
826         }
827         else
828           isAccepted = false;
829       }
830     }
831   }
832   return isAccepted;
833 }
834
835 bool editorControl(QObject* theObject)
836 {
837   QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>(theObject);
838   return aLineEdit;
839 }
840
841 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
842 {
843   bool isAccepted = false;
844   ModuleBase_Operation* aOperation = currentOperation();
845   ModuleBase_ModelWidget* anActiveWgt = 0;
846   // firstly the widget should process Delete action
847   ModuleBase_IPropertyPanel* aPanel;
848   bool isPPChildObject = false;
849   if (aOperation) {
850     aPanel = aOperation->propertyPanel();
851     if (aPanel) {
852       isPPChildObject = isChildObject(theObject, aPanel);
853       // process delete in active widget only if delete sender is child of property panel
854       // it is necessary for the case when OB is shown, user perform selection and click Delete
855       if (isPPChildObject) {
856         anActiveWgt = aPanel->activeWidget();
857         if (anActiveWgt) {
858           isAccepted = anActiveWgt->processAction(ActionDelete);
859         }
860       }
861     }
862   }
863   if (!isAccepted)
864   {
865     XGUI_ActiveControlSelector* anActiveSelector =
866       XGUI_Tools::workshop(myWorkshop)->activeControlMgr()->activeSelector();
867     if (anActiveSelector && anActiveSelector->getType() == XGUI_FacesPanelSelector::Type())
868       isAccepted = XGUI_Tools::workshop(myWorkshop)->facesPanel()->processAction(ActionDelete);
869   }
870   if (!isAccepted) {
871     // after widget, object browser and viewer should process delete
872     /// other widgets such as line edit controls should not lead to
873     /// processing delete by workshop
874     XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
875     QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
876     bool isToDeleteObject = false;
877     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
878     XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
879     if (theObject == aBrowser->treeView()) {
880       aContextMenuMgr->updateObjectBrowserMenu();
881       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
882     }
883     else if (isChildObject(theObject, aViewPort)) {
884       aContextMenuMgr->updateViewerMenu();
885       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
886     }
887     else if (isPPChildObject) {
888       // property panel child object is processed to process delete performed on Apply button of PP
889       isToDeleteObject = true;
890     }
891     else if (editorControl(theObject)) {
892       isToDeleteObject = false; /// Line Edit of Rename operation in ObjectBrowser
893       isAccepted = true;
894     }
895
896     if (isToDeleteObject) {
897       aWorkshop->deleteObjects();
898       isAccepted = true;
899     }
900   }
901
902   return isAccepted;
903 }
904
905 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
906 {
907   bool isPPChild = false;
908   if (theParent && theObject) {
909     QObject* aParent = (QObject*)theObject;
910     while (aParent ) {
911       isPPChild = aParent == theParent;
912       if (isPPChild)
913         break;
914       aParent = aParent->parent();
915     }
916   }
917   return isPPChild;
918 }
919
920 QMessageBox* XGUI_OperationMgr::createMessageBox(const QString& theMessage)
921 {
922   QMessageBox * aMessageBox = new QMessageBox(QMessageBox::Question,
923     QObject::tr("Abort operation"), theMessage, QMessageBox::Ok | QMessageBox::Cancel,
924     qApp->activeWindow());
925   aMessageBox->setDefaultButton(QMessageBox::Cancel);
926   aMessageBox->setEscapeButton(QMessageBox::No); // operation manager should process Esc key
927
928   return aMessageBox;
929 }
930
931 QMessageBox* XGUI_OperationMgr::createInformationBox(const QString& theMessage)
932 {
933   QMessageBox * aMessageBox = new QMessageBox(QMessageBox::Question,
934     QObject::tr("Validate operation"), theMessage, QMessageBox::Ok,
935     qApp->activeWindow());
936   aMessageBox->setDefaultButton(QMessageBox::Ok);
937   aMessageBox->setEscapeButton(QMessageBox::No); // operation manager should process Esc key
938
939   return aMessageBox;
940 }
941
942 XGUI_Workshop* XGUI_OperationMgr::xworkshop() const
943 {
944   XGUI_ModuleConnector* aConnector = (XGUI_ModuleConnector*) myWorkshop;
945   return aConnector->workshop();
946 }