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