Salome HOME
ae090a39ed3af7ddfdf4482d28589224916ee133
[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_WidgetShapeSelector.h"
10 #include "PartSet_WidgetPoint2dAngle.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_MenuMgr.h"
17 #include "PartSet_CustomPrs.h"
18 #include "PartSet_IconFactory.h"
19
20 #include "PartSet_Filters.h"
21 #include "PartSet_FilterInfinite.h"
22
23 #include <PartSetPlugin_Remove.h>
24 #include <PartSetPlugin_Part.h>
25 #include <PartSetPlugin_Duplicate.h>
26
27 #include <ModuleBase_Operation.h>
28 #include <ModuleBase_OperationAction.h>
29 #include <ModuleBase_IViewer.h>
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_IPropertyPanel.h>
32 #include <ModuleBase_WidgetEditor.h>
33 #include <ModuleBase_WidgetValidated.h>
34 #include <ModuleBase_FilterFactory.h>
35 #include <ModuleBase_Tools.h>
36 #include <ModuleBase_OperationFeature.h>
37
38 #include <GeomValidators_ShapeType.h>
39 #include <GeomValidators_Finite.h>
40 #include <GeomValidators_Face.h>
41 #include <GeomValidators_ConstructionComposite.h>
42 #include <GeomValidators_ZeroOffset.h>
43 #include <GeomValidators_BooleanArguments.h>
44 #include <GeomValidators_Different.h>
45 #include <GeomValidators_PartitionArguments.h>
46
47
48 #include <ModelAPI_Object.h>
49 #include <ModelAPI_Events.h>
50 #include <ModelAPI_Validator.h>
51 #include <ModelAPI_Data.h>
52 #include <ModelAPI_Session.h>
53 #include <GeomValidators_DifferentShapes.h>
54 #include <ModelAPI_ResultBody.h>
55 #include <ModelAPI_AttributeString.h>
56
57 #include <GeomDataAPI_Point2D.h>
58 #include <GeomDataAPI_Point.h>
59 #include <GeomDataAPI_Dir.h>
60
61 #include <XGUI_Displayer.h>
62 #include <XGUI_Workshop.h>
63 #include <XGUI_OperationMgr.h>
64 #include <XGUI_PropertyPanel.h>
65 #include <XGUI_ModuleConnector.h>
66 #include <XGUI_ContextMenuMgr.h>
67 #include <XGUI_Tools.h>
68 #include <XGUI_ObjectsBrowser.h>
69 #include <XGUI_SelectionMgr.h>
70 #include <XGUI_DataModel.h>
71 #include <XGUI_ErrorMgr.h>
72
73 #include <SketchPlugin_Feature.h>
74 #include <SketchPlugin_Sketch.h>
75 #include <SketchPlugin_Line.h>
76 #include <SketchPlugin_Arc.h>
77 #include <SketchPlugin_Circle.h>
78 #include <SketchPlugin_Point.h>
79 #include <SketchPlugin_ConstraintAngle.h>
80 #include <SketchPlugin_ConstraintLength.h>
81 #include <SketchPlugin_ConstraintDistance.h>
82 #include <SketchPlugin_ConstraintParallel.h>
83 #include <SketchPlugin_ConstraintPerpendicular.h>
84 #include <SketchPlugin_ConstraintRadius.h>
85
86 #include <SketcherPrs_SymbolPrs.h>
87 #include <SketcherPrs_Tools.h>
88
89 #include <Events_Loop.h>
90 #include <Config_PropManager.h>
91 #include <Config_Keywords.h>
92
93 #include <StdSelect_TypeOfFace.hxx>
94 #include <TopoDS_Vertex.hxx>
95 #include <TopoDS.hxx>
96 #include <TopoDS_Shape.hxx>
97 #include <BRep_Tool.hxx>
98 #include <AIS_Dimension.hxx>
99
100 #include <QObject>
101 #include <QMouseEvent>
102 #include <QString>
103 #include <QTimer>
104 #include <QApplication>
105 #include <QMessageBox>
106 #include <QMainWindow>
107 #include <QLineEdit>
108
109 #include <GeomAlgoAPI_FaceBuilder.h>
110 #include <GeomDataAPI_Dir.h>
111
112 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
113
114 #ifdef _DEBUG
115 #include <QDebug>
116 #endif
117
118 /*!Create and return new instance of XGUI_Module*/
119 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
120 {
121   return new PartSet_Module(theWshop);
122 }
123
124 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
125   : ModuleBase_IModule(theWshop),
126   myRestartingMode(RM_None), myVisualLayerId(0), myHasConstraintShown(true)
127 {
128   new PartSet_IconFactory();
129
130   mySketchMgr = new PartSet_SketcherMgr(this);
131
132   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
133   XGUI_Workshop* aWorkshop = aConnector->workshop();
134
135   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
136   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
137   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
138           this, SLOT(onOperationActivatedByPreselection()));
139
140   ModuleBase_IViewer* aViewer = theWshop->viewer();
141   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
142           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
143   connect(aViewer, SIGNAL(viewTransformed(int)),
144           SLOT(onViewTransformed(int)));
145   connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
146           SLOT(onViewCreated(ModuleBase_IViewWindow*)));
147
148   myMenuMgr = new PartSet_MenuMgr(this);
149   myCustomPrs = new PartSet_CustomPrs(theWshop);
150
151   Events_Loop* aLoop = Events_Loop::loop();
152   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
153
154   mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
155   mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
156 }
157
158 PartSet_Module::~PartSet_Module()
159 {
160   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
161   for (; aIt.More(); aIt.Next()) {
162     Handle(SelectMgr_Filter) aFilter = aIt.Value();
163     if (!aFilter.IsNull())
164       aFilter.Nullify();
165   }
166   delete myCustomPrs;
167 }
168
169 void PartSet_Module::activateSelectionFilters()
170 {
171   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
172   for (; aIt.More(); aIt.Next()) {
173     Handle(SelectMgr_Filter) aFilter = aIt.Value();
174     if (!aFilter.IsNull())
175       myWorkshop->viewer()->addSelectionFilter(aFilter);
176   }
177 }
178
179 void PartSet_Module::deactivateSelectionFilters()
180 {
181   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
182   for (; aIt.More(); aIt.Next()) {
183     Handle(SelectMgr_Filter) aFilter = aIt.Value();
184     if (!aFilter.IsNull())
185       myWorkshop->viewer()->removeSelectionFilter(aFilter);
186   }
187 }
188
189 void PartSet_Module::registerValidators()
190 {
191   //Registering of validators
192   SessionPtr aMgr = ModelAPI_Session::get();
193   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
194   aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
195   aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
196   aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
197   aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
198   aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
199   aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
200   aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
201   aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
202   aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
203   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
204   aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
205   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
206   aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
207
208   aFactory->registerValidator("GeomValidators_DifferentShapes", new GeomValidators_DifferentShapes);
209   aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
210   aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
211   aFactory->registerValidator("GeomValidators_Finite", new GeomValidators_Finite);
212
213   aFactory->registerValidator("GeomValidators_ConstructionComposite",
214                               new GeomValidators_ConstructionComposite);
215
216   aFactory->registerValidator("GeomValidators_ZeroOffset",
217                               new GeomValidators_ZeroOffset);
218
219   aFactory->registerValidator("GeomValidators_BooleanArguments",
220                               new GeomValidators_BooleanArguments);
221
222   aFactory->registerValidator("PartSet_SketchEntityValidator",
223                               new PartSet_SketchEntityValidator);
224
225   aFactory->registerValidator("GeomValidators_Different",
226                               new GeomValidators_Different);
227
228   aFactory->registerValidator("GeomValidators_PartitionArguments",
229                               new GeomValidators_PartitionArguments);
230 }
231
232 void PartSet_Module::registerFilters()
233 {
234   //Registering of selection filters
235   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
236   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
237 }
238
239 void PartSet_Module::registerProperties()
240 {
241   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
242                                    PLANE_SIZE);
243   Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
244                                    Config_Prop::Integer, SKETCH_WIDTH);
245 }
246
247 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation) 
248 {
249   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
250     mySketchMgr->commitNestedSketch(theOperation);
251   }
252
253   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
254   if (!aFOperation || aFOperation->isEditOperation())
255     return;
256   // the selection is cleared after commit the create operation
257   // in order to do not use the same selected objects in the restarted operation
258   // for common behaviour, the selection is cleared even if the operation is not restarted
259   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
260   XGUI_Workshop* aWorkshop = aConnector->workshop();
261   aWorkshop->selector()->clearSelection();
262
263   /// Restart sketcher operations automatically
264   FeaturePtr aFeature = aFOperation->feature();
265   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
266             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
267   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
268                      myRestartingMode == RM_EmptyFeatureUsed)) {
269     myLastOperationId = aFOperation->id();
270     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
271     if (!sketchMgr()->sketchSolverError())
272       launchOperation(myLastOperationId);
273   }
274   breakOperationSequence();
275 }
276
277 void PartSet_Module::breakOperationSequence()
278 {
279   myLastOperationId = "";
280   myLastFeature = FeaturePtr();
281   myRestartingMode = RM_None;
282 }
283
284 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
285 {
286   breakOperationSequence();
287 }
288
289 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
290 {
291   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
292     mySketchMgr->startSketch(theOperation);
293   }
294   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
295     mySketchMgr->startNestedSketch(theOperation);
296   }
297
298   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
299   if (aFOperation)
300     myCustomPrs->activate(aFOperation->feature(), true);
301 }
302
303 void PartSet_Module::onOperationResumed(ModuleBase_Operation* theOperation)
304 {
305   ModuleBase_IModule::onOperationResumed(theOperation);
306
307   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
308   if (aFOperation)
309     myCustomPrs->activate(aFOperation->feature(), true);
310 }
311
312 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
313 {
314   bool isModified = myCustomPrs->deactivate(false);
315
316   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
317     mySketchMgr->stopSketch(theOperation);
318   }
319   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
320     mySketchMgr->stopNestedSketch(theOperation);
321   }
322
323   //VSV: Viewer is updated on feature update and redisplay
324   //if (isModified) {
325   //  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
326   //  XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
327   //  aDisplayer->updateViewer();
328   //}
329   mySketchMgr->onShowConstraintsToggle(myHasConstraintShown);
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 == "DELETE_CMD" || theActionId == "MOVE_CMD") {
367     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
368     if (aFeature) {
369       // part features are removed in the PartSet module only.
370       if (aFeature->getKind() == PartSetPlugin_Part::ID())
371         aValid = false;
372     }
373   }
374   return aValid;
375 }
376
377 bool PartSet_Module::canCommitOperation() const
378 {
379   return mySketchMgr->canCommitOperation();
380 }
381
382 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
383 {
384   // the sketch manager put the restriction to the objects erase
385   return mySketchMgr->canEraseObject(theObject);
386 }
387
388 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
389 {
390   // the sketch manager put the restriction to the objects display
391   return mySketchMgr->canDisplayObject(theObject);
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
424   if (anError.isEmpty())
425     anError = sketchMgr()->getFeatureError(theFeature);
426
427   if (anError.isEmpty()) {
428     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
429     XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
430     
431     if (anOpMgr->isValidationLocked()) {
432       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
433                                                              (anOpMgr->currentOperation());
434       if (!aFOperation || theFeature == aFOperation->feature())
435         anError = "Validation is locked by the current operation";
436     }
437   }
438   return anError;
439 }
440
441 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
442                                          QStringList& theIds) const
443 {
444   myMenuMgr->grantedOperationIds(theOperation, theIds);
445
446   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
447     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
448     XGUI_Workshop* aWorkshop = aConnector->workshop();
449
450     theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
451   }
452 }
453
454 void PartSet_Module::activeSelectionModes(QIntList& theModes)
455 {
456   theModes.clear();
457   if (mySketchMgr->activeSketch().get())
458     PartSet_SketcherMgr::sketchSelectionModes(theModes);
459 }
460
461 bool PartSet_Module::isMouseOverWindow()
462 {
463   return mySketchMgr->isMouseOverWindow();
464 }
465
466 void PartSet_Module::closeDocument()
467 {
468   clearViewer();
469 }
470
471 void PartSet_Module::clearViewer()
472 {
473   myCustomPrs->clearPrs();
474
475   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
476   XGUI_Workshop* aWorkshop = aConnector->workshop();
477   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
478   aDisplayer->deactivateSelectionFilters();
479 }
480
481 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
482 {
483   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
484   if (!aFOperation)
485     return;
486
487   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
488   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation())) {
489     // we have to manually activate the sketch label in edit mode
490       aPanel->activateWidget(aPanel->modelWidgets().first());
491       return;
492   }
493
494   // Restart last operation type 
495   if ((aFOperation->id() == myLastOperationId) && myLastFeature) {
496     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
497     if (aFOperation->id().toStdString() == SketchPlugin_Line::ID()) {
498       // Initialise new line with first point equal to end of previous
499       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
500       if (aPnt2dWgt) {
501         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
502         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
503           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
504         if (aPoint) {
505           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
506           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), aFOperation->feature(), 
507             aWgt->attributeID(), aPoint->x(), aPoint->y());
508           aPanel->activateNextWidget(aPnt2dWgt);
509         }
510       }
511     }
512   }
513 }
514
515
516 void PartSet_Module::onSelectionChanged()
517 {
518   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
519   if (!aOperation)
520     return;
521
522   bool isSketcherOp = false;
523   // An edit operation is enable only if the current opeation is the sketch operation
524   if (mySketchMgr->activeSketch()) {
525     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
526       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
527   }
528   if (isSketcherOp) {
529     // Editing of constraints can be done on selection
530     ModuleBase_ISelection* aSelect = myWorkshop->selection();
531     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
532     if (aSelected.size() == 1) {
533       ModuleBase_ViewerPrs aPrs = aSelected.first();
534       ObjectPtr aObject = aPrs.object();
535       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
536       if (aFeature) {
537         std::string aId = aFeature->getKind();
538         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
539             (aId == SketchPlugin_ConstraintLength::ID()) || 
540             (aId == SketchPlugin_ConstraintDistance::ID()) ||
541             (aId == SketchPlugin_ConstraintAngle::ID())) {
542           editFeature(aFeature);
543         }
544       }
545     }
546   } 
547 }
548
549 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
550 {
551   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
552   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
553   anOpMgr->onKeyReleased(theEvent);
554 }
555
556 void PartSet_Module::onEnterReleased()
557 {
558   myRestartingMode = RM_EmptyFeatureUsed;
559 }
560
561 void PartSet_Module::onOperationActivatedByPreselection()
562 {
563   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
564   if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
565     // Set final definitions if they are necessary
566     //propertyPanelDefined(aOperation);
567
568     /// Commit sketcher operations automatically
569     anOperation->commit();
570   }
571 }
572
573 void PartSet_Module::onNoMoreWidgets()
574 {
575   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
576   if (anOperation) {
577     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
578       if (myRestartingMode != RM_Forbided)
579         myRestartingMode = RM_LastFeatureUsed;
580       XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
581       XGUI_Workshop* aWorkshop = aConnector->workshop();
582       XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
583       // do nothing if the feature can not be applyed
584       if (anOpMgr->isApplyEnabled())
585         anOperation->commit();
586     }
587   }
588 }
589
590 void PartSet_Module::onVertexSelected()
591 {
592   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
593   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
594     /// If last line finished on vertex the lines creation sequence has to be break
595     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
596     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
597     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
598     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
599     bool aFoundWidget = false;
600     bool aFoundObligatory = false;
601     for (; anIt != aLast && !aFoundObligatory; anIt++) {
602       if (!aFoundWidget)
603         aFoundWidget = *anIt == anActiveWidget;
604       else
605         aFoundObligatory = (*anIt)->isObligatory();
606     }
607     if (!aFoundObligatory)
608       myRestartingMode = RM_Forbided;
609   }
610 }
611
612 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
613                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
614 {
615   ModuleBase_IWorkshop* aWorkshop = workshop();
616   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
617   XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
618   ModuleBase_ModelWidget* aWgt = NULL;
619   if (theType == "sketch-start-label") {
620     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
621       theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
622     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
623       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
624     connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
625       mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
626     aWgt = aLabelWgt;
627   } else if (theType == "sketch-2dpoint_selector") {
628     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
629                                                                  theWidgetApi, theParentId);
630     aPointWgt->setSketch(mySketchMgr->activeSketch());
631     connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
632     aWgt = aPointWgt;
633   } else if (theType == "point2ddistance") {
634     PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
635                                                         aWorkshop, theWidgetApi, theParentId);
636     aDistanceWgt->setSketch(mySketchMgr->activeSketch());
637     aWgt = aDistanceWgt;
638   } else if(theType == "point2dangle") {
639     PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent,
640                                                            aWorkshop, theWidgetApi, theParentId);
641     anAngleWgt->setSketch(mySketchMgr->activeSketch());
642     aWgt = anAngleWgt;
643   } else if (theType == "sketch_shape_selector") {
644     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
645       new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
646     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
647     aWgt = aShapeSelectorWgt;
648   } else if (theType == "sketch_multi_selector") {
649     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
650       new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
651     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
652     aWgt = aShapeSelectorWgt;
653   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
654     aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
655   } else if (theType == "export_file_selector") {
656     aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
657   } else if (theType == "sketch_launcher") {
658     aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
659   }
660   return aWgt;
661 }
662
663
664 bool PartSet_Module::deleteObjects()
665 {
666   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
667   XGUI_Workshop* aWorkshop = aConnector->workshop();
668   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
669
670   //SessionPtr aMgr = ModelAPI_Session::get();
671   // 1. check whether the delete should be processed in the module
672   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
673   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
674        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
675   if (isSketchOp || isNestedOp) {
676     // 2. find selected presentations
677     // selected objects should be collected before the current operation abort because
678     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
679     ModuleBase_ISelection* aSel = workshop()->selection();
680     QObjectPtrList aSelectedObj = aSel->selectedPresentations();
681     // if there are no selected objects in the viewer, that means that the selection in another
682     // place cased this method. It is necessary to return the false value to understande in above
683     // method that delete is not processed
684     if (aSelectedObj.count() == 0)
685       return false;
686
687     // avoid delete of the objects, which are not belong to the current sketch
688     // in order to do not delete results of other sketches
689     QObjectPtrList aSketchObjects;
690     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
691     for ( ; anIt != aLast; anIt++) {
692       ObjectPtr anObject = *anIt;
693       if (mySketchMgr->isObjectOfSketch(anObject)) {
694         // sketch feature should be used in this list because workshop deletes features only
695         // results are skipped
696         FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
697         aSketchObjects.append(aSketchFeature);
698       }
699     }
700     // if the selection contains only local selected presentations from other sketches,
701     // the Delete operation should not be done at all
702     if (aSketchObjects.size() == 0)
703       return true;
704
705     // 3. start operation
706     QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
707     ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
708
709     // the active nested sketch operation should be aborted unconditionally
710     // the Delete action should be additionally granted for the Sketch operation
711     // in order to do not abort/commit it
712     if (!anOpMgr->canStartOperation(anOpAction->id()))
713       return true; // the objects are processed but can not be deleted
714
715     anOpMgr->startOperation(anOpAction);
716
717     // 4. delete features
718     // sketch feature should be skipped, only sub-features can be removed
719     // when sketch operation is active
720     aWorkshop->deleteFeatures(aSketchObjects);
721     // 5. stop operation
722     anOpMgr->commitOperation();
723   }
724   else {
725     bool isPartRemoved = false;
726     // Delete part with help of PartSet plugin
727     // TODO: the deleted objects has to be processed by multiselection
728     QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
729     if (aObjects.size() == 1) {
730       ObjectPtr aObj = aObjects.first();
731       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
732       if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
733         // Remove feature should be created in the document of the part results
734         ResultPtr aPartResult = aFeature->firstResult();
735         if (aPartResult.get()) {
736           std::shared_ptr<ModelAPI_ResultPart> aPart =
737                        std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
738           DocumentPtr aPartDoc = aPart->partDoc();
739           if (aPartDoc.get()) {
740             ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction
741                                               (PartSetPlugin_Remove::ID().c_str(), this);
742             if (!anOpMgr->canStartOperation(anOpAction->id()))
743               return true; // the objects are processed but can not be deleted
744
745             anOpMgr->startOperation(anOpAction);
746
747             FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
748             aFeature->execute();
749
750             anOpMgr->commitOperation();
751             isPartRemoved = true;
752           }
753         }
754       }
755     }
756     return isPartRemoved;
757   }
758   return true;
759 }
760
761 void PartSet_Module::onFeatureTriggered()
762 {
763   QAction* aCmd = dynamic_cast<QAction*>(sender());
764   if (aCmd->isCheckable() && aCmd->isChecked()) {
765     // 1. check whether the delete should be processed in the module
766     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
767     bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
768     if (isNestedOp) {
769       // in case if in the viewer nothing is displayed, the create operation should not be
770       // comitted even if all values of the feature are initialized
771       if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
772         // the action information should be saved before the operation is aborted
773         // because this abort leads to update command status, which unchecks this action
774         anOperation->abort();
775         launchOperation(aCmd->data().toString());
776       }
777     }
778   }
779   ModuleBase_IModule::onFeatureTriggered();
780 }
781
782 void PartSet_Module::launchOperation(const QString& theCmdId)
783 {
784   if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
785     // Show constraints if a constraint was anOperation
786     myHasConstraintShown = mySketchMgr->isConstraintsShown();
787     mySketchMgr->onShowConstraintsToggle(true);
788   }
789   ModuleBase_IModule::launchOperation(theCmdId);
790 }
791
792
793 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS) 
794 {
795   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
796   if (!anAIS.IsNull()) {
797     Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
798     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
799     if (!aDim.IsNull()) {
800       aCtx->SetZLayer(aDim, myVisualLayerId);
801     } else {
802       Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
803       if (!aCons.IsNull())
804         aCtx->SetZLayer(aCons, myVisualLayerId);
805     }
806   }
807 }
808
809 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
810 {
811   // this is obsolete
812   // it should be recomputed in order to disappear in the viewer if the corresponded object
813   // is erased
814   //if (myCustomPrs->isActive())
815   //  myCustomPrs->redisplay(theObject, false);
816 }
817
818 void PartSet_Module::onViewTransformed(int theTrsfType)
819 {
820   // Set length of arrows constant in pixel size
821   // if the operation is panning or rotate or panglobal then do nothing
822   if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
823     return;
824   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
825   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
826   if (aContext.IsNull())
827     return;
828
829   //Handle(V3d_View) aView = aViewer->activeView();
830
831   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
832   XGUI_Workshop* aWorkshop = aConnector->workshop();
833   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
834   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
835   Handle(V3d_View) aView;
836   double aScale = 0;
837   for (aV3dViewer->InitDefinedViews(); 
838        aV3dViewer->MoreDefinedViews(); 
839        aV3dViewer->NextDefinedViews()) {
840     Handle(V3d_View) aV = aV3dViewer->DefinedView();
841     double aS = aV->Scale();
842     if (aS > aScale) {
843       aScale = aS;
844       aView = aV;
845     }
846   }
847   if (aView.IsNull())
848     return;
849   double aLen = aView->Convert(20);
850
851   double aPrevLen = SketcherPrs_Tools::getArrowSize();
852   SketcherPrs_Tools::setArrowSize(aLen);
853   const double aPrevScale = aViewer->Scale(aViewer->activeView());
854   const double aCurScale = aViewer->activeView()->Camera()->Scale();
855   aViewer->SetScale(aViewer->activeView(), aCurScale);
856   SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
857   bool isModified = false;
858   QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
859   foreach (AISObjectPtr aAIS, aPrsList) {
860     Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
861
862     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
863     if (!aDim.IsNull()) {
864       aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
865       aContext->Redisplay(aDim, false);
866       isModified = true;
867     }
868   }
869   if (isModified)
870     aDisplayer->updateViewer();
871 }
872
873 bool PartSet_Module::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
874 {
875   bool isRedisplayed = false;
876   if (myCustomPrs->isActive())
877     isRedisplayed = myCustomPrs->redisplay(theObject, theUpdateViewer);
878
879   return isRedisplayed;
880 }
881
882 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
883 {
884   XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
885   if (aOB) {
886     //QLineEdit* aLabel = aOB->activeDocLabel();
887     //QPalette aPalet = aLabel->palette();
888     //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
889     //aLabel->setPalette(aPalet);
890     aOB->treeView()->setExpandsOnDoubleClick(false);
891     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)), 
892       SLOT(onTreeViewDoubleClick(const QModelIndex&)));
893   }
894 }
895
896 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
897 {
898   ObjectPtr anObject;
899   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
900   if (aOperation) {
901     /// If last line finished on vertex the lines creation sequence has to be break
902     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
903     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
904     // if there is an active widget, find the presented object in it
905     if (!anActiveWidget)
906       anActiveWidget = aPanel->preselectionWidget();
907     
908     ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
909                                                                            (anActiveWidget);
910     if (aWidgetValidated)
911       anObject = aWidgetValidated->findPresentedObject(theAIS);
912   }
913   return anObject;
914 }
915
916 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
917 {
918   QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
919   int aSelected = aObjects.size();
920   SessionPtr aMgr = ModelAPI_Session::get();
921   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
922   QAction* aActivatePartSetAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
923
924   ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
925   if (aSelected == 1) {
926     bool hasResult = false;
927     bool hasFeature = false;
928     bool hasParameter = false;
929     bool hasSubFeature = false;
930     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
931
932     ObjectPtr aObject = aObjects.first();
933     if (aObject) {
934       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
935       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
936       bool isPart = aPart.get() || 
937         (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
938       if (isPart) {
939         DocumentPtr aPartDoc;
940         if (!aPart.get()) {
941           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
942         }
943         if (aPart.get()) // this may be null is Part feature is disabled
944           aPartDoc = aPart->partDoc();
945           
946         theMenu->addAction(aActivatePartAction);
947         aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
948
949       } else if (aObject->document() == aMgr->activeDocument()) {
950         if (hasParameter || hasFeature) {
951           myMenuMgr->action("EDIT_CMD")->setEnabled(true);
952           theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
953           if (aCurrentOp && aFeature.get()) {
954             if (aCurrentOp->id().toStdString() == aFeature->getKind())
955               myMenuMgr->action("EDIT_CMD")->setEnabled(false);
956           }
957         }
958       }
959
960       ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
961       if( aResult.get() )
962         theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
963     } else {  // If feature is 0 the it means that selected root object (document)
964       theMenu->addAction(aActivatePartSetAction);
965       aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
966     }
967   } else if (aSelected == 0) {
968     // if there is no selection then it means that upper label is selected
969     QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
970     if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
971       theMenu->addAction(aActivatePartSetAction);
972       aActivatePartSetAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
973   }
974   bool aNotDeactivate = (aCurrentOp == 0);
975   if (!aNotDeactivate) {
976     aActivatePartAction->setEnabled(false);
977     aActivatePartSetAction->setEnabled(false);
978   }
979 }
980
981 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
982 {
983   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
984     // Do not change activation of parts if an operation active
985     static QStringList aAllowActivationList;
986     if (aAllowActivationList.isEmpty())
987       aAllowActivationList << 
988       QString(PartSetPlugin_Part::ID().c_str()) << 
989       QString(PartSetPlugin_Duplicate::ID().c_str()) <<
990       QString(PartSetPlugin_Remove::ID().c_str());
991     if (myWorkshop->currentOperation() && 
992       (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
993       return;
994     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
995     XGUI_Workshop* aWorkshop = aConnector->workshop();
996     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
997     QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
998     QPalette aPalet = aLabel->palette();
999
1000     SessionPtr aMgr = ModelAPI_Session::get();
1001     DocumentPtr aActiveDoc = aMgr->activeDocument();
1002     if (aActivePartIndex.isValid())
1003       aTreeView->setExpanded(aActivePartIndex, false);
1004     XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1005     aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
1006     if (aActivePartIndex.isValid())
1007       aTreeView->setExpanded(aActivePartIndex, true);
1008
1009     aLabel->setPalette(aPalet);
1010     aWorkshop->updateCommandStatus();
1011
1012     // Update displayed objects in order to update active color
1013     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1014     QObjectPtrList aObjects = aDisplayer->displayedObjects();
1015     bool aHidden;
1016     foreach(ObjectPtr aObj, aObjects) {
1017       //TODO: replace by redisplay event.
1018       aHidden = !aObj->data() || !aObj->data()->isValid() || 
1019         aObj->isDisabled() || (!aObj->isDisplayed());
1020       if (!aHidden)
1021         aDisplayer->redisplay(aObj, false);
1022     }
1023     aDisplayer->updateViewer();
1024   }
1025 }
1026
1027 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1028 {
1029   if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1030     return;
1031   SessionPtr aMgr = ModelAPI_Session::get();
1032   if (!theIndex.isValid()) {
1033     aMgr->setActiveDocument(aMgr->moduleDocument());
1034     return;
1035   }
1036   if (theIndex.column() != 0) // Use only first column
1037     return;
1038
1039   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1040   XGUI_Workshop* aWorkshop = aConnector->workshop();
1041   XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1042   // De not use non editable Indexes
1043   if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1044     return;
1045   ObjectPtr aObj = aDataModel->object(theIndex);
1046
1047   ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1048   if (!aPart.get()) { // Probably this is Feature
1049     FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1050     if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1051       aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1052     }
1053   }
1054   if (aPart.get()) { // if this is a part
1055     if (aPart->partDoc() == aMgr->activeDocument()) {
1056       myMenuMgr->activatePartSet();
1057     } else {
1058       aPart->activate();
1059     }
1060   }
1061 }
1062
1063
1064 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1065 {
1066   // z layer is created for all started operations in order to visualize operation AIS presentation
1067   // over the object
1068   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1069   if (aContext.IsNull())
1070     return;
1071
1072   Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1073   if (myVisualLayerId == 0) {
1074     if (myVisualLayerId == 0)
1075       aViewer->AddZLayer(myVisualLayerId);
1076   } else {
1077     TColStd_SequenceOfInteger aZList;
1078     aViewer->GetAllZLayers(aZList);
1079     bool aFound = false;
1080     for (int i = 1; i <= aZList.Length(); i++) {
1081       if (aZList(i) == myVisualLayerId) {
1082         aFound = true;
1083         break;
1084       }
1085     }
1086     if (!aFound)
1087       aViewer->AddZLayer(myVisualLayerId);
1088   }
1089   // if there is an active operation with validated widget,
1090   // the filters of this widget should be activated in the created view
1091   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1092   if (aOperation) {
1093     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1094     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1095     if (anActiveWidget) {
1096       ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1097                                                                              (anActiveWidget);
1098       if (aWidgetValidated)
1099         aWidgetValidated->activateFilters(true);
1100     }
1101   }
1102 }