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