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