]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetFeaturePointSelector.cpp
Salome HOME
Issue #18429: Provide selection of external objects for angle constraint.
[modules/shaper.git] / src / PartSet / PartSet_WidgetFeaturePointSelector.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_WidgetFeaturePointSelector.h"
21 #include "PartSet_Tools.h"
22 #include "PartSet_ExternalObjectsMgr.h"
23
24 #include <Config_WidgetAPI.h>
25
26 #include <Events_Loop.h>
27
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33
34 #include <ModuleBase_ISelection.h>
35 #include <ModuleBase_ViewerPrs.h>
36
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_AttributeReference.h>
39 #include <ModelAPI_Events.h>
40 #include <ModelAPI_Feature.h>
41 #include <ModelAPI_Tools.h>
42
43 #include <ModuleBase_IViewWindow.h>
44 #include <ModuleBase_IWorkshop.h>
45 #include <ModuleBase_IModule.h>
46
47 #include <SketchPlugin_Point.h>
48
49 #include <XGUI_Tools.h>
50 #include <XGUI_Workshop.h>
51 #include <XGUI_Displayer.h>
52 #include <XGUI_ViewerProxy.h>
53
54 #include <QWidget>
55 #include <QMouseEvent>
56
57 #define HIGHLIGHT_STAYS_PROBLEM
58 #ifdef HIGHLIGHT_STAYS_PROBLEM
59 #include <Quantity_Color.hxx>
60 #define SKETCH_ENTITY_COLOR "225,0,0"
61 #endif
62
63 PartSet_WidgetFeaturePointSelector::PartSet_WidgetFeaturePointSelector(QWidget* theParent,
64                                                          ModuleBase_IWorkshop* theWorkshop,
65                                                          const Config_WidgetAPI* theData)
66 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
67 {
68   std::string anAttributes = theData->getProperty("selection_attributes");
69   QStringList anAttributesList = QString(anAttributes.c_str()).split(' ', QString::SkipEmptyParts);
70
71   myHasPreview = anAttributesList.size() >= 4;
72
73   mySelectedObjectAttribute = anAttributesList[0].toStdString();
74   mySelectedPointAttribute = anAttributesList[1].toStdString();
75   if (myHasPreview) {
76     myPreviewObjectAttribute = anAttributesList[2].toStdString();
77     myPreviewPointAttribute = anAttributesList[3].toStdString();
78   }
79   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
80     theData->getProperty("can_create_external"), true);
81 }
82
83 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
84 {
85   delete myExternalObjectMgr;
86 }
87
88 //********************************************************************
89 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
90                                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
91 {
92   return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
93   //return true;
94 }
95
96 //********************************************************************
97 void PartSet_WidgetFeaturePointSelector::updateSelectionModesAndFilters(bool toActivate)
98 {
99 #ifdef HIGHLIGHT_STAYS_PROBLEM
100   Handle(AIS_InteractiveContext) aContext =
101                             XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
102   Quantity_Color aColor;
103   Handle(Prs3d_Drawer) aHStyle = aContext->HighlightStyle();
104   Handle(Prs3d_Drawer) aSStyle = aContext->SelectionStyle();
105   if (toActivate) {
106     std::vector<int> aColors;
107     aColors = Config_PropManager::color("Visualization", "sketch_entity_color");
108     aColor = Quantity_Color(aColors[0] / 255., aColors[1] / 255., aColors[2] / 255.,
109                             Quantity_TOC_RGB);
110     myHighlightColor = aHStyle->Color();
111     mySelectionColor = aSStyle->Color();
112   }
113   else {
114     aColor = myHighlightColor;
115   }
116   aHStyle->SetColor(aColor);
117   aContext->SetHighlightStyle(aHStyle);
118
119   aSStyle->SetColor(aColor);
120   aContext->SetSelectionStyle(aSStyle);
121
122 #endif
123
124   ModuleBase_WidgetShapeSelector::updateSelectionModesAndFilters(toActivate);
125 }
126
127 //********************************************************************
128 void PartSet_WidgetFeaturePointSelector::activateCustom()
129 {
130   ModuleBase_WidgetShapeSelector::activateCustom();
131
132   myWorkshop->module()->activateCustomPrs(myFeature,
133                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
134 }
135
136 //********************************************************************
137 void PartSet_WidgetFeaturePointSelector::deactivate()
138 {
139   ModuleBase_WidgetShapeSelector::deactivate();
140 }
141
142 //********************************************************************
143 void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
144                                                     QMouseEvent* theEvent)
145 {
146   ModuleBase_ISelection* aSelect = myWorkshop->selection();
147   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
148
149   ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
150                                                        : ModuleBase_ViewerPrsPtr();
151   fillFeature(aPrs, theWindow, theEvent);
152 }
153
154 //********************************************************************
155 void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* theWindow,
156                                                        QMouseEvent* theEvent)
157 {
158   // the contex menu release by the right button should not be processed by this widget
159   if (theEvent->button() != Qt::LeftButton)
160     return;
161
162   ModuleBase_ISelection* aSelection = myWorkshop->selection();
163   QList<ModuleBase_ViewerPrsPtr> aSelected = aSelection->getSelected(ModuleBase_ISelection::Viewer);
164
165   ModuleBase_ViewerPrsPtr aPrs =
166     !aSelected.empty() ? aSelected.first() : ModuleBase_ViewerPrsPtr();
167   if (aPrs.get() && aPrs->object().get()) {
168     myPreviewObject = aSelection->getResult(aPrs);
169     GeomShapePtr aShape = aSelection->getShape(aPrs);
170     myExternalObjectMgr->getGeomSelection(aPrs, myPreviewObject, aShape,
171       myWorkshop, sketch(), true);
172   }
173   myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
174
175   ObjectPtr aPreviewObject;
176   GeomPnt2dPtr aPreviewPoint;
177   if (myHasPreview) {
178     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
179                             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
180                             feature()->data()->attribute(myPreviewObjectAttribute));
181     aPreviewObject = aRefPreviewAttr->value();
182
183     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
184                             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185                             feature()->data()->attribute(myPreviewPointAttribute));
186     aPreviewPoint = aPointPreviewAttr->pnt();
187   }
188   else {
189     aPreviewObject = myPreviewObject;
190     aPreviewPoint = myPreviewPoint;
191   }
192
193   // do not move focus from the current widget if the object is not highlighted/selected
194   if (!aPreviewObject.get())
195     return;
196
197   // set parameters of preview into parameters of selection in the feature
198   std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
199                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200                           feature()->data()->attribute(mySelectedPointAttribute));
201   aPointSelectedAttr->setValue(aPreviewPoint);
202
203   AttributeReferencePtr aRefSelectedAttr = feature()->reference(mySelectedObjectAttribute);
204   if (aRefSelectedAttr)
205     aRefSelectedAttr->setValue(aPreviewObject);
206   else {
207     AttributeRefAttrPtr aRefAttrSelectedAttr = feature()->refattr(mySelectedObjectAttribute);
208     if (aRefAttrSelectedAttr)
209       aRefAttrSelectedAttr->setObject(aPreviewObject);
210   }
211
212   updateObject(feature());
213
214   // we need to deselect base feature for better visibility of selected feature
215   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
216
217   // focusOutWidget should be the last functionality in the method because after this emit,
218   // the widget may be deleted and members of this class are deleted (e.g. myWorkshop)
219   emit focusOutWidget(this);
220 }
221
222 //********************************************************************
223 bool PartSet_WidgetFeaturePointSelector::fillFeature(
224                             const std::shared_ptr<ModuleBase_ViewerPrs>& theSelectedPrs,
225                             ModuleBase_IViewWindow* theWindow,
226                             QMouseEvent* theEvent)
227 {
228   bool aFilled = false;
229   if (theSelectedPrs.get() && theSelectedPrs->object().get())
230     myPreviewObject = theSelectedPrs->object();
231   myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
232
233   if (myHasPreview) {
234     std::shared_ptr<ModelAPI_AttributeReference> aRef =
235                             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
236                             feature()->data()->attribute(myPreviewObjectAttribute));
237     aRef->setValue(myPreviewObject);
238
239     std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
240                     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
241                     feature()->data()->attribute(myPreviewPointAttribute));
242     anAttributePoint->setValue(myPreviewPoint);
243   }
244   // redisplay AIS presentation in viewer
245 #ifndef HIGHLIGHT_STAYS_PROBLEM
246   // an attempt to clear highlighted item in the viewer: but of OCCT
247   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
248 #endif
249   updateObject(feature());
250   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
251
252   aFilled = true;
253
254   return aFilled;
255 }
256
257 //********************************************************************
258 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeSelection() const
259 {
260   return QList<ModuleBase_ViewerPrsPtr>();
261 }
262
263 //********************************************************************
264 bool PartSet_WidgetFeaturePointSelector::setSelection(
265                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
266                                           const bool theToValidate)
267 {
268   // false is returned to do not emit focus out widget by selected sub-shape
269   return false;
270 }
271
272 void PartSet_WidgetFeaturePointSelector::setPreSelection(
273                                   const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
274                                   ModuleBase_IViewWindow* theWnd,
275                                   QMouseEvent* theEvent)
276 {
277   // the method is empty because firstly by starging of the feature there is no selection of
278   // sub-segments in the viewer, secondly preselection of restart operation is processed by
279   // special reentrant message sent by the feature
280 }
281
282 //********************************************************************
283 void PartSet_WidgetFeaturePointSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
284   ObjectPtr& theObject, GeomShapePtr& theShape)
285 {
286   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
287
288   myExternalObjectMgr->getGeomSelection(thePrs, theObject, theShape,
289     myWorkshop, sketch(), myIsInValidate);
290   myPreviewObject = theObject;
291 }
292
293 //********************************************************************
294 void PartSet_WidgetFeaturePointSelector::restoreAttributeValue(const AttributePtr& theAttribute,
295   const bool theValid)
296 {
297   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
298   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
299   myPreviewObject = ObjectPtr();
300 }