Salome HOME
2524a9ba7f1f2bb0aa9d64457abfaaf4adde078a
[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 #include "PartSet_Module.h"
10 #include "PartSet_PreviewPlanes.h"
11
12 #include "SketchPlugin_SketchEntity.h"
13
14 #include <XGUI_Workshop.h>
15 #include <XGUI_Displayer.h>
16 #include <XGUI_SelectionMgr.h>
17 #include <XGUI_Selection.h>
18 #include <XGUI_ViewerProxy.h>
19 #include <XGUI_ActionsMgr.h>
20 #include <XGUI_Tools.h>
21 #include <XGUI_ModuleConnector.h>
22
23 #include <ModuleBase_Operation.h>
24 #include <ModuleBase_ViewerPrs.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_IModule.h>
27
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_Tools.h>
30
31 #include <GeomAlgoAPI_FaceBuilder.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomDataAPI_Point.h>
34 #include <GeomDataAPI_Dir.h>
35 #include <GeomAPI_XYZ.h>
36 #include <GeomAPI_Face.h>
37
38 #include <SketchPlugin_Sketch.h>
39 #include <SketcherPrs_Tools.h>
40
41 #include <Precision.hxx>
42 #include <gp_Pln.hxx>
43 #include <gp_Pnt.hxx>
44 #include <gp_Dir.hxx>
45 #include <AIS_Shape.hxx>
46 #include <AIS_DimensionSelectionMode.hxx>
47
48 #include <Config_WidgetAPI.h>
49 #include <Config_PropManager.h>
50
51 #include <QLabel>
52 #include <QApplication>
53 #include <QVBoxLayout>
54 #include <QCheckBox>
55 #include <QGroupBox>
56 #include <QPushButton>
57 #include <QStackedWidget>
58
59
60 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
61                         ModuleBase_IWorkshop* theWorkshop,
62                         const Config_WidgetAPI* theData,
63                         const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
64 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
65 {
66   QVBoxLayout* aLayout = new QVBoxLayout(this);
67   ModuleBase_Tools::zeroMargins(aLayout);
68
69   myStackWidget = new QStackedWidget(this);
70   myStackWidget->setContentsMargins(0,0,0,0);
71   aLayout->addWidget(myStackWidget);
72
73   // Define label for plane selection
74   QWidget* aFirstWgt = new QWidget(this);
75
76   QString aText = QString::fromStdString(theData->getProperty("title"));
77   QLabel* aLabel = new QLabel(aText, aFirstWgt);
78   aLabel->setWordWrap(true);
79   QString aTooltip = QString::fromStdString(theData->getProperty("tooltip"));
80   aLabel->setToolTip(aTooltip);
81   aLabel->setIndent(5);
82
83   aLayout = new QVBoxLayout(aFirstWgt);
84   ModuleBase_Tools::zeroMargins(aLayout);
85   aLayout->addWidget(aLabel);
86
87   myStackWidget->addWidget(aFirstWgt);
88
89   // Define widget for sketch manmagement
90   QWidget* aSecondWgt = new QWidget(this);
91   aLayout = new QVBoxLayout(aSecondWgt);
92   ModuleBase_Tools::zeroMargins(aLayout);
93
94   QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
95   QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
96
97   myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
98   aViewLayout->addWidget(myViewInverted);
99
100   QPushButton* aSetViewBtn = new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
101   connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
102   aViewLayout->addWidget(aSetViewBtn);
103
104   aLayout->addWidget(aViewBox);
105
106   QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
107   aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
108   aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
109   aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
110
111   QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
112                                                         aLast = aStates.end();
113   for (; anIt != aLast; anIt++) {
114     QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
115     connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
116     aLayout->addWidget(aShowConstraints);
117
118     PartSet_Tools::ConstraintVisibleState aState = anIt.key();
119     myShowConstraints[aState] = aShowConstraints;
120
121     if (toShowConstraints.contains(aState))
122       aShowConstraints->setChecked(toShowConstraints[aState]);
123   }
124
125   myStackWidget->addWidget(aSecondWgt);
126   //setLayout(aLayout);
127
128   myPreviewPlanes = new PartSet_PreviewPlanes();
129 }
130
131 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
132 {
133 }
134
135 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
136                                              const bool theToValidate)
137 {
138   // do not use the given selection if the plane of the sketch has been already set.
139   // If this check is absent, a selected plane in the viewer can be set in the sketch
140   // even if the sketch is built on another plane.
141   if (plane().get())
142     return true;
143
144   ModuleBase_ViewerPrs aPrs = theValues.first();
145   bool aDone = setSelectionInternal(theValues, theToValidate);
146   if (aDone)
147     updateByPlaneSelected(aPrs);
148   return aDone;
149 }
150
151 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
152 {
153   QList<QWidget*> aResult;
154   aResult << myStackWidget;
155   return aResult;
156 }
157
158 void PartSet_WidgetSketchLabel::onSelectionChanged()
159 {
160   QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
161
162   if (aSelected.empty())
163     return;
164   ModuleBase_ViewerPrs aPrs = aSelected.first();
165   bool aDone = setSelectionInternal(aSelected, false);
166   if (aDone) {
167     updateByPlaneSelected(aPrs);
168     updateObject(myFeature);
169   }
170 }
171
172 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
173 {
174   QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
175
176   QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
177                           anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
178
179   PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
180   bool aFound = false;
181   for (; anIt != aLast && !aFound; anIt++) {
182     aFound = anIt.value() == aSenderCheckBox;
183     if (aFound)
184       aState = anIt.key();
185   }
186   if (aFound)
187     emit showConstraintToggled(aState, theOn);
188 }
189
190 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
191                                                const bool& theToBlock, bool& isFlushesActived,
192                                                bool& isAttributeSetInitializedBlocked)
193 {
194   ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
195                                              isAttributeSetInitializedBlocked);
196   // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
197   // attributes. It it is necessary, these states should be append to the method attributes
198   // or stored in the widget
199
200   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
201   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
202   QStringList aValues;
203   for(; anIt != aLast; anIt++) {
204     AttributePtr anAttribute = *anIt;
205     if (theToBlock)
206       anAttribute->blockSetInitialized(true);
207     else
208       anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
209   }
210 }
211
212 bool PartSet_WidgetSketchLabel::setSelectionInternal(const QList<ModuleBase_ViewerPrs>& theValues,
213                                                      const bool theToValidate)
214 {
215   bool aDone = false;
216   ModuleBase_ViewerPrs aValue;
217   if (theValues.empty()) {
218     // In order to make reselection possible, set empty object and shape should be done
219     setSelectionCustom(ModuleBase_ViewerPrs());
220     aDone = false;
221   }
222   else {
223     // it removes the processed value from the parameters list
224     aValue = theValues.first();//.takeFirst();
225     if (!theToValidate || isValidInFilters(aValue))
226       aDone = setSelectionCustom(aValue);
227   }
228
229   return aDone;
230 }
231
232 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
233 {
234   // 1. hide main planes if they have been displayed
235   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
236   // 2. if the planes were displayed, change the view projection
237   const GeomShapePtr& aShape = thePrs.shape();
238   std::shared_ptr<GeomAPI_Shape> aGShape;
239   std::shared_ptr<GeomAPI_Shape> aBaseShape;
240
241   DataPtr aData = feature()->data();
242   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
243                             (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
244
245   // selection happens in OCC viewer
246   if (aShape.get() && !aShape->isNull()) {
247     aGShape = aShape;
248
249     if (aSelAttr && aSelAttr->context()) {
250       aBaseShape = aSelAttr->context()->shape();
251     }
252   }
253   else { // selection happens in OCC viewer(on body) of in the OB browser
254     if (aSelAttr) {
255       aGShape = aSelAttr->value();
256     }
257   }
258   if (aGShape.get() != NULL) {
259     // get plane parameters
260     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
261     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
262     gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
263     double aTwist = 0.0;
264
265     // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
266     /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
267       aXYZ.Reverse();
268     }*/
269
270     // Rotate view if the sketcher plane is selected only from preview planes
271     // Preview planes are created only if there is no any shape
272     bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
273     if (aRotate) {
274       myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
275       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
276       if (aModule)
277         aModule->onViewTransformed();
278     }
279   }
280   // 3. Clear text in the label
281   myStackWidget->setCurrentIndex(1);
282   //myLabel->setText("");
283   //myLabel->setToolTip("");
284   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
285   disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()), 
286               this, SLOT(onSelectionChanged()));
287   // 4. deactivate face selection filter
288   activateFilters(false);
289
290   // 5. Clear selection mode and define sketching mode
291   //XGUI_Displayer* aDisp = aWorkshop->displayer();
292   //aDisp->closeLocalContexts();
293   emit planeSelected(plane());
294   // after the plane is selected in the sketch, the sketch selection should be activated
295   // it can not be performed in the sketch label widget because, we don't need to switch off
296   // the selection by any label deactivation, but need to switch it off by stop the sketch
297   activateSelection(true);
298
299   // 6. Update sketcher actions
300   XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
301   myWorkshop->updateCommandStatus();
302   myWorkshop->viewer()->update();
303 }
304
305 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
306 {
307   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
308   return PartSet_Tools::sketchPlane(aSketch);
309 }
310
311 bool PartSet_WidgetSketchLabel::focusTo()
312 {
313   ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
314   return true;
315 }
316
317 void PartSet_WidgetSketchLabel::enableFocusProcessing()
318 {
319   myStackWidget->installEventFilter(this);
320 }
321
322 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
323 {
324   ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
325 }
326
327 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
328                                                       const bool theValid)
329 {
330   ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
331
332   // it is not necessary to save the previous plane value because the plane is chosen once
333   DataPtr aData = feature()->data();
334   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
335     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
336   if (aSelAttr) {
337     ResultPtr anEmptyResult;
338     GeomShapePtr anEmptyShape;
339     aSelAttr->setValue(anEmptyResult, anEmptyShape);
340   }
341 }
342
343 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
344 {
345   return fillSketchPlaneBySelection(feature(), thePrs);
346 }
347
348 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrs& thePrs)
349 {
350   bool aCanFillSketch = true;
351   // avoid any selection on sketch object
352   ObjectPtr anObject = thePrs.object();
353   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
354   if (aResult.get()) {
355     FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
356     if (aFeature->getKind() == SketchPlugin_Sketch::ID())
357       aCanFillSketch = false;
358   }
359   // check plane or planar face of any non-sketch object
360   if (aCanFillSketch) {
361     std::shared_ptr<GeomAPI_Face> aGeomFace;
362
363     GeomShapePtr aGeomShape = thePrs.shape();
364     if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
365       aGeomShape = aResult->shape();
366     }
367
368     if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
369       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
370       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
371     }
372     else
373       aCanFillSketch = false;
374   }
375   return aCanFillSketch;
376 }
377
378 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
379                                                            const ModuleBase_ViewerPrs& thePrs)
380 {
381   bool isOwnerSet = false;
382
383   const GeomShapePtr& aShape = thePrs.shape();
384   std::shared_ptr<GeomAPI_Dir> aDir;
385
386   if (thePrs.object() && (theFeature != thePrs.object())) {
387     DataPtr aData = theFeature->data();
388     AttributeSelectionPtr aSelAttr = 
389       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
390       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
391     if (aSelAttr) {
392       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
393       if (aRes) {
394         GeomShapePtr aShapePtr(new GeomAPI_Shape());
395         if (!aShape.get() || aShape->isNull()) {  // selection happens in the OCC viewer
396           aShapePtr = ModelAPI_Tools::shape(aRes);
397         }
398         else { // selection happens in OB browser
399           aShapePtr = aShape;
400         }
401         if (aShapePtr.get() != NULL) {
402           aSelAttr->setValue(aRes, aShapePtr);
403           isOwnerSet = true;
404         }
405       }
406     }
407   }
408   else if (aShape.get() && !aShape->isNull()) {
409     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
410     aDir = setSketchPlane(theFeature, aTDShape);
411     isOwnerSet = aDir.get();
412   }
413   return isOwnerSet;
414 }
415
416 void PartSet_WidgetSketchLabel::activateCustom()
417 {
418   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
419   if (aPlane.get()) {
420     myStackWidget->setCurrentIndex(1);
421     activateSelection(true);
422     return;
423   }
424
425   myStackWidget->setCurrentIndex(0);
426   bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
427
428   // Clear previous selection mode It is necessary for correct activation of preview planes
429   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
430   XGUI_Displayer* aDisp = aWorkshop->displayer();
431   aDisp->activateObjects(QIntList(), aDisp->displayedObjects(), false);
432
433   if (!aBodyIsVisualized) {
434     // We have to select a plane before any operation
435     myPreviewPlanes->showPreviewPlanes(myWorkshop);
436   }
437   activateSelection(true);
438
439   //myLabel->setText(myText);
440   //myLabel->setToolTip(myTooltip);
441
442   connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
443           this, SLOT(onSelectionChanged()));
444   activateFilters(true);
445 }
446
447 void PartSet_WidgetSketchLabel::deactivate()
448 {
449   ModuleBase_ModelWidget::deactivate();
450   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
451   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
452   activateSelection(false);
453
454   activateFilters(false);
455   if (aHidePreview)
456     myWorkshop->viewer()->update();
457 }
458
459 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
460 {
461   if (toActivate) {
462     QIntList aModes;
463     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
464     if (aPlane.get()) {
465       myWorkshop->module()->activeSelectionModes(aModes);
466     }
467     else {
468       aModes << TopAbs_FACE;
469     }
470     myWorkshop->activateSubShapesSelection(aModes);
471   } else {
472     myWorkshop->deactivateSubShapesSelection();
473   }
474 }
475
476
477 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
478                                                                        const TopoDS_Shape& theShape)
479 {
480   if (theShape.IsNull())
481     return std::shared_ptr<GeomAPI_Dir>();
482
483   // get selected shape
484   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
485   aGShape->setImpl(new TopoDS_Shape(theShape));
486
487
488
489   // get plane parameters
490   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
491   if (!aPlane.get())
492     return std::shared_ptr<GeomAPI_Dir>();
493
494   // set plane parameters to feature
495   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
496   double anA, aB, aC, aD;
497   aPlane->coefficients(anA, aB, aC, aD);
498
499   // calculate attributes of the sketch
500   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
501   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
502   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
503   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
504   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
505   // X axis is preferable to be dirX on the sketch
506   const double tol = Precision::Confusion();
507   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
508   std::shared_ptr<GeomAPI_Dir> aTempDir(
509       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
510   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
511   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
512
513   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
514       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
515   anOrigin->setValue(anOrigPnt);
516   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
517       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
518   aNormal->setValue(aNormDir);
519   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
520       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
521   aDirX->setValue(aXDir);
522   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
523   return aDir;
524 }
525
526 void PartSet_WidgetSketchLabel::onSetPlaneView()
527 {
528   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
529   if (aPlane.get()) {
530     std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
531     gp_Dir aDir = aDirection->impl<gp_Dir>();
532     if (myViewInverted->isChecked())
533       aDir.Reverse();
534     myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
535     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
536     if (aModule)
537       aModule->onViewTransformed();
538   }
539 }