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