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