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