Salome HOME
updated copyright message
[modules/shaper.git] / src / PartSet / PartSet_WidgetFeaturePointSelector.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 =
70     QString(anAttributes.c_str()).split(' ', QString::SkipEmptyParts);
71
72   myHasPreview = anAttributesList.size() >= 4;
73
74   mySelectedObjectAttribute = anAttributesList[0].toStdString();
75   mySelectedPointAttribute = anAttributesList[1].toStdString();
76   if (myHasPreview) {
77     myPreviewObjectAttribute = anAttributesList[2].toStdString();
78     myPreviewPointAttribute = anAttributesList[3].toStdString();
79   }
80   myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
81     theData->getProperty("can_create_external"), true);
82 }
83
84 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
85 {
86   delete myExternalObjectMgr;
87 }
88
89 //********************************************************************
90 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
91                                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
92 {
93   return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
94   //return true;
95 }
96
97 //********************************************************************
98 void PartSet_WidgetFeaturePointSelector::activateCustom()
99 {
100   ModuleBase_WidgetShapeSelector::activateCustom();
101
102   myWorkshop->module()->activateCustomPrs(myFeature,
103                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
104 }
105
106 //********************************************************************
107 void PartSet_WidgetFeaturePointSelector::deactivate()
108 {
109   ModuleBase_WidgetShapeSelector::deactivate();
110 }
111
112 //********************************************************************
113 void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
114                                                     QMouseEvent* theEvent)
115 {
116   ModuleBase_ISelection* aSelect = myWorkshop->selection();
117   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
118
119   ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
120                                                        : ModuleBase_ViewerPrsPtr();
121   myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
122   if (myHasPreview) {
123     if (aPrs.get() && aPrs->object().get())
124       myPreviewObject = aPrs->object();
125     else
126       myPreviewObject = ObjectPtr();
127     fillFeature();
128     updateObject(feature());
129     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
130   }
131 }
132
133 //********************************************************************
134 void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* theWindow,
135                                                        QMouseEvent* theEvent)
136 {
137   // the contex menu release by the right button should not be processed by this widget
138   if (theEvent->button() != Qt::LeftButton)
139     return;
140
141   ModuleBase_ISelection* aSelection = myWorkshop->selection();
142   QList<ModuleBase_ViewerPrsPtr> aSelected =
143     aSelection->getSelected(ModuleBase_ISelection::Viewer);
144
145   ModuleBase_ViewerPrsPtr aPrs =
146     !aSelected.empty() ? aSelected.first() : ModuleBase_ViewerPrsPtr();
147   if (aPrs.get() && aPrs->object().get()) {
148     myPreviewObject = aSelection->getResult(aPrs);
149     GeomShapePtr aShape = aSelection->getShape(aPrs);
150     myExternalObjectMgr->getGeomSelection(aPrs, myPreviewObject, aShape,
151       myWorkshop, sketch(), true);
152   }
153   myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
154
155   ObjectPtr aPreviewObject;
156   GeomPnt2dPtr aPreviewPoint;
157   if (myHasPreview) {
158     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
159                             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
160                             feature()->data()->attribute(myPreviewObjectAttribute));
161     aPreviewObject = aRefPreviewAttr->value();
162
163     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
164                             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165                             feature()->data()->attribute(myPreviewPointAttribute));
166     aPreviewPoint = aPointPreviewAttr->pnt();
167   }
168   else {
169     aPreviewObject = myPreviewObject;
170     aPreviewPoint = myPreviewPoint;
171   }
172
173   // do not move focus from the current widget if the object is not highlighted/selected
174   if (!aPreviewObject.get())
175     return;
176
177   // Do not use non-sketcher objects
178   if (!sketch()->isSub(aPreviewObject))
179     return;
180
181   // set parameters of preview into parameters of selection in the feature
182   std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
183                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184                           feature()->data()->attribute(mySelectedPointAttribute));
185   aPointSelectedAttr->setValue(aPreviewPoint);
186
187   AttributeReferencePtr aRefSelectedAttr = feature()->reference(mySelectedObjectAttribute);
188   if (aRefSelectedAttr)
189     aRefSelectedAttr->setValue(aPreviewObject);
190   else {
191     AttributeRefAttrPtr aRefAttrSelectedAttr = feature()->refattr(mySelectedObjectAttribute);
192     if (aRefAttrSelectedAttr)
193       aRefAttrSelectedAttr->setObject(aPreviewObject);
194   }
195
196   updateObject(feature());
197
198   // we need to deselect base feature for better visibility of selected feature
199   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
200
201   // focusOutWidget should be the last functionality in the method because after this emit,
202   // the widget may be deleted and members of this class are deleted (e.g. myWorkshop)
203   emit focusOutWidget(this);
204 }
205
206 //********************************************************************
207 bool PartSet_WidgetFeaturePointSelector::fillFeature()
208 {
209   if (myHasPreview) {
210     std::shared_ptr<ModelAPI_AttributeReference> aRef =
211                             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
212                             feature()->data()->attribute(myPreviewObjectAttribute));
213     aRef->setValue(myPreviewObject);
214     if (myPreviewPoint.get()) {
215       std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
216         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217           feature()->data()->attribute(myPreviewPointAttribute));
218       anAttributePoint->setValue(myPreviewPoint);
219     }
220   }
221   else {
222     // Do not use non-sketcher objects
223     if (!sketch()->isSub(myPreviewObject))
224       return false;
225
226     // set parameters of preview into parameters of selection in the feature
227     if (myPreviewPoint.get()) {
228       std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
229         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
230           feature()->data()->attribute(mySelectedPointAttribute));
231       aPointSelectedAttr->setValue(myPreviewPoint);
232     }
233     AttributeReferencePtr aRefSelectedAttr = feature()->reference(mySelectedObjectAttribute);
234     if (aRefSelectedAttr)
235       aRefSelectedAttr->setValue(myPreviewObject);
236     else {
237       AttributeRefAttrPtr aRefAttrSelectedAttr = feature()->refattr(mySelectedObjectAttribute);
238       if (aRefAttrSelectedAttr)
239         aRefAttrSelectedAttr->setObject(myPreviewObject);
240     }
241   }
242   // redisplay AIS presentation in viewer
243 #ifndef HIGHLIGHT_STAYS_PROBLEM
244   // an attempt to clear highlighted item in the viewer: but of OCCT
245   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
246 #endif
247   return true;
248 }
249
250 //********************************************************************
251 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeSelection() const
252 {
253   return QList<ModuleBase_ViewerPrsPtr>();
254 }
255
256 //********************************************************************
257 bool PartSet_WidgetFeaturePointSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
258 {
259   if (!thePrs.get() || !thePrs->object().get())
260     return false;
261
262   ModuleBase_ISelection* aSelection = myWorkshop->selection();
263   myPreviewObject = aSelection->getResult(thePrs);
264   GeomShapePtr aShape = aSelection->getShape(thePrs);
265   myExternalObjectMgr->getGeomSelection(thePrs, myPreviewObject, aShape, myWorkshop,
266     sketch(), true);
267   return fillFeature();
268 }
269
270 //********************************************************************
271 void PartSet_WidgetFeaturePointSelector::setPreSelection(
272                                   const ModuleBase_ViewerPrsPtr& thePreSelected,
273                                   ModuleBase_IViewWindow* theWnd,
274                                   QMouseEvent* theEvent)
275 {
276   // the method is empty because firstly by starging of the feature there is no selection of
277   // sub-segments in the viewer, secondly preselection of restart operation is processed by
278   // special reentrant message sent by the feature
279 }
280
281 //********************************************************************
282 void PartSet_WidgetFeaturePointSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
283   ObjectPtr& theObject, GeomShapePtr& theShape)
284 {
285   ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
286
287   myExternalObjectMgr->getGeomSelection(thePrs, theObject, theShape,
288     myWorkshop, sketch(), myIsInValidate);
289   myPreviewObject = theObject;
290 }
291
292 //********************************************************************
293 void PartSet_WidgetFeaturePointSelector::restoreAttributeValue(const AttributePtr& theAttribute,
294   const bool theValid)
295 {
296   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
297   myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
298   myPreviewObject = ObjectPtr();
299 }