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