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