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