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     }
278     else // Global Selection
279     {
280       if (aShape.ShapeType() != TopAbs_FACE) {
281         aSelectedObject = GEOM::GEOM_Object::_nil();
282         aName = "";
283       }
284     }
285   }
286
287   myEditCurrentArgument->setText(aName);
288
289   if (myEditCurrentArgument == Group2F->LineEdit1) {
290     myFace1 = aSelectedObject;
291     if (!myFace1->_is_nil() && myFace2->_is_nil())
292       Group2F->PushButton2->click();
293   }
294   else if (myEditCurrentArgument == Group2F->LineEdit2) {
295     myFace2 = aSelectedObject;
296     if (!myFace2->_is_nil() && myFace1->_is_nil())
297       Group2F->PushButton1->click();
298   }
299   else if (myEditCurrentArgument == Group6F->LineEdit1) {
300     myFace1 = aSelectedObject;
301     if (!myFace1->_is_nil() && myFace2->_is_nil())
302       Group6F->PushButton2->click();
303   }
304   else if (myEditCurrentArgument == Group6F->LineEdit2) {
305     myFace2 = aSelectedObject;
306     if (!myFace2->_is_nil() && myFace3->_is_nil())
307       Group6F->PushButton3->click();
308   }
309   else if (myEditCurrentArgument == Group6F->LineEdit3) {
310     myFace3 = aSelectedObject;
311     if (!myFace3->_is_nil() && myFace4->_is_nil())
312       Group6F->PushButton4->click();
313   }
314   else if (myEditCurrentArgument == Group6F->LineEdit4) {
315     myFace4 = aSelectedObject;
316     if (!myFace4->_is_nil() && myFace5->_is_nil())
317       Group6F->PushButton5->click();
318   }
319   else if (myEditCurrentArgument == Group6F->LineEdit5) {
320     myFace5 = aSelectedObject;
321     if (!myFace5->_is_nil() && myFace6->_is_nil())
322       Group6F->PushButton6->click();
323   }
324   else if (myEditCurrentArgument == Group6F->LineEdit6) {
325     myFace6 = aSelectedObject;
326     if (!myFace6->_is_nil() && myFace1->_is_nil())
327       Group6F->PushButton1->click();
328   }
329
330   displayPreview();
331 }
332
333 //=================================================================================
334 // function : SetEditCurrentArgument()
335 // purpose  :
336 //=================================================================================
337 void BlocksGUI_BlockDlg::SetEditCurrentArgument()
338 {
339   QPushButton* aSender = (QPushButton*)sender();
340
341   // clear selection
342   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
343   if (myInitial)
344     myInitial = false;
345   else
346     myGeomGUI->getApp()->selectionMgr()->clearSelected();
347
348   // disable all
349   switch (myConstructorId) {
350   case 0:
351     Group2F->PushButton1->setDown(false);
352     Group2F->PushButton2->setDown(false);
353
354     Group2F->LineEdit1->setEnabled(false);
355     Group2F->LineEdit2->setEnabled(false);
356     break;
357   case 1:
358     Group6F->PushButton1->setDown(false);
359     Group6F->PushButton2->setDown(false);
360     Group6F->PushButton3->setDown(false);
361     Group6F->PushButton4->setDown(false);
362     Group6F->PushButton5->setDown(false);
363     Group6F->PushButton6->setDown(false);
364
365     Group6F->LineEdit1->setEnabled(false);
366     Group6F->LineEdit2->setEnabled(false);
367     Group6F->LineEdit3->setEnabled(false);
368     Group6F->LineEdit4->setEnabled(false);
369     Group6F->LineEdit5->setEnabled(false);
370     Group6F->LineEdit6->setEnabled(false);
371     break;
372   default:
373     break;
374   }
375
376   // set line edit as current argument
377   if (aSender == Group2F->PushButton1) {
378     myEditCurrentArgument = Group2F->LineEdit1;
379   }
380   else if (aSender == Group2F->PushButton2) {
381     myEditCurrentArgument = Group2F->LineEdit2;
382   }
383   else if (aSender == Group6F->PushButton1) {
384     myEditCurrentArgument = Group6F->LineEdit1;
385   }
386   else if (aSender == Group6F->PushButton2) {
387     myEditCurrentArgument = Group6F->LineEdit2;
388   }
389   else if (aSender == Group6F->PushButton3) {
390     myEditCurrentArgument = Group6F->LineEdit3;
391   }
392   else if (aSender == Group6F->PushButton4) {
393     myEditCurrentArgument = Group6F->LineEdit4;
394   }
395   else if (aSender == Group6F->PushButton5) {
396     myEditCurrentArgument = Group6F->LineEdit5;
397   }
398   else if (aSender == Group6F->PushButton6) {
399     myEditCurrentArgument = Group6F->LineEdit6;
400   }
401
402   // enable push button and line edit
403   myEditCurrentArgument->setEnabled(true);
404   myEditCurrentArgument->setFocus();
405   // after setFocus(), because it will be setDown(false) when loses focus
406   aSender->setDown(true);
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   displayPreview();
426 }
427
428 //=================================================================================
429 // function : enterEvent()
430 // purpose  :
431 //=================================================================================
432 void BlocksGUI_BlockDlg::enterEvent (QEvent*)
433 {
434   if (!mainFrame()->GroupConstructors->isEnabled())
435     ActivateThisDialog();
436 }
437
438 //=================================================================================
439 // function : createOperation
440 // purpose  :
441 //=================================================================================
442 GEOM::GEOM_IOperations_ptr BlocksGUI_BlockDlg::createOperation()
443 {
444   return getGeomEngine()->GetIBlocksOperations(getStudyId());
445 }
446
447 //=================================================================================
448 // function : isValid
449 // purpose  :
450 //=================================================================================
451 bool BlocksGUI_BlockDlg::isValid (QString&)
452 {
453   bool ok = false;
454   switch (getConstructorId()) {
455   case 0:
456     ok = !(myFace1->_is_nil() || myFace2->_is_nil());
457     break;
458   case 1:
459     ok =  !(myFace1->_is_nil() || myFace2->_is_nil() ||
460             myFace3->_is_nil() || myFace4->_is_nil() ||
461             myFace5->_is_nil() || myFace6->_is_nil());
462     break;
463   default:
464     break;
465   }
466   return ok;
467 }
468
469 //=================================================================================
470 // function : execute
471 // purpose  :
472 //=================================================================================
473 bool BlocksGUI_BlockDlg::execute (ObjectList& objects)
474 {
475   bool res = false;
476
477   GEOM::GEOM_Object_var anObj;
478
479   switch (getConstructorId()) {
480   case 0:
481     if (!CORBA::is_nil(myFace1) && !CORBA::is_nil(myFace2)) {
482       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->
483         MakeHexa2Faces(myFace1, myFace2);
484       res = true;
485     }
486     break;
487   case 1:
488     if (!CORBA::is_nil(myFace1) && !CORBA::is_nil(myFace2) &&
489         !CORBA::is_nil(myFace3) && !CORBA::is_nil(myFace4) &&
490         !CORBA::is_nil(myFace5) && !CORBA::is_nil(myFace6)) {
491       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->
492         MakeHexa(myFace1, myFace2, myFace3, myFace4, myFace5, myFace6);
493       res = true;
494     }
495     break;
496   }
497
498   if (!anObj->_is_nil())
499     objects.push_back(anObj._retn());
500
501   return res;
502 }
503
504 //=================================================================================
505 // function : addSubshapesToStudy
506 // purpose  : virtual method to add new SubObjects if local selection
507 //=================================================================================
508 void BlocksGUI_BlockDlg::addSubshapesToStudy()
509 {
510   QMap<QString, GEOM::GEOM_Object_var> objMap;
511
512   switch (getConstructorId()) {
513   case 0:
514     objMap[Group2F->LineEdit1->text()] = myFace1;
515     objMap[Group2F->LineEdit2->text()] = myFace2;
516     break;
517   case 1:
518     objMap[Group6F->LineEdit1->text()] = myFace1;
519     objMap[Group6F->LineEdit2->text()] = myFace2;
520     objMap[Group6F->LineEdit3->text()] = myFace3;
521     objMap[Group6F->LineEdit4->text()] = myFace4;
522     objMap[Group6F->LineEdit5->text()] = myFace5;
523     objMap[Group6F->LineEdit6->text()] = myFace6;
524     break;
525   }
526   addSubshapesToFather(objMap);
527 }