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