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