]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
Update translations to French and group messages by their context.
[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 <Config_Translator.h>
44
45 #include <ModelAPI_CompositeFeature.h>
46 #include <ModelAPI_Session.h>
47
48 #include <XGUI_PropertyPanel.h>
49 #include <QToolButton>
50 #include <QLineEdit>
51
52 #include <QMessageBox>
53 #include <QApplication>
54 #include <QKeyEvent>
55
56 //#define DEBUG_CURRENT_FEATURE
57
58 /// Processes "Delete" key event of application. This key is used by several application actions.
59 /// There is a logical order of the actions processing. So the key can not be set for actions
60 /// as a shortcut. The class listens the key event and call operation manager processor.
61 class XGUI_ShortCutListener : public QObject
62 {
63 public:
64   /// Constructor
65   /// \param theParent the parent to be deleted when the parent is deleted
66   /// \param theOperationMgr the class to perform deletion
67   XGUI_ShortCutListener(QObject* theParent, XGUI_OperationMgr* theOperationMgr)
68     : QObject(theParent), myOperationMgr(theOperationMgr), myIsActive(false)
69   {
70     qApp->installEventFilter(this);
71   }
72   ~XGUI_ShortCutListener() {}
73
74   /// Switch on short cut listener
75   void setActive(const bool theIsActive) { myIsActive = theIsActive; }
76
77   /// Redefinition of virtual function to process Delete key release
78   virtual bool eventFilter(QObject *theObject, QEvent *theEvent)
79   {
80     bool isAccepted = false;
81     if (myIsActive) {
82       if (theEvent->type() == QEvent::KeyRelease) {
83         QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
84         if (aKeyEvent) {
85           myOperationMgr->setSHIFTPressed(aKeyEvent->modifiers() & Qt::ShiftModifier);
86           switch (aKeyEvent->key()) {
87             case Qt::Key_Delete:
88               isAccepted = myOperationMgr->onProcessDelete(theObject);
89             break;
90             default:
91               isAccepted = myOperationMgr->onKeyReleased(theObject, aKeyEvent);
92               break;
93           }
94         }
95       }
96       else if (theEvent->type() == QEvent::KeyPress) {
97         if (!qApp->modalWindow()) {
98           if (myOperationMgr->hasOperation()) {
99             QKeyEvent* aKeyEvent = dynamic_cast<QKeyEvent*>(theEvent);
100             myOperationMgr->setSHIFTPressed(aKeyEvent->modifiers() & Qt::ShiftModifier);
101             isAccepted = myOperationMgr->onKeyPressed(theObject, aKeyEvent);
102           }
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     ModuleBase_OperationFeature* aOp = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
346     std::string aContext;
347     if (aOp)
348       aContext = aOp->feature()->getKind();
349     QString aTitle = Config_Translator::translate(aContext,
350       theOperation->getDescription()->description().toStdString()).c_str();
351
352     if (theMessageKind == XGUI_AbortOperationMessage) {
353       QString aMessage = tr("%1 operation will be aborted.").arg(aTitle);
354       myActiveMessageBox = createMessageBox(aMessage);
355       bool aResult = myActiveMessageBox->exec() == QMessageBox::Ok;
356       myActiveMessageBox = 0;
357       return aResult;
358     }
359     else if (theMessageKind == XGUI_InformationMessage) {
360       QString aMessage = tr("Please validate your %1 before saving.").arg(aTitle);
361       myActiveMessageBox = createInformationBox(aMessage);
362       myActiveMessageBox->exec();
363       myActiveMessageBox = 0;
364       return false;
365     }
366   }
367   return true;
368 }
369
370 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
371 {
372   theOperation->resume();
373 }
374
375 bool XGUI_OperationMgr::isGrantedOperation(const QString& theId)
376 {
377   bool isGranted = false;
378
379   QListIterator<ModuleBase_Operation*> anIt(myOperations);
380   anIt.toBack();
381   ModuleBase_Operation* aPreviousOperation = 0;
382   while (anIt.hasPrevious() && !isGranted) {
383     ModuleBase_Operation* anOp = anIt.previous();
384     if (anOp)
385       isGranted = anOp->isGranted(theId);
386   }
387   return isGranted;
388 }
389
390 void XGUI_OperationMgr::setCurrentFeature(const FeaturePtr& theFeature)
391 {
392   SessionPtr aMgr = ModelAPI_Session::get();
393   DocumentPtr aDoc = aMgr->activeDocument();
394   bool aIsOp = aMgr->isOperation();
395   if (!aIsOp)
396     aMgr->startOperation(QString("Set current feature: %1")
397     .arg(theFeature->getKind().c_str()).toStdString());
398   aDoc->setCurrentFeature(theFeature, false);
399 #ifdef DEBUG_CURRENT_FEATURE
400   qDebug(QString("   document->setCurrentFeature(false) = %1    SET").arg(
401          ModuleBase_Tools::objectName(
402          ModelAPI_Session::get()->activeDocument()->currentFeature(false))).toStdString().c_str());
403 #endif
404
405   if (!aIsOp)
406     aMgr->finishOperation();
407 }
408
409 bool XGUI_OperationMgr::canStartOperation(const QString& theId, bool& isCommitted)
410 {
411   bool aCanStart = true;
412   isCommitted = false;
413   ModuleBase_Operation* aCurrentOp = currentOperation();
414   if (aCurrentOp) {
415     bool aGranted = aCurrentOp->isGranted(theId);
416     // the started operation is granted for the current one,
417     // e.g. current - Sketch, started - Line
418     if (aGranted) {
419       aCanStart = true;
420     }
421     else {
422       if (!isGrantedOperation(theId)) {
423         // the operation is not granted in the current list of operations
424         // e.g. Edit Parameter when Sketch, Line in Sketch is active.
425         aCanStart = abortAllOperations();
426       }
427       else if (canStopOperation(aCurrentOp)) {
428         // the started operation is granted in the parrent operation,
429         // e.g. current - Line in Sketch, started Circle
430         stopOperation(aCurrentOp, isCommitted);
431       } else {
432         aCanStart = false;
433       }
434     }
435   }
436   return aCanStart;
437 }
438
439 void XGUI_OperationMgr::stopOperation(ModuleBase_Operation* theOperation, bool& isCommitted)
440 {
441   if (XGUI_Tools::workshop(myWorkshop)->errorMgr()->isApplyEnabled() &&
442       theOperation->isModified()) {
443     isCommitted = theOperation->commit();
444   } else {
445     isCommitted = false;
446     abortOperation(theOperation);
447   }
448 }
449
450 void XGUI_OperationMgr::abortOperation(ModuleBase_Operation* theOperation)
451 {
452   ModuleBase_Operation* aCurrentOperation = currentOperation();
453   if (theOperation && (theOperation == aCurrentOperation))
454     theOperation->abort();
455   else {
456     // it is possible to trigger upper operation(e.g. sketch, current is sketch line)
457     // all operation from the current to triggered should also be aborted
458     // operations over the parameter one are not aborted(e.g. extrusion cut, sketch abort)
459     while(hasOperation()) {
460       ModuleBase_Operation* aCurrentOperation = currentOperation();
461       aCurrentOperation->abort();
462       if(theOperation == aCurrentOperation)
463         break;
464     }
465   }
466 }
467
468 bool XGUI_OperationMgr::commitOperation()
469 {
470   bool isCommitted = false;
471   ModuleBase_Operation* anOperation = currentOperation();
472   if (anOperation && myWorkshop->module()->canCommitOperation())
473     isCommitted = anOperation->commit();
474   return isCommitted;
475 }
476
477 void XGUI_OperationMgr::onAbortOperation()
478 {
479   ModuleBase_Operation* aCurrentOperation = currentOperation();
480   if (aCurrentOperation && canStopOperation(aCurrentOperation)) {
481     abortOperation(aCurrentOperation);
482   }
483 }
484
485 void XGUI_OperationMgr::onAbortAllOperation()
486 {
487   abortAllOperations();
488 }
489
490 void XGUI_OperationMgr::onBeforeOperationStarted()
491 {
492   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
493   if (!aCurrentOperation)
494     return;
495
496   /// Set current feature and remeber old current feature
497   ModuleBase_OperationFeature* aFOperation =
498     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
499   if (aFOperation) {
500     SessionPtr aMgr = ModelAPI_Session::get();
501     DocumentPtr aDoc = aMgr->activeDocument();
502     // the parameter of current feature should be false, we should use all feature, not only
503     // visible in order to correctly save the previous feature of the nested operation, where the
504     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
505     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
506     // When sketch entity operation started, the sketch should be cashed here as the current.
507     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
508     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
509     // As a result the sketch disappears from the viewer.
510     // However after commit it is displayed back.
511     aFOperation->setPreviousCurrentFeature(aDoc->currentFeature(false));
512
513 #ifdef DEBUG_CURRENT_FEATURE
514     FeaturePtr aFeature = aFOperation->feature();
515     QString aKind = aFeature ? aFeature->getKind().c_str() : "";
516     qDebug("");
517     qDebug(QString("onBeforeOperationStarted() isEditOperation = %1, feature = %2")
518             .arg(aFOperation->isEditOperation())
519             .arg(ModuleBase_Tools::objectName(aFeature)).toStdString().c_str());
520     qDebug(QString("   document->currentFeature(false) = %1 : DO: setPreviousCurrentFeature").arg(
521             ModuleBase_Tools::objectName(aDoc->currentFeature(false))).toStdString().c_str());
522 #endif
523
524     if (aFOperation->isEditOperation()) {// it should be performed by the feature edit only
525       // in create operation, the current feature is changed by addFeature()
526       aDoc->setCurrentFeature(aFOperation->feature(), false);
527 #ifdef DEBUG_CURRENT_FEATURE
528       qDebug(QString("   document->setCurrentFeature(false) = %1").arg(
529              ModuleBase_Tools::objectName(aDoc->currentFeature(false))).toStdString().c_str());
530 #endif
531       // this is the only place where flushes must be called after setCurrentFeature for the
532       // current moment: after this the opertion is not finished, so, the ObjectBrowser
533       // state may be corrupted (issue #1457)
534       static Events_Loop* aLoop = Events_Loop::loop();
535       static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
536       aLoop->flush(aCreateEvent);
537       static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
538       aLoop->flush(aDeleteEvent);
539     }
540   }
541 }
542
543 void XGUI_OperationMgr::onOperationStarted()
544 {
545   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
546   updateApplyOfOperations(aSenderOperation);
547   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
548   aWorkshop->operationStarted(aSenderOperation);
549 }
550
551 void XGUI_OperationMgr::onBeforeOperationAborted()
552 {
553   onBeforeOperationCommitted();
554 }
555
556 void XGUI_OperationMgr::onOperationAborted()
557 {
558   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
559   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
560   aWorkshop->setStatusBarMessage("");
561   emit operationAborted(aSenderOperation);
562 }
563
564 void XGUI_OperationMgr::onBeforeOperationCommitted()
565 {
566   ModuleBase_Operation* aCurrentOperation = dynamic_cast<ModuleBase_Operation*>(sender());
567   if (!aCurrentOperation)
568     return;
569
570   /// Restore the previous current feature
571   ModuleBase_OperationFeature* aFOperation =
572     dynamic_cast<ModuleBase_OperationFeature*>(aCurrentOperation);
573   if (aFOperation) {
574 #ifdef DEBUG_CURRENT_FEATURE
575     QString aKind = aFOperation->feature()->getKind().c_str();
576     qDebug(QString("onBeforeOperationCommitted() isEditOperation = %1, feature = %2")
577             .arg(aFOperation->isEditOperation())
578             .arg(ModuleBase_Tools::objectName(aFOperation->feature())).toStdString().c_str());
579     qDebug(QString("   document->currentFeature(false) = %1").arg(
580             ModuleBase_Tools::objectName(
581             ModelAPI_Session::get()->activeDocument()->currentFeature(false)))
582             .toStdString().c_str());
583 #endif
584
585     if (aFOperation->isEditOperation()) {
586       /// Restore the previous current feature
587       setCurrentFeature(aFOperation->previousCurrentFeature());
588     }
589     else { // create operation
590       // the Top created feature should stays the current. In nested operations,
591       // like Line in the Sketch or
592       // Sketch in ExtrusionCut, a previous feature should be restored on commit.
593       // It is performed here
594       // in order to perform it in the current transaction without opening a new one.
595       if (myOperations.front() != aFOperation)
596         setCurrentFeature(aFOperation->previousCurrentFeature());
597     }
598     ModuleBase_IModule* aModule = myWorkshop->module();
599     if (aModule)
600       aModule->beforeOperationStopped(aFOperation);
601   }
602 }
603
604 void XGUI_OperationMgr::onOperationCommitted()
605 {
606   // apply state for all features from the stack of operations should be updated
607   updateApplyOfOperations();
608
609   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
610   emit operationCommitted(aSenderOperation);
611 }
612
613 void XGUI_OperationMgr::onOperationResumed()
614 {
615   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
616   emit operationResumed(aSenderOperation);
617 }
618
619 void XGUI_OperationMgr::onOperationStopped()
620 {
621   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
622   ModuleBase_Operation* aCurrentOperation = currentOperation();
623   if (!aSenderOperation || !aCurrentOperation || aSenderOperation != aCurrentOperation)
624     return;
625
626   myOperations.removeAll(aCurrentOperation);
627   aCurrentOperation->deleteLater();
628
629   emit operationStopped(aCurrentOperation);
630
631   // get last operation which can be resumed
632   ModuleBase_Operation* aResultOp = 0;
633   QListIterator<ModuleBase_Operation*> anIt(myOperations);
634   anIt.toBack();
635   while (anIt.hasPrevious()) {
636     ModuleBase_Operation* anOp = anIt.previous();
637     if (anOp) {
638       aResultOp = anOp;
639       break;
640     }
641   }
642   if (aResultOp) {
643     //bool isModified = aCurrentOperation->isModified();
644     //aResultOp->setIsModified(aResultOp->isModified() || isModified);
645     resumeOperation(aResultOp);
646     onValidateOperation();
647   }
648 }
649
650 bool XGUI_OperationMgr::onKeyReleased(QObject *theObject, QKeyEvent* theEvent)
651 {
652   bool isAccepted = false;
653
654   // Let the manager decide what to do with the given key combination.
655   ModuleBase_Operation* anOperation = currentOperation();
656   switch (theEvent->key()) {
657     case Qt::Key_Tab:
658     case Qt::Key_Backtab:
659     {
660       ModuleBase_Operation* aOperation = currentOperation();
661       if (aOperation) {
662         ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
663         if (aPanel) {
664           QWidget* aFocusedWidget = qApp->focusWidget();
665           bool isPPChildObject = aFocusedWidget && isChildObject(aFocusedWidget, aPanel);
666           if (!isPPChildObject) {
667             // check for case when the operation is started but property panel is not filled
668             XGUI_PropertyPanel* aPP = dynamic_cast<XGUI_PropertyPanel*>(aPanel);
669             aPP->setFocusNextPrevChild(theEvent->key() == Qt::Key_Tab);
670             isAccepted = true;
671           }
672         }
673       }
674     }
675     break;
676     case Qt::Key_Return:
677     case Qt::Key_Enter: {
678       isAccepted = onProcessEnter(theObject);
679     }
680     break;
681     case Qt::Key_N:
682     case Qt::Key_P: {
683       bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
684       if (noModifiers) {
685         ModuleBase_IViewer* aViewer = myWorkshop->viewer();
686         Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
687         if (!aContext.IsNull()) {
688           Handle(V3d_View) aView = aViewer->activeView();
689           if ((theEvent->key() == Qt::Key_N))
690             aContext->HilightNextDetected(aView);
691           else if ((theEvent->key() == Qt::Key_P))
692             aContext->HilightPreviousDetected(aView);
693           aViewer->updateHighlight();
694           isAccepted = true;
695         }
696       }
697       }
698       break;
699    case Qt::Key_H:
700      if ((theEvent->modifiers() == Qt::NoModifier))
701       myWorkshop->viewer()->hideSelectionHighlight();
702      break;
703    default:
704       isAccepted = false;
705       break;
706   }
707   //if(anOperation) {
708   //  anOperation->keyReleased(theEvent->key());
709   //}
710   return isAccepted;
711 }
712
713 bool XGUI_OperationMgr::onKeyPressed(QObject *theObject, QKeyEvent* theEvent)
714 {
715   // Let the manager decide what to do with the given key combination.
716   ModuleBase_Operation* anOperation = currentOperation();
717   bool isAccepted = false;
718   switch (theEvent->key()) {
719     case Qt::Key_Escape: {
720       // processing in message box
721       if (myActiveMessageBox)
722       {
723         myActiveMessageBox->reject();
724         isAccepted = true;
725       }
726       // processing in the active widget
727       ModuleBase_Operation* aOperation = currentOperation();
728       if (!isAccepted && aOperation) {
729         ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
730         if (aPanel) {
731           ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
732           if (anActiveWgt)
733           {
734             isAccepted = anActiveWgt && anActiveWgt->processAction(ActionEscape);
735             if (isAccepted) {
736               ModuleBase_OperationFeature* aFOperation =
737                 dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
738               if (aFOperation)
739                 aFOperation->setNeedToBeAborted(true);
740             }
741           }
742         }
743       }
744       if (!isAccepted)
745       {
746         XGUI_ActiveControlSelector* anActiveSelector =
747           XGUI_Tools::workshop(myWorkshop)->activeControlMgr()->activeSelector();
748         if (anActiveSelector && anActiveSelector->getType() == XGUI_FacesPanelSelector::Type())
749           isAccepted = XGUI_Tools::workshop(myWorkshop)->facesPanel()->processAction(ActionEscape);
750       }
751       // default Escape button functionality
752       if (!isAccepted && aOperation) {
753         onAbortOperation();
754         isAccepted = true;
755       }
756     }
757     break;
758     case Qt::Key_H:
759       if ((theEvent->modifiers() == Qt::NoModifier))
760         myWorkshop->viewer()->showSelectionHighlight();
761       break;
762   }
763   return isAccepted;
764 }
765
766 bool XGUI_OperationMgr::onProcessEnter(QObject* theObject)
767 {
768   bool isAccepted = false;
769   ModuleBase_Operation* aOperation = currentOperation();
770   // to avoid enter processing when operation has not been started yet
771   if (!aOperation)
772     return isAccepted;
773   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
774   if (!aPanel)
775     return isAccepted;
776   // the next code is obsolete as we want to process Enter in property panel always
777   // only property panel enter is processed in order to do not process enter in application dialogs
778   //bool isPPChild = isChildObject(theObject, aPanel);
779   //if (!isPPChild)
780   //  return isAccepted;
781
782   ModuleBase_ModelWidget* anActiveWgt = aPanel->activeWidget();
783   bool isAborted = false;
784   if (!anActiveWgt) {
785     QWidget* aFocusWidget = aPanel->focusWidget();
786     QToolButton* aCancelBtn =
787       dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_CANCEL);
788     if (aFocusWidget && aCancelBtn && aFocusWidget == aCancelBtn) {
789       abortOperation(aOperation);
790       isAccepted = true;
791       isAborted = true;
792     }
793   }
794   if (!isAborted) {
795     isAccepted = anActiveWgt && anActiveWgt->processAction(ActionEnter);
796     if (!isAccepted) {
797       isAccepted =
798         myWorkshop->module()->processEnter(anActiveWgt ? anActiveWgt->attributeID() : "");
799       if (!isAccepted) {
800         /// functionality is similar to Apply click
801         ModuleBase_OperationFeature* aFOperation =
802           dynamic_cast<ModuleBase_OperationFeature*>(currentOperation());
803         if (!aFOperation ||
804             myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty()) {
805           // key released is emitted to apply the current value to the model
806           // if it was modified in PP
807           emit keyEnterReleased();
808           commitOperation();
809           isAccepted = true;
810         }
811         else
812           isAccepted = false;
813       }
814     }
815   }
816   return isAccepted;
817 }
818
819 bool editorControl(QObject* theObject)
820 {
821   QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>(theObject);
822   return aLineEdit;
823 }
824
825 bool XGUI_OperationMgr::onProcessDelete(QObject* theObject)
826 {
827   bool isAccepted = false;
828   ModuleBase_Operation* aOperation = currentOperation();
829   ModuleBase_ModelWidget* anActiveWgt = 0;
830   // firstly the widget should process Delete action
831   ModuleBase_IPropertyPanel* aPanel;
832   bool isPPChildObject = false;
833   if (aOperation) {
834     aPanel = aOperation->propertyPanel();
835     if (aPanel) {
836       isPPChildObject = isChildObject(theObject, aPanel);
837       // process delete in active widget only if delete sender is child of property panel
838       // it is necessary for the case when OB is shown, user perform selection and click Delete
839       if (isPPChildObject) {
840         anActiveWgt = aPanel->activeWidget();
841         if (anActiveWgt) {
842           isAccepted = anActiveWgt->processAction(ActionDelete);
843         }
844       }
845     }
846   }
847   if (!isAccepted)
848   {
849     XGUI_ActiveControlSelector* anActiveSelector =
850       XGUI_Tools::workshop(myWorkshop)->activeControlMgr()->activeSelector();
851     if (anActiveSelector && anActiveSelector->getType() == XGUI_FacesPanelSelector::Type())
852       isAccepted = XGUI_Tools::workshop(myWorkshop)->facesPanel()->processAction(ActionDelete);
853   }
854   if (!isAccepted) {
855     // after widget, object browser and viewer should process delete
856     /// other widgets such as line edit controls should not lead to
857     /// processing delete by workshop
858     XGUI_ObjectsBrowser* aBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
859     QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
860     bool isToDeleteObject = true;
861     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
862     XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
863     if (theObject == aBrowser->treeView()) {
864       aContextMenuMgr->updateObjectBrowserMenu();
865       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
866     }
867     else if (isChildObject(theObject, aViewPort)) {
868       aContextMenuMgr->updateViewerMenu();
869       isToDeleteObject = aContextMenuMgr->action("DELETE_CMD")->isEnabled();
870     }
871     else if (isPPChildObject) {
872       // property panel child object is processed to process delete performed on Apply button of PP
873       isToDeleteObject = true;
874     }
875     else if (editorControl(theObject)) {
876       isToDeleteObject = false; /// Line Edit of Rename operation in ObjectBrowser
877       isAccepted = true;
878     }
879
880     if (isToDeleteObject) {
881       aWorkshop->deleteObjects();
882       isAccepted = true;
883     }
884   }
885
886   return isAccepted;
887 }
888
889 bool XGUI_OperationMgr::isChildObject(const QObject* theObject, const QObject* theParent)
890 {
891   bool isPPChild = false;
892   if (theParent && theObject) {
893     QObject* aParent = (QObject*)theObject;
894     while (aParent ) {
895       isPPChild = aParent == theParent;
896       if (isPPChild)
897         break;
898       aParent = aParent->parent();
899     }
900   }
901   return isPPChild;
902 }
903
904 QMessageBox* XGUI_OperationMgr::createMessageBox(const QString& theMessage)
905 {
906   QMessageBox * aMessageBox = new QMessageBox(QMessageBox::Question,
907     QObject::tr("Abort operation"), theMessage, QMessageBox::Ok | QMessageBox::Cancel,
908     qApp->activeWindow());
909   aMessageBox->setDefaultButton(QMessageBox::Cancel);
910   aMessageBox->setEscapeButton(QMessageBox::No); // operation manager should process Esc key
911
912   return aMessageBox;
913 }
914
915 QMessageBox* XGUI_OperationMgr::createInformationBox(const QString& theMessage)
916 {
917   QMessageBox * aMessageBox = new QMessageBox(QMessageBox::Question,
918     QObject::tr("Validate operation"), theMessage, QMessageBox::Ok,
919     qApp->activeWindow());
920   aMessageBox->setDefaultButton(QMessageBox::Ok);
921   aMessageBox->setEscapeButton(QMessageBox::No); // operation manager should process Esc key
922
923   return aMessageBox;
924 }