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