]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSketchLabel.cpp
Salome HOME
55753d22d11639578bbf2fc95250a6f47277bb26
[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 bool& theToBlock, bool& isFlushesActived,
191                                                bool& isAttributeSetInitializedBlocked)
192 {
193   ModuleBase_WidgetValidated::blockAttribute(theToBlock, isFlushesActived,
194                                              isAttributeSetInitializedBlocked);
195   // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
196   // attributes. It it is necessary, these states should be append to the method attributes
197   // or stored in the widget
198
199   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
200   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
201   QStringList aValues;
202   for(; anIt != aLast; anIt++) {
203     AttributePtr anAttribute = *anIt;
204     if (theToBlock)
205       anAttribute->blockSetInitialized(true);
206     else
207       anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
208   }
209 }
210
211 bool PartSet_WidgetSketchLabel::setSelectionInternal(const QList<ModuleBase_ViewerPrs>& theValues,
212                                                      const bool theToValidate)
213 {
214   bool aDone = false;
215   ModuleBase_ViewerPrs aValue;
216   if (theValues.empty()) {
217     // In order to make reselection possible, set empty object and shape should be done
218     setSelectionCustom(ModuleBase_ViewerPrs());
219     aDone = false;
220   }
221   else {
222     // it removes the processed value from the parameters list
223     aValue = theValues.first();//.takeFirst();
224     if (!theToValidate || isValidInFilters(aValue))
225       aDone = setSelectionCustom(aValue);
226   }
227
228   return aDone;
229 }
230
231 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
232 {
233   // 1. hide main planes if they have been displayed
234   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
235   // 2. if the planes were displayed, change the view projection
236   const GeomShapePtr& aShape = thePrs.shape();
237   std::shared_ptr<GeomAPI_Shape> aGShape;
238   std::shared_ptr<GeomAPI_Shape> aBaseShape;
239
240   DataPtr aData = feature()->data();
241   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
242                             (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
243
244   // selection happens in OCC viewer
245   if (aShape.get() && !aShape->isNull()) {
246     aGShape = aShape;
247
248     if (aSelAttr && aSelAttr->context()) {
249       aBaseShape = aSelAttr->context()->shape();
250     }
251   }
252   else { // selection happens in OCC viewer(on body) of in the OB browser
253     if (aSelAttr) {
254       aGShape = aSelAttr->value();
255     }
256   }
257   if (aGShape.get() != NULL) {
258     // get plane parameters
259     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
260     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
261     gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
262     double aTwist = 0.0;
263
264     // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
265     /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
266       aXYZ.Reverse();
267     }*/
268
269     // Rotate view if the sketcher plane is selected only from preview planes
270     // Preview planes are created only if there is no any shape
271     bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
272     if (aRotate) {
273       myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
274       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
275       if (aModule)
276         aModule->onViewTransformed();
277     }
278   }
279   // 3. Clear text in the label
280   myStackWidget->setCurrentIndex(1);
281   //myLabel->setText("");
282   //myLabel->setToolTip("");
283   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
284   disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()), 
285               this, SLOT(onSelectionChanged()));
286   // 4. deactivate face selection filter
287   activateFilters(false);
288
289   // 5. Clear selection mode and define sketching mode
290   //XGUI_Displayer* aDisp = aWorkshop->displayer();
291   //aDisp->closeLocalContexts();
292   emit planeSelected(plane());
293   // after the plane is selected in the sketch, the sketch selection should be activated
294   // it can not be performed in the sketch label widget because, we don't need to switch off
295   // the selection by any label deactivation, but need to switch it off by stop the sketch
296   activateSelection(true);
297
298   // 6. Update sketcher actions
299   XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
300   myWorkshop->updateCommandStatus();
301   myWorkshop->viewer()->update();
302 }
303
304 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
305 {
306   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
307   return PartSet_Tools::sketchPlane(aSketch);
308 }
309
310 bool PartSet_WidgetSketchLabel::focusTo()
311 {
312   ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
313   return true;
314 }
315
316 void PartSet_WidgetSketchLabel::enableFocusProcessing()
317 {
318   myStackWidget->installEventFilter(this);
319 }
320
321 void PartSet_WidgetSketchLabel::storeAttributeValue()
322 {
323   ModuleBase_WidgetValidated::storeAttributeValue();
324 }
325
326 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
327 {
328   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
329
330   // it is not necessary to save the previous plane value because the plane is chosen once
331   DataPtr aData = feature()->data();
332   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
333     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
334   if (aSelAttr) {
335     ResultPtr anEmptyResult;
336     GeomShapePtr anEmptyShape;
337     aSelAttr->setValue(anEmptyResult, anEmptyShape);
338   }
339 }
340
341 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
342 {
343   return fillSketchPlaneBySelection(feature(), thePrs);
344 }
345
346 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrs& thePrs)
347 {
348   bool aCanFillSketch = true;
349   // avoid any selection on sketch object
350   ObjectPtr anObject = thePrs.object();
351   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
352   if (aResult.get()) {
353     FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
354     if (aFeature->getKind() == SketchPlugin_Sketch::ID())
355       aCanFillSketch = false;
356   }
357   // check plane or planar face of any non-sketch object
358   if (aCanFillSketch) {
359     std::shared_ptr<GeomAPI_Face> aGeomFace;
360     const TopoDS_Shape aShape = thePrs.shape();
361     if (aShape.IsNull()) {
362       GeomShapePtr aGeomShape = aResult->shape();
363       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
364       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
365     }
366     else if (aShape.ShapeType() == TopAbs_FACE) {
367       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face());
368       aGeomFace->setImpl(new TopoDS_Shape(aShape));
369       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
370     }
371     else
372       aCanFillSketch = false;
373   }
374   return aCanFillSketch;
375 }
376
377 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
378                                                            const ModuleBase_ViewerPrs& thePrs)
379 {
380   bool isOwnerSet = false;
381
382   const GeomShapePtr& aShape = thePrs.shape();
383   std::shared_ptr<GeomAPI_Dir> aDir;
384
385   if (thePrs.object() && (theFeature != thePrs.object())) {
386     DataPtr aData = theFeature->data();
387     AttributeSelectionPtr aSelAttr = 
388       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
389       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
390     if (aSelAttr) {
391       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
392       if (aRes) {
393         GeomShapePtr aShapePtr(new GeomAPI_Shape());
394         if (!aShape.get() || aShape->isNull()) {  // selection happens in the OCC viewer
395           aShapePtr = ModelAPI_Tools::shape(aRes);
396         }
397         else { // selection happens in OB browser
398           aShapePtr = aShape;
399         }
400         if (aShapePtr.get() != NULL) {
401           aSelAttr->setValue(aRes, aShapePtr);
402           isOwnerSet = true;
403         }
404       }
405     }
406   }
407   else if (aShape.get() && !aShape->isNull()) {
408     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
409     aDir = setSketchPlane(aTDShape);
410     isOwnerSet = aDir.get();
411   }
412   return isOwnerSet;
413 }
414
415 void PartSet_WidgetSketchLabel::activateCustom()
416 {
417   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
418   if (aPlane.get()) {
419     myStackWidget->setCurrentIndex(1);
420     activateSelection(true);
421     return;
422   }
423
424   myStackWidget->setCurrentIndex(0);
425   bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
426   if (!aBodyIsVisualized) {
427     // We have to select a plane before any operation
428     myPreviewPlanes->showPreviewPlanes(myWorkshop);
429   }
430   activateSelection(true);
431
432   //myLabel->setText(myText);
433   //myLabel->setToolTip(myTooltip);
434
435   connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
436           this, SLOT(onSelectionChanged()));
437   activateFilters(true);
438 }
439
440 void PartSet_WidgetSketchLabel::deactivate()
441 {
442   ModuleBase_ModelWidget::deactivate();
443   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
444   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
445   activateSelection(false);
446
447   activateFilters(false);
448   if (aHidePreview)
449     myWorkshop->viewer()->update();
450 }
451
452 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
453 {
454   if (toActivate) {
455     QIntList aModes;
456     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
457     if (aPlane.get()) {
458       myWorkshop->module()->activeSelectionModes(aModes);
459     }
460     else {
461       aModes << TopAbs_FACE;
462     }
463     myWorkshop->activateSubShapesSelection(aModes);
464   } else {
465     myWorkshop->deactivateSubShapesSelection();
466   }
467 }
468
469
470 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
471                                                                        const TopoDS_Shape& theShape)
472 {
473   if (theShape.IsNull())
474     return std::shared_ptr<GeomAPI_Dir>();
475
476   // get selected shape
477   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
478   aGShape->setImpl(new TopoDS_Shape(theShape));
479
480
481
482   // get plane parameters
483   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
484   if (!aPlane.get())
485     return std::shared_ptr<GeomAPI_Dir>();
486
487   // set plane parameters to feature
488   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
489   double anA, aB, aC, aD;
490   aPlane->coefficients(anA, aB, aC, aD);
491
492   // calculate attributes of the sketch
493   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
494   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
495   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
496   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
497   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
498   // X axis is preferable to be dirX on the sketch
499   const double tol = Precision::Confusion();
500   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
501   std::shared_ptr<GeomAPI_Dir> aTempDir(
502       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
503   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
504   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
505
506   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
507       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
508   anOrigin->setValue(anOrigPnt);
509   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
510       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
511   aNormal->setValue(aNormDir);
512   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
513       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
514   aDirX->setValue(aXDir);
515   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
516   return aDir;
517 }
518
519 void PartSet_WidgetSketchLabel::onSetPlaneView()
520 {
521   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
522   if (aPlane.get()) {
523     std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
524     gp_Dir aDir = aDirection->impl<gp_Dir>();
525     if (myViewInverted->isChecked())
526       aDir.Reverse();
527     myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
528     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
529     if (aModule)
530       aModule->onViewTransformed();
531   }
532 }