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