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