1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Config_WidgetAPI.h>
22 #include <Events_Loop.h>
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>
30 #include <ModuleBase_ISelection.h>
31 #include <ModuleBase_ViewerPrs.h>
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>
39 #include <ModuleBase_IViewWindow.h>
40 #include <ModuleBase_IWorkshop.h>
41 #include <ModuleBase_IModule.h>
43 #include "PartSet_WidgetFeaturePointSelector.h"
44 #include "PartSet_Tools.h"
46 #include <SketchPlugin_Point.h>
48 #include <XGUI_Tools.h>
49 #include <XGUI_Workshop.h>
50 #include <XGUI_Displayer.h>
51 #include <XGUI_ViewerProxy.h>
54 #include <QMouseEvent>
56 #define HIGHLIGHT_STAYS_PROBLEM
57 #ifdef HIGHLIGHT_STAYS_PROBLEM
58 #include <Quantity_Color.hxx>
59 #define SKETCH_ENTITY_COLOR "225,0,0"
62 PartSet_WidgetFeaturePointSelector::PartSet_WidgetFeaturePointSelector(QWidget* theParent,
63 ModuleBase_IWorkshop* theWorkshop,
64 const Config_WidgetAPI* theData)
65 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
67 std::string anAttributes = theData->getProperty("selection_attributes");
68 QStringList anAttributesList = QString(anAttributes.c_str()).split(' ', QString::SkipEmptyParts);
70 myHasPreview = anAttributesList.size() >= 4;
72 mySelectedObjectAttribute = anAttributesList[0].toStdString();
73 mySelectedPointAttribute = anAttributesList[1].toStdString();
75 myPreviewObjectAttribute = anAttributesList[2].toStdString();
76 myPreviewPointAttribute = anAttributesList[3].toStdString();
80 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
84 //********************************************************************
85 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
86 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
88 return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
92 //********************************************************************
93 void PartSet_WidgetFeaturePointSelector::updateSelectionModesAndFilters(bool toActivate)
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();
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.,
106 myHighlightColor = aHStyle->Color();
107 mySelectionColor = aSStyle->Color();
110 aColor = myHighlightColor;
112 aHStyle->SetColor(aColor);
113 aContext->SetHighlightStyle(aHStyle);
115 aSStyle->SetColor(aColor);
116 aContext->SetSelectionStyle(aSStyle);
120 ModuleBase_WidgetShapeSelector::updateSelectionModesAndFilters(toActivate);
123 //********************************************************************
124 void PartSet_WidgetFeaturePointSelector::activateCustom()
126 ModuleBase_WidgetShapeSelector::activateCustom();
128 myWorkshop->module()->activateCustomPrs(myFeature,
129 ModuleBase_IModule::CustomizeHighlightedObjects, true);
132 //********************************************************************
133 void PartSet_WidgetFeaturePointSelector::deactivate()
135 ModuleBase_WidgetShapeSelector::deactivate();
138 //********************************************************************
139 void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
140 QMouseEvent* theEvent)
142 ModuleBase_ISelection* aSelect = myWorkshop->selection();
143 QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
145 ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
146 : ModuleBase_ViewerPrsPtr();
147 fillFeature(aPrs, theWindow, theEvent);
150 //********************************************************************
151 void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* theWindow,
152 QMouseEvent* theEvent)
154 // the contex menu release by the right button should not be processed by this widget
155 if (theEvent->button() != Qt::LeftButton)
158 ObjectPtr aPreviewObject;
159 GeomPnt2dPtr aPreviewPoint;
161 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
162 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
163 feature()->data()->attribute(myPreviewObjectAttribute));
164 aPreviewObject = aRefPreviewAttr->value();
166 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
167 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
168 feature()->data()->attribute(myPreviewPointAttribute));
169 aPreviewPoint = aPointPreviewAttr->pnt();
172 aPreviewObject = myPreviewObject;
173 aPreviewPoint = myPreviewPoint;
176 // do not move focus from the current widget if the object is not highlighted/selected
177 if (!aPreviewObject.get())
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);
186 AttributeReferencePtr aRefSelectedAttr = feature()->reference(mySelectedObjectAttribute);
187 if (aRefSelectedAttr)
188 aRefSelectedAttr->setValue(aPreviewObject);
190 AttributeRefAttrPtr aRefAttrSelectedAttr = feature()->refattr(mySelectedObjectAttribute);
191 if (aRefAttrSelectedAttr)
192 aRefAttrSelectedAttr->setObject(aPreviewObject);
195 updateObject(feature());
197 // we need to deselect base feature for better visibility of selected feature
198 XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
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);
205 //********************************************************************
206 bool PartSet_WidgetFeaturePointSelector::fillFeature(
207 const std::shared_ptr<ModuleBase_ViewerPrs>& theSelectedPrs,
208 ModuleBase_IViewWindow* theWindow,
209 QMouseEvent* theEvent)
211 bool aFilled = false;
212 if (theSelectedPrs.get() && theSelectedPrs->object().get())
213 myPreviewObject = theSelectedPrs->object();
214 myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
217 std::shared_ptr<ModelAPI_AttributeReference> aRef =
218 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
219 feature()->data()->attribute(myPreviewObjectAttribute));
220 aRef->setValue(myPreviewObject);
222 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
223 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
224 feature()->data()->attribute(myPreviewPointAttribute));
225 anAttributePoint->setValue(myPreviewPoint);
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);
232 updateObject(feature());
233 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
240 //********************************************************************
241 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeSelection() const
243 return QList<ModuleBase_ViewerPrsPtr>();
246 //********************************************************************
247 bool PartSet_WidgetFeaturePointSelector::setSelection(
248 QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
249 const bool theToValidate)
251 // false is returned to do not emit focus out widget by selected sub-shape
255 void PartSet_WidgetFeaturePointSelector::setPreSelection(
256 const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
257 ModuleBase_IViewWindow* theWnd,
258 QMouseEvent* theEvent)
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