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