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