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