Salome HOME
Task 2.5 Show where the remaining degrees of freedom are (issue #2997)
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchLabel.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_WidgetSketchLabel.h"
21 #include "PartSet_Tools.h"
22 #include "PartSet_Module.h"
23 #include "PartSet_PreviewPlanes.h"
24 #include "PartSet_SketcherReentrantMgr.h"
25
26 #include "SketchPlugin_SketchEntity.h"
27
28 #include <XGUI_ActionsMgr.h>
29 #include <XGUI_Displayer.h>
30 #include <XGUI_ModuleConnector.h>
31 #include <XGUI_SelectionActivate.h>
32 #include <XGUI_Selection.h>
33 #include <XGUI_SelectionMgr.h>
34 #include <XGUI_Tools.h>
35 #include <XGUI_ViewerProxy.h>
36 #include <XGUI_Workshop.h>
37
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_Tools.h>
40 #include <ModelAPI_AttributeString.h>
41 #include <ModelAPI_Events.h>
42
43 #include <ModuleBase_Operation.h>
44 #include <ModuleBase_ViewerPrs.h>
45 #include <ModuleBase_Tools.h>
46 #include <ModuleBase_IModule.h>
47 #include <ModuleBase_IPropertyPanel.h>
48
49 #include <GeomAlgoAPI_FaceBuilder.h>
50 #include <GeomAlgoAPI_ShapeTools.h>
51 #include <GeomDataAPI_Point.h>
52 #include <GeomDataAPI_Dir.h>
53 #include <GeomAPI_XYZ.h>
54 #include <GeomAPI_Face.h>
55 #include <GeomAPI_Edge.h>
56 #include <GeomAPI_ShapeExplorer.h>
57
58 #include <SketchPlugin_Sketch.h>
59 #include <SketcherPrs_Tools.h>
60
61 #include <Precision.hxx>
62 #include <gp_Pln.hxx>
63 #include <gp_Pnt.hxx>
64 #include <gp_Dir.hxx>
65 #include <AIS_Shape.hxx>
66 #include <AIS_DimensionSelectionMode.hxx>
67 #include <Bnd_Box.hxx>
68
69 #include <Config_WidgetAPI.h>
70 #include <Config_PropManager.h>
71
72 #include <QLabel>
73 #include <QApplication>
74 #include <QVBoxLayout>
75 #include <QHBoxLayout>
76 #include <QCheckBox>
77 #include <QGroupBox>
78 #include <QPushButton>
79 #include <QStackedWidget>
80 #include <QLineEdit>
81 #include <QDoubleValidator>
82
83 #ifndef DBL_MAX
84 #define DBL_MAX 1.7976931348623158e+308
85 #endif
86
87 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
88                         ModuleBase_IWorkshop* theWorkshop,
89                         const Config_WidgetAPI* theData,
90                         const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
91 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData), myOpenTransaction(false),
92 myIsSelection(false)
93 {
94   QVBoxLayout* aLayout = new QVBoxLayout(this);
95   ModuleBase_Tools::zeroMargins(aLayout);
96
97   myStackWidget = new QStackedWidget(this);
98   myStackWidget->setContentsMargins(0,0,0,0);
99   aLayout->addWidget(myStackWidget);
100
101   // Define label for plane selection
102   QWidget* aFirstWgt = new QWidget(this);
103
104   // Size of the View control
105   mySizeOfViewWidget = new QWidget(aFirstWgt);
106   QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
107   aSizeLayout->addWidget(new QLabel(tr("Size of the view"), mySizeOfViewWidget));
108   mySizeOfView = new QLineEdit(mySizeOfViewWidget);
109
110   QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
111   aValidator->setLocale(ModuleBase_Tools::doubleLocale());
112   aValidator->setNotation(QDoubleValidator::StandardNotation);
113   mySizeOfView->setValidator(aValidator);
114   aSizeLayout->addWidget(mySizeOfView);
115
116   QString aText = translate(theData->getProperty("title"));
117   QLabel* aLabel = new QLabel(aText, aFirstWgt);
118   aLabel->setWordWrap(true);
119   QString aTooltip = translate(theData->getProperty("tooltip"));
120   aLabel->setToolTip(aTooltip);
121   aLabel->setIndent(5);
122
123   aLayout = new QVBoxLayout(aFirstWgt);
124   ModuleBase_Tools::zeroMargins(aLayout);
125   aLayout->addWidget(mySizeOfViewWidget);
126   aLayout->addWidget(aLabel);
127
128   myRemoveExternal = new QCheckBox(tr("Remove external dependencies"), aFirstWgt);
129   myRemoveExternal->setChecked(false);
130   aLayout->addWidget(myRemoveExternal);
131   myRemoveExternal->setVisible(false);
132
133   aLayout->addStretch(1);
134
135   myStackWidget->addWidget(aFirstWgt);
136
137   // Define widget for sketch manmagement
138   QWidget* aSecondWgt = new QWidget(this);
139   aLayout = new QVBoxLayout(aSecondWgt);
140   ModuleBase_Tools::zeroMargins(aLayout);
141
142   QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
143   QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
144
145   myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
146   aViewLayout->addWidget(myViewInverted);
147
148   QPushButton* aSetViewBtn =
149     new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
150   connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
151   aViewLayout->addWidget(aSetViewBtn);
152
153   aLayout->addWidget(aViewBox);
154
155   QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
156   aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
157   aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
158   aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
159
160   QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
161                                                         aLast = aStates.end();
162   for (; anIt != aLast; anIt++) {
163     QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
164     connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
165     aLayout->addWidget(aShowConstraints);
166
167     PartSet_Tools::ConstraintVisibleState aState = anIt.key();
168     myShowConstraints[aState] = aShowConstraints;
169
170     if (toShowConstraints.contains(aState))
171       aShowConstraints->setChecked(toShowConstraints[aState]);
172   }
173   myShowPoints = new QCheckBox(tr("Show free points"), this);
174   connect(myShowPoints, SIGNAL(toggled(bool)), this, SIGNAL(showFreePoints(bool)));
175   aLayout->addWidget(myShowPoints);
176
177   myAutoConstraints = new QCheckBox(tr("Automatic constraints"), this);
178   myAutoConstraints->setToolTip(tr("Automatic vertical and horizontal constraints"));
179   connect(myAutoConstraints, SIGNAL(toggled(bool)), this, SIGNAL(autoConstraints(bool)));
180   aLayout->addWidget(myAutoConstraints);
181
182   QPushButton* aPlaneBtn = new QPushButton(tr("Change sketch plane"), aSecondWgt);
183   connect(aPlaneBtn, SIGNAL(clicked(bool)), SLOT(onChangePlane()));
184   aLayout->addWidget(aPlaneBtn);
185
186   aLayout->addSpacing(15);
187
188   myDoFLabel = new QLabel("", aSecondWgt);
189   aLayout->addWidget(myDoFLabel);
190
191   myShowDOFBtn = new QPushButton(tr("Show remaining DoFs"), aSecondWgt);
192   aLayout->addWidget(myShowDOFBtn);
193   myShowDOFBtn->setEnabled(false);
194   connect(myShowDOFBtn, SIGNAL(clicked(bool)), SLOT(onShowDOF()));
195
196   myStackWidget->addWidget(aSecondWgt);
197   //setLayout(aLayout);
198
199   myPreviewPlanes = new PartSet_PreviewPlanes();
200 }
201
202 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
203 {
204 }
205
206 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
207                                              const bool theToValidate)
208 {
209   // do not use the given selection if the plane of the sketch has been already set.
210   // If this check is absent, a selected plane in the viewer can be set in the sketch
211   // even if the sketch is built on another plane.
212   if (plane().get())
213     return true;
214
215   ModuleBase_ViewerPrsPtr aPrs = theValues.first();
216   bool aDone = setSelectionInternal(theValues, theToValidate);
217   if (aDone)
218     updateByPlaneSelected(aPrs);
219   return aDone;
220 }
221
222 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
223 {
224   QList<QWidget*> aResult;
225   aResult << myStackWidget;
226   return aResult;
227 }
228
229 bool PartSet_WidgetSketchLabel::processSelection()
230 {
231   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
232   if (aPlane.get())
233     return false;
234
235   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
236
237   if (aSelected.empty())
238     return false;
239   ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
240   bool aDone = setSelectionInternal(aSelected, false);
241   if (aDone) {
242     updateByPlaneSelected(aPrs);
243     updateObject(myFeature);
244   }
245
246   return aDone;
247 }
248
249 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
250 {
251   QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
252
253   QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
254                           anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
255
256   PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
257   bool aFound = false;
258   for (; anIt != aLast && !aFound; anIt++) {
259     aFound = anIt.value() == aSenderCheckBox;
260     if (aFound)
261       aState = anIt.key();
262   }
263   if (aFound)
264     emit showConstraintToggled(aState, theOn);
265 }
266
267 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
268                                                const bool& theToBlock, bool& isFlushesActived,
269                                                bool& isAttributeSetInitializedBlocked,
270                                                bool& isAttributeSendUpdatedBlocked)
271 {
272   ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
273                                              isAttributeSetInitializedBlocked,
274                                              isAttributeSendUpdatedBlocked);
275   // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
276   // attributes. It it is necessary, these states should be append to the method attributes
277   // or stored in the widget
278
279   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
280   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
281   QStringList aValues;
282   for(; anIt != aLast; anIt++) {
283     AttributePtr anAttribute = *anIt;
284     if (theToBlock)
285       anAttribute->blockSetInitialized(true);
286     else
287       anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
288   }
289 }
290
291 bool PartSet_WidgetSketchLabel::setSelectionInternal(
292                                           const QList<ModuleBase_ViewerPrsPtr>& theValues,
293                                           const bool theToValidate)
294 {
295   bool aDone = false;
296   if (theValues.empty()) {
297     // In order to make reselection possible, set empty object and shape should be done
298     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(
299                               new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
300     aDone = false;
301   }
302   else {
303     // it removes the processed value from the parameters list
304     ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
305     if (!theToValidate || isValidInFilters(aValue)) {
306       myIsSelection = true;
307       aDone = setSelectionCustom(aValue);
308       myIsSelection = false;
309     }
310   }
311
312   return aDone;
313 }
314
315 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
316 {
317   GeomPlanePtr aPlane = plane();
318   if (!aPlane.get())
319     return;
320   // 1. hide main planes if they have been displayed and display sketch preview plane
321   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
322
323   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
324   if (aModule) {
325     CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
326     bool isSetSizeOfView = false;
327     double aSizeOfView = 0;
328     QString aSizeOfViewStr = mySizeOfView->text();
329     if (!aSizeOfViewStr.isEmpty()) {
330       aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
331       if (isSetSizeOfView && aSizeOfView <= 0) {
332         isSetSizeOfView = false;
333       }
334     }
335     aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, isSetSizeOfView);
336     aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
337   }
338   // 2. if the planes were displayed, change the view projection
339
340   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
341   gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
342   double aTwist = 0.0;
343
344   // Rotate view if the sketcher plane is selected only from preview planes
345   // Preview planes are created only if there is no any shape
346   bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane");
347   if (aRotate) {
348     myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
349   }
350   QString aSizeOfViewStr = mySizeOfView->text();
351   if (!aSizeOfViewStr.isEmpty()) {
352     bool isOk;
353     double aSizeOfView = aSizeOfViewStr.toDouble(&isOk);
354     if (isOk && aSizeOfView > 0) {
355       Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
356       if (!aView3d.IsNull()) {
357         Bnd_Box aBndBox;
358         double aHalfSize = aSizeOfView/2.0;
359         aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
360         aView3d->FitAll(aBndBox, 0.01, false);
361       }
362     }
363   }
364   if (myOpenTransaction) {
365     SessionPtr aMgr = ModelAPI_Session::get();
366     aMgr->finishOperation();
367     myOpenTransaction = false;
368   }
369   // 3. Clear text in the label
370   myStackWidget->setCurrentIndex(1);
371   //myLabel->setText("");
372   //myLabel->setToolTip("");
373   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
374
375   // 5. Clear selection mode and define sketching mode
376   emit planeSelected(plane());
377   // after the plane is selected in the sketch, the sketch selection should be activated
378   // it can not be performed in the sketch label widget because, we don't need to switch off
379   // the selection by any label deactivation, but need to switch it off by stop the sketch
380   myWorkshop->selectionActivate()->updateSelectionFilters();
381   myWorkshop->selectionActivate()->updateSelectionModes();
382
383   if (aModule)
384     aModule->onViewTransformed();
385
386   // 6. Update sketcher actions
387   XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
388
389   myWorkshop->updateCommandStatus();
390   aWorkshop->selector()->clearSelection();
391   myWorkshop->viewer()->update();
392
393   myRemoveExternal->setVisible(false);
394 }
395
396 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
397 {
398   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
399   return PartSet_Tools::sketchPlane(aSketch);
400 }
401
402 bool PartSet_WidgetSketchLabel::focusTo()
403 {
404   ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
405   return true;
406 }
407
408 void PartSet_WidgetSketchLabel::enableFocusProcessing()
409 {
410   myStackWidget->installEventFilter(this);
411 }
412
413 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
414 {
415   ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
416 }
417
418 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
419                                                       const bool theValid)
420 {
421   ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
422
423   // it is not necessary to save the previous plane value because the plane is chosen once
424   DataPtr aData = feature()->data();
425   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
426     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
427   if (aSelAttr) {
428     ResultPtr anEmptyResult;
429     GeomShapePtr anEmptyShape;
430     aSelAttr->setValue(anEmptyResult, anEmptyShape);
431   }
432 }
433
434 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
435 {
436   if (myIsSelection && myRemoveExternal->isVisible()) {
437     if (myRemoveExternal->isChecked()) {
438       myFeature->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
439     }
440   }
441   return fillSketchPlaneBySelection(thePrs);
442 }
443
444 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
445 {
446   bool aCanFillSketch = true;
447   // avoid any selection on sketch object
448   ObjectPtr anObject = thePrs->object();
449   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
450   if (aResult.get()) {
451     FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
452     if (aFeature->getKind() == SketchPlugin_Sketch::ID())
453       aCanFillSketch = false;
454   }
455   // check plane or planar face of any non-sketch object
456   if (aCanFillSketch) {
457     std::shared_ptr<GeomAPI_Face> aGeomFace;
458
459     GeomShapePtr aGeomShape = thePrs->shape();
460     if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
461       aGeomShape = aResult->shape();
462     }
463
464     if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
465       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
466       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
467     }
468     else
469       aCanFillSketch = false;
470   }
471   return aCanFillSketch;
472 }
473
474 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_ViewerPrsPtr& thePrs)
475 {
476   bool isOwnerSet = false;
477
478   const GeomShapePtr& aShape = thePrs->shape();
479   std::shared_ptr<GeomAPI_Dir> aDir;
480
481   if (aShape.get() && !aShape->isNull()) {
482     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
483     aDir = setSketchPlane(aTDShape);
484     isOwnerSet = aDir.get();
485   }
486   if (thePrs->object() && (feature() != thePrs->object())) {
487     FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
488     DataPtr aData = feature()->data();
489     AttributeSelectionPtr aSelAttr =
490       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
491       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
492     if (aSelAttr.get()) {
493       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
494       if (aRes.get()) {
495         GeomShapePtr aShapePtr;
496         if (!aShape.get() || aShape->isNull()) {  // selection happens in the OCC viewer
497           aShapePtr = ModelAPI_Tools::shape(aRes);
498         }
499         else { // selection happens in OB browser
500           aShapePtr = aShape;
501         }
502         if (aShapePtr.get() && aShapePtr->isFace()) {
503           const TopoDS_Shape& aTDShape = aShapePtr->impl<TopoDS_Shape>();
504           setSketchPlane(aTDShape);
505           aSelAttr->setValue(aRes, aShapePtr);
506           isOwnerSet = true;
507         }
508       }
509       else {
510         aSelAttr->setValue(aFeature, GeomShapePtr());
511         GeomShapePtr aShape = aSelAttr->value();
512         if (!aShape.get() && aSelAttr->contextFeature().get() &&
513           aSelAttr->contextFeature()->firstResult().get()) {
514           aShape = aSelAttr->contextFeature()->firstResult()->shape();
515         }
516         if (aShape.get() && aShape->isPlanar()) {
517           const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
518           setSketchPlane(aTDShape);
519           isOwnerSet = true;
520         }
521       }
522     }
523   }
524   return isOwnerSet;
525 }
526
527 void PartSet_WidgetSketchLabel::activateCustom()
528 {
529   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
530   if (aModule) {
531     bool isBlocked = myAutoConstraints->blockSignals(true);
532     myAutoConstraints->setChecked(aModule->sketchReentranceMgr()->isAutoConstraints());
533     myAutoConstraints->blockSignals(isBlocked);
534   }
535
536   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
537   if (aPlane.get()) {
538     myStackWidget->setCurrentIndex(1);
539     return;
540   }
541
542   myStackWidget->setCurrentIndex(0);
543   bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
544
545   // Clear previous selection mode It is necessary for correct activation of preview planes
546   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
547   XGUI_Displayer* aDisp = aWorkshop->displayer();
548   aWorkshop->selectionActivate()->activateObjects(QIntList(), aDisp->displayedObjects(), false);
549
550   if (!aBodyIsVisualized) {
551     // We have to select a plane before any operation
552     myPreviewPlanes->showPreviewPlanes(myWorkshop);
553     mySizeOfViewWidget->setVisible(true);
554   }
555   else
556     mySizeOfViewWidget->setVisible(false);
557 }
558
559 void PartSet_WidgetSketchLabel::deactivate()
560 {
561   ModuleBase_WidgetValidated::deactivate();
562   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
563   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
564
565   if (aHidePreview)
566     myWorkshop->viewer()->update();
567
568   if (myOpenTransaction) {
569     SessionPtr aMgr = ModelAPI_Session::get();
570     aMgr->finishOperation();
571     myOpenTransaction = false;
572   }
573 }
574
575 void PartSet_WidgetSketchLabel::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
576 {
577   theModuleSelectionModes = -1;
578   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
579   if (!aPlane.get())
580     theModes << TopAbs_FACE;
581 }
582
583 void PartSet_WidgetSketchLabel::selectionFilters(QIntList& theModuleSelectionFilters,
584                                                  SelectMgr_ListOfFilter& theSelectionFilters)
585 {
586   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
587   if (aPlane.get())
588     return;
589   return ModuleBase_WidgetValidated::selectionFilters(theModuleSelectionFilters,
590                                                       theSelectionFilters);
591 }
592
593 std::shared_ptr<GeomAPI_Dir>
594   PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
595 {
596   if (theShape.IsNull())
597     return std::shared_ptr<GeomAPI_Dir>();
598
599   // get selected shape
600   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
601   aGShape->setImpl(new TopoDS_Shape(theShape));
602
603   // get plane parameters
604   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
605   std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
606   if (!aPlane.get())
607     return std::shared_ptr<GeomAPI_Dir>();
608   return setSketchPlane(aPlane);
609 }
610
611 std::shared_ptr<GeomAPI_Dir>
612   PartSet_WidgetSketchLabel::setSketchPlane(std::shared_ptr<GeomAPI_Pln> thePlane)
613 {
614   // set plane parameters to feature
615   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
616   double anA, aB, aC, aD;
617   thePlane->coefficients(anA, aB, aC, aD);
618
619   // calculate attributes of the sketch
620   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
621   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
622   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
623   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
624   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
625   // X axis is preferable to be dirX on the sketch
626   const double tol = Precision::Confusion();
627   bool isX = fabs(fabs(anA) - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
628   std::shared_ptr<GeomAPI_Dir> aTempDir(
629       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
630   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
631   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
632
633   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
634       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
635   anOrigin->setValue(anOrigPnt);
636   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
637       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
638   aNormal->setValue(aNormDir);
639   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
640       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
641   aDirX->setValue(aXDir);
642   std::shared_ptr<GeomAPI_Dir> aDir = thePlane->direction();
643   return aDir;
644 }
645
646 void PartSet_WidgetSketchLabel::onSetPlaneView()
647 {
648   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
649   if (aPlane.get()) {
650     std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
651     gp_Dir aDir = aDirection->impl<gp_Dir>();
652     if (myViewInverted->isChecked())
653       aDir.Reverse();
654     myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
655     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
656     if (aModule)
657       aModule->onViewTransformed();
658   }
659 }
660
661
662 //******************************************************
663 QList<std::shared_ptr<ModuleBase_ViewerPrs>> PartSet_WidgetSketchLabel::findCircularEdgesInPlane()
664 {
665   QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
666   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
667   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
668   QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
669
670   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
671   foreach(ObjectPtr aObj, aDispObjects) {
672     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
673     if (aResObj.get()) {
674       GeomShapePtr aShape = aResObj->shape();
675       if (aShape.get()) {
676         GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
677         for(; aExplorer.more(); aExplorer.next()) {
678           GeomShapePtr aEdgeShape = aExplorer.current();
679           GeomAPI_Edge anEdge(aEdgeShape);
680           if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
681                anEdge.isInPlane(aPlane)) {
682             bool isContains = false;
683             // Check that edge is not used.
684             // It is possible that the same edge will be taken from different faces
685             foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
686               GeomAPI_Edge aUsedEdge(aPrs->shape());
687               if (aUsedEdge.isEqual(aEdgeShape)) {
688                 isContains = true;
689                 break;
690               }
691             }
692             if (!isContains) {
693               std::shared_ptr<ModuleBase_ViewerPrs>
694                 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
695               aResult.append(aPrs);
696             }
697           }
698         }
699       }
700     }
701   }
702   return aResult;
703 }
704
705 //******************************************************
706 void PartSet_WidgetSketchLabel::onChangePlane()
707 {
708   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
709   if (aModule) {
710     mySizeOfViewWidget->setVisible(false);
711     myRemoveExternal->setVisible(true);
712     myStackWidget->setCurrentIndex(0);
713
714     bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
715
716     if (!aBodyIsVisualized) {
717       // We have to select a plane before any operation
718       myPreviewPlanes->showPreviewPlanes(myWorkshop);
719     }
720
721     CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
722     PartSet_Tools::nullifySketchPlane(aSketch);
723
724     Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_SketchPlaneFilter);
725     if (!aFilter.IsNull()) {
726       std::shared_ptr<GeomAPI_Pln> aPln;
727       Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(aPln);
728     }
729     XGUI_Workshop* aWorkshop = aModule->getWorkshop();
730
731     aWorkshop->selectionActivate()->updateSelectionFilters();
732     aWorkshop->selectionActivate()->updateSelectionModes();
733
734     SessionPtr aMgr = ModelAPI_Session::get();
735     aMgr->startOperation("Change Sketch plane");
736     myOpenTransaction = true;
737   }
738 }
739
740 void PartSet_WidgetSketchLabel::setShowPointsState(bool theState)
741 {
742   bool aBlock = myShowPoints->blockSignals(true);
743   myShowPoints->setChecked(theState);
744   myShowPoints->blockSignals(aBlock);
745 }
746
747 bool PartSet_WidgetSketchLabel::restoreValueCustom()
748 {
749   if (myFeature.get()) {
750     CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
751     if (aSketch.get() && (aSketch->numberOfSubs() > 0)) {
752       AttributeStringPtr aDOFStr = aSketch->string("SolverDOF");
753       if (aDOFStr.get()) {
754         QString aVal(aDOFStr->value().c_str());
755         if (aVal.contains('=')) {
756           // to support old data
757           aVal = aVal.right(aVal.length() - aVal.lastIndexOf('='));
758         }
759         int aDoF = aVal.toInt();
760         if (aDoF == 0) {
761           myDoFLabel->setText(tr("Sketch is fully fixed (DoF = 0)"));
762           myShowDOFBtn->setEnabled(false);
763         } else {
764           myDoFLabel->setText(tr("DoF (degrees of freedom) = ") + aVal);
765           myShowDOFBtn->setEnabled(true);
766         }
767       }
768     }
769     else {
770       myDoFLabel->setText("");
771       myShowDOFBtn->setEnabled(false);
772     }
773   }
774   return true;
775 }
776
777
778 void PartSet_WidgetSketchLabel::onShowDOF()
779 {
780   CompositeFeaturePtr aCompFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
781   if (aCompFeature.get()) {
782     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_GET_DOF_OBJECTS);
783     ModelAPI_EventCreator::get()->sendUpdated(aCompFeature, anEvent);
784     Events_Loop::loop()->flush(anEvent);
785   }
786 }