1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetFeaturePointSelector.cpp
4 // Created: 28 Feb 2017
5 // Author: Natalia ERMOLAEVA
7 #include <Config_WidgetAPI.h>
9 #include <Events_Loop.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomDataAPI_Point.h>
13 #include <GeomAPI_Edge.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAlgoAPI_ShapeTools.h>
17 #include <ModuleBase_ISelection.h>
18 #include <ModuleBase_ViewerPrs.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Tools.h>
25 #include <ModuleBase_IViewWindow.h>
26 #include <ModuleBase_IWorkshop.h>
27 #include <ModuleBase_IModule.h>
29 #include "PartSet_WidgetFeaturePointSelector.h"
30 #include "PartSet_Tools.h"
32 #include <SketchPlugin_Point.h>
34 #include <XGUI_Tools.h>
35 #include <XGUI_Workshop.h>
36 #include <XGUI_Displayer.h>
37 #include <XGUI_ViewerProxy.h>
40 #include <QMouseEvent>
42 #define HIGHLIGHT_STAYS_PROBLEM
43 #ifdef HIGHLIGHT_STAYS_PROBLEM
44 #include <Quantity_Color.hxx>
45 #define SKETCH_ENTITY_COLOR "225,0,0"
48 PartSet_WidgetFeaturePointSelector::PartSet_WidgetFeaturePointSelector(QWidget* theParent,
49 ModuleBase_IWorkshop* theWorkshop,
50 const Config_WidgetAPI* theData)
51 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
53 std::string anAttributes = theData->getProperty("selection_attributes");
54 QStringList anAttributesList = QString(anAttributes.c_str()).split(' ', QString::SkipEmptyParts);
56 mySelectedObjectAttribute = anAttributesList[0].toStdString();
57 mySelectedPointAttribute = anAttributesList[1].toStdString();
58 myPreviewObjectAttribute = anAttributesList[2].toStdString();
59 myPreviewPointAttribute = anAttributesList[3].toStdString();
62 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
66 //********************************************************************
67 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
68 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
70 return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
74 //********************************************************************
75 bool PartSet_WidgetFeaturePointSelector::activateSelectionAndFilters(bool toActivate)
77 #ifdef HIGHLIGHT_STAYS_PROBLEM
78 Handle(AIS_InteractiveContext) aContext =
79 XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
80 Quantity_Color aColor;
81 Handle(Graphic3d_HighlightStyle) aHStyle = aContext->HighlightStyle();
82 Handle(Graphic3d_HighlightStyle) aSStyle = aContext->SelectionStyle();
84 std::vector<int> aColors;
85 aColors = Config_PropManager::color("Visualization", "sketch_entity_color");
86 aColor = Quantity_Color(aColors[0] / 255., aColors[1] / 255., aColors[2] / 255.,
88 myHighlightColor = aHStyle->Color();
89 mySelectionColor = aSStyle->Color();
92 aColor = myHighlightColor;
94 aHStyle->SetColor(aColor);
95 aContext->SetHighlightStyle(aHStyle);
97 aSStyle->SetColor(aColor);
98 aContext->SetSelectionStyle(aSStyle);
102 return ModuleBase_WidgetShapeSelector::activateSelectionAndFilters(toActivate);
105 //********************************************************************
106 void PartSet_WidgetFeaturePointSelector::activateCustom()
108 ModuleBase_WidgetShapeSelector::activateCustom();
110 myWorkshop->module()->activateCustomPrs(myFeature,
111 ModuleBase_IModule::CustomizeHighlightedObjects, true);
114 //********************************************************************
115 void PartSet_WidgetFeaturePointSelector::deactivate()
117 ModuleBase_WidgetShapeSelector::deactivate();
120 //********************************************************************
121 void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theWindow,
122 QMouseEvent* theEvent)
124 ModuleBase_ISelection* aSelect = myWorkshop->selection();
125 QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
127 ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
128 : ModuleBase_ViewerPrsPtr();
129 fillFeature(aPrs, theWindow, theEvent);
132 //********************************************************************
133 void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* theWindow,
134 QMouseEvent* theEvent)
136 // the contex menu release by the right button should not be processed by this widget
137 if (theEvent->button() != Qt::LeftButton)
140 std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
141 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
142 feature()->data()->attribute(myPreviewObjectAttribute));
143 ObjectPtr aPreviewObject = aRefPreviewAttr->value();
144 // do not move focus from the current widget if the object is not highlighted/selected
145 if (!aPreviewObject.get())
148 // set parameters of preview into parameters of selection in the feature
149 std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
150 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
151 feature()->data()->attribute(mySelectedObjectAttribute));
152 aRefSelectedAttr->setValue(aRefPreviewAttr->value());
154 std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
155 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
156 feature()->data()->attribute(mySelectedPointAttribute));
157 std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
158 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
159 feature()->data()->attribute(myPreviewPointAttribute));
160 aPointSelectedAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
162 updateObject(feature());
164 emit focusOutWidget(this);
165 // we need to deselect base feature for better visibility of selected feature
166 XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
169 //********************************************************************
170 bool PartSet_WidgetFeaturePointSelector::fillFeature(
171 const std::shared_ptr<ModuleBase_ViewerPrs>& theSelectedPrs,
172 ModuleBase_IViewWindow* theWindow,
173 QMouseEvent* theEvent)
175 bool aFilled = false;
177 if (theSelectedPrs.get() && theSelectedPrs->object().get())
178 anObject = theSelectedPrs->object();
180 std::shared_ptr<ModelAPI_AttributeReference> aRef =
181 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
182 feature()->data()->attribute(myPreviewObjectAttribute));
183 aRef->setValue(anObject);
185 std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
186 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
187 feature()->data()->attribute(myPreviewPointAttribute));
188 std::shared_ptr<GeomAPI_Pnt2d> aPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
189 anAttributePoint->setValue(aPoint);
190 // redisplay AIS presentation in viewer
191 #ifndef HIGHLIGHT_STAYS_PROBLEM
192 // an attempt to clear highlighted item in the viewer: but of OCCT
193 XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
195 updateObject(feature());
196 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
203 //********************************************************************
204 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeSelection() const
206 return QList<ModuleBase_ViewerPrsPtr>();
209 //********************************************************************
210 bool PartSet_WidgetFeaturePointSelector::setSelection(
211 QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
212 const bool theToValidate)
214 // false is returned to do not emit focus out widget by selected sub-shape
218 void PartSet_WidgetFeaturePointSelector::setPreSelection(
219 const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
220 ModuleBase_IViewWindow* theWnd,
221 QMouseEvent* theEvent)
223 // the method is empty because firstly by starging of the feature there is no selection of
224 // sub-segments in the viewer, secondly preselection of restart operation is processed by
225 // special reentrant message sent by the feature