]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Merge branch 'Dev_2.8.0'
[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)
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 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
206 {
207   if (hasOperation())
208     currentOperation()->postpone();
209   myOperations.append(theOperation);
210
211   connect(theOperation, SIGNAL(beforeStarted()), SLOT(onBeforeOperationStarted()));
212   connect(theOperation, SIGNAL(beforeAborted()), SLOT(onBeforeOperationAborted()));
213   connect(theOperation, SIGNAL(beforeCommitted()), SLOT(onBeforeOperationCommitted()));
214
215   connect(theOperation, SIGNAL(started()), SLOT(onOperationStarted()));
216   connect(theOperation, SIGNAL(aborted()), SLOT(onOperationAborted()));
217   connect(theOperation, SIGNAL(committed()), SLOT(onOperationCommitted()));
218
219   connect(theOperation, SIGNAL(stopped()), SLOT(onOperationStopped()));
220   connect(theOperation, SIGNAL(resumed()), SLOT(onOperationResumed()));
221
222   bool isStarted = theOperation->start();
223   if (isStarted)
224     onValidateOperation();
225   return isStarted;
226 }
227
228 bool XGUI_OperationMgr::abortAllOperations(const XGUI_MessageKind& theMessageKind)
229 {
230   bool aResult = true;
231   if(!hasOperation())
232     return aResult;
233
234   if (operationsCount() == 1) {
235     ModuleBase_Operation* aCurrentOperation = currentOperation();
236     if (canStopOperation(aCurrentOperation, theMessageKind)) {
237       abortOperation(aCurrentOperation);
238     }
239     else
240       aResult = false;
241   }
242   else {
243     if (theMessageKind == XGUI_AbortOperationMessage) {
244       aResult = QMessageBox::question(qApp->activeWindow(),
245                                       tr("Abort operation"),
246                                       tr("All active operations will be aborted."),
247                                       QMessageBox::Ok | QMessageBox::Cancel,
248                                       QMessageBox::Cancel) == QMessageBox::Ok;
249     }
250     else if (theMessageKind == XGUI_InformationMessage) {
251       QString aMessage = tr("Please validate all your active operations before saving.");
252       QMessageBox::question(qApp->activeWindow(),
253                             tr("Validate operation"),
254                             aMessage,
255                             QMessageBox::Ok,
256                             QMessageBox::Ok);
257       aResult = false; // do not perform abort
258     }
259     while(aResult && hasOperation()) {
260       abortOperation(currentOperation());
261     }
262   }
263   return aResult;
264 }
265
266 bool XGUI_OperationMgr::commitAllOperations()
267 {
268   bool isCompositeCommitted = false, anOperationProcessed = false;
269   while (hasOperation()) {
270     ModuleBase_Operation* anOperation = currentOperation();
271     if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled()) {
272       anOperationProcessed = commitOperation();
273     } else {
274       abortOperation(anOperation);
275       anOperationProcessed = true;
276     }
277     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
278                                                                             (anOperation);
279     if (aFOperation) {
280       FeaturePtr aFeature = aFOperation->feature();
281       CompositeFeaturePtr aComposite =
282           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
283       isCompositeCommitted = aComposite.get();
284       if (isCompositeCommitted)
285         break;
286     }
287     // not processed[committed] operation might be used in composite feature,
288     // so the while will be stopped by the previous check.
289     // this code is not necessary, but logically should be done when the processing will not
290     // be done for not composite feature by some reasons
291     if (!anOperationProcessed)
292       break;
293   }
294   return true;
295 }
296
297 void XGUI_OperationMgr::onValidateOperation()
298 {
299   if (!hasOperation())
300     return;
301   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
302                                                                           (currentOperation());
303   if(aFOperation && aFOperation->feature().get())
304     XGUI_Tools::workshop(myWorkshop)->errorMgr()->updateActions(aFOperation->feature());
305 }
306
307 void XGUI_OperationMgr::updateApplyOfOperations(ModuleBase_Operation* theOperation)
308 {
309   XGUI_ErrorMgr* anErrorMgr = XGUI_Tools::workshop(myWorkshop)->errorMgr();
310   if (theOperation) {
311     ModuleBase_OperationFeature* aFOperation =
312       dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
313     if (aFOperation)
314       anErrorMgr->updateAcceptAllAction(aFOperation->feature());
315   }
316   else {
317     foreach(ModuleBase_Operation* anOperation, myOperations) {
318       if (anOperation)
319         updateApplyOfOperations(anOperation);
320     }
321   }
322   // Apply button of the current operation should also be updated
323   onValidateOperation();
324 }
325
326 bool XGUI_OperationMgr::canStopOperation(ModuleBase_Operation* theOperation,
327                                          const XGUI_OperationMgr::XGUI_MessageKind& theMessageKind)
328 {
329   //in case of nested (sketch) operation no confirmation needed
330   if (isGrantedOperation(theOperation->id()))
331     return true;
332   if (theOperation && theOperation->isModified()) {
333     if (theMessageKind == XGUI_AbortOperationMessage) {
334       QString aMessage = tr("%1 operation will be aborted.").arg(theOperation->id());
335       int anAnswer = QMessageBox::question(qApp->activeWindow(),
336                                            tr("Abort operation"),
337                                            aMessage,
338                                            QMessageBox::Ok | QMessageBox::Cancel,
339                                            QMessageBox::Cancel);
340       return anAnswer == QMessageBox::Ok;
341     }
342     else if (theMessageKind == XGUI_InformationMessage) {
343       QString aMessage = tr("Please validate your %1 before saving.").arg(theOperation->id());
344       QMessageBox::question(qApp->activeWindow(),
345                             tr("Validate operation"),
346                             aMessage,
347                             QMessageBox::Ok,
348                             QMessageBox::Ok);
349       return false;
350     }
351   }
352   return true;
353 }
354
355 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
356 {
357   theOperation->resume();
358 }
359
360 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
361 {
362   bool isGranted = false;
363
364   QListIterator<ModuleBase_Operation*> anIt(myOperations);
365   anIt.toBack();
366   ModuleBase_Operation* aPreviousOperation = 0;
367   while (anIt.hasPrevious() && !isGranted) {
368     ModuleBase_Operation* anOp = anIt.previous();
369     if (anOp)
370       isGranted = anOp->isGranted(theId);
371   }
372   return isGranted;
373 }
374
375 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
376 {
377   SessionPtr aMgr = ModelAPI_Session::get();
378   DocumentPtr aDoc = aMgr->activeDocument();
379   bool aIsOp = aMgr->isOperation();
380   if (!aIsOp)
381     aMgr->startOperation(QString("Set current feature: %1")
382     .arg(theFeature->getKind().c_str()).toStdString());
383   aDoc->setCurrentFeature(theFeature, false);
384 #ifdef DEBUG_CURRENT_FEATURE
385   qDebug(QString("   document->setCurrentFeature(false) = %1    SET").arg(
386          ModuleBase_Tools::objectName(
387          ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
388 #endif
389
390   if (!aIsOp)
391     aMgr->finishOperation();
392 }
393
394 bool XGUI_OperationMgr::canStartOperation(const QString& theId, bool& isCommitted)
395 {
396   bool aCanStart = true;
397   isCommitted = false;
398   ModuleBase_Operation* aCurrentOp = currentOperation();
399   if (aCurrentOp) {
400     bool aGranted = aCurrentOp->isGranted(theId);
401     // the started operation is granted for the current one,
402     // e.g. current - Sketch, started - Line
403     if (aGranted) {
404       aCanStart = true;
405     }
406     else {
407       if (!isGrantedOperation(theId)) {
408         // the operation is not granted in the current list of operations
409         // e.g. Edit Parameter when Sketch, Line in Sketch is active.
410         aCanStart = abortAllOperations();
411       }
412       else if (canStopOperation(aCurrentOp)) {
413         // the started operation is granted in the parrent operation,
414         // e.g. current - Line in Sketch, started Circle
415         stopOperation(aCurrentOp, isCommitted);
416       } else {
417         aCanStart = false;
418       }
419     }
420   }
421   return aCanStart;
422 }
423
424 void XGUI_OperationMgr::stopOperation(ModuleBase_Operation* theOperation, bool& isCommitted)
425 {
426   if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() && theOperation->isModified())
427     isCommitted = theOperation->commit();
428   else {
429     isCommitted = false;
430     abortOperation(theOperation);
431   }
432 }
433
434 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
435 {
436   ModuleBase_Operation* aCurrentOperation = currentOperation();
437   if (theOperation == aCurrentOperation)
438     theOperation->abort();
439   else {
440     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
441     // all operation from the current to triggered should also be aborted
442     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
443     while(hasOperation()) {
444       ModuleBase_Operation* aCurrentOperation = currentOperation();
445       aCurrentOperation->abort();
446       if(theOperation == aCurrentOperation)
447         break;
448     }
449   }
450 }
451
452 bool XGUI_OperationMgr::commitOperation()
453 {
454   bool isCommitted = false;
455   ModuleBase_Operation* anOperation = currentOperation();
456   if (anOperation && myWorkshop->module()->canCommitOperation())
457     isCommitted = anOperation->commit();
458   return isCommitted;
459 }
460
461 void XGUI_OperationMgr::onAbortOperation()
462 {
463   ModuleBase_Operation* aCurrentOperation = currentOperation();
464   if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
465     abortOperation(aCurrentOperation);
466   }
467 }
468
469 void XGUI_OperationMgr::onAbortAllOperation()
470 {
471   abortAllOperations();
472 }
473
474 void XGUI_OperationMgr::onBeforeOperationStarted()
475 {
476   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
477   if (!aCurrentOperation)
478     return;
479
480   /// Set current feature and remeber old current feature
481   ModuleBase_OperationFeature* aFOperation =
482     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
483   if (aFOperation) {
484     SessionPtr aMgr = ModelAPI_Session::get();
485     DocumentPtr aDoc = aMgr->activeDocument();
486     // the parameter of current feature should be false, we should use all feature, not only
487     // visible in order to correctly save the previous feature of the nested operation, where the
488     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
489     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
490     // When sketch entity operation started, the sketch should be cashed here as the current.
491     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
492     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
493     // As a result the sketch disappears from the viewer.
494     // However after commit it is displayed back.
495     aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
496
497 #ifdef DEBUG_CURRENT_FEATURE
498     FeaturePtr aFeature = aFOperation->feature();
499     QString aKind = aFeature ? aFeature->getKind().c_str() : "";
500     qDebug("");
501     qDebug(QString("onBeforeOperationStarted() isEditOperation = %1, feature = %2")
502             .arg(aFOperation->isEditOperation())
503             .arg(ModuleBase_Tools::objectName(aFeature)).toStdString().c_str());
504     qDebug(QString("   document->currentFeature(false) = %1 : DO: setPreviousCurrentFeature").arg(
505             ModuleBase_Tools::objectName(aDoc->currentFeature(false))).toStdString().c_str());
506 #endif
507
508     if (aFOperation->isEditOperation()) {// it should be performed by the feature edit only
509       // in create operation, the current feature is changed by addFeature()
510       aDoc->setCurrentFeature(aFOperation->feature(), false);
511 #ifdef DEBUG_CURRENT_FEATURE
512       qDebug(QString("   document->setCurrentFeature(false) = %1").arg(
513              ModuleBase_Tools::objectName(aDoc->currentFeature(false))).toStdString().c_str());
514 #endif
515       // this is the only place where flushes must be called after setCurrentFeature for the
516       // current moment: after this the opertion is not finished, so, the ObjectBrowser
517       // state may be corrupted (issue #1457)
518       static Events_Loop* aLoop = Events_Loop::loop();
519       static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
520       aLoop->flush(aCreateEvent);
521       static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
522       aLoop->flush(aDeleteEvent);
523     }
524   }
525 }
526
527 void XGUI_OperationMgr::onOperationStarted()
528 {
529   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
530   updateApplyOfOperations(aSenderOperation);
531   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
532   aWorkshop->operationStarted(aSenderOperation);
533 }
534
535 void XGUI_OperationMgr::onBeforeOperationAborted()
536 {
537   onBeforeOperationCommitted();
538 }
539
540 void XGUI_OperationMgr::onOperationAborted()
541 {
542   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
543   emit operationAborted(aSenderOperation);
544 }
545
546 void XGUI_OperationMgr::onBeforeOperationCommitted()
547 {
548   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
549   if (!aCurrentOperation)
550     return;
551
552   /// Restore the previous current feature
553   ModuleBase_OperationFeature* aFOperation =
554     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
555   if (aFOperation) {
556 #ifdef DEBUG_CURRENT_FEATURE
557     QString aKind = aFOperation->feature()->getKind().c_str();
558     qDebug(QString("onBeforeOperationCommitted() isEditOperation = %1, feature = %2")
559             .arg(aFOperation->isEditOperation())
560             .arg(ModuleBase_Tools::objectName(aFOperation->feature())).toStdString().c_str());
561     qDebug(QString("   document->currentFeature(false) = %1").arg(
562             ModuleBase_Tools::objectName(
563             ModelAPI_Session::get()->activeDocument()->currentFeature(false)))
564             .toStdString().c_str());
565 #endif
566
567     if (aFOperation->isEditOperation()) {
568       /// Restore the previous current feature
569       setCurrentFeature(aFOperation->previousCurrentFeature());
570     }
571     else { // create operation
572       // the Top created feature should stays the current. In nested operations,
573       // like Line in the Sketch or
574       // Sketch in ExtrusionCut, a previous feature should be restored on commit.
575       // It is performed here
576       // in order to perform it in the current transaction without opening a new one.
577       if (myOperations.front() != aFOperation)
578         setCurrentFeature(aFOperation->previousCurrentFeature());
579     }
580     ModuleBase_IModule* aModule = myWorkshop->module();
581     if (aModule)
582       aModule->beforeOperationStopped(aFOperation);
583   }
584 }
585
586 void XGUI_OperationMgr::onOperationCommitted()
587 {
588   // apply state for all features from the stack of operations should be updated
589   updateApplyOfOperations();
590
591   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
592   emit operationCommitted(aSenderOperation);
593 }
594
595 void XGUI_OperationMgr::onOperationResumed()
596 {
597   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
598   emit operationResumed(aSenderOperation);
599 }
600
601 void XGUI_OperationMgr::onOperationStopped()
602 {
603   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
604   ModuleBase_Operation* aCurrentOperation = currentOperation();
605   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
606     return;
607
608   myOperations.removeAll(aCurrentOperation);
609   aCurrentOperation->deleteLater();
610
611   emit operationStopped(aCurrentOperation);
612
613   // get last operation which can be resumed
614   ModuleBase_Operation* aResultOp = 0;
615   QListIterator<ModuleBase_Operation*> anIt(myOperations);
616   anIt.toBack();
617   while (anIt.hasPrevious()) {
618     ModuleBase_Operation* anOp = anIt.previous();
619     if (anOp) {
620       aResultOp = anOp;
621       break;
622     }
623   }
624   if (aResultOp) {
625     bool isModified = aCurrentOperation->isModified();
626     aResultOp->setIsModified(aResultOp->isModified() || isModified);
627     resumeOperation(aResultOp);
628     onValidateOperation();
629   }
630 }
631
632 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
633 {
634   // Let the manager decide what to do with the given key combination.
635   ModuleBase_Operation* anOperation = currentOperation();
636   bool isAccepted = false;
637   switch (theEvent->key()) {
638     case Qt::Key_Tab:
639     case Qt::Key_Backtab:
640     {
641       ModuleBase_Operation* aOperation = currentOperation();
642       if (aOperation) {
643         ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
644         if (aPanel) {
645           QWidget* aFocusedWidget = qApp->focusWidget();
646           bool isPPChildObject = aFocusedWidget && isChildObject(aFocusedWidget, aPanel);
647           if (!isPPChildObject) {
648             // check for case when the operation is started but property panel is not filled
649             XGUI_PropertyPanel* aPP = dynamic_cast<XGUI_PropertyPanel*>(aPanel);
650             aPP->setFocusNextPrevChild(theEvent->key() == Qt::Key_Tab);
651             isAccepted = true;
652           }
653         }
654       }
655     }
656     break;
657     case Qt::Key_Return:
658     case Qt::Key_Enter: {
659       isAccepted = onProcessEnter(theObject);
660     }
661     break;
662     case Qt::Key_N:
663     case Qt::Key_P: {
664       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
665       if (noModifiers) {
666         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
667         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
668         if (!aContext.IsNull()) {
669           Handle(V3d_View) aView = aViewer->activeView();
670           if ((theEvent->key() == Qt::Key_N))
671             aContext->HilightNextDetected(aView);
672           else if ((theEvent->key() == Qt::Key_P))
673             aContext->HilightPreviousDetected(aView);
674         }
675       }
676     }
677     break;
678     break;
679     default:
680       isAccepted = false;
681       break;
682   }
683   //if(anOperation) {
684   //  anOperation->keyReleased(theEvent->key());
685   //}
686   return isAccepted;
687 }
688
689 bool XGUI_OperationMgr::onKeyPressed(QObject *theObject, QKeyEvent* theEvent)
690 {
691   // Let the manager decide what to do with the given key combination.
692   ModuleBase_Operation* anOperation = currentOperation();
693   bool isAccepted = false;
694   switch (theEvent->key()) {
695     case Qt::Key_Escape: {
696       // processing in message box
697       if (myActiveMessageBox)
698       {
699         myActiveMessageBox->reject();
700         isAccepted = true;
701       }
702       // processing in the active widget
703       ModuleBase_Operation* aOperation = currentOperation();
704       if (!isAccepted && aOperation) {
705         ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
706         ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
707         if (anActiveWgt)
708           isAccepted = anActiveWgt && anActiveWgt->processEscape();
709       }
710       // default Escape button functionality
711       if (!isAccepted && aOperation) {
712         onAbortOperation();
713         isAccepted = true;
714       }
715     }
716     break;
717   }
718   return isAccepted;
719 }
720
721 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
722 {
723   bool isAccepted = false;
724   ModuleBase_Operation* aOperation = currentOperation();
725   // to avoid enter processing when operation has not been started yet
726   if (!aOperation)
727     return isAccepted;
728   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
729   // the next code is obsolete as we want to process Enter in property panel always
730   // only property panel enter is processed in order to do not process enter in application dialogs
731   //bool isPPChild = isChildObject(theObject, aPanel);
732   //if (!isPPChild)
733   //  return isAccepted;
734
735   ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
736   bool isAborted = false;
737   if (!anActiveWgt) {
738     QWidget* aFocusWidget = aPanel->focusWidget();
739     QToolButton* aCancelBtn =
740       dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
741     if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
742       abortOperation(aOperation);
743       isAccepted = true;
744       isAborted = true;
745     }
746   }
747   if (!isAborted) {
748     isAccepted = anActiveWgt && anActiveWgt->processEnter();
749     if (!isAccepted) {
750       isAccepted =
751         myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
752       if (!isAccepted) {
753         /// functionality is similar to Apply click
754         ModuleBase_OperationFeature* aFOperation =
755           dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
756         if (!aFOperation ||
757             myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
758           // key released is emitted to apply the current value to the model
759           // if it was modified in PP
760           emit keyEnterReleased();
761           commitOperation();
762           isAccepted = true;
763         }
764         else
765           isAccepted = false;
766       }
767     }
768   }
769   return isAccepted;
770 }
771
772 bool editorControl(QObject* theObject)
773 {
774   QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>(theObject);
775   return aLineEdit;
776 }
777
778 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
779 {
780   bool isAccepted = false;
781   ModuleBase_Operation* aOperation = currentOperation();
782   ModuleBase_ModelWidget* anActiveWgt = 0;
783   // firstly the widget should process Delete action
784   ModuleBase_IPropertyPanel* aPanel;
785   bool isPPChildObject = false;
786   if (aOperation) {
787     aPanel = aOperation->propertyPanel();
788     if (aPanel) {
789       isPPChildObject = isChildObject(theObject, aPanel);
790       // process delete in active widget only if delete sender is child of property panel
791       // it is necessary for the case when OB is shown, user perform selection and click Delete
792       if (isPPChildObject) {
793         anActiveWgt = aPanel->activeWidget();
794         if (anActiveWgt) {
795           isAccepted = anActiveWgt->processDelete();
796         }
797       }
798     }
799   }
800   if (!isAccepted) {
801     // after widget, object browser and viewer should process delete
802     /// other widgets such as line edit controls should not lead to
803     /// processing delete by workshop
804     XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
805     QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
806     bool isToDeleteObject = false;
807     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
808     XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
809     if (theObject == aBrowser->treeView()) {
810       aContextMenuMgr->updateObjectBrowserMenu();
811       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
812     }
813     else if (isChildObject(theObject, aViewPort)) {
814       aContextMenuMgr->updateViewerMenu();
815       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
816     }
817     else if (isPPChildObject) {
818       // property panel child object is processed to process delete performed on Apply button of PP
819       isToDeleteObject = true;
820     }
821     else if (editorControl(theObject)) {
822       isToDeleteObject = false; /// Line Edit of Rename operation in ObjectBrowser
823       isAccepted = true;
824     }
825
826     if (isToDeleteObject) {
827       aWorkshop->deleteObjects();
828       isAccepted = true;
829     }
830   }
831
832   return isAccepted;
833 }
834
835 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
836 {
837   bool isPPChild = false;
838   if (theParent && theObject) {
839     QObject* aParent = (QObject*)theObject;
840     while (aParent ) {
841       isPPChild = aParent == theParent;
842       if (isPPChild)
843         break;
844       aParent = aParent->parent();
845     }
846   }
847   return isPPChild;
848 }
849
850 QMessageBox* XGUI_OperationMgr::createMessageBox(const QString& theMessage)
851 {
852   QMessageBox * aMessageBox = new QMessageBox(QMessageBox::Question,
853     QObject::tr("Abort operation"), theMessage, QMessageBox::Ok | QMessageBox::Cancel,
854     qApp->activeWindow());
855   aMessageBox->setDefaultButton(QMessageBox::Cancel);
856   aMessageBox->setEscapeButton(QMessageBox::No); // operation manager should process Esc key
857
858   return aMessageBox;
859 }