]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetFeaturePointSelector.cpp
Salome HOME
Task 5.1.3 Sketcher: angle dimension (issue #3061)
[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 <Config_WidgetAPI.h>
21
22 #include <Events_Loop.h>
23
24 #include <GeomDataAPI_Point2D.h>
25 #include <GeomDataAPI_Point.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Pnt2d.h>
28 #include <GeomAlgoAPI_ShapeTools.h>
29
30 #include <ModuleBase_ISelection.h>
31 #include <ModuleBase_ViewerPrs.h>
32
33 #include <ModelAPI_AttributeRefAttr.h>
34 #include <ModelAPI_AttributeReference.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_Feature.h>
37 #include <ModelAPI_Tools.h>
38
39 #include <ModuleBase_IViewWindow.h>
40 #include <ModuleBase_IWorkshop.h>
41 #include <ModuleBase_IModule.h>
42
43 #include "PartSet_WidgetFeaturePointSelector.h"
44 #include "PartSet_Tools.h"
45
46 #include <SketchPlugin_Point.h>
47
48 #include <XGUI_Tools.h>
49 #include <XGUI_Workshop.h>
50 #include <XGUI_Displayer.h>
51 #include <XGUI_ViewerProxy.h>
52
53 #include <QWidget>
54 #include <QMouseEvent>
55
56 #define HIGHLIGHT_STAYS_PROBLEM
57 #ifdef HIGHLIGHT_STAYS_PROBLEM
58 #include <Quantity_Color.hxx>
59 #define SKETCH_ENTITY_COLOR "225,0,0"
60 #endif
61
62 PartSet_WidgetFeaturePointSelector::PartSet_WidgetFeaturePointSelector(QWidget* theParent,
63                                                          ModuleBase_IWorkshop* theWorkshop,
64                                                          const Config_WidgetAPI* theData)
65 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
66 {
67   std::string anAttributes = theData->getProperty("selection_attributes");
68   QStringList anAttributesList = QString(anAttributes.c_str()).split(' ', QString::SkipEmptyParts);
69
70   myHasPreview = anAttributesList.size() >= 4;
71
72   mySelectedObjectAttribute = anAttributesList[0].toStdString();
73   mySelectedPointAttribute = anAttributesList[1].toStdString();
74   if (myHasPreview) {
75     myPreviewObjectAttribute = anAttributesList[2].toStdString();
76     myPreviewPointAttribute = anAttributesList[3].toStdString();
77   }
78 }
79
80 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
81 {
82 }
83
84 //********************************************************************
85 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
86                                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
87 {
88   return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
89   //return true;
90 }
91
92 //********************************************************************
93 void PartSet_WidgetFeaturePointSelector::updateSelectionModesAndFilters(bool toActivate)
94 {
95 #ifdef HIGHLIGHT_STAYS_PROBLEM
96   Handle(AIS_InteractiveContext) aContext =
97                             XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
98   Quantity_Color aColor;
99   Handle(Prs3d_Drawer) aHStyle = aContext->HighlightStyle();
100   Handle(Prs3d_Drawer) aSStyle = aContext->SelectionStyle();
101   if (toActivate) {
102     std::vector<int> aColors;
103     aColors = Config_PropManager::color("Visualization", "sketch_entity_color");
104     aColor = Quantity_Color(aColors[0] / 255., aColors[1] / 255., aColors[2] / 255.,
105                             Quantity_TOC_RGB);
106     myHighlightColor = aHStyle->Color();
107     mySelectionColor = aSStyle->Color();
108   }
109   else {
110     aColor = myHighlightColor;
111   }
112   aHStyle->SetColor(aColor);
113   aContext->SetHighlightStyle(aHStyle);
114
115   aSStyle->SetColor(aColor);
116   aContext->SetSelectionStyle(aSStyle);
117
118 #endif
119
120   ModuleBase_WidgetShapeSelector::updateSelectionModesAndFilters(toActivate);
121 }
122
123 //********************************************************************
124 void PartSet_WidgetFeaturePointSelector::activateCustom()
125 {
126   ModuleBase_WidgetShapeSelector::activateCustom();
127
128   myWorkshop->module()->activateCustomPrs(myFeature,
129                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
130 }
131
132 //********************************************************************
133 void PartSet_WidgetFeaturePointSelector::deactivate()
134 {
135   ModuleBase_WidgetShapeSelector::deactivate();
136 }
137
138 //********************************************************************
139 void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
140                                                     QMouseEvent* theEvent)
141 {
142   ModuleBase_ISelection* aSelect = myWorkshop->selection();
143   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
144
145   ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
146                                                        : ModuleBase_ViewerPrsPtr();
147   fillFeature(aPrs, theWindow, theEvent);
148 }
149
150 //********************************************************************
151 void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* theWindow,
152                                                        QMouseEvent* theEvent)
153 {
154   // the contex menu release by the right button should not be processed by this widget
155   if (theEvent->button() != Qt::LeftButton)
156     return;
157
158   ObjectPtr aPreviewObject;
159   GeomPnt2dPtr aPreviewPoint;
160   if (myHasPreview) {
161     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
162                             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
163                             feature()->data()->attribute(myPreviewObjectAttribute));
164     aPreviewObject = aRefPreviewAttr->value();
165
166     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
167                             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
168                             feature()->data()->attribute(myPreviewPointAttribute));
169     aPreviewPoint = aPointPreviewAttr->pnt();
170   }
171   else {
172     aPreviewObject = myPreviewObject;
173     aPreviewPoint = myPreviewPoint;
174   }
175
176   // do not move focus from the current widget if the object is not highlighted/selected
177   if (!aPreviewObject.get())
178     return;
179
180   // set parameters of preview into parameters of selection in the feature
181   std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
182                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
183                           feature()->data()->attribute(mySelectedPointAttribute));
184   aPointSelectedAttr->setValue(aPreviewPoint);
185
186   AttributeReferencePtr aRefSelectedAttr = feature()->reference(mySelectedObjectAttribute);
187   if (aRefSelectedAttr)
188     aRefSelectedAttr->setValue(aPreviewObject);
189   else {
190     AttributeRefAttrPtr aRefAttrSelectedAttr = feature()->refattr(mySelectedObjectAttribute);
191     if (aRefAttrSelectedAttr)
192       aRefAttrSelectedAttr->setObject(aPreviewObject);
193   }
194
195   updateObject(feature());
196
197   // we need to deselect base feature for better visibility of selected feature
198   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
199
200   // focusOutWidget should be the last functionality in the method because after this emit,
201   // the widget may be deleted and members of this class are deleted (e.g. myWorkshop)
202   emit focusOutWidget(this);
203 }
204
205 //********************************************************************
206 bool PartSet_WidgetFeaturePointSelector::fillFeature(
207                             const std::shared_ptr<ModuleBase_ViewerPrs>& theSelectedPrs,
208                             ModuleBase_IViewWindow* theWindow,
209                             QMouseEvent* theEvent)
210 {
211   bool aFilled = false;
212   if (theSelectedPrs.get() && theSelectedPrs->object().get())
213     myPreviewObject = theSelectedPrs->object();
214   myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
215
216   if (myHasPreview) {
217     std::shared_ptr<ModelAPI_AttributeReference> aRef =
218                             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
219                             feature()->data()->attribute(myPreviewObjectAttribute));
220     aRef->setValue(myPreviewObject);
221
222     std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
223                     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
224                     feature()->data()->attribute(myPreviewPointAttribute));
225     anAttributePoint->setValue(myPreviewPoint);
226   }
227   // redisplay AIS presentation in viewer
228 #ifndef HIGHLIGHT_STAYS_PROBLEM
229   // an attempt to clear highlighted item in the viewer: but of OCCT
230   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
231 #endif
232   updateObject(feature());
233   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
234
235   aFilled = true;
236
237   return aFilled;
238 }
239
240 //********************************************************************
241 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeSelection() const
242 {
243   return QList<ModuleBase_ViewerPrsPtr>();
244 }
245
246 //********************************************************************
247 bool PartSet_WidgetFeaturePointSelector::setSelection(
248                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
249                                           const bool theToValidate)
250 {
251   // false is returned to do not emit focus out widget by selected sub-shape
252   return false;
253 }
254
255 void PartSet_WidgetFeaturePointSelector::setPreSelection(
256                                   const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
257                                   ModuleBase_IViewWindow* theWnd,
258                                   QMouseEvent* theEvent)
259 {
260   // the method is empty because firstly by starging of the feature there is no selection of
261   // sub-segments in the viewer, secondly preselection of restart operation is processed by
262   // special reentrant message sent by the feature
263 }