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