Salome HOME
b06a08f8acf082ce8cf2c2d425f1748deb1e5e11
[modules/geom.git] / src / GenerationGUI / GenerationGUI_FillingDlg.cxx
1 // Copyright (C) 2007-2023  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_FillingDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26
27 #include "GenerationGUI_FillingDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <TColStd_MapOfInteger.hxx>
39
40 #include <GEOMImpl_Types.hxx>
41
42 //=================================================================================
43 // class    : GenerationGUI_FillingDlg()
44 // purpose  : Constructs a GenerationGUI_FillingDlg which is a child of 'parent', with the
45 //            name 'name' and widget flags set to 'f'.
46 //            The dialog will by default be modeless, unless you set 'modal' to
47 //            TRUE to construct a modal dialog.
48 //=================================================================================
49 GenerationGUI_FillingDlg::GenerationGUI_FillingDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
50                                                     bool modal, Qt::WindowFlags fl)
51   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
52 {
53   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FILLING")));
54   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
55
56   setWindowTitle(tr("GEOM_FILLING_TITLE"));
57
58   /***************************************************************/
59   mainFrame()->GroupConstructors->setTitle(tr ("GEOM_FILLING"));
60   mainFrame()->RadioButton1->setIcon(image0);
61   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
62   mainFrame()->RadioButton2->close();
63   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
64   mainFrame()->RadioButton3->close();
65
66   GroupPoints = new DlgRef_1Sel5Spin1Check(centralWidget());
67   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
68   GroupPoints->TextLabel1->setText(tr("GEOM_FILLING_COMPOUND"));
69   GroupPoints->TextLabel2->setText(tr("GEOM_FILLING_MIN_DEG"));
70   GroupPoints->TextLabel3->setText(tr("GEOM_FILLING_TOL_2D"));
71   GroupPoints->TextLabel4->setText(tr("GEOM_FILLING_NB_ITER"));
72   GroupPoints->TextLabel5->setText(tr("GEOM_FILLING_MAX_DEG"));
73   GroupPoints->TextLabel6->setText(tr("GEOM_FILLING_TOL_3D"));
74   GroupPoints->CheckBox1->setText(tr("GEOM_FILLING_APPROX"));
75   GroupPoints->TextLabel7->setText(tr("GEOM_FILLING_METHOD"));
76   GroupPoints->PushButton1->setIcon(image1);
77   GroupPoints->LineEdit1->setReadOnly(true);
78
79   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
80   layout->setMargin(0); layout->setSpacing(6);
81   layout->addWidget(GroupPoints);
82   /***************************************************************/
83
84   setHelpFileName("create_filling_page.html");
85
86   /* Initialisations */
87   Init();
88 }
89
90 //=================================================================================
91 // function : ~GenerationGUI_FillingDlg()
92 // purpose  : Destroys the object and frees any allocated resources
93 //=================================================================================
94 GenerationGUI_FillingDlg::~GenerationGUI_FillingDlg()
95 {
96   // no need to delete child widgets, Qt does it all for us
97 }
98
99 //=================================================================================
100 // function : Init()
101 // purpose  :
102 //=================================================================================
103 void GenerationGUI_FillingDlg::Init()
104 {
105   /* init variables */
106   myEditCurrentArgument = GroupPoints->LineEdit1;
107   GroupPoints->LineEdit1->setReadOnly(true);
108
109   initSelection();
110
111   int SpecificStep1 = 1;
112   double SpecificStep2 = 0.0001;
113   /* min, max, step and decimals for spin boxes & initial values */
114   initSpinBox(GroupPoints->SpinBox1, 2, MAX_NUMBER,  SpecificStep1);
115   initSpinBox(GroupPoints->SpinBox2, 0.00001, 10000.0, SpecificStep2, "len_tol_precision");
116   initSpinBox(GroupPoints->SpinBox3, 0, MAX_NUMBER,  SpecificStep1);
117   initSpinBox(GroupPoints->SpinBox4, 1, MAX_NUMBER,  SpecificStep1);
118   initSpinBox(GroupPoints->SpinBox5, 0.00001, 10000.0, SpecificStep2, "len_tol_precision");
119
120   GroupPoints->SpinBox1->setValue(2);
121   GroupPoints->SpinBox2->setValue(0.0001);
122   GroupPoints->SpinBox3->setValue(0);
123   GroupPoints->SpinBox4->setValue(5);
124   GroupPoints->SpinBox5->setValue(0.0001);
125
126   GroupPoints->ComboBox1->addItem(tr("GEOM_FILLING_DEFAULT"));
127   GroupPoints->ComboBox1->addItem(tr("GEOM_FILLING_USEORI"));
128   GroupPoints->ComboBox1->addItem(tr("GEOM_FILLING_AUTO"));
129
130   showOnlyPreviewControl();
131
132   /* signals and slots connections */
133   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
134   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
135
136   connect(GroupPoints->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
137
138   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(MethodChanged()));
139
140   connect(GroupPoints->SpinBox1, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
141   connect(GroupPoints->SpinBox2, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
142   connect(GroupPoints->SpinBox3, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
143   connect(GroupPoints->SpinBox4, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
144   connect(GroupPoints->SpinBox5, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
145
146   connect(GroupPoints->CheckBox1, SIGNAL(stateChanged(int)),
147            this, SLOT(ApproxChanged()));
148
149   // san: Commented, as all spin boxes here either are integer or use specific non-default step value
150   //connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
151
152   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
153            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
154
155   initName(tr("GEOM_FILLING"));
156   resize(100,100);
157 }
158
159 //=================================================================================
160 // function : SetDoubleSpinBoxStep()
161 // purpose  : Double spin box management
162 //=================================================================================
163 void GenerationGUI_FillingDlg::SetDoubleSpinBoxStep(double step)
164 {
165   GroupPoints->SpinBox2->setSingleStep(step);
166   GroupPoints->SpinBox5->setSingleStep(step);
167 }
168
169 //=================================================================================
170 // function : ClickOnOk()
171 // purpose  :
172 //=================================================================================
173 void GenerationGUI_FillingDlg::ClickOnOk()
174 {
175   setIsApplyAndClose(true);
176   if (ClickOnApply())
177     ClickOnCancel();
178 }
179
180 //=================================================================================
181 // function : ClickOnApply()
182 // purpose  :
183 //=================================================================================
184 bool GenerationGUI_FillingDlg::ClickOnApply()
185 {
186   if (!onAccept())
187     return false;
188
189   initName();
190   initSelection();
191
192   return true;
193 }
194
195 //=================================================================================
196 // function : initSelection
197 // purpose  :
198 //=================================================================================
199 void GenerationGUI_FillingDlg::initSelection()
200 {
201   TColStd_MapOfInteger aTypes;
202   aTypes.Add( GEOM_EDGE );
203   aTypes.Add( GEOM_WIRE );
204   aTypes.Add( GEOM_COMPOUND );
205
206   std::list<int> needTypes;
207   needTypes.push_back( TopAbs_EDGE );
208   needTypes.push_back( TopAbs_WIRE );
209   needTypes.push_back( TopAbs_COMPOUND );
210
211   globalSelection( aTypes );
212   localSelection( needTypes );
213 }
214
215 //=================================================================================
216 // function : SelectionIntoArgument()
217 // purpose  : Called when selection as changed or other case
218 //=================================================================================
219 void GenerationGUI_FillingDlg::SelectionIntoArgument()
220 {
221   erasePreview();
222   myEditCurrentArgument->setText("");
223
224   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
225     QList<GEOM::GeomObjPtr> objs = getSelected(TopAbs_SHAPE, -1);
226     GEOMBase::Synchronize(myObjects, objs);
227     if ( !myObjects.isEmpty() ) {
228       QString aName = myObjects.count() > 1 ? QString( "%1_objects").arg( myObjects.count() ) : GEOMBase::GetName( myObjects[0].get() );
229       myEditCurrentArgument->setText( aName );
230     }
231   }
232   processPreview();
233 }
234
235 //=================================================================================
236 // function : SetEditCurrentArgument()
237 // purpose  :
238 //=================================================================================
239 void GenerationGUI_FillingDlg::SetEditCurrentArgument()
240 {
241   QPushButton* send = (QPushButton*)sender();
242
243   if (send == GroupPoints->PushButton1) {
244     GroupPoints->LineEdit1->setFocus();
245     myEditCurrentArgument = GroupPoints->LineEdit1;
246     initSelection();
247     this->SelectionIntoArgument();
248   }
249 }
250
251 //=================================================================================
252 // function : ActivateThisDialog()
253 // purpose  :
254 //=================================================================================
255 void GenerationGUI_FillingDlg::ActivateThisDialog()
256 {
257   GEOMBase_Skeleton::ActivateThisDialog();
258   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
259            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
260   initSelection();
261   processPreview();
262 }
263
264 //=================================================================================
265 // function : enterEvent()
266 // purpose  :
267 //=================================================================================
268 void GenerationGUI_FillingDlg::enterEvent(QEvent*)
269 {
270   if (!mainFrame()->GroupConstructors->isEnabled())
271     ActivateThisDialog();
272 }
273
274 //=================================================================================
275 // function : ValueChangedInSpinBox()
276 // purpose  :
277 //=================================================================================
278 void GenerationGUI_FillingDlg::ValueChangedInSpinBox(double /*newValue*/)
279 {
280   processPreview();
281 }
282
283 //=================================================================================
284 // function : MethodChanged
285 // purpose  :
286 //=================================================================================
287 void GenerationGUI_FillingDlg::MethodChanged()
288 {
289   processPreview();
290 }
291
292 //=================================================================================
293 // function : ApproxChanged()
294 // purpose  :
295 //=================================================================================
296 void GenerationGUI_FillingDlg::ApproxChanged()
297 {
298   if (GroupPoints->CheckBox1->isChecked()) {
299     // tol2d
300     GroupPoints->TextLabel3->setEnabled(false);
301     GroupPoints->SpinBox2->setEnabled(false);
302     // nbIter
303     GroupPoints->TextLabel4->setEnabled(false);
304     GroupPoints->SpinBox3->setEnabled(false);
305     // method
306     GroupPoints->TextLabel7->setEnabled(false);
307     GroupPoints->ComboBox1->setEnabled(false);
308   }
309   else {
310     // tol2d
311     GroupPoints->TextLabel3->setEnabled(true);
312     GroupPoints->SpinBox2->setEnabled(true);
313     // nbIter
314     GroupPoints->TextLabel4->setEnabled(true);
315     GroupPoints->SpinBox3->setEnabled(true);
316     // method
317     GroupPoints->TextLabel7->setEnabled(true);
318     GroupPoints->ComboBox1->setEnabled(true);
319   }
320
321   processPreview();
322 }
323
324 //=================================================================================
325 // function : createOperation
326 // purpose  :
327 //=================================================================================
328 GEOM::GEOM_IOperations_ptr GenerationGUI_FillingDlg::createOperation()
329 {
330   return getGeomEngine()->GetI3DPrimOperations();
331 }
332
333 //=================================================================================
334 // function : isValid
335 // purpose  :
336 //=================================================================================
337 bool GenerationGUI_FillingDlg::isValid(QString& msg)
338 {
339   bool ok = ( GroupPoints->SpinBox1->isValid(msg, !IsPreview()) &&
340               GroupPoints->SpinBox2->isValid(msg, !IsPreview()) &&
341               GroupPoints->SpinBox3->isValid(msg, !IsPreview()) &&
342               GroupPoints->SpinBox4->isValid(msg, !IsPreview()) &&
343               GroupPoints->SpinBox5->isValid(msg, !IsPreview()) &&
344               myObjects.count() );
345   return ok;
346 }
347
348 //=================================================================================
349 // function : execute
350 // purpose  :
351 //=================================================================================
352 bool GenerationGUI_FillingDlg::execute(ObjectList& objects)
353 {
354   GEOM::GEOM_I3DPrimOperations_var anOper =
355     GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
356
357   GEOM::filling_oper_method aMethod = GEOM::FOM_Default; // todo: aMethod must be explicitly initialized to avoid warning (see below)
358   switch (GroupPoints->ComboBox1->currentIndex())
359   {
360   case 0:  aMethod = GEOM::FOM_Default; break;
361   case 1:  aMethod = GEOM::FOM_UseOri; break;
362   case 2:  aMethod = GEOM::FOM_AutoCorrect; break;
363   default: break;
364   }
365
366   GEOM::ListOfGO_var objList = new GEOM::ListOfGO;
367   objList->length( myObjects.count() );
368   for ( int i = 0; i < myObjects.count(); ++i )
369     objList[i] = myObjects[i].copy();
370
371   GEOM::GEOM_Object_var anObj =
372     anOper->MakeFilling(objList,
373                         GroupPoints->SpinBox1->value(),
374                         GroupPoints->SpinBox4->value(),
375                         GroupPoints->SpinBox2->value(),
376                         GroupPoints->SpinBox5->value(),
377                         GroupPoints->SpinBox3->value(),
378                         aMethod,
379                         GroupPoints->CheckBox1->isChecked()); // todo: aMethod must be explicitly initialized to avoid warning (see below)
380   if (!anObj->_is_nil())
381   {
382     if (!IsPreview())
383     {
384       QStringList aParameters;
385       aParameters << GroupPoints->SpinBox1->text();
386       aParameters << GroupPoints->SpinBox2->text();
387       aParameters << GroupPoints->SpinBox3->text();
388       aParameters << GroupPoints->SpinBox4->text();
389       aParameters << GroupPoints->SpinBox5->text();
390       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
391     }
392     objects.push_back(anObj._retn());
393   }
394
395   return true;
396 }
397
398 //=================================================================================
399 // function : addSubshapesToStudy
400 // purpose  : virtual method to add new SubObjects if local selection
401 //=================================================================================
402 void GenerationGUI_FillingDlg::addSubshapesToStudy()
403 {
404   foreach( GEOM::GeomObjPtr o, myObjects )
405     GEOMBase::PublishSubObject( o.get() );
406 }
407
408 //=================================================================================
409 // function : getSourceObjects
410 // purpose  : virtual method to get source objects
411 //=================================================================================
412 QList<GEOM::GeomObjPtr> GenerationGUI_FillingDlg::getSourceObjects()
413 {
414   return myObjects;
415 }