]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetFeaturePointSelector.cpp
Salome HOME
Issue #2552: Intersection edition is impossible
[modules/shaper.git] / src / PartSet / PartSet_WidgetFeaturePointSelector.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Config_WidgetAPI.h>
22
23 #include <Events_Loop.h>
24
25 #include <GeomDataAPI_Point2D.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Pnt2d.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30
31 #include <ModuleBase_ISelection.h>
32 #include <ModuleBase_ViewerPrs.h>
33
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   mySelectedObjectAttribute = anAttributesList[0].toStdString();
71   mySelectedPointAttribute = anAttributesList[1].toStdString();
72   myPreviewObjectAttribute = anAttributesList[2].toStdString();
73   myPreviewPointAttribute = anAttributesList[3].toStdString();
74 }
75
76 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
77 {
78 }
79
80 //********************************************************************
81 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
82                                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
83 {
84   return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
85   //return true;
86 }
87
88 //********************************************************************
89 void PartSet_WidgetFeaturePointSelector::updateSelectionModesAndFilters(bool toActivate)
90 {
91 #ifdef HIGHLIGHT_STAYS_PROBLEM
92   Handle(AIS_InteractiveContext) aContext =
93                             XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
94   Quantity_Color aColor;
95   Handle(Prs3d_Drawer) aHStyle = aContext->HighlightStyle();
96   Handle(Prs3d_Drawer) aSStyle = aContext->SelectionStyle();
97   if (toActivate) {
98     std::vector<int> aColors;
99     aColors = Config_PropManager::color("Visualization", "sketch_entity_color");
100     aColor = Quantity_Color(aColors[0] / 255., aColors[1] / 255., aColors[2] / 255.,
101                             Quantity_TOC_RGB);
102     myHighlightColor = aHStyle->Color();
103     mySelectionColor = aSStyle->Color();
104   }
105   else {
106     aColor = myHighlightColor;
107   }
108   aHStyle->SetColor(aColor);
109   aContext->SetHighlightStyle(aHStyle);
110
111   aSStyle->SetColor(aColor);
112   aContext->SetSelectionStyle(aSStyle);
113
114 #endif
115
116   ModuleBase_WidgetShapeSelector::updateSelectionModesAndFilters(toActivate);
117 }
118
119 //********************************************************************
120 void PartSet_WidgetFeaturePointSelector::activateCustom()
121 {
122   ModuleBase_WidgetShapeSelector::activateCustom();
123
124   myWorkshop->module()->activateCustomPrs(myFeature,
125                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
126 }
127
128 //********************************************************************
129 void PartSet_WidgetFeaturePointSelector::deactivate()
130 {
131   ModuleBase_WidgetShapeSelector::deactivate();
132 }
133
134 //********************************************************************
135 void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
136                                                     QMouseEvent* theEvent)
137 {
138   ModuleBase_ISelection* aSelect = myWorkshop->selection();
139   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
140
141   ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
142                                                        : ModuleBase_ViewerPrsPtr();
143   fillFeature(aPrs, theWindow, theEvent);
144 }
145
146 //********************************************************************
147 void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* theWindow,
148                                                        QMouseEvent* theEvent)
149 {
150   // the contex menu release by the right button should not be processed by this widget
151   if (theEvent->button() != Qt::LeftButton)
152     return;
153
154   std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
155                           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
156                           feature()->data()->attribute(myPreviewObjectAttribute));
157   ObjectPtr aPreviewObject = aRefPreviewAttr->value();
158   // do not move focus from the current widget if the object is not highlighted/selected
159   if (!aPreviewObject.get())
160     return;
161
162   // set parameters of preview into parameters of selection in the feature
163   std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
164                           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
165                           feature()->data()->attribute(mySelectedObjectAttribute));
166   aRefSelectedAttr->setValue(aRefPreviewAttr->value());
167
168   std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
169                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
170                           feature()->data()->attribute(mySelectedPointAttribute));
171   std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
172                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
173                           feature()->data()->attribute(myPreviewPointAttribute));
174   aPointSelectedAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
175
176   updateObject(feature());
177
178   // we need to deselect base feature for better visibility of selected feature
179   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
180
181   // focusOutWidget should be the last functionality in the method because after this emit,
182   // the widget may be deleted and members of this class are deleted (e.g. myWorkshop)
183   emit focusOutWidget(this);
184 }
185
186 //********************************************************************
187 bool PartSet_WidgetFeaturePointSelector::fillFeature(
188                             const std::shared_ptr<ModuleBase_ViewerPrs>& theSelectedPrs,
189                             ModuleBase_IViewWindow* theWindow,
190                             QMouseEvent* theEvent)
191 {
192   bool aFilled = false;
193   ObjectPtr anObject;
194   if (theSelectedPrs.get() && theSelectedPrs->object().get())
195     anObject = theSelectedPrs->object();
196
197   std::shared_ptr<ModelAPI_AttributeReference> aRef =
198                           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
199                           feature()->data()->attribute(myPreviewObjectAttribute));
200   aRef->setValue(anObject);
201
202   std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
203                   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204                   feature()->data()->attribute(myPreviewPointAttribute));
205   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
206   anAttributePoint->setValue(aPoint);
207   // redisplay AIS presentation in viewer
208 #ifndef HIGHLIGHT_STAYS_PROBLEM
209   // an attempt to clear highlighted item in the viewer: but of OCCT
210   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
211 #endif
212   updateObject(feature());
213   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
214
215   aFilled = true;
216
217   return aFilled;
218 }
219
220 //********************************************************************
221 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeSelection() const
222 {
223   return QList<ModuleBase_ViewerPrsPtr>();
224 }
225
226 //********************************************************************
227 bool PartSet_WidgetFeaturePointSelector::setSelection(
228                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
229                                           const bool theToValidate)
230 {
231   // false is returned to do not emit focus out widget by selected sub-shape
232   return false;
233 }
234
235 void PartSet_WidgetFeaturePointSelector::setPreSelection(
236                                   const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
237                                   ModuleBase_IViewWindow* theWnd,
238                                   QMouseEvent* theEvent)
239 {
240   // the method is empty because firstly by starging of the feature there is no selection of
241   // sub-segments in the viewer, secondly preselection of restart operation is processed by
242   // special reentrant message sent by the feature
243 }