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