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