Salome HOME
Merge from BR_WIN_INDUS_514 04/10/2010
[modules/geom.git] / src / BlocksGUI / BlocksGUI_BlockDlg.cxx
1 //  Copyright (C) 2007-2010  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
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 = myFace2 = GEOM::GEOM_Object::_nil();
164   myFace3 = myFace4 = myFace5 = myFace6 = myFace1;
165
166   switch (constructorId) {
167   case 0:
168     Group6F->hide();
169     Group2F->show();
170
171     Group2F->LineEdit1->setText(tr(""));
172     Group2F->LineEdit2->setText(tr(""));
173
174     Group2F->PushButton1->click();
175     break;
176   case 1:
177     Group2F->hide();
178     Group6F->show();
179
180     Group6F->LineEdit1->setText(tr(""));
181     Group6F->LineEdit2->setText(tr(""));
182     Group6F->LineEdit3->setText(tr(""));
183     Group6F->LineEdit4->setText(tr(""));
184     Group6F->LineEdit5->setText(tr(""));
185     Group6F->LineEdit6->setText(tr(""));
186
187     Group6F->PushButton1->click();
188     break;
189   default:
190     break;
191   }
192
193   qApp->processEvents();
194   updateGeometry();
195   resize(minimumSizeHint());
196
197   // on dialog initialization we init the first field with a selected object (if any)
198   SelectionIntoArgument();
199 }
200
201 //=================================================================================
202 // function : ClickOnOk()
203 // purpose  :
204 //=================================================================================
205 void BlocksGUI_BlockDlg::ClickOnOk()
206 {
207   if (ClickOnApply())
208     ClickOnCancel();
209 }
210
211 //=================================================================================
212 // function : ClickOnApply()
213 // purpose  :
214 //=================================================================================
215 bool BlocksGUI_BlockDlg::ClickOnApply()
216 {
217   if (!onAccept())
218     return false;
219
220   initName();
221   return true;
222 }
223
224 //=================================================================================
225 // function : SelectionIntoArgument()
226 // purpose  : Called when selection is changed or on dialog initialization or activation
227 //=================================================================================
228 void BlocksGUI_BlockDlg::SelectionIntoArgument()
229 {
230   erasePreview();
231   myEditCurrentArgument->setText("");
232
233   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
234   SALOME_ListIO aSelList;
235   aSelMgr->selectedObjects(aSelList);
236
237   if (aSelList.Extent() != 1) {
238     if      (myEditCurrentArgument == Group2F->LineEdit1) myFace1 = GEOM::GEOM_Object::_nil();
239     else if (myEditCurrentArgument == Group2F->LineEdit2) myFace2 = GEOM::GEOM_Object::_nil();
240     else if (myEditCurrentArgument == Group6F->LineEdit1) myFace1 = GEOM::GEOM_Object::_nil();
241     else if (myEditCurrentArgument == Group6F->LineEdit2) myFace2 = GEOM::GEOM_Object::_nil();
242     else if (myEditCurrentArgument == Group6F->LineEdit3) myFace3 = GEOM::GEOM_Object::_nil();
243     else if (myEditCurrentArgument == Group6F->LineEdit4) myFace4 = GEOM::GEOM_Object::_nil();
244     else if (myEditCurrentArgument == Group6F->LineEdit5) myFace5 = GEOM::GEOM_Object::_nil();
245     else if (myEditCurrentArgument == Group6F->LineEdit6) myFace6 = GEOM::GEOM_Object::_nil();
246     return;
247   }
248
249   // nbSel == 1
250   Standard_Boolean testResult = Standard_False;
251   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
252
253   if (!testResult || CORBA::is_nil(aSelectedObject))
254     return;
255
256   QString aName = GEOMBase::GetName(aSelectedObject);
257
258   // Get Selected object if selected subshape
259   TopoDS_Shape aShape;
260   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
261   {
262     TColStd_IndexedMapOfInteger aMap;
263     aSelMgr->GetIndexes(aSelList.First(), aMap);
264     if (aMap.Extent() == 1) // Local Selection
265     {
266       int anIndex = aMap(1);
267       aName += QString(":face_%1").arg(anIndex);
268
269       //Find SubShape Object in Father
270       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
271
272       if (aFindedObject->_is_nil()) { // Object not found in study
273         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
274         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
275       }
276       else {
277         aSelectedObject = aFindedObject; // get Object from study
278       }
279     }
280     else // Global Selection
281     {
282       if (aShape.ShapeType() != TopAbs_FACE) {
283         aSelectedObject = GEOM::GEOM_Object::_nil();
284         aName = "";
285       }
286     }
287   }
288
289   myEditCurrentArgument->setText(aName);
290
291   if (myEditCurrentArgument == Group2F->LineEdit1) {
292     myFace1 = aSelectedObject;
293     if (!myFace1->_is_nil() && myFace2->_is_nil())
294       Group2F->PushButton2->click();
295   }
296   else if (myEditCurrentArgument == Group2F->LineEdit2) {
297     myFace2 = aSelectedObject;
298     if (!myFace2->_is_nil() && myFace1->_is_nil())
299       Group2F->PushButton1->click();
300   }
301   else if (myEditCurrentArgument == Group6F->LineEdit1) {
302     myFace1 = aSelectedObject;
303     if (!myFace1->_is_nil() && myFace2->_is_nil())
304       Group6F->PushButton2->click();
305   }
306   else if (myEditCurrentArgument == Group6F->LineEdit2) {
307     myFace2 = aSelectedObject;
308     if (!myFace2->_is_nil() && myFace3->_is_nil())
309       Group6F->PushButton3->click();
310   }
311   else if (myEditCurrentArgument == Group6F->LineEdit3) {
312     myFace3 = aSelectedObject;
313     if (!myFace3->_is_nil() && myFace4->_is_nil())
314       Group6F->PushButton4->click();
315   }
316   else if (myEditCurrentArgument == Group6F->LineEdit4) {
317     myFace4 = aSelectedObject;
318     if (!myFace4->_is_nil() && myFace5->_is_nil())
319       Group6F->PushButton5->click();
320   }
321   else if (myEditCurrentArgument == Group6F->LineEdit5) {
322     myFace5 = aSelectedObject;
323     if (!myFace5->_is_nil() && myFace6->_is_nil())
324       Group6F->PushButton6->click();
325   }
326   else if (myEditCurrentArgument == Group6F->LineEdit6) {
327     myFace6 = aSelectedObject;
328     if (!myFace6->_is_nil() && myFace1->_is_nil())
329       Group6F->PushButton1->click();
330   }
331
332   displayPreview();
333 }
334
335 //=================================================================================
336 // function : SetEditCurrentArgument()
337 // purpose  :
338 //=================================================================================
339 void BlocksGUI_BlockDlg::SetEditCurrentArgument()
340 {
341   QPushButton* aSender = (QPushButton*)sender();
342
343   // clear selection
344   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
345   if (myInitial)
346     myInitial = false;
347   else
348     myGeomGUI->getApp()->selectionMgr()->clearSelected();
349
350   // disable all
351   switch (myConstructorId) {
352   case 0:
353     Group2F->PushButton1->setDown(false);
354     Group2F->PushButton2->setDown(false);
355
356     Group2F->LineEdit1->setEnabled(false);
357     Group2F->LineEdit2->setEnabled(false);
358     break;
359   case 1:
360     Group6F->PushButton1->setDown(false);
361     Group6F->PushButton2->setDown(false);
362     Group6F->PushButton3->setDown(false);
363     Group6F->PushButton4->setDown(false);
364     Group6F->PushButton5->setDown(false);
365     Group6F->PushButton6->setDown(false);
366
367     Group6F->LineEdit1->setEnabled(false);
368     Group6F->LineEdit2->setEnabled(false);
369     Group6F->LineEdit3->setEnabled(false);
370     Group6F->LineEdit4->setEnabled(false);
371     Group6F->LineEdit5->setEnabled(false);
372     Group6F->LineEdit6->setEnabled(false);
373     break;
374   default:
375     break;
376   }
377
378   // set line edit as current argument
379   if (aSender == Group2F->PushButton1) {
380     myEditCurrentArgument = Group2F->LineEdit1;
381   }
382   else if (aSender == Group2F->PushButton2) {
383     myEditCurrentArgument = Group2F->LineEdit2;
384   }
385   else if (aSender == Group6F->PushButton1) {
386     myEditCurrentArgument = Group6F->LineEdit1;
387   }
388   else if (aSender == Group6F->PushButton2) {
389     myEditCurrentArgument = Group6F->LineEdit2;
390   }
391   else if (aSender == Group6F->PushButton3) {
392     myEditCurrentArgument = Group6F->LineEdit3;
393   }
394   else if (aSender == Group6F->PushButton4) {
395     myEditCurrentArgument = Group6F->LineEdit4;
396   }
397   else if (aSender == Group6F->PushButton5) {
398     myEditCurrentArgument = Group6F->LineEdit5;
399   }
400   else if (aSender == Group6F->PushButton6) {
401     myEditCurrentArgument = Group6F->LineEdit6;
402   }
403
404   // enable push button and line edit
405   myEditCurrentArgument->setEnabled(true);
406   myEditCurrentArgument->setFocus();
407   // after setFocus(), because it will be setDown(false) when loses focus
408   aSender->setDown(true);
409
410   globalSelection(); // close local contexts, if any
411   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_FACE); //Select Faces on All Shapes
412   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
413           this, SLOT(SelectionIntoArgument()));
414 }
415
416 //=================================================================================
417 // function : ActivateThisDialog()
418 // purpose  :
419 //=================================================================================
420 void BlocksGUI_BlockDlg::ActivateThisDialog()
421 {
422   GEOMBase_Skeleton::ActivateThisDialog();
423   globalSelection(); // close local contexts, if any
424   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_FACE); //Select Faces on All Shapes
425   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
426           this, SLOT(SelectionIntoArgument()));
427   displayPreview();
428 }
429
430 //=================================================================================
431 // function : enterEvent()
432 // purpose  :
433 //=================================================================================
434 void BlocksGUI_BlockDlg::enterEvent (QEvent*)
435 {
436   if (!mainFrame()->GroupConstructors->isEnabled())
437     ActivateThisDialog();
438 }
439
440 //=================================================================================
441 // function : createOperation
442 // purpose  :
443 //=================================================================================
444 GEOM::GEOM_IOperations_ptr BlocksGUI_BlockDlg::createOperation()
445 {
446   return getGeomEngine()->GetIBlocksOperations(getStudyId());
447 }
448
449 //=================================================================================
450 // function : isValid
451 // purpose  :
452 //=================================================================================
453 bool BlocksGUI_BlockDlg::isValid (QString&)
454 {
455   bool ok = false;
456   switch (getConstructorId()) {
457   case 0:
458     ok = !(myFace1->_is_nil() || myFace2->_is_nil());
459     break;
460   case 1:
461     ok =  !(myFace1->_is_nil() || myFace2->_is_nil() ||
462             myFace3->_is_nil() || myFace4->_is_nil() ||
463             myFace5->_is_nil() || myFace6->_is_nil());
464     break;
465   default:
466     break;
467   }
468   return ok;
469 }
470
471 //=================================================================================
472 // function : execute
473 // purpose  :
474 //=================================================================================
475 bool BlocksGUI_BlockDlg::execute (ObjectList& objects)
476 {
477   bool res = false;
478
479   GEOM::GEOM_Object_var anObj;
480
481   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow(getOperation());
482
483   switch (getConstructorId()) {
484   case 0:
485     if (!CORBA::is_nil(myFace1) && !CORBA::is_nil(myFace2)) {
486       anObj = anOper->MakeHexa2Faces(myFace1, myFace2);
487       res = true;
488     }
489     break;
490   case 1:
491     if (!CORBA::is_nil(myFace1) && !CORBA::is_nil(myFace2) &&
492         !CORBA::is_nil(myFace3) && !CORBA::is_nil(myFace4) &&
493         !CORBA::is_nil(myFace5) && !CORBA::is_nil(myFace6)) {
494       anObj = anOper->MakeHexa(myFace1, myFace2, myFace3, myFace4, myFace5, myFace6);
495       res = true;
496     }
497     break;
498   }
499
500   if (!anObj->_is_nil())
501     objects.push_back(anObj._retn());
502
503   return res;
504 }
505
506 //=================================================================================
507 // function : addSubshapesToStudy
508 // purpose  : virtual method to add new SubObjects if local selection
509 //=================================================================================
510 void BlocksGUI_BlockDlg::addSubshapesToStudy()
511 {
512   QMap<QString, GEOM::GEOM_Object_var> objMap;
513
514   switch (getConstructorId()) {
515   case 0:
516     objMap[Group2F->LineEdit1->text()] = myFace1;
517     objMap[Group2F->LineEdit2->text()] = myFace2;
518     break;
519   case 1:
520     objMap[Group6F->LineEdit1->text()] = myFace1;
521     objMap[Group6F->LineEdit2->text()] = myFace2;
522     objMap[Group6F->LineEdit3->text()] = myFace3;
523     objMap[Group6F->LineEdit4->text()] = myFace4;
524     objMap[Group6F->LineEdit5->text()] = myFace5;
525     objMap[Group6F->LineEdit6->text()] = myFace6;
526     break;
527   }
528   addSubshapesToFather(objMap);
529 }