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