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