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