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