]> SALOME platform Git repositories - modules/geom.git/blob - src/BooleanGUI/BooleanGUI_Dialog.cxx
Salome HOME
0022081: EDF 2386 GEOM: Union of a list of objects
[modules/geom.git] / src / BooleanGUI / BooleanGUI_Dialog.cxx
1 // Copyright (C) 2007-2013  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   : BooleanGUI_Dialog.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BooleanGUI.h"
28 #include "BooleanGUI_Dialog.h"
29
30 #include <DlgRef.h>
31 #include <GeometryGUI.h>
32 #include <GEOMBase.h>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <SALOME_ListIteratorOfListIO.hxx>
39
40 // VSR 22/08/2012: issue 0021787: remove "Preview" button from BOP and Partition operations
41 // Comment next line to enable preview in BOP dialog box
42 #define NO_PREVIEW
43
44 //=================================================================================
45 // class    : BooleanGUI_Dialog()
46 // purpose  : Constructs a BooleanGUI_Dialog which is a child of 'parent', with the
47 //            name 'name' and widget flags set to 'f'.
48 //            The dialog will by default be modeless, unless you set 'modal' to
49 //            TRUE to construct a modal dialog.
50 //=================================================================================
51 BooleanGUI_Dialog::BooleanGUI_Dialog (const int theOperation, GeometryGUI* theGeometryGUI,
52                                       QWidget* parent, bool modal, Qt::WindowFlags fl)
53   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
54     myOperation(theOperation)
55 {
56   QPixmap image0;
57   QString aTitle, aCaption;
58   switch (myOperation) {
59   case BooleanGUI::COMMON:
60     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_COMMON")));
61     aTitle = tr("GEOM_COMMON");
62     aCaption = tr("GEOM_COMMON_TITLE");
63     setHelpFileName("common_operation_page.html");
64     break;
65   case BooleanGUI::CUT:
66     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CUT")));
67     aTitle = tr("GEOM_CUT");
68     aCaption = tr("GEOM_CUT_TITLE");
69     setHelpFileName("cut_operation_page.html");
70     break;
71   case BooleanGUI::FUSE:
72     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FUSE")));
73     aTitle = tr("GEOM_FUSE");
74     aCaption = tr("GEOM_FUSE_TITLE");
75     setHelpFileName("fuse_operation_page.html");
76     break;
77   case BooleanGUI::SECTION:
78     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SECTION")));
79     aTitle = tr("GEOM_SECTION");
80     aCaption = tr("GEOM_SECTION_TITLE");
81     setHelpFileName("section_opeartion_page.html");
82     break;
83   }
84   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
85
86   setWindowTitle(aCaption);
87
88   /***************************************************************/
89   mainFrame()->GroupConstructors->setTitle(aTitle);
90   mainFrame()->RadioButton1->setIcon(image0);
91   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
92   mainFrame()->RadioButton2->close();
93   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
94   mainFrame()->RadioButton3->close();
95
96   myGroup = new DlgRef_2Sel(centralWidget());
97
98   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
99   if (myOperation == BooleanGUI::CUT) {
100     myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
101     myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECTS"));
102   }
103   else if (myOperation == BooleanGUI::SECTION) {
104     myGroup->TextLabel1->setText(tr("GEOM_OBJECT_I").arg(1));
105     myGroup->TextLabel2->setText(tr("GEOM_OBJECT_I").arg(2));
106   } else { // Fuse or Common
107     myGroup->TextLabel1->setText(tr( "GEOM_SELECTED_OBJECTS" ));
108     myGroup->TextLabel2->hide();
109     myGroup->PushButton2->hide();
110     myGroup->LineEdit2->hide();
111   }
112
113   myGroup->PushButton1->setIcon(image1);
114   myGroup->LineEdit1->setReadOnly(true);
115
116   if (myOperation != BooleanGUI::FUSE && myOperation != BooleanGUI::COMMON) {
117     myGroup->PushButton2->setIcon(image1);
118     myGroup->LineEdit2->setReadOnly(true);
119   }
120
121   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
122   layout->setMargin(0); layout->setSpacing(6);
123   layout->addWidget(myGroup);
124   /***************************************************************/
125
126 #ifdef NO_PREVIEW
127   mainFrame()->CheckBoxPreview->setChecked( false );
128   mainFrame()->CheckBoxPreview->hide();
129 #endif
130   // Initialisation
131   Init();
132 }
133
134 //=================================================================================
135 // function : ~BooleanGUI_Dialog()
136 // purpose  : Destroys the object and frees any allocated resources
137 //=================================================================================
138 BooleanGUI_Dialog::~BooleanGUI_Dialog()
139 {
140 }
141
142 //=================================================================================
143 // function : Init()
144 // purpose  :
145 //=================================================================================
146 void BooleanGUI_Dialog::Init()
147 {
148   mainFrame()->GroupBoxPublish->show();
149
150   // init variables
151   myEditCurrentArgument = myGroup->LineEdit1;
152
153   myGroup->LineEdit1->setText("");
154   myGroup->LineEdit2->setText("");
155   myObject1.nullify();
156   reset();
157  
158   // signals and slots connections
159   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
160   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
161
162   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
163
164   if (!myGroup->PushButton2->isHidden()) {
165     connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
166   }
167
168   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
169            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
170
171   initName(mainFrame()->GroupConstructors->title());
172
173   setTabOrder(mainFrame()->GroupConstructors, mainFrame()->GroupBoxName);
174   setTabOrder(mainFrame()->GroupBoxName, mainFrame()->GroupMedium);
175   setTabOrder(mainFrame()->GroupMedium, mainFrame()->GroupButtons);
176
177   mainFrame()->RadioButton1->setFocus();
178
179   globalSelection(GEOM_ALLSHAPES);
180
181   myGroup->PushButton1->click();
182   SelectionIntoArgument();
183   resize(100,100);
184 }
185
186 //=================================================================================
187 // function : ClickOnOk()
188 // purpose  :
189 //=================================================================================
190 void BooleanGUI_Dialog::ClickOnOk()
191 {
192   setIsApplyAndClose( true );
193   if (ClickOnApply())
194     ClickOnCancel();
195 }
196
197 //=================================================================================
198 // function : ClickOnApply()
199 // purpose  :
200 //=================================================================================
201 bool BooleanGUI_Dialog::ClickOnApply()
202 {
203   if (!onAccept())
204     return false;
205
206   initName();
207   // activate selection and connect selection manager
208   myGroup->PushButton1->click();
209   return true;
210 }
211
212 //=================================================================================
213 // function : reset()
214 // purpose  : 
215 //=================================================================================
216 void BooleanGUI_Dialog::reset()
217 {
218   myObjects = new GEOM::ListOfGO;
219   myObjects->length( 0 );       
220 }
221
222 //=================================================================================
223 // function : singleSelection
224 // purpose  : Performs single selection. Called from SelectionIntoArgument()
225 //=================================================================================
226 void BooleanGUI_Dialog::singleSelection()
227 {
228   myEditCurrentArgument->setText("");
229
230   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_SHAPE );
231   TopoDS_Shape aShape;
232   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
233     QString aName = GEOMBase::GetName( aSelectedObject.get() );
234     myEditCurrentArgument->setText( aName );
235
236     // clear selection
237     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
238     myGeomGUI->getApp()->selectionMgr()->clearSelected();
239     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
240             this, SLOT(SelectionIntoArgument()));
241
242     if (myEditCurrentArgument == myGroup->LineEdit1) {
243       myObject1 = aSelectedObject;
244       if (!myGroup->PushButton2->isHidden() && !myObjects->length())
245         myGroup->PushButton2->click();
246     }
247     else if (myEditCurrentArgument == myGroup->LineEdit2) {
248       myObjects->length(1);
249       myObjects[0] = aSelectedObject.get();
250       if (!myObject1)
251         myGroup->PushButton1->click();
252     }
253   }
254   else {
255     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
256     else if (myEditCurrentArgument == myGroup->LineEdit2) reset();
257   }
258 }
259
260 //=================================================================================
261 // function : multipleSelection
262 // purpose  : Performs multiple selection. Called from SelectionIntoArgument()
263 //=================================================================================
264 void BooleanGUI_Dialog::multipleSelection()
265 {
266   myEditCurrentArgument->setText( "" );
267   reset();
268         
269   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
270   SALOME_ListIO aSelList;
271   aSelMgr->selectedObjects(aSelList);
272   myObjects->length(aSelList.Extent());
273
274   int i = 0;
275   for (SALOME_ListIteratorOfListIO anIt (aSelList); anIt.More(); anIt.Next()) {
276     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value() );
277
278     if ( !CORBA::is_nil( aSelectedObject ) ) {
279       myObjects[i++] = aSelectedObject;
280     }
281   }
282
283   myObjects->length( i );
284   if ( i == 1 ) {
285     myEditCurrentArgument->setText( GEOMBase::GetName( myObjects[0] ) );
286   } else if ( i > 0 ) {
287     myEditCurrentArgument->setText( QString::number( i ) + "_" + tr( "GEOM_OBJECTS" ) );
288   }
289 }
290
291 //=================================================================================
292 // function : SelectionIntoArgument()
293 // purpose  : Called when selection is changed or on dialog initialization or activation
294 //=================================================================================
295 void BooleanGUI_Dialog::SelectionIntoArgument()
296 {
297   if ( myOperation == BooleanGUI::SECTION ||
298       (myOperation == BooleanGUI::CUT &&
299        myEditCurrentArgument == myGroup->LineEdit1)) {
300     singleSelection();
301   } else {
302     multipleSelection();
303   }
304
305   processPreview();
306 }
307
308 //=================================================================================
309 // function : SetEditCurrentArgument()
310 // purpose  :
311 //=================================================================================
312 void BooleanGUI_Dialog::SetEditCurrentArgument()
313 {
314   QPushButton* send = (QPushButton*)sender();
315
316   if (send == myGroup->PushButton1) {
317     myEditCurrentArgument = myGroup->LineEdit1;
318
319     if (!myGroup->PushButton2->isHidden()) {
320       myGroup->PushButton2->setDown(false);
321       myGroup->LineEdit2->setEnabled(false);
322     }
323   }
324   else if (send == myGroup->PushButton2) {
325     myEditCurrentArgument = myGroup->LineEdit2;
326
327     myGroup->PushButton1->setDown(false);
328     myGroup->LineEdit1->setEnabled(false);
329   }
330
331   // enable line edit
332   myEditCurrentArgument->setEnabled(true);
333   myEditCurrentArgument->setFocus();
334   // after setFocus(), because it will be setDown(false) when loses focus
335   send->setDown(true);
336 }
337
338 //=================================================================================
339 // function : ActivateThisDialog()
340 // purpose  :
341 //=================================================================================
342 void BooleanGUI_Dialog::ActivateThisDialog()
343 {
344   GEOMBase_Skeleton::ActivateThisDialog();
345
346   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
347            this, SLOT( SelectionIntoArgument() ) );
348   processPreview();
349 }
350
351 //=================================================================================
352 // function : enterEvent()
353 // purpose  : when mouse enter onto the QWidget
354 //=================================================================================
355 void BooleanGUI_Dialog::enterEvent (QEvent*)
356 {
357   if (!mainFrame()->GroupConstructors->isEnabled())
358     ActivateThisDialog();
359 }
360
361 //=================================================================================
362 // function : createOperation
363 // purpose  :
364 //=================================================================================
365 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
366 {
367   return getGeomEngine()->GetIBooleanOperations(getStudyId());
368 }
369
370 //=================================================================================
371 // function : isValid
372 // purpose  :
373 //=================================================================================
374 bool BooleanGUI_Dialog::isValid (QString&)
375 {
376   bool isOK = false;
377
378   switch (myOperation) {
379     case BooleanGUI::FUSE:
380     case BooleanGUI::COMMON:
381       isOK = myObjects->length() > 1;
382     break;
383   case BooleanGUI::CUT:
384       isOK = myObject1 && myObjects->length();
385     break;
386   case BooleanGUI::SECTION:
387       isOK = myObject1 && (myObjects->length() == 1);
388     break;
389   default:
390     break;
391   }
392
393   return isOK;
394 }
395
396 //=================================================================================
397 // function : execute
398 // purpose  :
399 //=================================================================================
400 bool BooleanGUI_Dialog::execute (ObjectList& objects)
401 {
402   GEOM::GEOM_Object_var anObj;
403
404   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
405
406   switch (myOperation) {
407     case BooleanGUI::FUSE:
408       anObj = anOper->MakeFuseList(myObjects);
409     break;
410     case BooleanGUI::COMMON:
411       anObj = anOper->MakeCommonList(myObjects);
412     break;
413   case BooleanGUI::CUT:
414       anObj = anOper->MakeCutList(myObject1.get(), myObjects);
415     break;
416   case BooleanGUI::SECTION:
417       anObj = anOper->MakeBoolean(myObject1.get(), myObjects[0], myOperation);
418     break;
419   default:
420     break;
421   }
422
423   if (!anObj->_is_nil())
424     objects.push_back(anObj._retn());
425
426   return true;
427 }
428
429 //=================================================================================
430 // function : restoreSubShapes
431 // purpose  :
432 //=================================================================================
433 void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
434                                           SALOMEDS::SObject_ptr theSObject)
435 {
436   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
437     // empty list of arguments means that all arguments should be restored
438     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
439                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
440                                          /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT,
441                                          mainFrame()->CheckBoxAddPrefix->isChecked()); // ? false
442   }
443 }