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