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