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