Salome HOME
Introduce internal edit operation state in PartSet module in order to have a virtual...
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_Module.h"
4 #include "PartSet_WidgetSketchLabel.h"
5 #include "PartSet_Validators.h"
6 #include "PartSet_Tools.h"
7 #include "PartSet_WidgetPoint2d.h"
8 #include "PartSet_WidgetPoint2dDistance.h"
9 #include "PartSet_WidgetPoint2DFlyout.h"
10 #include "PartSet_WidgetShapeSelector.h"
11 #include "PartSet_WidgetPoint2dAngle.h"
12 #include "PartSet_WidgetMultiSelector.h"
13 #include "PartSet_WidgetEditor.h"
14 #include "PartSet_WidgetFileSelector.h"
15 #include "PartSet_WidgetSketchCreator.h"
16 #include "PartSet_SketcherMgr.h"
17 #include "PartSet_MenuMgr.h"
18 #include "PartSet_CustomPrs.h"
19 #include "PartSet_IconFactory.h"
20
21 #include "PartSet_Filters.h"
22 #include "PartSet_FilterInfinite.h"
23
24 #include <PartSetPlugin_Remove.h>
25 #include <PartSetPlugin_Part.h>
26 #include <PartSetPlugin_Duplicate.h>
27
28 #include <ModuleBase_Operation.h>
29 #include <ModuleBase_OperationAction.h>
30 #include <ModuleBase_IViewer.h>
31 #include <ModuleBase_IViewWindow.h>
32 #include <ModuleBase_IPropertyPanel.h>
33 #include <ModuleBase_WidgetEditor.h>
34 #include <ModuleBase_WidgetValidated.h>
35 #include <ModuleBase_FilterFactory.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_OperationFeature.h>
38
39 #include <GeomValidators_ShapeType.h>
40 #include <GeomValidators_Finite.h>
41 #include <GeomValidators_Face.h>
42 #include <GeomValidators_ConstructionComposite.h>
43 #include <GeomValidators_ZeroOffset.h>
44 #include <GeomValidators_BooleanArguments.h>
45 #include <GeomValidators_Different.h>
46 #include <GeomValidators_PartitionArguments.h>
47
48
49 #include <ModelAPI_Object.h>
50 #include <ModelAPI_Events.h>
51 #include <ModelAPI_Validator.h>
52 #include <ModelAPI_Data.h>
53 #include <ModelAPI_Session.h>
54 #include <GeomValidators_DifferentShapes.h>
55 #include <ModelAPI_ResultBody.h>
56 #include <ModelAPI_AttributeString.h>
57
58 #include <GeomDataAPI_Point2D.h>
59 #include <GeomDataAPI_Point.h>
60 #include <GeomDataAPI_Dir.h>
61
62 #include <XGUI_Displayer.h>
63 #include <XGUI_Workshop.h>
64 #include <XGUI_OperationMgr.h>
65 #include <XGUI_PropertyPanel.h>
66 #include <XGUI_ModuleConnector.h>
67 #include <XGUI_ContextMenuMgr.h>
68 #include <XGUI_Tools.h>
69 #include <XGUI_ObjectsBrowser.h>
70 #include <XGUI_SelectionMgr.h>
71 #include <XGUI_DataModel.h>
72 #include <XGUI_ErrorMgr.h>
73
74 #include <SketchPlugin_Feature.h>
75 #include <SketchPlugin_Sketch.h>
76 #include <SketchPlugin_Line.h>
77 #include <SketchPlugin_Arc.h>
78 #include <SketchPlugin_Circle.h>
79 #include <SketchPlugin_Point.h>
80 #include <SketchPlugin_ConstraintAngle.h>
81 #include <SketchPlugin_ConstraintLength.h>
82 #include <SketchPlugin_ConstraintDistance.h>
83 #include <SketchPlugin_ConstraintParallel.h>
84 #include <SketchPlugin_ConstraintPerpendicular.h>
85 #include <SketchPlugin_ConstraintRadius.h>
86
87 #include <SketcherPrs_SymbolPrs.h>
88 #include <SketcherPrs_Tools.h>
89
90 #include <Events_Loop.h>
91 #include <Config_PropManager.h>
92 #include <Config_Keywords.h>
93
94 #include <StdSelect_TypeOfFace.hxx>
95 #include <TopoDS_Vertex.hxx>
96 #include <TopoDS.hxx>
97 #include <TopoDS_Shape.hxx>
98 #include <BRep_Tool.hxx>
99 #include <AIS_Dimension.hxx>
100
101 #include <QObject>
102 #include <QMouseEvent>
103 #include <QString>
104 #include <QTimer>
105 #include <QApplication>
106 #include <QMessageBox>
107 #include <QMainWindow>
108 #include <QLineEdit>
109
110 #include <GeomAlgoAPI_FaceBuilder.h>
111 #include <GeomDataAPI_Dir.h>
112
113 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
114
115 #ifdef _DEBUG
116 #include <QDebug>
117 #endif
118
119 /*!Create and return new instance of XGUI_Module*/
120 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
121 {
122   return new PartSet_Module(theWshop);
123 }
124
125 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
126   : ModuleBase_IModule(theWshop),
127   myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true),
128   myIsInternalEditOperation(false)
129 {
130   new PartSet_IconFactory();
131
132   mySketchMgr = new PartSet_SketcherMgr(this);
133
134   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
135   XGUI_Workshop* aWorkshop = aConnector->workshop();
136
137   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
138   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
139   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
140           this, SLOT(onOperationActivatedByPreselection()));
141
142   ModuleBase_IViewer* aViewer = theWshop->viewer();
143   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
144           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
145   connect(aViewer, SIGNAL(viewTransformed(int)),
146           SLOT(onViewTransformed(int)));
147   connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
148           SLOT(onViewCreated(ModuleBase_IViewWindow*)));
149
150   myMenuMgr = new PartSet_MenuMgr(this);
151   myCustomPrs = new PartSet_CustomPrs(theWshop);
152
153   Events_Loop* aLoop = Events_Loop::loop();
154   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
155
156   mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
157   mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
158 }
159
160 PartSet_Module::~PartSet_Module()
161 {
162   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
163   for (; aIt.More(); aIt.Next()) {
164     Handle(SelectMgr_Filter) aFilter = aIt.Value();
165     if (!aFilter.IsNull())
166       aFilter.Nullify();
167   }
168   delete myCustomPrs;
169 }
170
171 void PartSet_Module::activateSelectionFilters()
172 {
173   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
174   for (; aIt.More(); aIt.Next()) {
175     Handle(SelectMgr_Filter) aFilter = aIt.Value();
176     if (!aFilter.IsNull())
177       myWorkshop->viewer()->addSelectionFilter(aFilter);
178   }
179 }
180
181 void PartSet_Module::deactivateSelectionFilters()
182 {
183   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
184   for (; aIt.More(); aIt.Next()) {
185     Handle(SelectMgr_Filter) aFilter = aIt.Value();
186     if (!aFilter.IsNull())
187       myWorkshop->viewer()->removeSelectionFilter(aFilter);
188   }
189 }
190
191 void PartSet_Module::registerValidators()
192 {
193   //Registering of validators
194   SessionPtr aMgr = ModelAPI_Session::get();
195   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
196   aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
197   aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
198   aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
199   aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
200   aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
201   aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
202   aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
203   aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
204   aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
205   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
206   aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
207   aFactory->registerValidator("PartSet_EqualSelection", new PartSet_EqualSelection);
208   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
209   aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
210
211   aFactory->registerValidator("GeomValidators_DifferentShapes", new GeomValidators_DifferentShapes);
212   aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
213   aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
214   aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
215
216   aFactory->registerValidator("GeomValidators_ConstructionComposite",
217                               new GeomValidators_ConstructionComposite);
218
219   aFactory->registerValidator("GeomValidators_ZeroOffset",
220                               new GeomValidators_ZeroOffset);
221
222   aFactory->registerValidator("GeomValidators_BooleanArguments",
223                               new GeomValidators_BooleanArguments);
224
225   aFactory->registerValidator("PartSet_SketchEntityValidator",
226                               new PartSet_SketchEntityValidator);
227
228   aFactory->registerValidator("GeomValidators_Different",
229                               new GeomValidators_Different);
230
231   aFactory->registerValidator("GeomValidators_PartitionArguments",
232                               new GeomValidators_PartitionArguments);
233 }
234
235 void PartSet_Module::registerFilters()
236 {
237   //Registering of selection filters
238   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
239   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
240 }
241
242 void PartSet_Module::registerProperties()
243 {
244   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
245                                    PLANE_SIZE);
246   Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
247                                    Config_Prop::Integer, SKETCH_WIDTH);
248 }
249
250 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation) 
251 {
252   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
253     mySketchMgr->commitNestedSketch(theOperation);
254   }
255
256   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
257   if (!aFOperation)
258     return;
259   // the selection is cleared after commit the create operation
260   // in order to do not use the same selected objects in the restarted operation
261   // for common behaviour, the selection is cleared even if the operation is not restarted
262   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
263   XGUI_Workshop* aWorkshop = aConnector->workshop();
264   aWorkshop->selector()->clearSelection();
265
266   /// Restart sketcher operations automatically
267   FeaturePtr aFeature = aFOperation->feature();
268   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
269             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
270   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
271                      myRestartingMode == RM_EmptyFeatureUsed)) {
272     myLastOperationId = aFOperation->id();
273     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
274     if (!sketchMgr()->sketchSolverError()) {
275       if (!aFOperation->isEditOperation()) {
276         FeaturePtr anOperationFeature = aFOperation->feature();
277         if (anOperationFeature.get() != NULL) {
278           editFeature(anOperationFeature);
279           myIsInternalEditOperation = true;
280           onInternalActivateFirstWidgetSelection();
281
282           // activate the last active widget in the Property Panel
283           if (!myPreviousAttributeID.empty()) {
284             ModuleBase_Operation* anEditOperation = currentOperation();
285             if (anEditOperation) {
286               ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
287               ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
288               QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
289               for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
290                 if (aWidgets[i]->attributeID() == myPreviousAttributeID)
291                   aPreviousAttributeWidget = aWidgets[i];
292               }
293               // If the current widget is a selector, do nothing, it processes the mouse press
294               if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
295                 aPreviousAttributeWidget->focusTo();
296             }
297           }
298         }
299       }
300       else {
301         // the flag should be reset before start to do not react to the widget deactivate
302         myIsInternalEditOperation = false;
303         launchOperation(myLastOperationId);
304         breakOperationSequence();
305       }
306     }
307   }
308 }
309
310 void PartSet_Module::breakOperationSequence()
311 {
312   myLastOperationId = "";
313   myLastFeature = FeaturePtr();
314   myRestartingMode = RM_None;
315 }
316
317 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
318 {
319   myIsInternalEditOperation = false;
320   breakOperationSequence();
321 }
322
323 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
324 {
325   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
326     mySketchMgr->startSketch(theOperation);
327   }
328   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
329     mySketchMgr->startNestedSketch(theOperation);
330   }
331
332   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
333   if (aFOperation)
334     myCustomPrs->activate(aFOperation->feature(), true);
335 }
336
337 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
338 {
339   ModuleBase_IModule::onOperationResumed(theOperation);
340
341   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
342   if (aFOperation)
343     myCustomPrs->activate(aFOperation->feature(), true);
344 }
345
346 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
347 {
348   bool isModified = myCustomPrs->deactivate(false);
349
350   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
351     mySketchMgr->stopSketch(theOperation);
352   }
353   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
354     mySketchMgr->stopNestedSketch(theOperation);
355   }
356
357   //VSV: Viewer is updated on feature update and redisplay
358   //if (isModified) {
359   //  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
360   //  XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
361   //  aDisplayer->updateViewer();
362   //}
363   mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
364 }
365
366 ModuleBase_Operation* PartSet_Module::currentOperation() const
367 {
368   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
369   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
370   return anOpMgr->currentOperation();
371 }
372
373 bool PartSet_Module::canUndo() const
374 {
375   bool aCanUndo = false;
376   SessionPtr aMgr = ModelAPI_Session::get();
377   if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
378     aCanUndo = !aMgr->isOperation();
379     if (!aCanUndo) // check the enable state additionally by sketch manager
380       aCanUndo = aMgr->canUndo();
381   }
382   return aCanUndo;
383 }
384
385 bool PartSet_Module::canRedo() const
386 {
387   bool aCanRedo = false;
388   SessionPtr aMgr = ModelAPI_Session::get();
389   if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
390     aCanRedo = !aMgr->isOperation();
391     if (!aCanRedo) // check the enable state additionally by sketch manager
392       aCanRedo = aMgr->canRedo();
393   }
394   return aCanRedo;
395 }
396
397 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
398 {
399   bool aValid = true;
400   if (theActionId == "DELETE_CMD" || theActionId == "MOVE_CMD") {
401     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
402     if (aFeature) {
403       // part features are removed in the PartSet module only.
404       if (aFeature->getKind() == PartSetPlugin_Part::ID())
405         aValid = false;
406     }
407   }
408   return aValid;
409 }
410
411 bool PartSet_Module::canCommitOperation() const
412 {
413   return mySketchMgr->canCommitOperation();
414 }
415
416 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
417 {
418   // the sketch manager put the restriction to the objects erase
419   return mySketchMgr->canEraseObject(theObject);
420 }
421
422 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
423 {
424   // the sketch manager put the restriction to the objects display
425   return mySketchMgr->canDisplayObject(theObject);
426 }
427
428 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
429 {
430   bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
431
432   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
433   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
434        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
435   if (isSketchOp || isNestedOp) {
436     // in active sketch operation it is possible to activate operation object in selection
437     // in the edit operation, e.g. points of the line can be moved when the line is edited
438     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
439     aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
440   }
441   return aCanActivate;
442 }
443
444 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
445 {
446   return myMenuMgr->addViewerMenu(theMenu, theStdActions);
447 }
448
449 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
450 {
451   myMenuMgr->updateViewerMenu(theStdActions);
452 }
453
454 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
455 {
456   QString anError = ModuleBase_IModule::getFeatureError(theFeature);
457
458   if (anError.isEmpty())
459     anError = sketchMgr()->getFeatureError(theFeature);
460
461   if (anError.isEmpty()) {
462     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
463     XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
464     
465     if (anOpMgr->isValidationLocked()) {
466       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
467                                                              (anOpMgr->currentOperation());
468       if (!aFOperation || theFeature == aFOperation->feature())
469         anError = "Validation is locked by the current operation";
470     }
471   }
472   return anError;
473 }
474
475 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
476                                          QStringList& theIds) const
477 {
478   myMenuMgr->grantedOperationIds(theOperation, theIds);
479
480   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
481     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
482     XGUI_Workshop* aWorkshop = aConnector->workshop();
483
484     theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
485   }
486 }
487
488 void PartSet_Module::activeSelectionModes(QIntList& theModes)
489 {
490   theModes.clear();
491   if (mySketchMgr->activeSketch().get())
492     PartSet_SketcherMgr::sketchSelectionModes(theModes);
493 }
494
495 bool PartSet_Module::isMouseOverWindow()
496 {
497   return mySketchMgr->isMouseOverWindow();
498 }
499
500 void PartSet_Module::closeDocument()
501 {
502   clearViewer();
503 }
504
505 void PartSet_Module::clearViewer()
506 {
507   myCustomPrs->clearPrs();
508
509   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
510   XGUI_Workshop* aWorkshop = aConnector->workshop();
511   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
512   aDisplayer->deactivateSelectionFilters();
513 }
514
515 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
516 {
517   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
518   if (!aFOperation)
519     return;
520
521   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
522   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation())) {
523     // we have to manually activate the sketch label in edit mode
524       aPanel->activateWidget(aPanel->modelWidgets().first());
525       return;
526   }
527
528   // Restart last operation type 
529   if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
530     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
531     if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
532       // Initialise new line with first point equal to end of previous
533       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
534       if (aPnt2dWgt) {
535         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
536         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
537           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
538         if (aPoint) {
539           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
540           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(), 
541             aWgt->attributeID(), aPoint->x(), aPoint->y());
542           aPanel->activateNextWidget(aPnt2dWgt);
543         }
544       }
545     }
546   }
547 }
548
549
550 void PartSet_Module::onSelectionChanged()
551 {
552   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
553   if (!aOperation)
554     return;
555
556   bool isSketcherOp = false;
557   // An edit operation is enable only if the current opeation is the sketch operation
558   if (mySketchMgr->activeSketch()) {
559     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
560       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
561   }
562   if (isSketcherOp) {
563     // Editing of constraints can be done on selection
564     ModuleBase_ISelection* aSelect = myWorkshop->selection();
565     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
566     if (aSelected.size() == 1) {
567       ModuleBase_ViewerPrs aPrs = aSelected.first();
568       ObjectPtr aObject = aPrs.object();
569       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
570       if (aFeature) {
571         std::string aId = aFeature->getKind();
572         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
573             (aId == SketchPlugin_ConstraintLength::ID()) || 
574             (aId == SketchPlugin_ConstraintDistance::ID()) ||
575             (aId == SketchPlugin_ConstraintAngle::ID())) {
576           editFeature(aFeature);
577         }
578       }
579     }
580   } 
581 }
582
583 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
584 {
585   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
586   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
587   anOpMgr->onKeyReleased(theEvent);
588 }
589
590 void PartSet_Module::onEnterReleased()
591 {
592   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
593                                                                   (currentOperation());
594   if (/*!aFOperation->isEditOperation() || */myIsInternalEditOperation)
595     myRestartingMode = RM_EmptyFeatureUsed;
596 }
597
598 void PartSet_Module::onOperationActivatedByPreselection()
599 {
600   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
601   if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
602     // Set final definitions if they are necessary
603     //propertyPanelDefined(aOperation);
604
605     /// Commit sketcher operations automatically
606     anOperation->commit();
607   }
608 }
609
610 void PartSet_Module::onNoMoreWidgets(const std::string& thePreviousAttributeID)
611 {
612   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
613   if (anOperation) {
614     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
615       if (myRestartingMode != RM_Forbided) {
616         myRestartingMode = RM_LastFeatureUsed;
617         myPreviousAttributeID = thePreviousAttributeID;
618       }
619       XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
620       XGUI_Workshop* aWorkshop = aConnector->workshop();
621       XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
622       // do nothing if the feature can not be applyed
623       if (anOpMgr->isApplyEnabled())
624         anOperation->commit();
625     }
626   }
627 }
628
629 void PartSet_Module::onInternalActivateFirstWidgetSelection()
630 {
631   if (!myIsInternalEditOperation)
632     return;
633
634   ModuleBase_ModelWidget* aFirstWidget = activeWidget();
635   ModuleBase_IPropertyPanel* aPanel = currentOperation()->propertyPanel();
636   if (aFirstWidget != aPanel->activeWidget()) {
637     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
638     if (aWSelector)
639       aWSelector->activateSelectionAndFilters(true);
640   }
641 }
642
643 void PartSet_Module::onVertexSelected()
644 {
645   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
646   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
647     /// If last line finished on vertex the lines creation sequence has to be break
648     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
649     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
650     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
651     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
652     bool aFoundWidget = false;
653     bool aFoundObligatory = false;
654     for (; anIt != aLast && !aFoundObligatory; anIt++) {
655       if (!aFoundWidget)
656         aFoundWidget = *anIt == anActiveWidget;
657       else
658         aFoundObligatory = (*anIt)->isObligatory();
659     }
660     if (!aFoundObligatory)
661       myRestartingMode = RM_Forbided;
662   }
663 }
664
665 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
666                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
667 {
668   ModuleBase_IWorkshop* aWorkshop = workshop();
669   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
670   XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
671   ModuleBase_ModelWidget* aWgt = NULL;
672   if (theType == "sketch-start-label") {
673     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
674       theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
675     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
676       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
677     connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
678       mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
679     aWgt = aLabelWgt;
680   } else if (theType == "sketch-2dpoint_selector") {
681     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
682                                                                  theWidgetApi, theParentId);
683     aPointWgt->setSketch(mySketchMgr->activeSketch());
684     connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
685     aWgt = aPointWgt;
686   } else if (theType == "sketch-2dpoint_flyout_selector") {
687     PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop,
688                                                                  theWidgetApi, theParentId);
689     aPointWgt->setSketch(mySketchMgr->activeSketch());
690     connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
691     aWgt = aPointWgt;
692   } else if (theType == "point2ddistance") {
693     PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
694                                                         aWorkshop, theWidgetApi, theParentId);
695     aDistanceWgt->setSketch(mySketchMgr->activeSketch());
696     aWgt = aDistanceWgt;
697   } else if(theType == "point2dangle") {
698     PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
699                                                            aWorkshop, theWidgetApi, theParentId);
700     anAngleWgt->setSketch(mySketchMgr->activeSketch());
701     aWgt = anAngleWgt;
702   } else if (theType == "sketch_shape_selector") {
703     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
704       new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
705     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
706     aWgt = aShapeSelectorWgt;
707   } else if (theType == "sketch_multi_selector") {
708     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
709       new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
710     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
711     aWgt = aShapeSelectorWgt;
712   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
713     aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
714   } else if (theType == "export_file_selector") {
715     aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
716   } else if (theType == "sketch_launcher") {
717     aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
718   }
719   return aWgt;
720 }
721
722 ModuleBase_ModelWidget* PartSet_Module::activeWidget() const
723 {
724   ModuleBase_ModelWidget* anActiveWidget = 0;
725   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
726   if (aOperation) {
727     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
728     anActiveWidget = aPanel->activeWidget();
729     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
730       // finds the first widget which can accept a value
731       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
732       ModuleBase_ModelWidget* aFirstWidget = 0;
733       ModuleBase_ModelWidget* aWgt;
734       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
735       for (aWIt = aWidgets.begin(); aWIt != aWidgets.end() && !aFirstWidget; ++aWIt) {
736         aWgt = (*aWIt);
737         if (aWgt->canSetValue())
738           aFirstWidget = aWgt;
739       }
740       if (aFirstWidget)
741         anActiveWidget = aFirstWidget;
742     }
743   }
744
745   return anActiveWidget;
746 }
747
748 bool PartSet_Module::deleteObjects()
749 {
750   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
751   XGUI_Workshop* aWorkshop = aConnector->workshop();
752   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
753
754   //SessionPtr aMgr = ModelAPI_Session::get();
755   // 1. check whether the delete should be processed in the module
756   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
757   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
758        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
759   if (isSketchOp || isNestedOp) {
760     // 2. find selected presentations
761     // selected objects should be collected before the current operation abort because
762     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
763     ModuleBase_ISelection* aSel = workshop()->selection();
764     QObjectPtrList aSelectedObj = aSel->selectedPresentations();
765     // if there are no selected objects in the viewer, that means that the selection in another
766     // place cased this method. It is necessary to return the false value to understande in above
767     // method that delete is not processed
768     if (aSelectedObj.count() == 0)
769       return false;
770
771     // avoid delete of the objects, which are not belong to the current sketch
772     // in order to do not delete results of other sketches
773     QObjectPtrList aSketchObjects;
774     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
775     for ( ; anIt != aLast; anIt++) {
776       ObjectPtr anObject = *anIt;
777       if (mySketchMgr->isObjectOfSketch(anObject)) {
778         // sketch feature should be used in this list because workshop deletes features only
779         // results are skipped
780         FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
781         aSketchObjects.append(aSketchFeature);
782       }
783     }
784     // if the selection contains only local selected presentations from other sketches,
785     // the Delete operation should not be done at all
786     if (aSketchObjects.size() == 0)
787       return true;
788
789     // 3. start operation
790     QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
791     ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
792
793     // the active nested sketch operation should be aborted unconditionally
794     // the Delete action should be additionally granted for the Sketch operation
795     // in order to do not abort/commit it
796     if (!anOpMgr->canStartOperation(anOpAction->id()))
797       return true; // the objects are processed but can not be deleted
798
799     anOpMgr->startOperation(anOpAction);
800
801     // 4. delete features
802     // sketch feature should be skipped, only sub-features can be removed
803     // when sketch operation is active
804     aWorkshop->deleteFeatures(aSketchObjects);
805     // 5. stop operation
806     anOpMgr->commitOperation();
807   }
808   else {
809     bool isPartRemoved = false;
810     // Delete part with help of PartSet plugin
811     // TODO: the deleted objects has to be processed by multiselection
812     QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
813     if (aObjects.size() == 1) {
814       ObjectPtr aObj = aObjects.first();
815       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
816       if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
817         // Remove feature should be created in the document of the part results
818         ResultPtr aPartResult = aFeature->firstResult();
819         if (aPartResult.get()) {
820           std::shared_ptr<ModelAPI_ResultPart> aPart =
821                        std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
822           DocumentPtr aPartDoc = aPart->partDoc();
823           if (aPartDoc.get()) {
824             ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
825                                               (PartSetPlugin_Remove::ID().c_str(), this);
826             if (!anOpMgr->canStartOperation(anOpAction->id()))
827               return true; // the objects are processed but can not be deleted
828
829             anOpMgr->startOperation(anOpAction);
830
831             FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
832             aFeature->execute();
833
834             anOpMgr->commitOperation();
835             isPartRemoved = true;
836           }
837         }
838       }
839     }
840     return isPartRemoved;
841   }
842   return true;
843 }
844
845 void PartSet_Module::onFeatureTriggered()
846 {
847   QAction* aCmd = dynamic_cast<QAction*>(sender());
848   if (aCmd->isCheckable() && aCmd->isChecked()) {
849     // 1. check whether the delete should be processed in the module
850     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
851     bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
852     if (isNestedOp) {
853       // in case if in the viewer nothing is displayed, the create operation should not be
854       // comitted even if all values of the feature are initialized
855       if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
856         // the action information should be saved before the operation is aborted
857         // because this abort leads to update command status, which unchecks this action
858         anOperation->abort();
859         launchOperation(aCmd->data().toString());
860       }
861     }
862   }
863   ModuleBase_IModule::onFeatureTriggered();
864 }
865
866 void PartSet_Module::launchOperation(const QString& theCmdId)
867 {
868   if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
869     // Show constraints if a constraint was anOperation
870     myHasConstraintShown = mySketchMgr->isConstraintsShown();
871     mySketchMgr->onShowConstraintsToggle(true);
872   }
873   ModuleBase_IModule::launchOperation(theCmdId);
874 }
875
876
877 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS) 
878 {
879   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
880   if (!anAIS.IsNull()) {
881     Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
882     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
883     if (!aDim.IsNull()) {
884       aCtx->SetZLayer(aDim, myVisualLayerId);
885     } else {
886       Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
887       if (!aCons.IsNull())
888         aCtx->SetZLayer(aCons, myVisualLayerId);
889     }
890   }
891 }
892
893 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
894 {
895   // this is obsolete
896   // it should be recomputed in order to disappear in the viewer if the corresponded object
897   // is erased
898   //if (myCustomPrs->isActive())
899   //  myCustomPrs->redisplay(theObject, false);
900 }
901
902 void PartSet_Module::onViewTransformed(int theTrsfType)
903 {
904   // Set length of arrows constant in pixel size
905   // if the operation is panning or rotate or panglobal then do nothing
906   if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
907     return;
908   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
909   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
910   if (aContext.IsNull())
911     return;
912
913   //Handle(V3d_View) aView = aViewer->activeView();
914
915   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
916   XGUI_Workshop* aWorkshop = aConnector->workshop();
917   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
918   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
919   Handle(V3d_View) aView;
920   double aScale = 0;
921   for (aV3dViewer->InitDefinedViews(); 
922        aV3dViewer->MoreDefinedViews(); 
923        aV3dViewer->NextDefinedViews()) {
924     Handle(V3d_View) aV = aV3dViewer->DefinedView();
925     double aS = aV->Scale();
926     if (aS > aScale) {
927       aScale = aS;
928       aView = aV;
929     }
930   }
931   if (aView.IsNull())
932     return;
933   double aLen = aView->Convert(20);
934
935   double aPrevLen = SketcherPrs_Tools::getArrowSize();
936   SketcherPrs_Tools::setArrowSize(aLen);
937   const double aPrevScale = aViewer->Scale(aViewer->activeView());
938   const double aCurScale = aViewer->activeView()->Camera()->Scale();
939   aViewer->SetScale(aViewer->activeView(), aCurScale);
940   SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
941   bool isModified = false;
942   QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
943   foreach (AISObjectPtr aAIS, aPrsList) {
944     Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
945
946     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
947     if (!aDim.IsNull()) {
948       aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
949       aContext->Redisplay(aDim, false);
950       isModified = true;
951     }
952   }
953   if (isModified)
954     aDisplayer->updateViewer();
955 }
956
957 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
958 {
959   bool isRedisplayed = false;
960   if (myCustomPrs->isActive())
961     isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
962
963   return isRedisplayed;
964 }
965
966 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
967 {
968   XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
969   if (aOB) {
970     //QLineEdit* aLabel = aOB->activeDocLabel();
971     //QPalette aPalet = aLabel->palette();
972     //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
973     //aLabel->setPalette(aPalet);
974     aOB->treeView()->setExpandsOnDoubleClick(false);
975     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)), 
976       SLOT(onTreeViewDoubleClick(const QModelIndex&)));
977   }
978 }
979
980 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
981 {
982   ObjectPtr anObject;
983   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
984   if (aOperation) {
985     /// If last line finished on vertex the lines creation sequence has to be break
986     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
987     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
988     // if there is an active widget, find the presented object in it
989     if (!anActiveWidget)
990       anActiveWidget = aPanel->preselectionWidget();
991     
992     ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
993                                                                            (anActiveWidget);
994     if (aWidgetValidated)
995       anObject = aWidgetValidated->findPresentedObject(theAIS);
996   }
997   return anObject;
998 }
999
1000 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
1001 {
1002   QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
1003   int aSelected = aObjects.size();
1004   SessionPtr aMgr = ModelAPI_Session::get();
1005   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
1006   QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
1007
1008   ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
1009   if (aSelected == 1) {
1010     bool hasResult = false;
1011     bool hasFeature = false;
1012     bool hasParameter = false;
1013     bool hasSubFeature = false;
1014     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
1015
1016     ObjectPtr aObject = aObjects.first();
1017     if (aObject) {
1018       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1019       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
1020       bool isPart = aPart.get() || 
1021         (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
1022       if (isPart) {
1023         DocumentPtr aPartDoc;
1024         if (!aPart.get()) {
1025           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
1026         }
1027         if (aPart.get()) // this may be null is Part feature is disabled
1028           aPartDoc = aPart->partDoc();
1029           
1030         theMenu->addAction(aActivatePartAction);
1031         aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
1032
1033       } else if (aObject->document() == aMgr->activeDocument()) {
1034         if (hasParameter || hasFeature) {
1035           myMenuMgr->action("EDIT_CMD")->setEnabled(true);
1036           theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
1037           if (aCurrentOp && aFeature.get()) {
1038             if (aCurrentOp->id().toStdString() == aFeature->getKind())
1039               myMenuMgr->action("EDIT_CMD")->setEnabled(false);
1040           }
1041         }
1042       }
1043
1044       ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
1045       if( aResult.get() )
1046         theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
1047     } else {  // If feature is 0 the it means that selected root object (document)
1048       theMenu->addAction(aActivatePartSetAction);
1049       aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
1050     }
1051   } else if (aSelected == 0) {
1052     // if there is no selection then it means that upper label is selected
1053     QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
1054     if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
1055       theMenu->addAction(aActivatePartSetAction);
1056       aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
1057   }
1058   bool aNotDeactivate = (aCurrentOp == 0);
1059   if (!aNotDeactivate) {
1060     aActivatePartAction->setEnabled(false);
1061     aActivatePartSetAction->setEnabled(false);
1062   }
1063 }
1064
1065 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
1066 {
1067   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
1068     // Do not change activation of parts if an operation active
1069     static QStringList aAllowActivationList;
1070     if (aAllowActivationList.isEmpty())
1071       aAllowActivationList << 
1072       QString(PartSetPlugin_Part::ID().c_str()) << 
1073       QString(PartSetPlugin_Duplicate::ID().c_str()) <<
1074       QString(PartSetPlugin_Remove::ID().c_str());
1075     if (myWorkshop->currentOperation() && 
1076       (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
1077       return;
1078     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1079     XGUI_Workshop* aWorkshop = aConnector->workshop();
1080     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
1081     QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
1082     QPalette aPalet = aLabel->palette();
1083
1084     SessionPtr aMgr = ModelAPI_Session::get();
1085     DocumentPtr aActiveDoc = aMgr->activeDocument();
1086     if (aActivePartIndex.isValid())
1087       aTreeView->setExpanded(aActivePartIndex, false);
1088     XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1089     aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
1090     if (aActivePartIndex.isValid())
1091       aTreeView->setExpanded(aActivePartIndex, true);
1092
1093     aLabel->setPalette(aPalet);
1094     aWorkshop->updateCommandStatus();
1095
1096     // Update displayed objects in order to update active color
1097     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1098     QObjectPtrList aObjects = aDisplayer->displayedObjects();
1099     bool aHidden;
1100     foreach(ObjectPtr aObj, aObjects) {
1101       //TODO: replace by redisplay event.
1102       aHidden = !aObj->data() || !aObj->data()->isValid() || 
1103         aObj->isDisabled() || (!aObj->isDisplayed());
1104       if (!aHidden)
1105         aDisplayer->redisplay(aObj, false);
1106     }
1107     aDisplayer->updateViewer();
1108   }
1109 }
1110
1111 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1112 {
1113   if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1114     return;
1115   SessionPtr aMgr = ModelAPI_Session::get();
1116   if (!theIndex.isValid()) {
1117     aMgr->setActiveDocument(aMgr->moduleDocument());
1118     return;
1119   }
1120   if (theIndex.column() != 0) // Use only first column
1121     return;
1122
1123   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1124   XGUI_Workshop* aWorkshop = aConnector->workshop();
1125   XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1126   // De not use non editable Indexes
1127   if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1128     return;
1129   ObjectPtr aObj = aDataModel->object(theIndex);
1130
1131   ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1132   if (!aPart.get()) { // Probably this is Feature
1133     FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1134     if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1135       aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1136     }
1137   }
1138   if (aPart.get()) { // if this is a part
1139     if (aPart->partDoc() == aMgr->activeDocument()) {
1140       myMenuMgr->activatePartSet();
1141     } else {
1142       aPart->activate();
1143     }
1144   }
1145 }
1146
1147
1148 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1149 {
1150   // z layer is created for all started operations in order to visualize operation AIS presentation
1151   // over the object
1152   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1153   if (aContext.IsNull())
1154     return;
1155
1156   Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1157   if (myVisualLayerId == 0) {
1158     if (myVisualLayerId == 0)
1159       aViewer->AddZLayer(myVisualLayerId);
1160   } else {
1161     TColStd_SequenceOfInteger aZList;
1162     aViewer->GetAllZLayers(aZList);
1163     bool aFound = false;
1164     for (int i = 1; i <= aZList.Length(); i++) {
1165       if (aZList(i) == myVisualLayerId) {
1166         aFound = true;
1167         break;
1168       }
1169     }
1170     if (!aFound)
1171       aViewer->AddZLayer(myVisualLayerId);
1172   }
1173   // if there is an active operation with validated widget,
1174   // the filters of this widget should be activated in the created view
1175   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1176   if (aOperation) {
1177     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
1178     if (anActiveWidget) {
1179       ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(anActiveWidget);
1180       if (aWSelector)
1181         aWSelector->activateSelectionAndFilters(true);
1182     }
1183   }
1184 }