Salome HOME
07ceb91b2713009275d7ff041c66eca21186817e
[modules/geom.git] / src / BlocksGUI / BlocksGUI_BlockDlg.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, 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   : BlocksGUI_BlockDlg.cxx
25 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
26 //
27 #include "BlocksGUI_BlockDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <GEOMImpl_Types.hxx>
39
40 // OCCT Includes
41 #include <TColStd_IndexedMapOfInteger.hxx>
42
43 //=================================================================================
44 // class    : BlocksGUI_BlockDlg()
45 // purpose  : Constructs a BlocksGUI_BlockDlg which is a child of 'parent'.
46 //=================================================================================
47 BlocksGUI_BlockDlg::BlocksGUI_BlockDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
48   : GEOMBase_Skeleton(theGeometryGUI, parent),
49     myInitial(true)
50 {
51   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BLOCK_2F")));
52   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BLOCK_6F")));
53   QPixmap imageS (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
54
55   setWindowTitle(tr("GEOM_BLOCK_TITLE"));
56
57   /***************************************************************/
58   mainFrame()->GroupConstructors->setTitle(tr("GEOM_BLOCK"));
59
60   mainFrame()->RadioButton1->setIcon(image0);
61   mainFrame()->RadioButton2->setIcon(image1);
62   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
63   mainFrame()->RadioButton3->close();
64
65   // Create first group
66   Group2F = new DlgRef_2Sel (centralWidget());
67   Group2F->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
68   Group2F->TextLabel1->setText(tr("FACE_1"));
69   Group2F->TextLabel2->setText(tr("FACE_2"));
70   Group2F->PushButton1->setIcon(imageS);
71   Group2F->PushButton2->setIcon(imageS);
72
73   // Create second group
74   Group6F = new DlgRef_6Sel (centralWidget());
75   Group6F->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
76   Group6F->TextLabel1->setText(tr("FACE_1"));
77   Group6F->TextLabel2->setText(tr("FACE_2"));
78   Group6F->TextLabel3->setText(tr("FACE_3"));
79   Group6F->TextLabel4->setText(tr("FACE_4"));
80   Group6F->TextLabel5->setText(tr("FACE_5"));
81   Group6F->TextLabel6->setText(tr("FACE_6"));
82   Group6F->PushButton1->setIcon(imageS);
83   Group6F->PushButton2->setIcon(imageS);
84   Group6F->PushButton3->setIcon(imageS);
85   Group6F->PushButton4->setIcon(imageS);
86   Group6F->PushButton5->setIcon(imageS);
87   Group6F->PushButton6->setIcon(imageS);
88
89   // Add groups to layout
90   QVBoxLayout* layout = new QVBoxLayout (centralWidget());
91   layout->setMargin(0);
92   layout->setSpacing(6);
93   layout->addWidget(Group2F);
94   layout->addWidget(Group6F);
95   /***************************************************************/
96
97   setHelpFileName("build_by_blocks_page.html#hexa_solid_anchor");
98
99   Init();
100 }
101
102 //=================================================================================
103 // function : ~BlocksGUI_BlockDlg()
104 // purpose  : Destroys the object and frees any allocated resources
105 //=================================================================================
106 BlocksGUI_BlockDlg::~BlocksGUI_BlockDlg()
107 {
108   // no need to delete child widgets, Qt does it all for us
109 }
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void BlocksGUI_BlockDlg::Init()
116 {
117   // init variables
118   Group2F->LineEdit1->setReadOnly(true);
119   Group2F->LineEdit2->setReadOnly(true);
120
121   Group6F->LineEdit1->setReadOnly(true);
122   Group6F->LineEdit2->setReadOnly(true);
123   Group6F->LineEdit3->setReadOnly(true);
124   Group6F->LineEdit4->setReadOnly(true);
125   Group6F->LineEdit5->setReadOnly(true);
126   Group6F->LineEdit6->setReadOnly(true);
127
128   // signals and slots connections
129   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
130   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
131
132   connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
133
134   connect(Group2F->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
135   connect(Group2F->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
136
137   connect(Group6F->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
138   connect(Group6F->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
139   connect(Group6F->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140   connect(Group6F->PushButton4, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
141   connect(Group6F->PushButton5, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142   connect(Group6F->PushButton6, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
143
144   // init controls and fields
145   initName(tr("GEOM_BLOCK"));
146
147   myConstructorId = -1;
148   ConstructorsClicked(0);
149 }
150
151 //=================================================================================
152 // function : ConstructorsClicked()
153 // purpose  : Radio button management
154 //=================================================================================
155 void BlocksGUI_BlockDlg::ConstructorsClicked (int constructorId)
156 {
157   if (myConstructorId == constructorId)
158     return;
159
160   myConstructorId = constructorId;
161
162   // init fields
163   myFace1.nullify();
164   myFace2.nullify();
165   myFace3.nullify();
166   myFace4.nullify();
167   myFace5.nullify();
168   myFace6.nullify();
169
170   switch (constructorId) {
171   case 0:
172     Group6F->hide();
173     Group2F->show();
174
175     Group2F->LineEdit1->setText(tr(""));
176     Group2F->LineEdit2->setText(tr(""));
177
178     Group2F->PushButton1->click();
179     break;
180   case 1:
181     Group2F->hide();
182     Group6F->show();
183
184     Group6F->LineEdit1->setText(tr(""));
185     Group6F->LineEdit2->setText(tr(""));
186     Group6F->LineEdit3->setText(tr(""));
187     Group6F->LineEdit4->setText(tr(""));
188     Group6F->LineEdit5->setText(tr(""));
189     Group6F->LineEdit6->setText(tr(""));
190
191     Group6F->PushButton1->click();
192     break;
193   default:
194     break;
195   }
196
197   qApp->processEvents();
198   updateGeometry();
199   resize(minimumSizeHint());
200
201   // on dialog initialization we init the first field with a selected object (if any)
202   SelectionIntoArgument();
203 }
204
205 //=================================================================================
206 // function : ClickOnOk()
207 // purpose  :
208 //=================================================================================
209 void BlocksGUI_BlockDlg::ClickOnOk()
210 {
211   setIsApplyAndClose( true );
212   if (ClickOnApply())
213     ClickOnCancel();
214 }
215
216 //=================================================================================
217 // function : ClickOnApply()
218 // purpose  :
219 //=================================================================================
220 bool BlocksGUI_BlockDlg::ClickOnApply()
221 {
222   if (!onAccept())
223     return false;
224
225   initName();
226   return true;
227 }
228
229 //=================================================================================
230 // function : SelectionIntoArgument()
231 // purpose  : Called when selection is changed or on dialog initialization or activation
232 //=================================================================================
233 void BlocksGUI_BlockDlg::SelectionIntoArgument()
234 {
235   erasePreview();
236
237   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_FACE );
238   TopoDS_Shape aShape;
239   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
240     QString aName = GEOMBase::GetName( aSelectedObject.get() );
241     myEditCurrentArgument->setText( aName );
242     if ( myEditCurrentArgument == Group2F->LineEdit1 ) {
243       myFace1 = aSelectedObject;
244       if (!myFace2) Group2F->PushButton2->click();
245     }
246     else if ( myEditCurrentArgument == Group2F->LineEdit2 ) {
247       myFace2 = aSelectedObject;
248       if (!myFace1) Group2F->PushButton1->click();
249     }
250     else if ( myEditCurrentArgument == Group6F->LineEdit1 ) {
251       myFace1 = aSelectedObject;
252       if      (!myFace2) Group6F->PushButton2->click();
253       else if (!myFace3) Group6F->PushButton3->click();
254       else if (!myFace4) Group6F->PushButton4->click();
255       else if (!myFace5) Group6F->PushButton5->click();
256       else if (!myFace6) Group6F->PushButton6->click();
257     }
258     else if ( myEditCurrentArgument == Group6F->LineEdit2 ) {
259       myFace2 = aSelectedObject;
260       if      (!myFace3) Group6F->PushButton3->click();
261       else if (!myFace4) Group6F->PushButton4->click();
262       else if (!myFace5) Group6F->PushButton5->click();
263       else if (!myFace6) Group6F->PushButton6->click();
264       else if (!myFace1) Group6F->PushButton1->click();
265     }
266     else if ( myEditCurrentArgument == Group6F->LineEdit3 ) {
267       myFace3 = aSelectedObject;
268       if      (!myFace4) Group6F->PushButton4->click();
269       else if (!myFace5) Group6F->PushButton5->click();
270       else if (!myFace6) Group6F->PushButton6->click();
271       else if (!myFace1) Group6F->PushButton1->click();
272       else if (!myFace2) Group6F->PushButton2->click();
273     }
274     else if ( myEditCurrentArgument == Group6F->LineEdit4 ) {
275       myFace4 = aSelectedObject;
276       if      (!myFace5) Group6F->PushButton5->click();
277       else if (!myFace6) Group6F->PushButton6->click();
278       else if (!myFace1) Group6F->PushButton1->click();
279       else if (!myFace2) Group6F->PushButton2->click();
280       else if (!myFace3) Group6F->PushButton3->click();
281     }
282     else if ( myEditCurrentArgument == Group6F->LineEdit5 ) {
283       myFace5 = aSelectedObject;
284       if      (!myFace6) Group6F->PushButton6->click();
285       else if (!myFace1) Group6F->PushButton1->click();
286       else if (!myFace2) Group6F->PushButton2->click();
287       else if (!myFace3) Group6F->PushButton3->click();
288       else if (!myFace4) Group6F->PushButton4->click();
289     }
290     else if ( myEditCurrentArgument == Group6F->LineEdit6 ) {
291       myFace6 = aSelectedObject;
292       if      (!myFace1) Group6F->PushButton1->click();
293       else if (!myFace2) Group6F->PushButton2->click();
294       else if (!myFace3) Group6F->PushButton3->click();
295       else if (!myFace4) Group6F->PushButton4->click();
296       else if (!myFace5) Group6F->PushButton5->click();
297     }
298   }
299   else {
300     if      (myEditCurrentArgument == Group2F->LineEdit1) myFace1.nullify();
301     else if (myEditCurrentArgument == Group2F->LineEdit2) myFace2.nullify();
302     else if (myEditCurrentArgument == Group6F->LineEdit1) myFace1.nullify();
303     else if (myEditCurrentArgument == Group6F->LineEdit2) myFace2.nullify();
304     else if (myEditCurrentArgument == Group6F->LineEdit3) myFace3.nullify();
305     else if (myEditCurrentArgument == Group6F->LineEdit4) myFace4.nullify();
306     else if (myEditCurrentArgument == Group6F->LineEdit5) myFace5.nullify();
307     else if (myEditCurrentArgument == Group6F->LineEdit6) myFace6.nullify();
308     myEditCurrentArgument->setText( "" );
309   }
310
311   displayPreview(true);
312 }
313
314 //=================================================================================
315 // function : SetEditCurrentArgument()
316 // purpose  :
317 //=================================================================================
318 void BlocksGUI_BlockDlg::SetEditCurrentArgument()
319 {
320   QPushButton* aSender = (QPushButton*)sender();
321
322   // clear selection
323   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
324   if (myInitial)
325     myInitial = false;
326   else
327     myGeomGUI->getApp()->selectionMgr()->clearSelected();
328
329   // disable all
330   switch (myConstructorId) {
331   case 0:
332     Group2F->PushButton1->setDown(false);
333     Group2F->PushButton2->setDown(false);
334
335     Group2F->LineEdit1->setEnabled(false);
336     Group2F->LineEdit2->setEnabled(false);
337     break;
338   case 1:
339     Group6F->PushButton1->setDown(false);
340     Group6F->PushButton2->setDown(false);
341     Group6F->PushButton3->setDown(false);
342     Group6F->PushButton4->setDown(false);
343     Group6F->PushButton5->setDown(false);
344     Group6F->PushButton6->setDown(false);
345
346     Group6F->LineEdit1->setEnabled(false);
347     Group6F->LineEdit2->setEnabled(false);
348     Group6F->LineEdit3->setEnabled(false);
349     Group6F->LineEdit4->setEnabled(false);
350     Group6F->LineEdit5->setEnabled(false);
351     Group6F->LineEdit6->setEnabled(false);
352     break;
353   default:
354     break;
355   }
356
357   // set line edit as current argument
358   if (aSender == Group2F->PushButton1) {
359     myEditCurrentArgument = Group2F->LineEdit1;
360   }
361   else if (aSender == Group2F->PushButton2) {
362     myEditCurrentArgument = Group2F->LineEdit2;
363   }
364   else if (aSender == Group6F->PushButton1) {
365     myEditCurrentArgument = Group6F->LineEdit1;
366   }
367   else if (aSender == Group6F->PushButton2) {
368     myEditCurrentArgument = Group6F->LineEdit2;
369   }
370   else if (aSender == Group6F->PushButton3) {
371     myEditCurrentArgument = Group6F->LineEdit3;
372   }
373   else if (aSender == Group6F->PushButton4) {
374     myEditCurrentArgument = Group6F->LineEdit4;
375   }
376   else if (aSender == Group6F->PushButton5) {
377     myEditCurrentArgument = Group6F->LineEdit5;
378   }
379   else if (aSender == Group6F->PushButton6) {
380     myEditCurrentArgument = Group6F->LineEdit6;
381   }
382
383   // enable push button and line edit
384   myEditCurrentArgument->setEnabled(true);
385   myEditCurrentArgument->setFocus();
386   // after setFocus(), because it will be setDown(false) when loses focus
387   aSender->setDown(true);
388
389   globalSelection(); // close local contexts, if any
390   localSelection(TopAbs_FACE); //Select Faces on All Shapes
391   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
392           this, SLOT(SelectionIntoArgument()));
393 }
394
395 //=================================================================================
396 // function : ActivateThisDialog()
397 // purpose  :
398 //=================================================================================
399 void BlocksGUI_BlockDlg::ActivateThisDialog()
400 {
401   GEOMBase_Skeleton::ActivateThisDialog();
402   globalSelection(); // close local contexts, if any
403   localSelection(TopAbs_FACE); //Select Faces on All Shapes
404   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
405           this, SLOT(SelectionIntoArgument()));
406   displayPreview(true);
407 }
408
409 //=================================================================================
410 // function : enterEvent()
411 // purpose  :
412 //=================================================================================
413 void BlocksGUI_BlockDlg::enterEvent (QEvent*)
414 {
415   if (!mainFrame()->GroupConstructors->isEnabled())
416     ActivateThisDialog();
417 }
418
419 //=================================================================================
420 // function : createOperation
421 // purpose  :
422 //=================================================================================
423 GEOM::GEOM_IOperations_ptr BlocksGUI_BlockDlg::createOperation()
424 {
425   return getGeomEngine()->GetIBlocksOperations();
426 }
427
428 //=================================================================================
429 // function : isValid
430 // purpose  :
431 //=================================================================================
432 bool BlocksGUI_BlockDlg::isValid(QString&)
433 {
434   bool ok = false;
435   switch (getConstructorId()) {
436   case 0:
437     ok = myFace1 && myFace2;
438     break;
439   case 1:
440     ok = myFace1 && myFace2 && myFace3 && myFace4 && myFace5 && myFace6;
441     break;
442   default:
443     break;
444   }
445   return ok;
446 }
447
448 //=================================================================================
449 // function : execute
450 // purpose  :
451 //=================================================================================
452 bool BlocksGUI_BlockDlg::execute (ObjectList& objects)
453 {
454   bool res = false;
455
456   GEOM::GEOM_Object_var anObj;
457
458   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow(getOperation());
459
460   switch (getConstructorId()) {
461   case 0:
462     if ( myFace1 && myFace2 ) {
463       anObj = anOper->MakeHexa2Faces(myFace1.get(), myFace2.get());
464       res = true;
465     }
466     break;
467   case 1:
468     if ( myFace1 && myFace2 && myFace3 && myFace4 && myFace5 && myFace6 ) {
469       anObj = anOper->MakeHexa(myFace1.get(), myFace2.get(), myFace3.get(), myFace4.get(), myFace5.get(), myFace6.get());
470       res = true;
471     }
472     break;
473   }
474
475   if (!anObj->_is_nil())
476     objects.push_back(anObj._retn());
477
478   return res;
479 }
480
481 //=================================================================================
482 // function : addSubshapesToStudy
483 // purpose  : virtual method to add new SubObjects if local selection
484 //=================================================================================
485 void BlocksGUI_BlockDlg::addSubshapesToStudy()
486 {
487   switch (getConstructorId()) {
488   case 0:
489     GEOMBase::PublishSubObject( myFace1.get() );
490     GEOMBase::PublishSubObject( myFace2.get() );
491     break;
492   case 1:
493     GEOMBase::PublishSubObject( myFace1.get() );
494     GEOMBase::PublishSubObject( myFace2.get() );
495     GEOMBase::PublishSubObject( myFace3.get() );
496     GEOMBase::PublishSubObject( myFace4.get() );
497     GEOMBase::PublishSubObject( myFace5.get() );
498     GEOMBase::PublishSubObject( myFace6.get() );
499     break;
500   default:
501     break;
502   }
503 }
504
505 //=================================================================================
506 // function : getSourceObjects
507 // purpose  : virtual method to get source objects
508 //=================================================================================
509 QList<GEOM::GeomObjPtr> BlocksGUI_BlockDlg::getSourceObjects()
510 {
511   QList<GEOM::GeomObjPtr> res;
512   res << myFace1 << myFace2 << myFace3
513       << myFace4 << myFace5 << myFace6;
514   return res;
515 }