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