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