Salome HOME
Color preferences for the sketch entities.
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchLabel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetSketchLabel.cpp
4 // Created:     07 July 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_WidgetSketchLabel.h"
8 #include "PartSet_Tools.h"
9
10 #include <XGUI_Workshop.h>
11 #include <XGUI_Displayer.h>
12 #include <XGUI_SelectionMgr.h>
13 #include <XGUI_Selection.h>
14 #include <XGUI_ViewerProxy.h>
15 #include <XGUI_ActionsMgr.h>
16
17 #include <ModuleBase_Operation.h>
18 #include <ModuleBase_ViewerPrs.h>
19
20 #include <GeomAlgoAPI_FaceBuilder.h>
21 #include <GeomDataAPI_Point.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomAPI_XYZ.h>
24
25 #include <SketchPlugin_Sketch.h>
26
27 #include <Precision.hxx>
28 #include <gp_Pln.hxx>
29 #include <gp_Pnt.hxx>
30 #include <gp_Dir.hxx>
31 #include <AIS_Shape.hxx>
32 #include <AIS_DimensionSelectionMode.hxx>
33
34 #include <Config_WidgetAPI.h>
35 #include <Config_PropManager.h>
36
37 #include <QLabel>
38 #include <QTimer>
39 #include <QApplication>
40
41
42 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
43                                                      const Config_WidgetAPI* theData,
44                                                      const std::string& theParentId)
45     : ModuleBase_ModelWidget(theParent, theData, theParentId), myPreviewDisplayed(false)
46 {
47   myText = QString::fromStdString(theData->getProperty("title"));
48   myLabel = new QLabel("", theParent);
49   myLabel->setWordWrap(true);
50   myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
51   myLabel->setToolTip("");
52   myLabel->setIndent(5);
53
54   mySelectionTimer = new QTimer(this);
55   connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
56   mySelectionTimer->setSingleShot(true);
57 }
58
59 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
60 {
61   erasePreviewPlanes();
62 }
63
64 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
65 {
66   return QList<QWidget*>();
67 }
68
69 QWidget* PartSet_WidgetSketchLabel::getControl() const
70 {
71   return myLabel;
72 }
73
74 void PartSet_WidgetSketchLabel::onPlaneSelected()
75 {
76   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
77   QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
78   if (!aSelected.empty()) {
79     ModuleBase_ViewerPrs aPrs = aSelected.first();
80     TopoDS_Shape aShape = aPrs.shape();
81     if (!aShape.IsNull()) {
82       std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
83       if (aDir) {
84         erasePreviewPlanes();
85
86         if (aPrs.object() && (feature() != aPrs.object())) {
87           DataPtr aData = feature()->data();
88           AttributeSelectionPtr aSelAttr = 
89             std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
90             (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
91           if (aSelAttr) {
92             ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
93             if (aRes) {
94               GeomShapePtr aShapePtr(new GeomAPI_Shape());
95               aShapePtr->setImpl(new TopoDS_Shape(aShape));
96               aSelAttr->setValue(aRes, aShapePtr);
97             }
98           }
99         } else
100           myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
101
102         // Clear text in the label
103         myLabel->setText("");
104         myLabel->setToolTip("");
105         disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
106                    this, SLOT(onPlaneSelected()));
107
108         // Clear selection mode and define sketching mode
109         XGUI_Displayer* aDisp = myWorkshop->displayer();
110         aDisp->removeSelectionFilter(myFaceFilter);
111         //aDisp->closeLocalContexts();
112         emit planeSelected(plane());
113         setSketchingMode();
114
115         // Update sketcher actions
116         XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
117         anActMgr->update();
118         myWorkshop->viewer()->update();
119       }
120     }
121   }
122 }
123
124 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
125 {
126   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
127   return PartSet_Tools::sketchPlane(aSketch);
128
129 }
130
131 void PartSet_WidgetSketchLabel::activateCustom()
132 {
133   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
134   if (aPlane) {
135     //setSketchingMode();
136     // In order to avoid Opening/Closing of context too often
137     mySelectionTimer->start(20);
138   } else {
139     // We have to select a plane before any operation
140     showPreviewPlanes();
141
142     XGUI_Displayer* aDisp = myWorkshop->displayer();
143     //aDisp->openLocalContext();
144     //aDisp->activateObjects(QIntList());
145     if (myFaceFilter.IsNull())
146       myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
147     aDisp->addSelectionFilter(myFaceFilter);
148     QIntList aModes;
149     aModes << TopAbs_FACE;
150     aDisp->activateObjects(aModes);
151
152     myLabel->setText(myText);
153     myLabel->setToolTip(myTooltip);
154
155     connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
156     aDisp->updateViewer();
157   }
158 }
159
160 void PartSet_WidgetSketchLabel::deactivate()
161 {
162   // Do not set selection mode if the widget was activated for a small moment 
163   mySelectionTimer->stop();
164   XGUI_Displayer* aDisp = myWorkshop->displayer();
165   aDisp->removeSelectionFilter(myFaceFilter);
166   //aDisp->removeSelectionFilter(mySketchFilter);
167   //aDisp->closeLocalContexts();
168   erasePreviewPlanes();
169 }
170
171 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
172 {
173   if (myPreviewDisplayed) {
174     XGUI_Displayer* aDisp = myWorkshop->displayer();
175     aDisp->eraseAIS(myYZPlane, false);
176     aDisp->eraseAIS(myXZPlane, false);
177     aDisp->eraseAIS(myXYPlane, false);
178     myPreviewDisplayed = false;
179   }
180 }
181
182 void PartSet_WidgetSketchLabel::showPreviewPlanes()
183 {
184   if (myPreviewDisplayed)
185     return;
186
187   if (!myYZPlane) { // If planes are not created
188     // Create Preview
189     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
190     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
191     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
192     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
193
194     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
195     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
196                                                         YZ_PLANE_COLOR);
197     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
198                                                         XZ_PLANE_COLOR);
199     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
200                                                         XY_PLANE_COLOR);
201     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
202     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
203     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
204
205     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
206     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
207     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
208   }
209   XGUI_Displayer* aDisp = myWorkshop->displayer();
210   aDisp->displayAIS(myYZPlane, false);
211   aDisp->displayAIS(myXZPlane, false);
212   aDisp->displayAIS(myXYPlane, false);
213   myPreviewDisplayed = true;
214 }
215
216
217 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
218                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
219                                                            const int theRGB[3])
220 {
221   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
222   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
223   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
224   aAIS->createShape(aFace);
225   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
226   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
227   return aAIS;
228 }
229
230
231 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
232 {
233   if (theShape.IsNull())
234     return std::shared_ptr<GeomAPI_Dir>();
235
236   // get selected shape
237   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
238   aGShape->setImpl(new TopoDS_Shape(theShape));
239
240   // get plane parameters
241   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
242
243   // set plane parameters to feature
244   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
245   double anA, aB, aC, aD;
246   aPlane->coefficients(anA, aB, aC, aD);
247
248   // calculate attributes of the sketch
249   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
250   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
251   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
252   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
253   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
254   // X axis is preferable to be dirX on the sketch
255   const double tol = Precision::Confusion();
256   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
257   std::shared_ptr<GeomAPI_Dir> aTempDir(
258       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
259   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
260   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
261
262   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
263       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
264   anOrigin->setValue(anOrigPnt);
265   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
266       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
267   aNormal->setValue(aNormDir);
268   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
269       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
270   aDirX->setValue(aXDir);
271   std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
272       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
273   aDirY->setValue(aYDir);
274   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
275   return aDir;
276 }
277
278
279 void PartSet_WidgetSketchLabel::setSketchingMode()
280 {
281   XGUI_Displayer* aDisp = myWorkshop->displayer();
282   // Clear standard selection modes if they are defined
283   //aDisp->activateObjects(aModes);
284   //aDisp->openLocalContext();
285
286   // Get default selection modes
287   QIntList aModes;
288   aModes.append(AIS_DSM_Text);
289   aModes.append(AIS_DSM_Line);
290   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
291   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
292
293   aDisp->activateObjects(aModes);
294 }