]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetFeaturePointSelector.cpp
Salome HOME
Issue #2149 Split does not highlight the selected edge
[modules/shaper.git] / src / PartSet / PartSet_WidgetFeaturePointSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetFeaturePointSelector.cpp
4 // Created:     28 Feb 2017
5 // Author:      Natalia ERMOLAEVA
6
7 #include <Config_WidgetAPI.h>
8
9 #include <Events_Loop.h>
10
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomDataAPI_Point.h>
13 #include <GeomAPI_Edge.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAlgoAPI_ShapeTools.h>
16
17 #include <ModuleBase_ISelection.h>
18 #include <ModuleBase_ViewerPrs.h>
19
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Tools.h>
24
25 #include <ModuleBase_IViewWindow.h>
26 #include <ModuleBase_IWorkshop.h>
27 #include <ModuleBase_IModule.h>
28
29 #include "PartSet_WidgetFeaturePointSelector.h"
30 #include "PartSet_Tools.h"
31
32 #include <SketchPlugin_Point.h>
33
34 #include <XGUI_Tools.h>
35 #include <XGUI_Workshop.h>
36 #include <XGUI_Displayer.h>
37 #include <XGUI_ViewerProxy.h>
38
39 #include <QWidget>
40 #include <QMouseEvent>
41
42 #define HIGHLIGHT_STAYS_PROBLEM
43 #ifdef HIGHLIGHT_STAYS_PROBLEM
44 #include <Quantity_Color.hxx>
45 #define SKETCH_ENTITY_COLOR "225,0,0"
46 #endif
47
48 PartSet_WidgetFeaturePointSelector::PartSet_WidgetFeaturePointSelector(QWidget* theParent,
49                                                          ModuleBase_IWorkshop* theWorkshop,
50                                                          const Config_WidgetAPI* theData)
51 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
52 {
53   std::string anAttributes = theData->getProperty("selection_attributes");
54   QStringList anAttributesList = QString(anAttributes.c_str()).split(' ', QString::SkipEmptyParts);
55
56   mySelectedObjectAttribute = anAttributesList[0].toStdString();
57   mySelectedPointAttribute = anAttributesList[1].toStdString();
58   myPreviewObjectAttribute = anAttributesList[2].toStdString();
59   myPreviewPointAttribute = anAttributesList[3].toStdString();
60 }
61
62 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
63 {
64 }
65
66 //********************************************************************
67 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
68                                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
69 {
70   return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
71   //return true;
72 }
73
74 //********************************************************************
75 bool PartSet_WidgetFeaturePointSelector::activateSelectionAndFilters(bool toActivate)
76 {
77 #ifdef HIGHLIGHT_STAYS_PROBLEM
78   Handle(AIS_InteractiveContext) aContext =
79                             XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
80   Quantity_Color aColor;
81   Handle(Graphic3d_HighlightStyle) aHStyle = aContext->HighlightStyle();
82   Handle(Graphic3d_HighlightStyle) aSStyle = aContext->SelectionStyle();
83   if (toActivate) {
84     std::vector<int> aColors;
85     aColors = Config_PropManager::color("Visualization", "sketch_entity_color");
86     aColor = Quantity_Color(aColors[0] / 255., aColors[1] / 255., aColors[2] / 255.,
87                             Quantity_TOC_RGB);
88     myHighlightColor = aHStyle->Color();
89     mySelectionColor = aSStyle->Color();
90   }
91   else {
92     aColor = myHighlightColor;
93   }
94   aHStyle->SetColor(aColor);
95   aContext->SetHighlightStyle(aHStyle);
96
97   aSStyle->SetColor(aColor);
98   aContext->SetSelectionStyle(aSStyle);
99
100 #endif
101
102   return ModuleBase_WidgetShapeSelector::activateSelectionAndFilters(toActivate);
103 }
104
105 //********************************************************************
106 void PartSet_WidgetFeaturePointSelector::activateCustom()
107 {
108   ModuleBase_WidgetShapeSelector::activateCustom();
109
110   myWorkshop->module()->activateCustomPrs(myFeature,
111                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
112 }
113
114 //********************************************************************
115 void PartSet_WidgetFeaturePointSelector::deactivate()
116 {
117   ModuleBase_WidgetShapeSelector::deactivate();
118 }
119
120 //********************************************************************
121 void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
122                                                     QMouseEvent* theEvent)
123 {
124   ModuleBase_ISelection* aSelect = myWorkshop->selection();
125   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
126
127   ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
128                                                        : ModuleBase_ViewerPrsPtr();
129   fillFeature(aPrs, theWindow, theEvent);
130 }
131
132 //********************************************************************
133 void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* theWindow,
134                                                        QMouseEvent* theEvent)
135 {
136   // the contex menu release by the right button should not be processed by this widget
137   if (theEvent->button() != Qt::LeftButton)
138     return;
139
140   std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
141                           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
142                           feature()->data()->attribute(myPreviewObjectAttribute));
143   ObjectPtr aPreviewObject = aRefPreviewAttr->value();
144   // do not move focus from the current widget if the object is not highlighted/selected
145   if (!aPreviewObject.get())
146     return;
147
148   // set parameters of preview into parameters of selection in the feature
149   std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
150                           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
151                           feature()->data()->attribute(mySelectedObjectAttribute));
152   aRefSelectedAttr->setValue(aRefPreviewAttr->value());
153
154   std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
155                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
156                           feature()->data()->attribute(mySelectedPointAttribute));
157   std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
158                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
159                           feature()->data()->attribute(myPreviewPointAttribute));
160   aPointSelectedAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
161
162   updateObject(feature());
163
164   emit focusOutWidget(this);
165   // we need to deselect base feature for better visibility of selected feature
166   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
167 }
168
169 //********************************************************************
170 bool PartSet_WidgetFeaturePointSelector::fillFeature(
171                             const std::shared_ptr<ModuleBase_ViewerPrs>& theSelectedPrs,
172                             ModuleBase_IViewWindow* theWindow,
173                             QMouseEvent* theEvent)
174 {
175   bool aFilled = false;
176   ObjectPtr anObject;
177   if (theSelectedPrs.get() && theSelectedPrs->object().get())
178     anObject = theSelectedPrs->object();
179
180   std::shared_ptr<ModelAPI_AttributeReference> aRef =
181                           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
182                           feature()->data()->attribute(myPreviewObjectAttribute));
183   aRef->setValue(anObject);
184
185   std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
186                   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
187                   feature()->data()->attribute(myPreviewPointAttribute));
188   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
189   anAttributePoint->setValue(aPoint);
190   // redisplay AIS presentation in viewer
191 #ifndef HIGHLIGHT_STAYS_PROBLEM
192   // an attempt to clear highlighted item in the viewer: but of OCCT
193   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
194 #endif
195   updateObject(feature());
196   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
197
198   aFilled = true;
199
200   return aFilled;
201 }
202
203 //********************************************************************
204 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeSelection() const
205 {
206   return QList<ModuleBase_ViewerPrsPtr>();
207 }
208
209 //********************************************************************
210 bool PartSet_WidgetFeaturePointSelector::setSelection(
211                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
212                                           const bool theToValidate)
213 {
214   // false is returned to do not emit focus out widget by selected sub-shape
215   return false;
216 }
217
218 void PartSet_WidgetFeaturePointSelector::setPreSelection(
219                                   const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
220                                   ModuleBase_IViewWindow* theWnd,
221                                   QMouseEvent* theEvent)
222 {
223   // the method is empty because firstly by starging of the feature there is no selection of
224   // sub-segments in the viewer, secondly preselection of restart operation is processed by
225   // special reentrant message sent by the feature
226 }