]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
483e34ade4123e8120804dfca8dc97bf38959d40
[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
18 #include "PartSet_Filters.h"
19 #include "PartSet_FilterInfinite.h"
20
21 #include <PartSetPlugin_Remove.h>
22 #include <PartSetPlugin_Part.h>
23
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_IViewWindow.h>
27 #include <ModuleBase_IPropertyPanel.h>
28 #include <ModuleBase_WidgetEditor.h>
29 #include <ModuleBase_FilterFactory.h>
30 #include <ModuleBase_Tools.h>
31 #include <GeomValidators_ShapeType.h>
32
33 #include <GeomValidators_Face.h>
34 #include <GeomValidators_ConstructionComposite.h>
35 #include <GeomValidators_ZeroOffset.h>
36
37
38 #include <ModelAPI_Object.h>
39 #include <ModelAPI_Events.h>
40 #include <ModelAPI_Validator.h>
41 #include <ModelAPI_Data.h>
42 #include <ModelAPI_Session.h>
43 #include <ModelAPI_ShapeValidator.h>
44
45 #include <GeomDataAPI_Point2D.h>
46 #include <GeomDataAPI_Point.h>
47 #include <GeomDataAPI_Dir.h>
48
49 #include <XGUI_Displayer.h>
50 #include <XGUI_Workshop.h>
51 #include <XGUI_OperationMgr.h>
52 #include <XGUI_PropertyPanel.h>
53 #include <XGUI_ModuleConnector.h>
54 #include <XGUI_ContextMenuMgr.h>
55 #include <XGUI_Tools.h>
56 #include <XGUI_ObjectsBrowser.h>
57
58 #include <SketchPlugin_Feature.h>
59 #include <SketchPlugin_Sketch.h>
60 #include <SketchPlugin_Line.h>
61 #include <SketchPlugin_Arc.h>
62 #include <SketchPlugin_Circle.h>
63 #include <SketchPlugin_Point.h>
64 #include <SketchPlugin_ConstraintLength.h>
65 #include <SketchPlugin_ConstraintDistance.h>
66 #include <SketchPlugin_ConstraintParallel.h>
67 #include <SketchPlugin_ConstraintPerpendicular.h>
68 #include <SketchPlugin_ConstraintRadius.h>
69
70 #include <SketcherPrs_SymbolPrs.h>
71 #include <SketcherPrs_Tools.h>
72
73 #include <Events_Loop.h>
74 #include <Config_PropManager.h>
75 #include <Config_Keywords.h>
76
77 #include <StdSelect_TypeOfFace.hxx>
78 #include <TopoDS_Vertex.hxx>
79 #include <TopoDS.hxx>
80 #include <TopoDS_Shape.hxx>
81 #include <BRep_Tool.hxx>
82 #include <AIS_Dimension.hxx>
83
84 #include <QObject>
85 #include <QMouseEvent>
86 #include <QString>
87 #include <QTimer>
88 #include <QApplication>
89 #include <QMessageBox>
90 #include <QMainWindow>
91 #include <QLineEdit>
92
93 #include <GeomAlgoAPI_FaceBuilder.h>
94 #include <GeomDataAPI_Dir.h>
95
96 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
97
98 #ifdef _DEBUG
99 #include <QDebug>
100 #endif
101
102
103
104 /*!Create and return new instance of XGUI_Module*/
105 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
106 {
107   return new PartSet_Module(theWshop);
108 }
109
110 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
111   : ModuleBase_IModule(theWshop),
112   myRestartingMode(RM_None), myVisualLayerId(0)
113 {
114   mySketchMgr = new PartSet_SketcherMgr(this);
115   myDataModel = new PartSet_DocumentDataModel(this);
116
117   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
118   XGUI_Workshop* aWorkshop = aConnector->workshop();
119
120   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
121   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
122   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
123           this, SLOT(onOperationActivatedByPreselection()));
124
125   ModuleBase_IViewer* aViewer = theWshop->viewer();
126   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
127           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
128   connect(aViewer, SIGNAL(viewTransformed(int)),
129           SLOT(onViewTransformed(int)));
130
131   myMenuMgr = new PartSet_MenuMgr(this);
132
133   Events_Loop* aLoop = Events_Loop::loop();
134   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
135
136   mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
137   mySelectionFilters.Append(new PartSet_FilterInfinite());
138 }
139
140 PartSet_Module::~PartSet_Module()
141 {
142   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
143   for (; aIt.More(); aIt.Next()) {
144     Handle(SelectMgr_Filter) aFilter = aIt.Value();
145     if (!aFilter.IsNull())
146       aFilter.Nullify();
147   }
148 }
149
150 void PartSet_Module::activateSelectionFilters()
151 {
152   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
153   for (; aIt.More(); aIt.Next()) {
154     Handle(SelectMgr_Filter) aFilter = aIt.Value();
155     if (!aFilter.IsNull())
156       myWorkshop->viewer()->addSelectionFilter(aFilter);
157   }
158 }
159
160 void PartSet_Module::deactivateSelectionFilters()
161 {
162   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
163   for (; aIt.More(); aIt.Next()) {
164     Handle(SelectMgr_Filter) aFilter = aIt.Value();
165     if (!aFilter.IsNull())
166       myWorkshop->viewer()->removeSelectionFilter(aFilter);
167   }
168 }
169
170 void PartSet_Module::registerValidators()
171 {
172   //Registering of validators
173   SessionPtr aMgr = ModelAPI_Session::get();
174   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
175   aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
176   aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
177   aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
178   aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
179   aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
180   aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
181   aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
182   aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
183   aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
184   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
185
186   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
187   aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
188
189   aFactory->registerValidator("GeomValidators_ShapeType", new GeomValidators_ShapeType);
190   aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
191
192   aFactory->registerValidator("GeomValidators_ConstructionComposite",
193                               new GeomValidators_ConstructionComposite);
194
195   aFactory->registerValidator("GeomValidators_ZeroOffset",
196                               new GeomValidators_ZeroOffset);
197
198   aFactory->registerValidator("PartSet_SketchEntityValidator",
199                               new PartSet_SketchEntityValidator);
200
201   aFactory->registerValidator("PartSet_SameTypeAttr",
202                               new PartSet_SameTypeAttrValidator);
203 }
204
205 void PartSet_Module::registerFilters()
206 {
207   //Registering of selection filters
208   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
209   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
210 }
211
212 void PartSet_Module::registerProperties()
213 {
214   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
215                                    PLANE_SIZE);
216   Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
217                                    Config_Prop::Integer, SKETCH_WIDTH);
218 }
219
220 void PartSet_Module::onOperationCommitted(ModuleBase_Operation* theOperation) 
221 {
222   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
223     mySketchMgr->commitNestedSketch(theOperation);
224   }
225
226   if (theOperation->isEditOperation())
227     return;
228   // the selection is cleared after commit the create operation
229   // in order to do not use the same selected objects in the restarted operation
230   // for common behaviour, the selection is cleared even if the operation is not restarted
231   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
232   if (!aContext.IsNull())
233     aContext->ClearSelected();
234
235   /// Restart sketcher operations automatically
236   FeaturePtr aFeature = theOperation->feature();
237   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
238             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
239   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
240                      myRestartingMode == RM_EmptyFeatureUsed)) {
241     myLastOperationId = theOperation->id();
242     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
243     
244     launchOperation(myLastOperationId);
245   }
246   breakOperationSequence();
247 }
248
249 void PartSet_Module::breakOperationSequence()
250 {
251   myLastOperationId = "";
252   myLastFeature = FeaturePtr();
253   myRestartingMode = RM_None;
254 }
255
256 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
257 {
258   breakOperationSequence();
259 }
260
261 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
262 {
263   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
264     Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
265     aViewer->AddZLayer(myVisualLayerId);
266     mySketchMgr->startSketch(theOperation);
267   }
268   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
269     mySketchMgr->startNestedSketch(theOperation);
270   }
271 }
272
273 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
274 {
275   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
276     mySketchMgr->stopSketch(theOperation);
277     Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
278     aViewer->RemoveZLayer(myVisualLayerId);
279     myVisualLayerId = 0;
280   }
281   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
282     mySketchMgr->stopNestedSketch(theOperation);
283   }
284 }
285
286 ModuleBase_Operation* PartSet_Module::currentOperation() const
287 {
288   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
289   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
290   return anOpMgr->currentOperation();
291 }
292
293 bool PartSet_Module::canUndo() const
294 {
295   bool aCanUndo = false;
296   SessionPtr aMgr = ModelAPI_Session::get();
297   if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
298     aCanUndo = !aMgr->isOperation();
299     if (!aCanUndo) // check the enable state additionally by sketch manager
300       aCanUndo = aMgr->canUndo();
301   }
302   return aCanUndo;
303 }
304
305 bool PartSet_Module::canRedo() const
306 {
307   bool aCanRedo = false;
308   SessionPtr aMgr = ModelAPI_Session::get();
309   if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
310     aCanRedo = !aMgr->isOperation();
311     if (!aCanRedo) // check the enable state additionally by sketch manager
312       aCanRedo = aMgr->canRedo();
313   }
314   return aCanRedo;
315 }
316
317 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
318 {
319   // the sketch manager put the restriction to the objects display
320   return mySketchMgr->canDisplayObject(theObject);
321 }
322
323 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
324 {
325   bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
326
327   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
328   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
329        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
330   if (isSketchOp || isNestedOp) {
331     // in active sketch operation it is possible to activate operation object in selection
332     // in the edit operation, e.g. points of the line can be moved when the line is edited
333     aCanActivate = aCanActivate || anOperation->isEditOperation();
334   }
335   return aCanActivate;
336 }
337
338 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
339 {
340   return myMenuMgr->addViewerMenu(theMenu, theStdActions);
341 }
342
343 void PartSet_Module::activeSelectionModes(QIntList& theModes)
344 {
345   theModes.clear();
346   if (mySketchMgr->activeSketch().get())
347     PartSet_SketcherMgr::sketchSelectionModes(theModes);
348 }
349
350 bool PartSet_Module::isMouseOverWindow()
351 {
352   return mySketchMgr->isMouseOverWindow();
353 }
354
355 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
356 {
357   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
358   if (PartSet_SketcherMgr::isSketchOperation(theOperation) &&  (theOperation->isEditOperation())) {
359     // we have to manually activate the sketch label in edit mode
360       aPanel->activateWidget(aPanel->modelWidgets().first());
361       return;
362   }
363
364   // Restart last operation type 
365   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
366     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
367     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
368       // Initialise new line with first point equal to end of previous
369       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
370       if (aPnt2dWgt) {
371         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
372         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
373           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
374         if (aPoint) {
375           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
376           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
377             aWgt->attributeID(), aPoint->x(), aPoint->y());
378           aPanel->activateNextWidget(aPnt2dWgt);
379         }
380       }
381     }
382   } else {
383     // Start editing constraint
384     if (theOperation->isEditOperation()) {
385       // TODO: #391 - to be removed
386       std::string aId = theOperation->id().toStdString();
387       if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
388           PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
389         // Find and activate widget for management of point for dimension line position
390         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
391         foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
392           PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
393           if (aPntWgt) {
394             aPanel->activateWidget(aPntWgt);
395             return;
396           }
397         }
398       }
399     }
400   }
401 }
402
403
404 void PartSet_Module::onSelectionChanged()
405 {
406   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
407   if (!aOperation)
408     return;
409
410   bool isSketcherOp = false;
411   // An edit operation is enable only if the current opeation is the sketch operation
412   if (mySketchMgr->activeSketch()) {
413     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
414       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
415   }
416   if (isSketcherOp) {
417     // Editing of constraints can be done on selection
418     ModuleBase_ISelection* aSelect = myWorkshop->selection();
419     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
420     if (aSelected.size() == 1) {
421       ModuleBase_ViewerPrs aPrs = aSelected.first();
422       ObjectPtr aObject = aPrs.object();
423       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
424       if (aFeature) {
425         std::string aId = aFeature->getKind();
426         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
427             (aId == SketchPlugin_ConstraintLength::ID()) || 
428             (aId == SketchPlugin_ConstraintDistance::ID())) {
429           editFeature(aFeature);
430         }
431       }
432     }
433   } 
434 }
435
436 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
437 {
438   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
439   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
440   anOpMgr->onKeyReleased(theEvent);
441 }
442
443 void PartSet_Module::onEnterReleased()
444 {
445   myRestartingMode = RM_EmptyFeatureUsed;
446 }
447
448 void PartSet_Module::onOperationActivatedByPreselection()
449 {
450   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
451   if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
452     // Set final definitions if they are necessary
453     //propertyPanelDefined(aOperation);
454
455     /// Commit sketcher operations automatically
456     anOperation->commit();
457   }
458 }
459
460 void PartSet_Module::onNoMoreWidgets()
461 {
462   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
463   if (anOperation) {
464     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
465       if (myRestartingMode != RM_Forbided)
466         myRestartingMode = RM_LastFeatureUsed;
467       anOperation->commit();
468     }
469   }
470 }
471
472 void PartSet_Module::onVertexSelected()
473 {
474   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
475   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
476     /// If last line finished on vertex the lines creation sequence has to be break
477     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
478     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
479     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
480     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
481     bool aFoundWidget = false;
482     bool aFoundObligatory = false;
483     for (; anIt != aLast && !aFoundObligatory; anIt++) {
484       if (!aFoundWidget)
485         aFoundWidget = *anIt == anActiveWidget;
486       else
487         aFoundObligatory = (*anIt)->isObligatory();
488     }
489     if (!aFoundObligatory)
490       myRestartingMode = RM_Forbided;
491   }
492 }
493
494 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
495                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
496 {
497   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
498   XGUI_Workshop* aWorkshop = aConnector->workshop();
499   ModuleBase_ModelWidget* aWgt = NULL;
500   if (theType == "sketch-start-label") {
501     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, 
502       theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
503     aLabelWgt->setWorkshop(aWorkshop);
504     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
505       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
506     connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
507       mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
508     aWgt = aLabelWgt;
509   } else if (theType == "sketch-2dpoint_selector") {
510     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
511     aPointWgt->setWorkshop(aWorkshop);
512     aPointWgt->setSketch(mySketchMgr->activeSketch());
513     connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
514     aWgt = aPointWgt;
515   } else if (theType == "point2ddistance") {
516     PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
517     aDistanceWgt->setWorkshop(aWorkshop);
518     aDistanceWgt->setSketch(mySketchMgr->activeSketch());
519     aWgt = aDistanceWgt;
520   } else if(theType == "point2dangle") {
521     PartSet_WidgetPoint2dAngle* anAngleWgt = new PartSet_WidgetPoint2dAngle(theParent, theWidgetApi, theParentId);
522     anAngleWgt->setWorkshop(aWorkshop);
523     anAngleWgt->setSketch(mySketchMgr->activeSketch());
524     aWgt = anAngleWgt;
525   } else if (theType == "sketch_shape_selector") {
526     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
527       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
528     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
529     aWgt = aShapeSelectorWgt;
530   } else if (theType == "sketch_multi_selector") {
531     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
532       new PartSet_WidgetMultiSelector(theParent, workshop(), theWidgetApi, theParentId);
533     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
534     aWgt = aShapeSelectorWgt;
535   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
536     aWgt = new PartSet_WidgetEditor(theParent, workshop(), theWidgetApi, theParentId);
537   } else if (theType == "export_file_selector") {
538     aWgt = new PartSet_WidgetFileSelector(theParent, workshop(), theWidgetApi, theParentId);
539   } else if (theType == "sketch_launcher") {
540     aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
541   }
542   return aWgt;
543 }
544
545
546 bool PartSet_Module::deleteObjects()
547 {
548   SessionPtr aMgr = ModelAPI_Session::get();
549   // 1. check whether the delete should be processed in the module
550   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
551   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
552        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
553   if (isSketchOp || isNestedOp) {
554     // 2. find selected presentations
555     // selected objects should be collected before the current operation abort because
556     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
557     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
558     XGUI_Workshop* aWorkshop = aConnector->workshop();
559     ModuleBase_ISelection* aSel = workshop()->selection();
560     QObjectPtrList aSelectedObj = aSel->selectedPresentations();
561     // if there are no selected objects in the viewer, that means that the selection in another
562     // place cased this method. It is necessary to return the false value to understande in above
563     // method that delete is not processed
564     if (aSelectedObj.count() == 0)
565       return false;
566
567     // avoid delete of the objects, which are not belong to the current sketch
568     // in order to do not delete results of other sketches
569     QObjectPtrList aSketchObjects;
570     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
571     for ( ; anIt != aLast; anIt++) {
572       ObjectPtr anObject = *anIt;
573       if (mySketchMgr->isObjectOfSketch(anObject)) {
574         // sketch feature should be used in this list because workshop deletes features only
575         // results are skipped
576         FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
577         aSketchObjects.append(aSketchFeature);
578       }
579     }
580     // if the selection contains only local selected presentations from other sketches,
581     // the Delete operation should not be done at all
582     if (aSketchObjects.size() == 0)
583       return true;
584
585     // the active nested sketch operation should be aborted unconditionally
586     if (isNestedOp)
587       anOperation->abort();
588
589     // 3. start operation
590     QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
591     aMgr->startOperation(aDescription.toStdString());
592
593     // 4. delete features
594     // sketch feature should be skipped, only sub-features can be removed
595     // when sketch operation is active
596     std::set<FeaturePtr> anIgnoredFeatures;
597     anIgnoredFeatures.insert(mySketchMgr->activeSketch());
598     aWorkshop->deleteFeatures(aSketchObjects, anIgnoredFeatures);
599   
600     // 5. stop operation
601     aWorkshop->displayer()->updateViewer();
602     aMgr->finishOperation();
603   } else {
604     bool isPartRemoved = false;
605     // Delete part with help of PartSet plugin
606     // TODO: the deleted objects has to be processed by multiselection
607     QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
608     if (aObjects.size() == 1) {
609       ObjectPtr aObj = aObjects.first();
610       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
611       if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
612         // Remove feature should be created in the document of the part results
613         ResultPtr aPartResult = aFeature->firstResult();
614         if (aPartResult.get()) {
615           std::shared_ptr<ModelAPI_ResultPart> aPart =
616                        std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartResult);
617           DocumentPtr aPartDoc = aPart->partDoc();
618           if (aPartDoc.get()) {
619             aMgr->startOperation(PartSetPlugin_Remove::ID());
620             FeaturePtr aFeature = aPartDoc->addFeature(PartSetPlugin_Remove::ID());
621             aFeature->execute();
622             aMgr->finishOperation();
623             isPartRemoved = true;
624           }
625         }
626       }
627     }
628     return isPartRemoved;
629   }
630   return true;
631 }
632
633 void PartSet_Module::onFeatureTriggered()
634 {
635   SessionPtr aMgr = ModelAPI_Session::get();
636   // 1. check whether the delete should be processed in the module
637   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
638   bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
639   if (isNestedOp) {
640     // in case if in the viewer nothing is displayed, the create operation should not be
641     // comitted even if all values of the feature are initialized
642     if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
643       QAction* aCmd = dynamic_cast<QAction*>(sender());
644       //Do nothing on uncheck
645       if (aCmd->isCheckable() && !aCmd->isChecked())
646         return;
647
648       // the action information should be saved before the operation is aborted
649       // because this abort leads to update command status, which unchecks this action
650       anOperation->abort();
651
652       launchOperation(aCmd->data().toString());
653     }
654   }
655   ModuleBase_IModule::onFeatureTriggered();
656 }
657
658 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS) 
659 {
660   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
661   if (!anAIS.IsNull()) {
662     Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
663     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
664     if (!aDim.IsNull()) {
665       aCtx->SetZLayer(aDim, myVisualLayerId);
666     } else {
667       Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
668       if (!aCons.IsNull())
669         aCtx->SetZLayer(aCons, myVisualLayerId);
670     }
671   }
672 }
673
674 void PartSet_Module::onViewTransformed(int theTrsfType)
675 {
676   // Set length of arrows constant in pixel size
677   // if the operation is panning or rotate or panglobal then do nothing
678   if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
679     return;
680
681   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
682   //Handle(V3d_View) aView = aViewer->activeView();
683
684   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
685   XGUI_Workshop* aWorkshop = aConnector->workshop();
686   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
687   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
688
689   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
690   Handle(V3d_View) aView;
691   double aScale = 0;
692   for (aV3dViewer->InitDefinedViews(); 
693        aV3dViewer->MoreDefinedViews(); 
694        aV3dViewer->NextDefinedViews()) {
695     Handle(V3d_View) aV = aV3dViewer->DefinedView();
696     double aS = aV->Scale();
697     if (aS > aScale) {
698       aScale = aS;
699       aView = aV;
700     }
701   }
702   if (aView.IsNull())
703     return;
704   double aLen = aView->Convert(20);
705
706   double aPrevLen = SketcherPrs_Tools::getArrowSize();
707   SketcherPrs_Tools::setArrowSize(aLen);
708   const double aPrevScale = aViewer->Scale(aViewer->activeView());
709   const double aCurScale = aViewer->activeView()->Camera()->Scale();
710   aViewer->SetScale(aViewer->activeView(), aCurScale);
711   SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
712   bool isModified = false;
713   QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
714   foreach (AISObjectPtr aAIS, aPrsList) {
715     Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
716
717     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
718     if (!aDim.IsNull()) {
719       aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
720       aContext->Redisplay(aDim, false);
721       isModified = true;
722     }
723   }
724   if (isModified)
725     aDisplayer->updateViewer();
726 }
727
728
729 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
730 {
731   XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
732   if (aOB) {
733     QLineEdit* aLabel = aOB->activeDocLabel();
734     QPalette aPalet = aLabel->palette();
735     aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
736     aLabel->setPalette(aPalet);
737     aOB->treeView()->setExpandsOnDoubleClick(false);
738     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)), 
739       SLOT(onTreeViewDoubleClick(const QModelIndex&)));
740     connect(aOB, SIGNAL(headerMouseDblClicked(const QModelIndex&)), 
741       SLOT(onTreeViewDoubleClick(const QModelIndex&)));
742     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)), 
743       myDataModel, SLOT(onMouseDoubleClick(const QModelIndex&)));
744   }
745 }
746
747
748 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
749 {
750   QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
751   int aSelected = aObjects.size();
752   SessionPtr aMgr = ModelAPI_Session::get();
753   if (aSelected == 1) {
754     bool hasResult = false;
755     bool hasFeature = false;
756     bool hasParameter = false;
757     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
758
759     ObjectPtr aObject = aObjects.first();
760     if (aObject) {
761       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
762       FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
763       bool isPart = aPart.get() || 
764         (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID()));
765       if (isPart) {
766         DocumentPtr aPartDoc;
767         if (!aPart.get()) {
768           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
769         }
770         if (aPart.get()) // this may be null is Part feature is disabled
771           aPartDoc = aPart->partDoc();
772         if (aMgr->activeDocument() == aPartDoc)
773           theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
774         else
775           theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
776       } else if (aObject->document() == aMgr->activeDocument()) {
777         if (hasParameter || hasFeature)
778           theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
779       }
780     } else {  // If feature is 0 the it means that selected root object (document)
781       if (aMgr->activeDocument() != aMgr->moduleDocument())
782         theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
783     }
784   } else if (aSelected == 0) {
785     // if there is no selection then it means that upper label is selected
786     QModelIndexList aIndexes = myWorkshop->selection()->selectedIndexes();
787     if (aIndexes.size() == 0) // it means that selection happens in top label outside of tree view
788       if (aMgr->activeDocument() != aMgr->moduleDocument())
789         theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
790   }
791 }
792
793 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
794 {
795   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
796     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
797     XGUI_Workshop* aWorkshop = aConnector->workshop();
798     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
799     QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
800     QPalette aPalet = aLabel->palette();
801
802     SessionPtr aMgr = ModelAPI_Session::get();
803     DocumentPtr aActiveDoc = aMgr->activeDocument();
804     DocumentPtr aDoc = aMgr->moduleDocument();
805     QModelIndex aOldIndex = myDataModel->activePartTree();
806     if (aActiveDoc == aDoc) {
807       if (aOldIndex.isValid())
808         aTreeView->setExpanded(aOldIndex, false);
809       myDataModel->deactivatePart();
810       aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
811     } else {
812       std::string aGrpName = ModelAPI_ResultPart::group();
813       for (int i = 0; i < aDoc->size(aGrpName); i++) {
814         ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
815         if (aPart->partDoc() == aActiveDoc) {
816           QModelIndex aIndex = myDataModel->partIndex(aPart);
817           if (myDataModel->activatePart(aIndex)) {
818             if (aOldIndex.isValid())
819               aTreeView->setExpanded(aOldIndex, false);
820             aTreeView->setExpanded(myDataModel->activePartTree(), true);
821             aPalet.setColor(QPalette::Text, Qt::black);
822           }
823           break;
824         }
825       }
826     }
827     aLabel->setPalette(aPalet);
828     aWorkshop->updateCommandStatus();
829
830     // Update displayed objects in order to update active color
831     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
832     QObjectPtrList aObjects = aDisplayer->displayedObjects();
833     foreach(ObjectPtr aObj, aObjects)
834       aDisplayer->redisplay(aObj, false);
835     aDisplayer->updateViewer();
836   }
837 }
838
839 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
840 {
841   SessionPtr aMgr = ModelAPI_Session::get();
842   if (!theIndex.isValid()) {
843     aMgr->setActiveDocument(aMgr->moduleDocument());
844     return;
845   }
846   if (theIndex.column() != 0) // Use only first column
847     return;
848   ObjectPtr aObj = myDataModel->object(theIndex);
849   ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
850   if (!aPart.get()) { // Probably this is Feature
851     FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
852     if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
853       aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
854     }
855   }
856   if (aPart.get()) { // if this is a part
857     if (aPart->partDoc() == aMgr->activeDocument()) {
858       aMgr->setActiveDocument(aMgr->moduleDocument());
859     } else {
860       aPart->activate();
861     }
862   }
863 }