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