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