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