Salome HOME
Updated copyright comment
[modules/geom.git] / src / GenerationGUI / GenerationGUI_ThicknessDlg.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GenerationGUI_ThicknessDlg.cxx
25 // Author : Sergey KHROMOV, Open CASCADE S.A.S.
26
27 #include "GenerationGUI_ThicknessDlg.h"
28
29 #include <GEOMBase.h>
30 #include <GeometryGUI.h>
31
32 #include <LightApp_SelectionMgr.h>
33 #include <SalomeApp_Application.h>
34 #include <SalomeApp_DoubleSpinBox.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37
38 #include <QApplication>
39 #include <QCheckBox>
40 #include <QGridLayout>
41 #include <QGroupBox>
42 #include <QLabel>
43 #include <QLineEdit>
44 #include <QPushButton>
45 #include <QRadioButton>
46
47 #include <TopExp.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <Precision.hxx>
50
51 #define GEOM_SOLID_THICK     0
52 #define GEOM_SOLID_HOLLOWED  1
53
54 //==============================================================================
55 // class    : GenerationGUI_ThicknessDlg()
56 // purpose  : 
57 //==============================================================================
58 GenerationGUI_ThicknessDlg::GenerationGUI_ThicknessDlg
59                             (GeometryGUI* theGeometryGUI, QWidget* parent,
60                              bool modal, Qt::WindowFlags fl)
61   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
62     myFacesLbl       (0),
63     myObjSelBtn      (0),
64     myFacesSelBtn    (0),
65     myObjEdit        (0),
66     myFacesEdit      (0),
67     myThicknessSpin  (0),
68     myInsideCheck    (0)
69 {
70   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
71   QPixmap image0(aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
72   QPixmap image1(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_THICKNESS")));
73   QPixmap image2
74     (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_THICKNESS_HOLLOWED")));
75
76   setWindowTitle(tr("GEOM_THICKNESS_TITLE"));
77
78   /***************************************************************/
79   mainFrame()->GroupConstructors->setTitle(tr("GEOM_THICKNESS"));
80   mainFrame()->RadioButton1->setIcon(image1);
81   mainFrame()->RadioButton2->setIcon(image2);
82
83   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
84   mainFrame()->RadioButton3->close();
85
86   mainFrame()->RadioButton1->setChecked(true);
87
88   QGroupBox   *aMainGrp      = new QGroupBox (tr("GEOM_ARGUMENTS"), this);
89   QGridLayout *aParamsLayout = new QGridLayout(aMainGrp);
90   QLabel      *anObjLbl      = new QLabel (tr("GEOM_OBJECT"), aMainGrp);
91   QLabel      *aThicknessLbl = new QLabel (tr("GEOM_THICKNESS"), aMainGrp);
92   
93   myFacesLbl      = new QLabel (tr("GEOM_FACES"), aMainGrp);
94   myObjSelBtn     = new QPushButton(aMainGrp);
95   myFacesSelBtn   = new QPushButton(aMainGrp);
96   myObjEdit       = new QLineEdit(aMainGrp);
97   myFacesEdit     = new QLineEdit(aMainGrp);
98   myThicknessSpin = new SalomeApp_DoubleSpinBox(aMainGrp);
99   myInsideCheck   = new QCheckBox(tr("GEOM_TOWARDS_INSIDE"), aMainGrp);
100
101   myObjSelBtn->setIcon(image0);
102   myFacesSelBtn->setIcon(image0);
103   myObjEdit->setReadOnly(true);
104   myObjEdit->setSizePolicy
105     (QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
106   myFacesEdit->setReadOnly(true);
107   myFacesEdit->setSizePolicy
108     (QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
109
110   aParamsLayout->setMargin(9);
111   aParamsLayout->setSpacing(6);
112   aParamsLayout->addWidget(anObjLbl,        0, 0);
113   aParamsLayout->addWidget(myObjSelBtn,     0, 1);
114   aParamsLayout->addWidget(myObjEdit,       0, 2);
115   aParamsLayout->addWidget(myFacesLbl,      1, 0);
116   aParamsLayout->addWidget(myFacesSelBtn,   1, 1);
117   aParamsLayout->addWidget(myFacesEdit,     1, 2);
118   aParamsLayout->addWidget(aThicknessLbl,   2, 0);
119   aParamsLayout->addWidget(myThicknessSpin, 2, 1, 1, 2);
120   aParamsLayout->addWidget(myInsideCheck,   3, 0, 1, 3);
121
122   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
123
124   layout->setMargin(0);
125   layout->setSpacing(6);
126   layout->addWidget(aMainGrp);
127
128   myHelpFileName = "create_thickness_page.html";
129
130   /* Initialisation */
131   Init();
132 }
133
134 //==============================================================================
135 // function : ~GenerationGUI_ThicknessDlg()
136 // purpose  : 
137 //==============================================================================
138 GenerationGUI_ThicknessDlg::~GenerationGUI_ThicknessDlg()
139 {
140   // no need to delete child widgets, Qt does it all for us
141 }
142
143 //==============================================================================
144 // function : Init()
145 // purpose  :
146 //==============================================================================
147 void GenerationGUI_ThicknessDlg::Init()
148 {
149   // Get setting of step value from file configuration
150   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
151   double aStep = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
152   double aThickness    = 10.;
153   double aThicknessMin = Precision::Confusion() * 10.0;
154
155   initSpinBox(myThicknessSpin, aThicknessMin,
156               COORD_MAX, aStep, "length_precision");
157   myThicknessSpin->setValue(aThickness);
158   myObject.nullify();
159   myFaces.clear();
160
161   showOnlyPreviewControl();
162     
163   // signals and slots connections
164   connect(buttonOk(),      SIGNAL(clicked()), this, SLOT(ClickOnOk()));
165   connect(buttonApply(),   SIGNAL(clicked()), this, SLOT(ClickOnApply()));
166   connect(this,            SIGNAL(constructorsClicked(int)),
167           this,            SLOT(ConstructorsClicked(int)));
168   connect(myObjSelBtn,     SIGNAL(clicked()),
169           this,            SLOT(SetEditCurrentArgument()));
170   connect(myFacesSelBtn,   SIGNAL(clicked()),
171           this,            SLOT(SetEditCurrentArgument()));
172   connect(myThicknessSpin, SIGNAL(valueChanged(double)),
173           this,            SLOT(ValueChangedInSpinBox()));
174   connect(myGeomGUI,       SIGNAL(SignalDefaultStepValueChanged(double)),
175           this,            SLOT(SetDoubleSpinBoxStep(double)));
176   connect(myInsideCheck,   SIGNAL(toggled(bool)),
177           this,            SLOT(onChangeDirection(bool)));
178
179   connect(myGeomGUI->getApp()->selectionMgr(),
180           SIGNAL(currentSelectionChanged()),
181           this, SLOT(SelectionIntoArgument()));
182
183   initName(tr("GEOM_THICKNESS"));
184
185   ConstructorsClicked(0);
186 }
187
188 //==============================================================================
189 // function : SetDoubleSpinBoxStep()
190 // purpose  : Double spin box management
191 //==============================================================================
192 void GenerationGUI_ThicknessDlg::SetDoubleSpinBoxStep (double step)
193 {
194   myThicknessSpin->setSingleStep(step);
195 }
196
197 //==============================================================================
198 // function : ConstructorsClicked()
199 // purpose  : Radio button management
200 //==============================================================================
201 void GenerationGUI_ThicknessDlg::ConstructorsClicked (int constructorId)
202 {
203   switch (constructorId) {
204   case GEOM_SOLID_THICK:
205     myObjSelBtn->setDown(false);
206     myFacesLbl->hide();
207     myFacesSelBtn->hide();
208     myFacesEdit->hide();
209     myFaces.clear();
210     break;
211   case GEOM_SOLID_HOLLOWED:
212     myObjSelBtn->setDown(true);
213     myFacesLbl->show();
214     myFacesSelBtn->show();
215     myFacesEdit->show();
216     break;
217   default:
218     break;
219   }
220
221   myEditCurrentArgument = myObjEdit;
222   qApp->processEvents();
223   updateGeometry();
224   resize(minimumSizeHint());
225
226   SelectionIntoArgument();
227 }
228
229 //==============================================================================
230 // function : ClickOnOk()
231 // purpose  :
232 //==============================================================================
233 void GenerationGUI_ThicknessDlg::ClickOnOk()
234 {
235   setIsApplyAndClose(true);
236
237   if (ClickOnApply()) {
238     ClickOnCancel();
239   }
240 }
241
242 //==============================================================================
243 // function : ClickOnApply()
244 // purpose  :
245 //==============================================================================
246 bool GenerationGUI_ThicknessDlg::ClickOnApply()
247 {
248   if (!onAccept()) {
249     return false;
250   }
251
252   initName();
253
254   // activate selection and connect selection manager
255   ConstructorsClicked(getConstructorId());
256
257   return true;
258 }
259
260 //==============================================================================
261 // function : SelectionIntoArgument()
262 // purpose  : Called when selection is changed or on dialog initialization or
263 //            activation
264 //==============================================================================
265 void GenerationGUI_ThicknessDlg::SelectionIntoArgument()
266 {
267   erasePreview();
268   myEditCurrentArgument->setText("");
269
270   if (myEditCurrentArgument == myObjEdit) {
271     QList<TopAbs_ShapeEnum> aTypes;
272     const int               anID = getConstructorId();
273
274     if (anID == GEOM_SOLID_THICK) {
275       aTypes << TopAbs_SHELL << TopAbs_FACE;
276     } else if (anID == GEOM_SOLID_HOLLOWED) {
277       aTypes << TopAbs_SOLID;
278     }
279
280     myObject = getSelected(aTypes);
281
282     if (myObject) {
283       QString aName = GEOMBase::GetName(myObject.get());
284
285       myEditCurrentArgument->setText(aName);
286
287       if (anID == GEOM_SOLID_HOLLOWED && myFaces.empty()) {
288         myFacesSelBtn->click();
289       }
290     }
291   } else if (myEditCurrentArgument == myFacesEdit) {
292     myFaces.clear();
293     myFaces = getSelected(TopAbs_FACE, -1);
294
295     int i = myFaces.count();
296
297     if (i == 1) {
298       myEditCurrentArgument->setText(GEOMBase::GetName(myFaces.first().get()));
299     } else if (i > 0) {
300       myEditCurrentArgument->setText
301         (QString::number( i ) + "_" + tr("GEOM_OBJECTS"));
302     }
303   }
304
305   processPreview();
306 }
307
308 //==============================================================================
309 // function : SetEditCurrentArgument()
310 // purpose  :
311 //==============================================================================
312 void GenerationGUI_ThicknessDlg::SetEditCurrentArgument()
313 {
314   QPushButton* send = (QPushButton*)sender();
315
316   if (send == myObjSelBtn) {
317     myFacesSelBtn->setDown(false);
318     myEditCurrentArgument = myObjEdit;
319     globalSelection(GEOM_ALLSHAPES);
320   } else if (send == myFacesSelBtn) {
321     myObjSelBtn->setDown(false);
322     myEditCurrentArgument = myFacesEdit;
323     globalSelection(); // close local contexts, if any
324     localSelection(myObject.get(), TopAbs_FACE);
325   }
326
327   myEditCurrentArgument->setFocus();
328
329   const int anID = getConstructorId();
330
331   // after setFocus(), because it will be setDown(false) when loses focus
332   send->setDown(anID == GEOM_SOLID_HOLLOWED);
333
334   // seems we need it only to avoid preview disappearing, caused by selection
335   // mode change
336   processPreview();
337 }
338
339 //==============================================================================
340 // function : ActivateThisDialog()
341 // purpose  :
342 //==============================================================================
343 void GenerationGUI_ThicknessDlg::ActivateThisDialog()
344 {
345   GEOMBase_Skeleton::ActivateThisDialog();
346
347   connect(myGeomGUI->getApp()->selectionMgr(),
348           SIGNAL(currentSelectionChanged()),
349           this, SLOT(SelectionIntoArgument()));
350
351   ConstructorsClicked(getConstructorId());
352 }
353
354 //==============================================================================
355 // function : enterEvent()
356 // purpose  : when mouse enter onto the QWidget
357 //==============================================================================
358 void GenerationGUI_ThicknessDlg::enterEvent (QEvent*)
359 {
360   if (!mainFrame()->GroupConstructors->isEnabled())
361     ActivateThisDialog();
362 }
363
364 //==============================================================================
365 // function : ValueChangedInSpinBox()
366 // purpose  :
367 //==============================================================================
368 void GenerationGUI_ThicknessDlg::ValueChangedInSpinBox()
369 {
370   processPreview();
371 }
372
373 //==============================================================================
374 // function : createOperation
375 // purpose  :
376 //==============================================================================
377 GEOM::GEOM_IOperations_ptr GenerationGUI_ThicknessDlg::createOperation()
378 {
379   return getGeomEngine()->GetI3DPrimOperations();
380 }
381
382 //==============================================================================
383 // function : isValid
384 // purpose  :
385 //==============================================================================
386 bool GenerationGUI_ThicknessDlg::isValid (QString& msg)
387 {
388   bool isOk = false;
389
390   switch (getConstructorId()) {
391   case GEOM_SOLID_THICK:
392     isOk = myThicknessSpin->isValid(msg, !IsPreview()) && myObject;
393     break;
394   case GEOM_SOLID_HOLLOWED:
395     isOk = myThicknessSpin->isValid(msg, !IsPreview()) &&
396            myObject && !myFaces.empty();
397     break;
398   default:
399     break;
400   }
401
402   return isOk;
403 }
404
405 //==============================================================================
406 // function : onChangeDirection(bool)
407 // purpose  :
408 //==============================================================================
409 void GenerationGUI_ThicknessDlg::onChangeDirection(bool)
410 {
411   processPreview();
412 }
413
414 //==============================================================================
415 // function : execute
416 // purpose  :
417 //==============================================================================
418 bool GenerationGUI_ThicknessDlg::execute (ObjectList& objects)
419 {
420   GEOM::GEOM_Object_var            anObj;
421   GEOM::GEOM_I3DPrimOperations_var anOper       =
422     GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
423   double                           aThickness   = myThicknessSpin->value();
424   bool                             anInside = myInsideCheck->isChecked();
425   GEOM::ListOfLong_var             anObjIDsList = new GEOM::ListOfLong();
426   TopoDS_Shape                     aShape;
427
428   if (GEOMBase::GetShape(myObject.get(), aShape)) {
429     TopTools_IndexedMapOfShape aMainMap;
430     QList<int>                 aListIDs;
431
432     TopExp::MapShapes(aShape, aMainMap);
433
434     for (int i = 0; i < myFaces.count(); i++) {
435       TopoDS_Shape aFace;
436
437       if (GEOMBase::GetShape(myFaces[i].get(), aFace)) {
438         int anIndex = aMainMap.FindIndex(aFace);
439
440         if (anIndex >= 0) {
441           aListIDs << anIndex;
442         }
443       }
444     }
445
446
447     if (!aListIDs.empty()) {
448       anObjIDsList->length(aListIDs.length());
449
450       for (int i = 0; i < aListIDs.length(); i++) {
451         anObjIDsList[i] = aListIDs[i];
452       }
453     }
454   }
455
456   anObj = anOper->MakeThickening
457     (myObject.get(), anObjIDsList.in(), aThickness, true, anInside);
458     
459   if (!anObj->_is_nil()) {
460     if (!IsPreview()) {
461       QStringList aParameters;
462       aParameters << myThicknessSpin->text();
463       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
464     }
465     objects.push_back(anObj._retn());
466   }
467
468   return true;
469 }
470
471 //=================================================================================
472 // function : getSourceObjects
473 // purpose  : virtual method to get source objects
474 //=================================================================================
475 QList<GEOM::GeomObjPtr> GenerationGUI_ThicknessDlg::getSourceObjects()
476 {
477   QList<GEOM::GeomObjPtr> res(myFaces);
478   res << myObject;
479   return res;
480 }