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