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