]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
Issue #3221: Visual separation for features
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_Module.h"
21 #include "PartSet_WidgetSketchLabel.h"
22 #include "PartSet_Validators.h"
23 #include "PartSet_Tools.h"
24 #include "PartSet_PreviewPlanes.h"
25 #include "PartSet_WidgetBSplinePoints.h"
26 #include "PartSet_WidgetPoint2d.h"
27 #include "PartSet_WidgetPoint2DFlyout.h"
28 #include "PartSet_WidgetShapeSelector.h"
29 #include "PartSet_WidgetMultiSelector.h"
30 #include "PartSet_WidgetFeaturePointSelector.h"
31 #include "PartSet_WidgetEditor.h"
32 #include "PartSet_WidgetFileSelector.h"
33 #include "PartSet_WidgetSketchCreator.h"
34 #include "PartSet_SketcherMgr.h"
35 #include "PartSet_SketcherReentrantMgr.h"
36 #include "PartSet_ResultSketchPrs.h"
37 #include "PartSet_MenuMgr.h"
38 #include "PartSet_CustomPrs.h"
39 #include "PartSet_IconFactory.h"
40 #include "PartSet_OverconstraintListener.h"
41 #include "PartSet_TreeNodes.h"
42 #include "PartSet_FieldStepPrs.h"
43 #include "PartSet_BSplineWidget.h"
44
45 #include "PartSet_Filters.h"
46 #include "PartSet_FilterInfinite.h"
47
48 #ifdef _DEBUG
49 #include <QDebug>
50 #endif
51
52 #include <PartSetPlugin_Remove.h>
53 #include <PartSetPlugin_Part.h>
54 #include <PartSetPlugin_Duplicate.h>
55
56 #include <SketchPlugin_ConstraintCoincidence.h>
57
58 #include <ModuleBase_Operation.h>
59 #include <ModuleBase_IViewer.h>
60 #include <ModuleBase_IViewWindow.h>
61 #include <ModuleBase_IPropertyPanel.h>
62 #include <ModuleBase_ISelectionActivate.h>
63 #include <ModuleBase_WidgetChoice.h>
64 #include <ModuleBase_WidgetEditor.h>
65 #include <ModuleBase_WidgetValidated.h>
66 #include <ModuleBase_Tools.h>
67 #include <ModuleBase_OperationFeature.h>
68 #include <ModuleBase_WidgetFactory.h>
69 #include <ModuleBase_OperationDescription.h>
70 #include <ModuleBase_ViewerPrs.h>
71 #include <ModuleBase_ResultPrs.h>
72
73 #include <ModelAPI_ResultField.h>
74 #include <ModelAPI_Object.h>
75 #include <ModelAPI_Events.h>
76 #include <ModelAPI_Validator.h>
77 #include <ModelAPI_Data.h>
78 #include <ModelAPI_Session.h>
79 #include <ModelAPI_ResultBody.h>
80 #include <ModelAPI_AttributeString.h>
81 #include <ModelAPI_AttributeSelectionList.h>
82 #include <ModelAPI_Tools.h>
83 #include <ModelAPI_ResultConstruction.h>
84 #include <ModelAPI_AttributeIntArray.h>
85 #include <ModelAPI_ResultGroup.h>
86
87 #include <GeomDataAPI_Point2D.h>
88 #include <GeomDataAPI_Point.h>
89 #include <GeomDataAPI_Dir.h>
90
91 #include <XGUI_ActiveControlMgr.h>
92 #include <XGUI_ActiveControlSelector.h>
93 #include <XGUI_ActionsMgr.h>
94 #include <XGUI_ContextMenuMgr.h>
95 #include <XGUI_DataModel.h>
96 #include <XGUI_Displayer.h>
97 #include <XGUI_ErrorMgr.h>
98 #include <XGUI_FacesPanelSelector.h>
99 #include <XGUI_ModuleConnector.h>
100 #include <XGUI_ObjectsBrowser.h>
101 #include <XGUI_OperationMgr.h>
102 #include <XGUI_PropertyPanel.h>
103 #include <XGUI_SelectionMgr.h>
104 #include <XGUI_Tools.h>
105 #include <XGUI_Workshop.h>
106
107 #include <SketchPlugin_ConstraintAngle.h>
108 #include <SketchPlugin_ConstraintLength.h>
109 #include <SketchPlugin_ConstraintDistance.h>
110 #include <SketchPlugin_ConstraintParallel.h>
111 #include <SketchPlugin_ConstraintPerpendicular.h>
112 #include <SketchPlugin_ConstraintRadius.h>
113 #include <SketchPlugin_Feature.h>
114 #include <SketchPlugin_Projection.h>
115 #include <SketchPlugin_Sketch.h>
116
117 #include <SketcherPrs_SymbolPrs.h>
118 #include <SketcherPrs_PositionMgr.h>
119 #include <SketcherPrs_Coincident.h>
120 #include <SketcherPrs_Tools.h>
121
122 #include <Events_Loop.h>
123 #include <Config_PropManager.h>
124 #include <Config_Keywords.h>
125
126 #include <AIS_Dimension.hxx>
127 #include <AIS_InteractiveObject.hxx>
128 #include <StdSelect_TypeOfFace.hxx>
129 #include <TopoDS_Vertex.hxx>
130 #include <TopoDS.hxx>
131 #include <TopoDS_Shape.hxx>
132 #include <BRep_Tool.hxx>
133
134 #include <QObject>
135 #include <QMouseEvent>
136 #include <QString>
137 #include <QTimer>
138 #include <QApplication>
139 #include <QMessageBox>
140 #include <QMainWindow>
141 #include <QLineEdit>
142 #include <QString>
143
144 #include <GeomAlgoAPI_FaceBuilder.h>
145 #include <GeomDataAPI_Dir.h>
146
147 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
148
149 #define FEATURE_ITEM_COLOR "0,0,225"
150
151
152 /*!Create and return new instance of XGUI_Module*/
153 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
154 {
155   return new PartSet_Module(theWshop);
156 }
157
158 //******************************************************
159 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
160 : ModuleBase_IModule(theWshop),
161   myIsOperationIsLaunched(false),
162   myVisualLayerId(0),
163   myRoot(0)
164 {
165   new PartSet_IconFactory(this);
166
167   mySketchMgr = new PartSet_SketcherMgr(this);
168   mySketchReentrantMgr = new PartSet_SketcherReentrantMgr(theWshop);
169
170   ModuleBase_IViewer* aViewer = theWshop->viewer();
171   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
172           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
173   connect(aViewer, SIGNAL(viewTransformed(int)),
174           SLOT(onViewTransformed(int)));
175   connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
176           SLOT(onViewCreated(ModuleBase_IViewWindow*)));
177   myMenuMgr = new PartSet_MenuMgr(this);
178   myCustomPrs = new PartSet_CustomPrs(theWshop);
179
180   myOverconstraintListener = new PartSet_OverconstraintListener(theWshop);
181
182   Events_Loop* aLoop = Events_Loop::loop();
183   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
184   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
185
186   registerSelectionFilter(SF_GlobalFilter, new PartSet_GlobalFilter(myWorkshop));
187   registerSelectionFilter(SF_FilterInfinite, new PartSet_FilterInfinite(myWorkshop));
188   Handle(PartSet_ResultGroupNameFilter) aCRFilter = new PartSet_ResultGroupNameFilter(myWorkshop);
189   std::set<std::string> aCRGroupNames;
190   aCRGroupNames.insert(ModelAPI_ResultConstruction::group());
191   aCRFilter->setGroupNames(aCRGroupNames);
192   registerSelectionFilter(SF_ResultGroupNameFilter, aCRFilter);
193
194   setDefaultConstraintShown();
195
196   //Config_PropManager::registerProp("Visualization", "object_default_color", "Object color",
197   //                                 Config_Prop::Color, "225,225,225");
198
199   Config_PropManager::registerProp("Visualization", "result_body_color", "Result color",
200     Config_Prop::Color, ModelAPI_ResultBody::DEFAULT_COLOR());
201
202   Config_PropManager::registerProp("Visualization", "result_group_color", "Group color",
203     Config_Prop::Color, ModelAPI_ResultGroup::DEFAULT_COLOR());
204
205   Config_PropManager::registerProp("Visualization", "result_construction_color",
206     "Construction color",
207     Config_Prop::Color,
208     ModelAPI_ResultConstruction::DEFAULT_COLOR());
209
210   Config_PropManager::registerProp("Visualization", "result_part_color", "Part color",
211     Config_Prop::Color, ModelAPI_ResultPart::DEFAULT_COLOR());
212
213   Config_PropManager::registerProp("Visualization", "result_field_color", "Field color",
214     Config_Prop::Color, ModelAPI_ResultField::DEFAULT_COLOR());
215
216   Config_PropManager::registerProp("Visualization", "operation_parameter_color",
217                           "Reference shape wireframe color in operation", Config_Prop::Color,
218                           PartSet_CustomPrs::OPERATION_PARAMETER_COLOR());
219   Config_PropManager::registerProp("Visualization", "operation_result_color",
220                           "Result shape wireframe color in operation", Config_Prop::Color,
221                           PartSet_CustomPrs::OPERATION_RESULT_COLOR());
222   Config_PropManager::registerProp("Visualization", "operation_highlight_color",
223                           "Multi selector item color in operation", Config_Prop::Color,
224                           PartSet_CustomPrs::OPERATION_HIGHLIGHT_COLOR());
225   Config_PropManager::registerProp("Visualization", "operation_remove_feature_color",
226                           "Color of removed feature in operation", Config_Prop::Color,
227                           PartSet_CustomPrs::OPERATION_REMOVE_FEATURE_COLOR());
228   Config_PropManager::registerProp("Visualization", "sketch_preview_plane",
229                           "Color of sketch plane", Config_Prop::Color,
230                           PartSet_CustomPrs::OPERATION_SKETCH_PLANE());
231
232   Config_PropManager::registerProp("Visualization", "hidden_face_transparency",
233                                    "Hidden faces transparency",
234                                    Config_Prop::DblSpin,
235                                    "0.8");
236   std::ostringstream aStream;
237   aStream << SketcherPrs_Tools::getDefaultArrowSize();
238   Config_PropManager::registerProp("Visualization", "dimension_arrow_size",
239     "Dimension arrow size", Config_Prop::IntSpin, aStream.str());
240
241   Config_PropManager::registerProp("Visualization", "dimension_font", "Dimension font",
242     Config_Prop::String, "Times-bold");
243
244   aStream.str("");
245   aStream.clear();
246   aStream << SketcherPrs_Tools::getDefaultTextHeight();
247   Config_PropManager::registerProp("Visualization", "dimension_value_size",
248     "Dimension value size", Config_Prop::IntSpin, aStream.str());
249
250   Config_PropManager::registerProp("Visualization", "sketch_dimension_color",
251     "Dimension color",
252     Config_Prop::Color, SKETCH_DIMENSION_COLOR);
253
254   Config_PropManager::registerProp("Visualization", "feature_objectbrowser_color",
255     "Feature items in Object Browser",
256     Config_Prop::Color, FEATURE_ITEM_COLOR);
257
258   Config_PropManager::registerProp("Shortcuts", "add_parameter_shortcut",
259     "Add parameter in parameters manager dialog",
260     Config_Prop::Shortcut, "Ctrl+A");
261
262   Config_PropManager::registerProp("Windows", "use_hide_faces_panel",
263     "Use HideFaces panel in operations", Config_Prop::Boolean, "false");
264 }
265
266 //******************************************************
267 PartSet_Module::~PartSet_Module()
268 {
269   delete myCustomPrs;
270   delete myOverconstraintListener;
271   delete myRoot;
272 }
273
274 //******************************************************
275 void PartSet_Module::createFeatures()
276 {
277   ModuleBase_IModule::createFeatures();
278   myRoot = new PartSet_RootNode();
279   myRoot->setWorkshop(workshop());
280 }
281
282
283 //******************************************************
284 void PartSet_Module::storeSelection()
285 {
286   // cash is used only to restore selection, so it should be filled in storeSelection and
287   // after applying immediatelly cleared in restoreSelection
288   myCurrentSelection.clear();
289   sketchMgr()->storeSelection(PartSet_SketcherMgr::ST_SelectType, myCurrentSelection);
290 }
291
292 //******************************************************
293 void PartSet_Module::restoreSelection()
294 {
295   // cash is used only to restore selection, so it should be filled in storeSelection and
296   // after applying immediatelly cleared in restoreSelection
297   sketchMgr()->restoreSelection(myCurrentSelection);
298   myCurrentSelection.clear();
299 }
300
301 //******************************************************
302 void PartSet_Module::registerValidators()
303 {
304   //Registering of validators
305   SessionPtr aMgr = ModelAPI_Session::get();
306   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
307   aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
308   aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
309   aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
310   aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
311   aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
312   aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
313   aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
314   aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
315   aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
316   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
317   aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
318   aFactory->registerValidator("PartSet_EqualSelection", new PartSet_EqualSelection);
319   aFactory->registerValidator("PartSet_CollinearSelection", new PartSet_CollinearSelection);
320   aFactory->registerValidator("PartSet_MiddlePointSelection", new PartSet_MiddlePointSelection);
321   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
322   aFactory->registerValidator("PartSet_DifferentPoints", new PartSet_DifferentPointsValidator);
323   aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
324   aFactory->registerValidator("PartSet_MultyTranslationSelection",
325     new PartSet_MultyTranslationSelection);
326   aFactory->registerValidator("PartSet_SplitSelection", new PartSet_SplitSelection);
327   aFactory->registerValidator("PartSet_ProjectionSelection", new PartSet_ProjectionSelection);
328   aFactory->registerValidator("PartSet_IntersectionSelection", new PartSet_IntersectionSelection);
329 }
330
331 //******************************************************
332 void PartSet_Module::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget,
333                                             const bool isToConnect)
334 {
335   mySketchMgr->connectToPropertyPanel(theWidget, isToConnect);
336 }
337
338 //******************************************************
339 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
340 {
341   if (sketchMgr()->isNestedSketchOperation(theOperation)) {
342     mySketchMgr->commitNestedSketch(theOperation);
343   }
344   /// deactivate of overconstraint listener should be performed after Sketch commit (#2176)
345   if (PartSet_SketcherMgr::isSketchOperation(theOperation))
346     overconstraintListener()->setActive(false);
347
348   /// Restart sketcher operations automatically
349   if (!mySketchReentrantMgr->operationCommitted(theOperation)) {
350
351     ModuleBase_OperationFeature* aFOperation =
352       dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
353     if (aFOperation && !aFOperation->isEditOperation()) {
354       // the selection is cleared after commit the create operation
355       // in order to do not use the same selected objects in the restarted operation
356       // for common behaviour, the selection is cleared even if the operation is not restarted
357       getWorkshop()->selector()->clearSelection();
358     }
359   }
360 }
361
362 //******************************************************
363 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
364 {
365   /// Restart sketcher operations automatically
366   mySketchReentrantMgr->operationAborted(theOperation);
367   /// deactivate of overconstraint listener should be performed after Sketch abort (#2176)
368   if (PartSet_SketcherMgr::isSketchOperation(theOperation))
369     overconstraintListener()->setActive(false);
370 }
371
372 //******************************************************
373 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
374 {
375   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
376                                                                                 (theOperation);
377   if (aFOperation) {
378     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(workshop());
379     XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
380
381     ModuleBase_ModelWidget* aFilledWidget = 0;
382     bool aPostonedWidgetActivation = false;
383
384     FeaturePtr aFeature = aFOperation->feature();
385     /// Restart sketcher operations automatically
386     /// it is important to call method of sketch reentrant manager before filling of PP
387     /// because it fills some created feature attributes, these new values should be used
388     /// to fill the property panel
389     mySketchReentrantMgr->operationStarted(theOperation);
390
391     aWorkshop->fillPropertyPanel(aFOperation);
392     // filling the operation values by the current selection
393     // if the operation can be committed after the controls filling, the method perform should
394     // be stopped. Otherwise unnecessary presentations can be shown(e.g. operation prs in sketch)
395     bool isOperationCommitted = false;
396     if (!aFOperation->isEditOperation()) {
397       std::string aGreedAttributeId = ModuleBase_Tools::findGreedAttribute(workshop(), aFeature);
398       // if there is a greed attribute, automatic commit by preselection
399       // for this feature is prohibited
400       aFilledWidget = aFOperation->activateByPreselection(aGreedAttributeId);
401       if (currentOperation() != aFOperation)
402         isOperationCommitted = true;
403       else {
404         if (aGreedAttributeId.empty()) {
405           // a signal should be emitted before the next widget activation
406           // because, the activation of the next widget will give a focus to the widget.
407           // As a result the value of the widget is initialized.
408           // And commit may happens until the value is entered.
409           if (aFilledWidget) {
410             if (mySketchReentrantMgr->canBeCommittedByPreselection())
411               isOperationCommitted = mySketchMgr->operationActivatedByPreselection();
412             // activate the next obligatory widget
413             if (!isOperationCommitted)
414               aPropertyPanel->activateNextWidget(aFilledWidget);
415           }
416         }
417         else { // there is a greed widget
418           const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
419           std::string aFirstAttributeId = aWidgets.front()->attributeID();
420           // activate next widget after greeded if it is the first widget in the panel
421           // else the first panel widget is already activated by operation start
422           if (aFirstAttributeId == aGreedAttributeId)
423             aPostonedWidgetActivation = true;
424         }
425       }
426     } if (!isOperationCommitted) {
427       aWorkshop->connectToPropertyPanel(true);
428       updateSketcherOnStart(aFOperation);
429       updatePresentationsOnStart(aFOperation);
430
431       // the objects of the current operation should be deactivated
432       QObjectPtrList anObjects;
433       anObjects.append(aFeature);
434       std::list<ResultPtr> aResults;
435       ModelAPI_Tools::allResults(aFeature, aResults);
436       std::list<ResultPtr>::const_iterator aIt;
437       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
438         anObjects.append(*aIt);
439       }
440       QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
441       for (; anIt != aLast; anIt++)
442         aWorkshop->deactivateActiveObject(*anIt, false);
443       if (anObjects.size() > 0) {
444         XGUI_Displayer* aDisplayer = aWorkshop->displayer();
445         aDisplayer->updateViewer();
446       }
447       /// state of command actions should be updated after displayed objects modification because
448       /// deactivation(for example) of objects may influence on selection in the viewer
449       /// State of command actions may depend on selection in the viewer(e.g. Sketch)
450       workshop()->updateCommandStatus();
451     }
452     if (aPostonedWidgetActivation) {
453       // if the widget is an empty in the chain of activated widgets, the current operation
454       // is restarted. It should be performed after functionality of the operation starting
455       aPropertyPanel->activateNextWidget(aFilledWidget);
456     }
457   }
458 }
459
460 //******************************************************
461 void PartSet_Module::updateSketcherOnStart(ModuleBase_Operation* theOperation)
462 {
463   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
464     mySketchMgr->startSketch(theOperation);
465   }
466   // It is switched off because of
467   // Task #3067: 5.2.2 Drawing in the sketcher: change the mouse cursor arrow
468   else if (sketchMgr()->isNestedSketchOperation(theOperation)) {
469     mySketchMgr->startNestedSketch(theOperation);
470   }
471 }
472
473 //******************************************************
474 void PartSet_Module::updatePresentationsOnStart(ModuleBase_Operation* theOperation)
475 {
476   ModuleBase_OperationFeature* aFOperation =
477     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
478   if (aFOperation) {
479     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, false);
480     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, false);
481   }
482 }
483
484 //******************************************************
485 void PartSet_Module::operationResumed(ModuleBase_Operation* theOperation)
486 {
487   ModuleBase_IModule::operationResumed(theOperation);
488
489   ModuleBase_OperationFeature* aFOperation =
490     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
491   if (aFOperation) {
492     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, false);
493     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, false);
494   }
495 }
496
497 //******************************************************
498 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
499 {
500   bool isModifiedArgs = myCustomPrs->deactivate(ModuleBase_IModule::CustomizeArguments, false);
501   bool isModifiedResults = myCustomPrs->deactivate(ModuleBase_IModule::CustomizeResults, false);
502   bool isModified = isModifiedArgs || isModifiedResults;
503
504   if (sketchMgr()->isNestedSketchOperation(theOperation)) {
505     mySketchMgr->stopNestedSketch(theOperation);
506   }
507   else if (PartSet_SketcherMgr::isSketchOperation(theOperation))
508     setDefaultConstraintShown();
509
510   //VSV: Viewer is updated on feature update and redisplay
511   if (isModified) {
512     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
513     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
514     aDisplayer->updateViewer();
515   }
516
517   QMap<PartSet_Tools::ConstraintVisibleState, bool>::const_iterator
518     anIt = myHasConstraintShown.begin(), aLast = myHasConstraintShown.end();
519   for (; anIt != aLast; anIt++) {
520     mySketchMgr->updateBySketchParameters(anIt.key(), anIt.value());
521   }
522 }
523
524 //******************************************************
525 ModuleBase_Operation* PartSet_Module::currentOperation() const
526 {
527   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
528   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
529   return anOpMgr->currentOperation();
530 }
531
532 //******************************************************
533 bool PartSet_Module::canUndo() const
534 {
535   bool aCanUndo = false;
536   SessionPtr aMgr = ModelAPI_Session::get();
537   if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
538     aCanUndo = !aMgr->isOperation();
539     if (!aCanUndo) // check the enable state additionally by sketch manager
540       aCanUndo = aMgr->canUndo();
541   }
542   return aCanUndo;
543 }
544
545 //******************************************************
546 bool PartSet_Module::canRedo() const
547 {
548   bool aCanRedo = false;
549   SessionPtr aMgr = ModelAPI_Session::get();
550   if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
551     aCanRedo = !aMgr->isOperation();
552     if (!aCanRedo) // check the enable state additionally by sketch manager
553       aCanRedo = aMgr->canRedo();
554   }
555   return aCanRedo;
556 }
557
558 //******************************************************
559 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
560 {
561   bool aValid = true;
562   if (theActionId == "MOVE_CMD" || theActionId == "MOVE_SPLIT_CMD") {
563     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
564     if (aFeature) {
565       ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
566       // part features can not be moved in the history.
567       if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
568         aValid = false;
569     }
570   }
571   return aValid;
572 }
573
574 //******************************************************
575 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
576 {
577   // the sketch manager put the restriction to the objects erase
578   return mySketchMgr->canEraseObject(theObject);
579 }
580
581 //******************************************************
582 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
583 {
584   // the sketch manager put the restriction to the objects display
585   return mySketchMgr->canDisplayObject(theObject);
586 }
587
588 //******************************************************
589 bool PartSet_Module::canUsePreselection(const QString& thePreviousOperationKind,
590                                         const QString& theStartedOperationKind)
591 {
592   if (ModuleBase_IModule::canUsePreselection(thePreviousOperationKind, theStartedOperationKind))
593     return true;
594
595   return mySketchMgr->isNestedSketchFeature(theStartedOperationKind);
596 }
597
598 /*void PartSet_Module::processHiddenObject(const std::list<ObjectPtr>& theObjects)
599 {
600   mySketchMgr->processHiddenObject(theObjects);
601 }*/
602
603 //******************************************************
604 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
605 {
606   bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
607
608   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
609   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
610        isNestedOp = sketchMgr()->isNestedSketchOperation(anOperation);
611   if (isSketchOp || isNestedOp) {
612     // in active sketch operation it is possible to activate operation object in selection
613     // in the edit operation, e.g. points of the line can be moved when the line is edited
614     ModuleBase_OperationFeature* aFOperation =
615       dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
616     aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
617   }
618   return aCanActivate;
619 }
620
621 //******************************************************
622 bool PartSet_Module::addViewerMenu(const QMap<QString, QAction*>& theStdActions,
623                                    QWidget* theParent,
624                                    QMap<int, QAction*>& theMenuActions) const
625 {
626   return myMenuMgr->addViewerMenu(theStdActions, theParent, theMenuActions);
627 }
628
629 //******************************************************
630 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
631 {
632   myMenuMgr->updateViewerMenu(theStdActions);
633 }
634
635 //******************************************************
636 bool PartSet_Module::isActionEnableStateFixed(const int theActionId) const
637 {
638   bool isEnabledFixed = false;
639   if (theActionId == XGUI_ActionsMgr::AcceptAll &&
640       mySketchReentrantMgr->isInternalEditActive())
641     isEnabledFixed = true;
642   return isEnabledFixed;
643 }
644
645 //******************************************************
646 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
647 {
648   QString anError = ModuleBase_IModule::getFeatureError(theFeature);
649   if (anError.isEmpty())
650     anError = sketchMgr()->getFeatureError(theFeature);
651
652   return anError;
653 }
654
655 //******************************************************
656 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
657                                          QStringList& theIds) const
658 {
659   myMenuMgr->grantedOperationIds(theOperation, theIds);
660
661   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
662     XGUI_Workshop* aWorkshop = getWorkshop();
663     theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
664   }
665 }
666
667 //******************************************************
668 void PartSet_Module::activeSelectionModes(QIntList& theModes)
669 {
670   if (mySketchMgr->activeSketch().get())
671     PartSet_SketcherMgr::sketchSelectionModes(mySketchMgr->activeSketch(), theModes);
672   else
673     theModes = XGUI_Tools::workshop(myWorkshop)->viewerSelectionModes();
674 }
675
676 //******************************************************
677 void PartSet_Module::moduleSelectionModes(int theModesType, QIntList& theModes)
678 {
679   customSubShapesSelectionModes(theModes);
680   //theModes.append(XGUI_Tools::workshop(myWorkshop)->viewerSelectionModes());
681   //myWorkshop->module()->activeSelectionModes(theModes);
682 }
683
684 //******************************************************
685 void PartSet_Module::moduleSelectionFilters(const QIntList& theFilterTypes,
686                                             SelectMgr_ListOfFilter& theSelectionFilters)
687 {
688   bool isSketchActive = mySketchMgr->activeSketch().get();
689
690   std::map<ModuleBase_SelectionFilterType, Handle(SelectMgr_Filter)>::const_iterator aFiltersIt =
691     mySelectionFilters.begin();
692   for (; aFiltersIt != mySelectionFilters.end(); aFiltersIt++) {
693     int aFilterType = aFiltersIt->first;
694     // do not add not participating filters in given parameters
695     if (!theFilterTypes.contains(aFilterType))
696       continue;
697
698     // using sketch filters only if sketch operation is active
699     if (!isSketchActive &&
700         mySketchMgr->sketchSelectionFilter((ModuleBase_SelectionFilterType)aFilterType))
701       continue;
702
703     // using filtering of construction results only from faces panel
704     if (aFilterType == SF_ResultGroupNameFilter)
705       continue;
706
707     theSelectionFilters.Append(aFiltersIt->second);
708   }
709 }
710
711 //******************************************************
712 void PartSet_Module::customSubShapesSelectionModes(QIntList& theModes)
713 {
714   if (theModes.contains(TopAbs_FACE))
715     theModes.append(SketcherPrs_Tools::Sel_Sketch_Face);
716   if (theModes.contains(TopAbs_WIRE))
717     theModes.append(SketcherPrs_Tools::Sel_Sketch_Wire);
718
719   if (mySketchMgr->activeSketch().get())
720     PartSet_SketcherMgr::sketchSelectionModes(mySketchMgr->activeSketch(), theModes);
721 }
722
723 //******************************************************
724 void PartSet_Module::getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theSelected,
725                                       ObjectPtr& theObject, AttributePtr& theAttribute)
726 {
727   ObjectPtr anObject = theSelected->object();
728   GeomShapePtr aShape = theSelected->shape();
729
730   theAttribute = findAttribute(anObject, aShape);
731   // TODO: try to create result if object is an external object
732   theObject = anObject;
733 }
734
735 //******************************************************
736 bool PartSet_Module::isMouseOverWindow()
737 {
738   return mySketchMgr->isMouseOverWindow();
739 }
740
741 //******************************************************
742 bool PartSet_Module::isSketchNeutralPointActivated() const
743 {
744   bool isNeutralPoint = true;
745   if (sketchReentranceMgr()->isInternalEditActive())
746     isNeutralPoint = false;
747   if (myIsOperationIsLaunched)
748     isNeutralPoint = false;
749
750   return isNeutralPoint;
751 }
752
753 //******************************************************
754 void PartSet_Module::closeDocument()
755 {
756   myActivePartIndex = QModelIndex();
757   SketcherPrs_PositionMgr::get()->clearAll();
758 }
759
760 //******************************************************
761 void PartSet_Module::clearViewer()
762 {
763   myCustomPrs->clearPrs();
764
765   XGUI_Workshop* aWorkshop = getWorkshop();
766   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
767   aDisplayer->deactivateSelectionFilters(false);
768 }
769
770 //******************************************************
771 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
772 {
773   ModuleBase_OperationFeature* aFOperation =
774     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
775   if (!aFOperation)
776     return;
777
778   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
779   // we have to manually activate the sketch label in edit mode
780   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation()))
781     aPanel->activateWidget(aPanel->modelWidgets().first());
782 }
783
784 //******************************************************
785 bool PartSet_Module::createWidgets(const FeaturePtr& theFeature, const QString& theXmlRepr,
786                                    QList<ModuleBase_ModelWidget*>& theWidgets) const
787 {
788   bool aProcessed = false;
789
790   XGUI_Workshop* aWorkshop = getWorkshop();
791   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
792   if (mySketchMgr->activeSketch().get() && aPropertyPanel) {
793     ModuleBase_ISelection* aSelection = workshop()->selection();
794     // click on a point in sketch leads here with the point is highlighted, not yet selected
795     QList<ModuleBase_ViewerPrsPtr> aPreselection = aSelection->getHighlighted();
796     if (aPreselection.size() == 1) {
797       ModuleBase_ViewerPrsPtr aSelectedPrs = aPreselection[0];
798       ObjectPtr anObject = aSelectedPrs->object();
799
800       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
801       GeomShapePtr aShape = aSelectedPrs->shape();
802       // click on the digit of dimension constrain comes here
803       // with an empty shape, so we need the check
804       if (aFeature == theFeature && aShape.get() && !aShape->isNull()) {
805         // if feature has only one result and shape of result is equal to selected shape
806         // this attribute is not processed. It is a case of Sketch Point.
807         if (aFeature->results().size() == 1) {
808           ResultPtr aResult = aFeature->results().front();
809           if (aResult.get() && aResult->shape()->isEqual(aShape))
810             return aProcessed;
811         }
812         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
813         std::pair<AttributePtr, int> anAttribute =
814             PartSet_Tools::findAttributeBy2dPoint(anObject, aTDShape, mySketchMgr->activeSketch());
815         if (anAttribute.first.get()) {
816           ModuleBase_WidgetFactory aFactory(theXmlRepr.toStdString(), workshop());
817
818           const std::string anAttributeId = anAttribute.first->id();
819           aFactory.createWidget(aPropertyPanel->contentWidget(), anAttributeId);
820
821           theWidgets = aFactory.getModelWidgets();
822           // it is possible that the point does not present in XML definition,
823           // in this case, we assume that it is not processed by this module
824           // e.g. "Intersection point" feature
825           aProcessed = !theWidgets.isEmpty();
826         }
827       }
828     }
829   }
830   return aProcessed;
831 }
832
833 //******************************************************
834 void PartSet_Module::onSelectionChanged()
835 {
836   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
837   if (!aOperation)
838     return;
839
840   bool isSketcherOp = false;
841   // An edit operation is enable only if the current opeation is the sketch operation
842   if (mySketchMgr->activeSketch()) {
843     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
844       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
845   }
846   if (isSketcherOp) {
847     // Editing of constraints can be done on selection
848     ModuleBase_ISelection* aSelect = myWorkshop->selection();
849     QList<ModuleBase_ViewerPrsPtr> aSelected = aSelect->getSelected();
850     if (aSelected.size() == 1) {
851       ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
852       ObjectPtr aObject = aPrs->object();
853       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
854       if (aFeature) {
855         std::string aId = aFeature->getKind();
856         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
857             (aId == SketchPlugin_ConstraintLength::ID()) ||
858             (aId == SketchPlugin_ConstraintDistance::ID()) ||
859             (aId == SketchPlugin_ConstraintAngle::ID())) {
860           editFeature(aFeature);
861         }
862       }
863     }
864   }
865 }
866
867 //******************************************************
868 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
869 {
870   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
871   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
872   anOpMgr->onKeyReleased(theWnd->viewPort(), theEvent);
873 }
874
875 //******************************************************
876 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType,
877                                                            QWidget* theParent,
878                                                            Config_WidgetAPI* theWidgetApi)
879 {
880   ModuleBase_IWorkshop* aWorkshop = workshop();
881   ModuleBase_ModelWidget* aWgt = NULL;
882   if (theType == "sketch-start-label") {
883     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
884                                                                theWidgetApi, myHasConstraintShown);
885     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
886       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
887     connect(aLabelWgt, SIGNAL(showConstraintToggled(int, bool)),
888       mySketchMgr, SLOT(onShowConstraintsToggle(int, bool)));
889     connect(aLabelWgt, SIGNAL(showFreePoints(bool)), mySketchMgr, SLOT(onShowPoints(bool)));
890     connect(aLabelWgt, SIGNAL(autoConstraints(bool)),
891       sketchReentranceMgr(), SLOT(onAutoConstraints(bool)));
892     aLabelWgt->setShowPointsState(mySketchMgr->isShowFreePointsShown());
893     aWgt = aLabelWgt;
894   } else if (theType == "sketch-2dpoint_selector") {
895     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
896                                                                  theWidgetApi);
897     aPointWgt->setSketch(mySketchMgr->activeSketch());
898     connect(aPointWgt, SIGNAL(vertexSelected()), sketchReentranceMgr(), SLOT(onVertexSelected()));
899     aWgt = aPointWgt;
900   }else if (theType == "sketch-2dpoint_flyout_selector") {
901     PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop,
902                                                                              theWidgetApi);
903     aPointWgt->setSketch(mySketchMgr->activeSketch());
904     connect(aPointWgt, SIGNAL(vertexSelected()), sketchReentranceMgr(), SLOT(onVertexSelected()));
905     aWgt = aPointWgt;
906   } else if (theType == "sketch_shape_selector") {
907     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
908                           new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi);
909     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
910     aWgt = aShapeSelectorWgt;
911   } else if (theType == "sketch_multi_selector") {
912     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
913                           new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi);
914     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
915     aWgt = aShapeSelectorWgt;
916   }
917   else if (theType == "sketch_feature_point_selector") {
918     PartSet_WidgetFeaturePointSelector* aPointSelectorWgt =
919             new PartSet_WidgetFeaturePointSelector(theParent, aWorkshop, theWidgetApi);
920     aPointSelectorWgt->setSketcher(mySketchMgr->activeSketch());
921     aWgt = aPointSelectorWgt;
922   }
923   else if (theType == "sketch-bspline_selector") {
924     PartSet_WidgetBSplinePoints* aBSplineWgt =
925         new PartSet_WidgetBSplinePoints(theParent, aWorkshop, theWidgetApi);
926     aBSplineWgt->setSketch(mySketchMgr->activeSketch());
927     aWgt = aBSplineWgt;
928   }
929   else if (theType == WDG_DOUBLEVALUE_EDITOR) {
930     aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi);
931   } else if (theType == "export_file_selector") {
932     aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi);
933   } else if (theType == "sketch_launcher") {
934     aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi);
935   }
936   else if (theType == "module_choice") {
937     aWgt = new ModuleBase_WidgetChoice(theParent, theWidgetApi);
938     connect(aWgt, SIGNAL(itemSelected(ModuleBase_ModelWidget*, int)),
939       this, SLOT(onChoiceChanged(ModuleBase_ModelWidget*, int)));
940   } else if (theType == "bspline-panel") {
941     PartSet_BSplineWidget* aPanel = new PartSet_BSplineWidget(theParent, theWidgetApi);
942     //aPanel->setFeature(theFeature);
943     aWgt = aPanel;
944   }
945   return aWgt;
946 }
947
948 //******************************************************
949 ModuleBase_ModelWidget* PartSet_Module::activeWidget() const
950 {
951   ModuleBase_ModelWidget* anActiveWidget = 0;
952
953   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
954   if (!aOperation)
955     return anActiveWidget;
956
957   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
958   if (!aPanel)
959     return anActiveWidget;
960
961   return aPanel->activeWidget(true);
962 }
963
964 //******************************************************
965 bool PartSet_Module::deleteObjects()
966 {
967   bool isProcessed = false;
968
969   XGUI_Workshop* aWorkshop = getWorkshop();
970   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
971
972   //SessionPtr aMgr = ModelAPI_Session::get();
973   // 1. check whether the delete should be processed in the module
974   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
975   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
976        isNestedOp = sketchMgr()->isNestedSketchOperation(anOperation);
977   if (isSketchOp || isNestedOp) {
978     isProcessed = true;
979     // 2. find selected presentations
980     // selected objects should be collected before the current operation abort because
981     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
982     ModuleBase_ISelection* aSel = workshop()->selection();
983     QObjectPtrList aSelectedObj = aSel->selectedPresentations();
984     // if there are no selected objects in the viewer, that means that the selection in another
985     // place cased this method. It is necessary to return the false value to understande in above
986     // method that delete is not processed
987     if (aSelectedObj.count() == 0)
988       return false;
989
990     // avoid delete of the objects, which are not belong to the current sketch
991     // in order to do not delete results of other sketches
992     QObjectPtrList aSketchObjects;
993     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
994     for ( ; anIt != aLast; anIt++) {
995       ObjectPtr anObject = *anIt;
996       if (mySketchMgr->isObjectOfSketch(anObject)) {
997         // sketch feature should be used in this list because workshop deletes features only
998         // results are skipped
999         FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
1000         aSketchObjects.append(aSketchFeature);
1001       }
1002     }
1003     // if the selection contains only local selected presentations from other sketches,
1004     // the Delete operation should not be done at all
1005     if (aSketchObjects.size() == 0)
1006       return true;
1007
1008     // 3. start operation
1009     QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
1010     ModuleBase_Operation* anOpAction = new ModuleBase_Operation(aDescription, this);
1011
1012     // the active nested sketch operation should be aborted unconditionally
1013     // the Delete action should be additionally granted for the Sketch operation
1014     // in order to do not abort/commit it
1015     bool isCommitted;
1016     if (!anOpMgr->canStartOperation(anOpAction->id(), isCommitted))
1017       return true; // the objects are processed but can not be deleted
1018
1019     anOpMgr->startOperation(anOpAction);
1020
1021     // WORKAROUND, should be done to avoid viewer highlight update after deletetion of objects
1022     // the problem is in AIS Dimensions recompute
1023     // if a line and the dim are removed, line is the first
1024     // it causes the AIS recompute, where the base line is null,
1025     // the result is empty AIS in the viewer
1026     XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
1027
1028     // 4. delete features
1029     // sketch feature should be skipped, only sub-features can be removed
1030     // when sketch operation is active
1031     aWorkshop->deleteFeatures(aSketchObjects);
1032     // 5. stop operation
1033     anOpMgr->commitOperation();
1034   }
1035   return isProcessed;
1036 }
1037
1038 //******************************************************
1039 void PartSet_Module::editFeature(FeaturePtr theFeature)
1040 {
1041   storeConstraintsState(theFeature->getKind());
1042   ModuleBase_IModule::editFeature(theFeature);
1043 }
1044
1045 //******************************************************
1046 bool PartSet_Module::canCommitOperation() const
1047 {
1048   return true;
1049 }
1050
1051 //******************************************************
1052 void PartSet_Module::launchOperation(const QString& theCmdId, const bool& isStartAfterCommitOnly)
1053 {
1054   myIsOperationIsLaunched = true;
1055   storeConstraintsState(theCmdId.toStdString());
1056   updateConstraintsState(theCmdId.toStdString());
1057
1058   ModuleBase_IModule::launchOperation(theCmdId, isStartAfterCommitOnly);
1059
1060   myIsOperationIsLaunched = false;
1061 }
1062
1063 //******************************************************
1064 void PartSet_Module::storeConstraintsState(const std::string& theFeatureKind)
1065 {
1066   if (myWorkshop->currentOperation() &&
1067       myWorkshop->currentOperation()->id().toStdString() == SketchPlugin_Sketch::ID()) {
1068     const QMap<PartSet_Tools::ConstraintVisibleState, bool>& aShownStates =
1069                                                   mySketchMgr->showConstraintStates();
1070     myHasConstraintShown = aShownStates;
1071   }
1072 }
1073
1074 //******************************************************
1075 void PartSet_Module::updateConstraintsState(const std::string& theFeatureKind)
1076 {
1077   if (PartSet_SketcherMgr::constraintsIdList().contains(theFeatureKind.c_str()) ||
1078       PartSet_SketcherMgr::replicationsIdList().contains(theFeatureKind.c_str())) {
1079     // Show constraints if a constraint was anOperation
1080     mySketchMgr->updateBySketchParameters(PartSet_Tools::Geometrical, true);
1081     mySketchMgr->updateBySketchParameters(PartSet_Tools::Dimensional, true);
1082     mySketchMgr->updateBySketchParameters(PartSet_Tools::Expressions,
1083                                           myHasConstraintShown[PartSet_Tools::Expressions]);
1084   }
1085 }
1086
1087 //******************************************************
1088 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
1089 {
1090   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
1091   if (!anAIS.IsNull()) {
1092     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1093     if (aFeature.get()) {
1094       bool aToUseZLayer = false;
1095       if (PartSet_Tools::findRefsToMeFeature(aFeature,SketchPlugin_Projection::ID()))
1096         aToUseZLayer = true;
1097       else {
1098         CompositeFeaturePtr aParent = ModelAPI_Tools::compositeOwner(aFeature);
1099         aToUseZLayer = (aParent.get() && (aParent->getKind() == SketchPlugin_Sketch::ID()));
1100       }
1101       if (aToUseZLayer) {
1102         Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
1103         if (aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
1104           aCtx->SetZLayer(anAIS, Graphic3d_ZLayerId_Top);
1105         else
1106           aCtx->SetZLayer(anAIS, myVisualLayerId);
1107       }
1108     }
1109   }
1110 }
1111
1112 //******************************************************
1113 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
1114 {
1115   // this is obsolete
1116   // it should be recomputed in order to disappear in the viewer if the corresponded object
1117   // is erased
1118   //if (myCustomPrs->isActive())
1119   //  myCustomPrs->redisplay(theObject, false);
1120 }
1121
1122 //******************************************************
1123 void PartSet_Module::onViewTransformed(int theTrsfType)
1124 {
1125   // Set length of arrows constant in pixel size
1126   // if the operation is panning or rotate or panglobal then do nothing
1127   if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
1128     return;
1129   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
1130   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1131   if (aContext.IsNull())
1132     return;
1133
1134   //Handle(V3d_View) aView = aViewer->activeView();
1135
1136   XGUI_Workshop* aWorkshop = getWorkshop();
1137   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1138   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
1139   Handle(V3d_View) aView;
1140   double aScale = 0;
1141   for (aV3dViewer->InitDefinedViews();
1142        aV3dViewer->MoreDefinedViews();
1143        aV3dViewer->NextDefinedViews()) {
1144     Handle(V3d_View) aV = aV3dViewer->DefinedView();
1145     double aS = aV->Scale();
1146     if (aS > aScale) {
1147       aScale = aS;
1148       aView = aV;
1149     }
1150   }
1151   if (aView.IsNull())
1152     return;
1153
1154   bool isModified = false;
1155   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
1156   if (aCurrentOperation &&
1157     (PartSet_SketcherMgr::isSketchOperation(aCurrentOperation) ||
1158      sketchMgr()->isNestedSketchOperation(aCurrentOperation) ||
1159      (aCurrentOperation->id() == "Measurement")))
1160   {
1161     double aLen = aView->Convert(SketcherPrs_Tools::getConfigArrowSize());
1162     SketcherPrs_Tools::setArrowSize(aLen);
1163     const double aCurScale = aViewer->activeView()->Camera()->Scale();
1164     aViewer->SetScale(aViewer->activeView(), aCurScale);
1165 #ifdef OPTIMIZE_PRS
1166     QList<Handle(AIS_InteractiveObject)> aPrsList = aDisplayer->displayedPresentations();
1167     foreach(Handle(AIS_InteractiveObject) aAisObj, aPrsList) {
1168 #else
1169     QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
1170     foreach(AISObjectPtr aAIS, aPrsList) {
1171       Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
1172 #endif
1173       Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
1174       if (!aDim.IsNull()) {
1175         aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
1176         aContext->Redisplay(aDim, false);
1177         isModified = true;
1178       }
1179     }
1180     if (isModified)
1181       aDisplayer->updateViewer();
1182   }
1183
1184 }
1185
1186 //******************************************************
1187 bool PartSet_Module::isCustomPrsActivated(const ModuleBase_CustomizeFlag& theFlag) const
1188 {
1189   return myCustomPrs->isActive(theFlag);
1190 }
1191
1192 //******************************************************
1193 void PartSet_Module::activateCustomPrs(const FeaturePtr& theFeature,
1194                                        const ModuleBase_CustomizeFlag& theFlag,
1195                                        const bool theUpdateViewer)
1196 {
1197   myCustomPrs->activate(theFeature, theFlag, theUpdateViewer);
1198 }
1199
1200 //******************************************************
1201 void PartSet_Module::deactivateCustomPrs(const ModuleBase_CustomizeFlag& theFlag,
1202                                          const bool theUpdateViewer)
1203 {
1204   myCustomPrs->deactivate(theFlag, theUpdateViewer);
1205 }
1206
1207 //******************************************************
1208 //bool PartSet_Module::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
1209 //                                           std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
1210 //{
1211 //  bool aCustomized = false;
1212 //
1213 //  XGUI_Workshop* aWorkshop = getWorkshop();
1214 //  XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1215 //  ObjectPtr anObject = aDisplayer->getObject(thePrs);
1216 //  if (!anObject)
1217 //    return aCustomized;
1218 //
1219 //  if (!theResult.get()) {
1220 //    std::vector<int> aColor;
1221 //    XGUI_CustomPrs::getDefaultColor(anObject, true, aColor);
1222 //    if (!aColor.empty()) {
1223 //      aCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
1224 //    }
1225 //  }
1226 //  // customize dimentional constrains
1227 //  sketchMgr()->customisePresentation(anObject);
1228 //
1229 //  return aCustomized;
1230 //}
1231 //
1232 ////******************************************************
1233 //bool PartSet_Module::afterCustomisePresentation(std::shared_ptr<ModelAPI_Result> theResult,
1234 //                                                AISObjectPtr thePrs,
1235 //                                                GeomCustomPrsPtr theCustomPrs)
1236 //{
1237 //  bool aCustomized = false;
1238 //
1239 //  XGUI_Workshop* aWorkshop = getWorkshop();
1240 //  XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1241 //  ObjectPtr anObject = aDisplayer->getObject(thePrs);
1242 //  if (!anObject)
1243 //    return aCustomized;
1244 //
1245 //  std::vector<int> aColor;
1246 //  bool aUseCustomColor = true;
1247 //  if (aUseCustomColor)
1248 //    myOverconstraintListener->getCustomColor(anObject, aColor);
1249 //  // customize sketch symbol presentation
1250 //  Handle(AIS_InteractiveObject) anAISIO = thePrs->impl<Handle(AIS_InteractiveObject)>();
1251 //  if (!anAISIO.IsNull()) {
1252 //    if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
1253 //      Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
1254 //      if (!aPrs.IsNull()) {
1255 //        aPrs->SetCustomColor(aColor);
1256 //        aCustomized = true;
1257 //      }
1258 //    } else if (!Handle(SketcherPrs_Coincident)::DownCast(anAISIO).IsNull()) {
1259 //      Handle(SketcherPrs_Coincident) aPrs = Handle(SketcherPrs_Coincident)::DownCast(anAISIO);
1260 //      if (!aPrs.IsNull()) {
1261 //        aPrs->SetCustomColor(aColor);
1262 //        aCustomized = true;
1263 //      }
1264 //    }
1265 //  }
1266 //  // customize sketch dimension constraint presentation
1267 //  if (!aCustomized) {
1268 //    if (!aColor.empty()) { // otherwise presentation has the default color
1269 //      aCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
1270 //    }
1271 //  }
1272 //  return aCustomized;
1273 //}
1274
1275 //******************************************************
1276 bool PartSet_Module::customizeFeature(ObjectPtr theObject, const ModuleBase_CustomizeFlag& theFlag,
1277                                      const bool theUpdateViewer)
1278 {
1279   bool isRedisplayed = false;
1280   if (myCustomPrs->isActive(theFlag))
1281     isRedisplayed = myCustomPrs->redisplay(theObject, theFlag, theUpdateViewer);
1282
1283   return isRedisplayed;
1284 }
1285
1286 //******************************************************
1287 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
1288 {
1289   XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
1290   if (aOB) {
1291     QLabel* aLabel = aOB->activeDocLabel();
1292     aLabel->installEventFilter(myMenuMgr);
1293     connect(aLabel, SIGNAL(customContextMenuRequested(const QPoint&)),
1294           SLOT(onActiveDocPopup(const QPoint&)));
1295     aOB->treeView()->setExpandsOnDoubleClick(false);
1296     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
1297       SLOT(onTreeViewDoubleClick(const QModelIndex&)));
1298
1299     Events_Loop* aLoop = Events_Loop::loop();
1300     aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1301   }
1302 }
1303
1304 //******************************************************
1305 void PartSet_Module::onActiveDocPopup(const QPoint& thePnt)
1306 {
1307   SessionPtr aMgr = ModelAPI_Session::get();
1308   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
1309
1310   XGUI_Workshop* aWorkshop = getWorkshop();
1311   QLabel* aHeader = aWorkshop->objectBrowser()->activeDocLabel();
1312
1313   aActivatePartAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
1314
1315   QMenu aMenu;
1316   aMenu.addAction(aActivatePartAction);
1317
1318 #ifndef HAVE_SALOME
1319   if (aMgr->activeDocument() == aMgr->moduleDocument()) {
1320     DocumentPtr aDoc = aMgr->moduleDocument();
1321     int aNbParts = aDoc->size(ModelAPI_ResultPart::group());
1322     bool aHaveToActivate = false;
1323     for (int i = 0; i < aNbParts; i++) {
1324       ObjectPtr aObj = aDoc->object(ModelAPI_ResultPart::group(), i);
1325       ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1326       if (!aPartRes->partDoc().get()) {
1327         aHaveToActivate = true;
1328         break;
1329       }
1330     }
1331     if (aHaveToActivate) {
1332       QAction* aActivateAllPartAction = myMenuMgr->action("ACTIVATE_ALL_PARTS_CMD");
1333       aMenu.addAction(aActivateAllPartAction);
1334     }
1335   }
1336 #endif
1337
1338   aMenu.exec(aHeader->mapToGlobal(thePnt));
1339 }
1340
1341 //******************************************************
1342 AISObjectPtr PartSet_Module::createPresentation(const ObjectPtr& theObject)
1343 {
1344   Handle(AIS_InteractiveObject) anAISPrs = mySketchMgr->createPresentation(theObject);
1345   if (anAISPrs.IsNull()) {
1346     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1347     if (aResult.get()) {
1348       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
1349       if (aShapePtr.get() != NULL)
1350         anAISPrs = new ModuleBase_ResultPrs(aResult);
1351     }
1352     else {
1353       FieldStepPtr aStep =
1354         std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(theObject);
1355       if (aStep.get()) {
1356         anAISPrs = new PartSet_FieldStepPrs(aStep);
1357       }
1358     }
1359   }
1360   AISObjectPtr anAIS;
1361   if (!anAISPrs.IsNull()) {
1362     Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
1363     if (!aShapePrs.IsNull())
1364       ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*)aShapePrs.get());
1365
1366     anAIS = AISObjectPtr(new GeomAPI_AISObject());
1367     anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
1368     customizePresentation(theObject, anAIS);
1369   }
1370   return anAIS;
1371 }
1372
1373 //******************************************************
1374 void getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
1375 {
1376   ModelAPI_Tools::getColor(theResult, theColor);
1377   if (theColor.empty())
1378     PartSet_Tools::getDefaultColor(theResult, false, theColor);
1379 }
1380
1381 //******************************************************
1382 double getResultDeflection(const ResultPtr& theResult)
1383 {
1384   double aDeflection = ModelAPI_Tools::getDeflection(theResult);
1385   if (aDeflection < 0)
1386     aDeflection = PartSet_Tools::getDefaultDeflection(theResult);
1387   return aDeflection;
1388 }
1389
1390 //******************************************************
1391 double getResultTransparency(const ResultPtr& theResult)
1392 {
1393   double aTransparency = ModelAPI_Tools::getTransparency(theResult);
1394   if (aTransparency < 0)
1395     aTransparency = PartSet_Tools::getDefaultTransparency();
1396   return aTransparency;
1397 }
1398
1399
1400 //******************************************************
1401 void PartSet_Module::customizePresentation(const ObjectPtr& theObject,
1402                                            const AISObjectPtr& thePrs) const
1403 {
1404   if (mySketchMgr->isObjectOfSketch(theObject)) {
1405     mySketchMgr->customizeSketchPresentation(theObject, thePrs);
1406   }
1407   else {
1408     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1409     if (aResult.get()) {
1410       std::vector<int> aColor;
1411       getResultColor(aResult, aColor);
1412
1413       SessionPtr aMgr = ModelAPI_Session::get();
1414       if (aMgr->activeDocument() != aResult->document()) {
1415         QColor aQColor(aColor[0], aColor[1], aColor[2]);
1416         QColor aNewColor =
1417           QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF() / 3., aQColor.valueF());
1418         aColor[0] = aNewColor.red();
1419         aColor[1] = aNewColor.green();
1420         aColor[2] = aNewColor.blue();
1421       }
1422       thePrs->setColor(aColor[0], aColor[1], aColor[2]);
1423
1424       thePrs->setDeflection(getResultDeflection(aResult));
1425
1426       thePrs->setTransparency(getResultTransparency(aResult));
1427     }
1428     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1429     if (aFeature.get()) {
1430       if (aFeature->getKind() == SketchPlugin_Sketch::ID())
1431         thePrs->setWidth(2);
1432     }
1433   }
1434 }
1435
1436
1437 //******************************************************
1438 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
1439 {
1440   ObjectPtr anObject;
1441   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1442   if (aOperation) {
1443     /// If last line finished on vertex the lines creation sequence has to be break
1444     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1445     if (aPanel) {
1446       ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1447       // if there is an active widget, find the presented object in it
1448       if (!anActiveWidget)
1449         anActiveWidget = aPanel->preselectionWidget();
1450
1451       ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1452                                                                              (anActiveWidget);
1453       if (aWidgetValidated)
1454         anObject = aWidgetValidated->findPresentedObject(theAIS);
1455     }
1456   }
1457   return anObject;
1458 }
1459
1460 //******************************************************
1461 bool PartSet_Module::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
1462 {
1463   bool aCanBeShaged = true;
1464
1465   Handle(PartSet_ResultSketchPrs) aPrs = Handle(PartSet_ResultSketchPrs)::DownCast(theAIS);
1466   if (!aPrs.IsNull())
1467     aCanBeShaged = false;
1468
1469   return aCanBeShaged;
1470 }
1471
1472 //******************************************************
1473 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
1474 {
1475   QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
1476   int aSelected = aObjects.size();
1477   SessionPtr aMgr = ModelAPI_Session::get();
1478   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
1479
1480   bool hasResult = false;
1481   bool hasFeature = false;
1482   bool hasParameter = false;
1483   bool hasCompositeOwner = false;
1484   bool hasResultInHistory = false;
1485   bool hasFolder = false;
1486   ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter,
1487                                   hasCompositeOwner, hasResultInHistory, hasFolder);
1488
1489   ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
1490   if (aSelected == 1) {
1491     ObjectPtr aObject = aObjects.first();
1492     if (aObject) {
1493       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1494       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
1495       bool isPart = aPart.get() ||
1496         (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
1497       if (isPart) {
1498         DocumentPtr aPartDoc;
1499         if (!aPart.get()) {
1500           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
1501         }
1502         if (aPart.get()) // this may be null is Part feature is disabled
1503           aPartDoc = aPart->partDoc();
1504
1505         theMenu->addAction(aActivatePartAction);
1506         aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
1507
1508       } else if (aObject->document() == aMgr->activeDocument()) {
1509         if (hasParameter || hasFeature) {
1510           myMenuMgr->action("EDIT_CMD")->setEnabled(true);
1511           theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
1512           if (aCurrentOp && aFeature.get()) {
1513             if (aCurrentOp->id().toStdString() == aFeature->getKind())
1514               myMenuMgr->action("EDIT_CMD")->setEnabled(false);
1515           }
1516         }
1517       }
1518     }
1519   } else {
1520     if (hasFeature) {
1521       myMenuMgr->action("EDIT_CMD")->setEnabled(aCurrentOp == 0);
1522       theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
1523       theMenu->addSeparator();
1524     }
1525   }
1526   bool aNotDeactivate = (aCurrentOp == 0);
1527   if (!aNotDeactivate) {
1528     aActivatePartAction->setEnabled(false);
1529   }
1530 }
1531
1532 //******************************************************
1533 #define EXPAND_PARENT(OBJ) \
1534 QModelIndex aObjIndex = aDataModel->objectIndex(OBJ); \
1535 if (aObjIndex.isValid()) { \
1536   QModelIndex aParent = aObjIndex.parent(); \
1537   int aCount = aDataModel->rowCount(aParent); \
1538   if (aCount == 1) \
1539     aTreeView->setExpanded(aParent, true); \
1540 }
1541
1542 //******************************************************
1543 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
1544 {
1545   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
1546     // Do not change activation of parts if an operation active
1547     static QStringList aAllowActivationList;
1548     if (aAllowActivationList.isEmpty())
1549       aAllowActivationList <<
1550       QString(PartSetPlugin_Part::ID().c_str()) <<
1551       QString(PartSetPlugin_Duplicate::ID().c_str()) <<
1552       QString(PartSetPlugin_Remove::ID().c_str());
1553     if (myWorkshop->currentOperation() &&
1554       (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
1555       return;
1556     XGUI_Workshop* aWorkshop = getWorkshop();
1557     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
1558     QLabel* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
1559     QPalette aPalet = aLabel->palette();
1560
1561     SessionPtr aMgr = ModelAPI_Session::get();
1562     DocumentPtr aActiveDoc = aMgr->activeDocument();
1563
1564     // Clear active part index if there is no Part documents
1565     // It could be not null if document was closed and opened a new
1566     // without closeDocument call
1567     if (aMgr->allOpenedDocuments().size() <= 1)
1568       myActivePartIndex = QModelIndex();
1569
1570     XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1571     QModelIndex aOldActive = myActivePartIndex;
1572     myActivePartIndex = aDataModel->documentRootIndex(aActiveDoc, 0);
1573     bool needUpdate = false;
1574     if (myActivePartIndex.isValid()) {
1575       needUpdate = aTreeView->isExpanded(myActivePartIndex);
1576       if (!needUpdate)
1577         aTreeView->setExpanded(myActivePartIndex, true);
1578     }
1579     if ((aOldActive != myActivePartIndex) && (aOldActive.isValid()))
1580       aTreeView->setExpanded(aOldActive, false);
1581
1582     aLabel->setPalette(aPalet);
1583     aWorkshop->updateCommandStatus();
1584
1585     // Update displayed objects in order to update active color
1586     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1587     QObjectPtrList aObjects = aDisplayer->displayedObjects();
1588     bool aHidden;
1589     foreach(ObjectPtr aObj, aObjects) {
1590       aHidden = !aObj->data() || !aObj->data()->isValid() ||
1591         aObj->isDisabled() || (!aObj->isDisplayed());
1592       if (!aHidden) {
1593         aDisplayer->redisplay(aObj, false);
1594       }
1595     }
1596     aDisplayer->updateViewer();
1597     // Update tree items if they are expanded
1598     if (needUpdate) {
1599       aTreeView->viewport()->update(aTreeView->viewport()->rect());
1600     }
1601   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
1602     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
1603         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
1604     std::set<ObjectPtr> aObjects = aUpdMsg->objects();
1605
1606     ObjectPtr aConstrObj;
1607     ObjectPtr aResultObj;
1608     std::set<ObjectPtr>::const_iterator aIt;
1609     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
1610     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
1611       ObjectPtr aObject = (*aIt);
1612       if ((!aResultObj.get()) && (aObject->groupName() == ModelAPI_ResultBody::group())
1613           && (aObject->document() != aRootDoc))
1614         aResultObj = aObject;
1615       if ((!aConstrObj.get()) && (aObject->groupName() == ModelAPI_ResultConstruction::group())
1616           && (aObject->document() != aRootDoc))
1617         aConstrObj = aObject;
1618       if (aResultObj.get() && aConstrObj.get())
1619         break;
1620     }
1621
1622     if (aResultObj.get() || aConstrObj.get()) {
1623       XGUI_Workshop* aWorkshop = getWorkshop();
1624       XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
1625       XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1626
1627       if (aResultObj.get()) {
1628         EXPAND_PARENT(aResultObj)
1629       }
1630       if (aConstrObj.get()) {
1631         EXPAND_PARENT(aConstrObj)
1632       }
1633     }
1634   }
1635   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
1636     CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
1637     if (aSketch.get()) {
1638       ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
1639       if (PartSet_SketcherMgr::isSketchOperation(anOperation))
1640         mySketchMgr->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
1641     }
1642   }
1643 }
1644
1645 //******************************************************
1646 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1647 {
1648   if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1649     return;
1650   SessionPtr aMgr = ModelAPI_Session::get();
1651   if (!theIndex.isValid()) {
1652     // It seems that this code is obsolete
1653     //aMgr->setActiveDocument(aMgr->moduleDocument());
1654     return;
1655   }
1656   if (theIndex.column() != 1) // Use only first column
1657     return;
1658
1659   XGUI_Workshop* aWorkshop = getWorkshop();
1660   XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1661   // De not use non editable Indexes
1662   if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1663     return;
1664   ObjectPtr aObj = aDataModel->object(theIndex);
1665
1666   ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1667   if (!aPart.get()) { // Probably this is Feature
1668     FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1669     if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1670       aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1671     }
1672   }
1673   if (aPart.get()) { // if this is a part
1674     if (aPart->partDoc() == aMgr->activeDocument()) {
1675       myMenuMgr->activatePartSet();
1676     } else {
1677       myMenuMgr->activatePart(aPart);
1678     }
1679   }
1680 }
1681
1682 //******************************************************
1683 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1684 {
1685   // z layer is created for all started operations in order to visualize operation AIS presentation
1686   // over the object
1687   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1688   if (aContext.IsNull())
1689     return;
1690
1691   Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1692   if (myVisualLayerId == 0) {
1693     if (myVisualLayerId == 0)
1694       aViewer->AddZLayer(myVisualLayerId);
1695   } else {
1696     TColStd_SequenceOfInteger aZList;
1697     aViewer->GetAllZLayers(aZList);
1698     bool aFound = false;
1699     for (int i = 1; i <= aZList.Length(); i++) {
1700       if (aZList(i) == myVisualLayerId) {
1701         aFound = true;
1702         break;
1703       }
1704     }
1705     if (!aFound)
1706       aViewer->AddZLayer(myVisualLayerId);
1707   }
1708   // if there is an active operation with validated widget,
1709   // the filters of this widget should be activated in the created view
1710   myWorkshop->selectionActivate()->updateSelectionFilters();
1711   myWorkshop->selectionActivate()->updateSelectionModes();
1712 }
1713
1714 //******************************************************
1715 void PartSet_Module::widgetStateChanged(int thePreviousState)
1716 {
1717   mySketchMgr->widgetStateChanged(thePreviousState);
1718 }
1719
1720 //******************************************************
1721 bool PartSet_Module::processEnter(const std::string& thePreviousAttributeID)
1722 {
1723   return mySketchReentrantMgr->processEnter(thePreviousAttributeID);
1724 }
1725
1726 //******************************************************
1727 void PartSet_Module::beforeOperationStopped(ModuleBase_Operation* theOperation)
1728 {
1729   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
1730     mySketchMgr->stopSketch(theOperation);
1731   }
1732 }
1733
1734 //******************************************************
1735 GeomShapePtr PartSet_Module::findShape(const AttributePtr& theAttribute)
1736 {
1737   GeomShapePtr aGeomShape;
1738
1739   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
1740   if (anOperation && sketchMgr()->isNestedSketchOperation(anOperation)) {
1741     aGeomShape = PartSet_Tools::findShapeBy2DPoint(theAttribute, myWorkshop);
1742   }
1743   return aGeomShape;
1744 }
1745
1746 //******************************************************
1747 AttributePtr PartSet_Module::findAttribute(const ObjectPtr& theObject,
1748                                            const GeomShapePtr& theGeomShape)
1749 {
1750   AttributePtr anAttribute;
1751   GeomShapePtr aGeomShape = theGeomShape;
1752   if (!aGeomShape.get()) {
1753     // processing shape of result, e.g. sketch circle center is selected, this is a result
1754     // the geom shape is empty, the shape of result should be used
1755     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1756     if (aResult.get()) {
1757       aGeomShape = aResult->shape();
1758     }
1759   }
1760
1761   if (aGeomShape.get()) {
1762     TopoDS_Shape aTDSShape = aGeomShape->impl<TopoDS_Shape>();
1763     std::pair<AttributePtr, int> anAttrAndIndex =
1764         PartSet_Tools::findAttributeBy2dPoint(theObject, aTDSShape, mySketchMgr->activeSketch());
1765     return anAttrAndIndex.first;
1766   }
1767   return anAttribute;
1768 }
1769
1770 //******************************************************
1771 std::shared_ptr<Events_Message> PartSet_Module::reentrantMessage()
1772 {
1773   return sketchReentranceMgr()->reentrantMessage();
1774 }
1775
1776 //******************************************************
1777 void PartSet_Module::setReentrantPreSelection(const std::shared_ptr<Events_Message>& theMessage)
1778 {
1779   sketchReentranceMgr()->setReentrantPreSelection(theMessage);
1780 }
1781
1782 //******************************************************
1783 void PartSet_Module::onChoiceChanged(ModuleBase_ModelWidget* theWidget,
1784                                      int theIndex)
1785 {
1786   ModuleBase_WidgetChoice* aChoiceWidget = dynamic_cast<ModuleBase_WidgetChoice*>(theWidget);
1787   if (!aChoiceWidget)
1788     return;
1789
1790   QString aChoiceTitle = aChoiceWidget->getPropertyPanelTitle(theIndex);
1791   if (!aChoiceTitle.isEmpty()) {
1792     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1793     if (!aOperation)
1794       return;
1795     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1796     if (aPanel)
1797       aPanel->setWindowTitle(aChoiceTitle);
1798   }
1799 }
1800
1801 //******************************************************
1802 XGUI_Workshop* PartSet_Module::getWorkshop() const
1803 {
1804   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
1805   return aConnector->workshop();
1806 }
1807
1808 void PartSet_Module::setDefaultConstraintShown()
1809 {
1810   myHasConstraintShown[PartSet_Tools::Geometrical] = true;
1811   myHasConstraintShown[PartSet_Tools::Dimensional] = true;
1812   myHasConstraintShown[PartSet_Tools::Expressions] = false;
1813 }
1814
1815 //******************************************************
1816 ModuleBase_ITreeNode* PartSet_Module::rootNode() const
1817 {
1818   return myRoot;
1819 }
1820
1821 //******************************************************
1822 void PartSet_Module::disableCustomMode(ModuleBase_CustomizeFlag theMode) {
1823   myCustomPrs->disableCustomMode(theMode);
1824 }
1825
1826 //******************************************************
1827 void PartSet_Module::enableCustomModes() {
1828   myCustomPrs->enableCustomModes();
1829 }