Salome HOME
0022338: implement check self-intersection option for boolean operations
[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_2Sel2Spin1Check(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   myGroup->TextLabel3->hide();
122   myGroup->TextLabel4->hide();
123   myGroup->SpinBox_DX->hide();
124   myGroup->SpinBox_DY->hide();
125   myGroup->CheckButton1->setText(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
126
127   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
128   layout->setMargin(0); layout->setSpacing(6);
129   layout->addWidget(myGroup);
130   /***************************************************************/
131
132 #ifdef NO_PREVIEW
133   mainFrame()->CheckBoxPreview->setChecked( false );
134   mainFrame()->CheckBoxPreview->hide();
135 #endif
136   // Initialisation
137   Init();
138 }
139
140 //=================================================================================
141 // function : ~BooleanGUI_Dialog()
142 // purpose  : Destroys the object and frees any allocated resources
143 //=================================================================================
144 BooleanGUI_Dialog::~BooleanGUI_Dialog()
145 {
146 }
147
148 //=================================================================================
149 // function : Init()
150 // purpose  :
151 //=================================================================================
152 void BooleanGUI_Dialog::Init()
153 {
154   mainFrame()->GroupBoxPublish->show();
155
156   // init variables
157   myEditCurrentArgument = myGroup->LineEdit1;
158
159   myGroup->LineEdit1->setText("");
160   myGroup->LineEdit2->setText("");
161   myGroup->CheckButton1->setChecked(true);
162   myObject1.nullify();
163   reset();
164  
165   // signals and slots connections
166   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
167   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
168
169   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
170
171   if (!myGroup->PushButton2->isHidden()) {
172     connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
173   }
174
175   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
176            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
177
178   initName(mainFrame()->GroupConstructors->title());
179
180   setTabOrder(mainFrame()->GroupConstructors, mainFrame()->GroupBoxName);
181   setTabOrder(mainFrame()->GroupBoxName, mainFrame()->GroupMedium);
182   setTabOrder(mainFrame()->GroupMedium, mainFrame()->GroupButtons);
183
184   mainFrame()->RadioButton1->setFocus();
185
186   globalSelection(GEOM_ALLSHAPES);
187
188   myGroup->PushButton1->click();
189   SelectionIntoArgument();
190   resize(100,100);
191 }
192
193 //=================================================================================
194 // function : ClickOnOk()
195 // purpose  :
196 //=================================================================================
197 void BooleanGUI_Dialog::ClickOnOk()
198 {
199   setIsApplyAndClose( true );
200   if (ClickOnApply())
201     ClickOnCancel();
202 }
203
204 //=================================================================================
205 // function : ClickOnApply()
206 // purpose  :
207 //=================================================================================
208 bool BooleanGUI_Dialog::ClickOnApply()
209 {
210   if (!onAccept())
211     return false;
212
213   initName();
214   // activate selection and connect selection manager
215   myGroup->PushButton1->click();
216   return true;
217 }
218
219 //=================================================================================
220 // function : reset()
221 // purpose  : 
222 //=================================================================================
223 void BooleanGUI_Dialog::reset()
224 {
225   myObjects = new GEOM::ListOfGO;
226   myObjects->length( 0 );       
227 }
228
229 //=================================================================================
230 // function : singleSelection
231 // purpose  : Performs single selection. Called from SelectionIntoArgument()
232 //=================================================================================
233 void BooleanGUI_Dialog::singleSelection()
234 {
235   myEditCurrentArgument->setText("");
236
237   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_SHAPE );
238   TopoDS_Shape aShape;
239   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
240     QString aName = GEOMBase::GetName( aSelectedObject.get() );
241     myEditCurrentArgument->setText( aName );
242
243     // clear selection
244     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
245     myGeomGUI->getApp()->selectionMgr()->clearSelected();
246     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
247             this, SLOT(SelectionIntoArgument()));
248
249     if (myEditCurrentArgument == myGroup->LineEdit1) {
250       myObject1 = aSelectedObject;
251       if (!myGroup->PushButton2->isHidden() && !myObjects->length())
252         myGroup->PushButton2->click();
253     }
254     else if (myEditCurrentArgument == myGroup->LineEdit2) {
255       myObjects->length(1);
256       myObjects[0] = aSelectedObject.get();
257       if (!myObject1)
258         myGroup->PushButton1->click();
259     }
260   }
261   else {
262     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
263     else if (myEditCurrentArgument == myGroup->LineEdit2) reset();
264   }
265 }
266
267 //=================================================================================
268 // function : multipleSelection
269 // purpose  : Performs multiple selection. Called from SelectionIntoArgument()
270 //=================================================================================
271 void BooleanGUI_Dialog::multipleSelection()
272 {
273   myEditCurrentArgument->setText( "" );
274   reset();
275         
276   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
277   SALOME_ListIO aSelList;
278   aSelMgr->selectedObjects(aSelList);
279   myObjects->length(aSelList.Extent());
280
281   int i = 0;
282   for (SALOME_ListIteratorOfListIO anIt (aSelList); anIt.More(); anIt.Next()) {
283     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value() );
284
285     if ( !CORBA::is_nil( aSelectedObject ) ) {
286       myObjects[i++] = aSelectedObject;
287     }
288   }
289
290   myObjects->length( i );
291   if ( i == 1 ) {
292     myEditCurrentArgument->setText( GEOMBase::GetName( myObjects[0] ) );
293   } else if ( i > 0 ) {
294     myEditCurrentArgument->setText( QString::number( i ) + "_" + tr( "GEOM_OBJECTS" ) );
295   }
296 }
297
298 //=================================================================================
299 // function : SelectionIntoArgument()
300 // purpose  : Called when selection is changed or on dialog initialization or activation
301 //=================================================================================
302 void BooleanGUI_Dialog::SelectionIntoArgument()
303 {
304   if ( myOperation == BooleanGUI::SECTION ||
305       (myOperation == BooleanGUI::CUT &&
306        myEditCurrentArgument == myGroup->LineEdit1)) {
307     singleSelection();
308   } else {
309     multipleSelection();
310   }
311
312   processPreview();
313 }
314
315 //=================================================================================
316 // function : SetEditCurrentArgument()
317 // purpose  :
318 //=================================================================================
319 void BooleanGUI_Dialog::SetEditCurrentArgument()
320 {
321   QPushButton* send = (QPushButton*)sender();
322
323   if (send == myGroup->PushButton1) {
324     myEditCurrentArgument = myGroup->LineEdit1;
325
326     if (!myGroup->PushButton2->isHidden()) {
327       myGroup->PushButton2->setDown(false);
328       myGroup->LineEdit2->setEnabled(false);
329     }
330   }
331   else if (send == myGroup->PushButton2) {
332     myEditCurrentArgument = myGroup->LineEdit2;
333
334     myGroup->PushButton1->setDown(false);
335     myGroup->LineEdit1->setEnabled(false);
336   }
337
338   // enable line edit
339   myEditCurrentArgument->setEnabled(true);
340   myEditCurrentArgument->setFocus();
341   // after setFocus(), because it will be setDown(false) when loses focus
342   send->setDown(true);
343 }
344
345 //=================================================================================
346 // function : ActivateThisDialog()
347 // purpose  :
348 //=================================================================================
349 void BooleanGUI_Dialog::ActivateThisDialog()
350 {
351   GEOMBase_Skeleton::ActivateThisDialog();
352
353   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
354            this, SLOT( SelectionIntoArgument() ) );
355   processPreview();
356 }
357
358 //=================================================================================
359 // function : enterEvent()
360 // purpose  : when mouse enter onto the QWidget
361 //=================================================================================
362 void BooleanGUI_Dialog::enterEvent (QEvent*)
363 {
364   if (!mainFrame()->GroupConstructors->isEnabled())
365     ActivateThisDialog();
366 }
367
368 //=================================================================================
369 // function : createOperation
370 // purpose  :
371 //=================================================================================
372 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
373 {
374   return getGeomEngine()->GetIBooleanOperations(getStudyId());
375 }
376
377 //=================================================================================
378 // function : isValid
379 // purpose  :
380 //=================================================================================
381 bool BooleanGUI_Dialog::isValid (QString&)
382 {
383   bool isOK = false;
384
385   switch (myOperation) {
386     case BooleanGUI::FUSE:
387     case BooleanGUI::COMMON:
388       isOK = myObjects->length() > 1;
389     break;
390   case BooleanGUI::CUT:
391       isOK = myObject1 && myObjects->length();
392     break;
393   case BooleanGUI::SECTION:
394       isOK = myObject1 && (myObjects->length() == 1);
395     break;
396   default:
397     break;
398   }
399
400   return isOK;
401 }
402
403 //=================================================================================
404 // function : execute
405 // purpose  :
406 //=================================================================================
407 bool BooleanGUI_Dialog::execute (ObjectList& objects)
408 {
409   GEOM::GEOM_Object_var anObj;
410
411   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
412   const bool isCheckSelfInte = myGroup->CheckButton1->isChecked();
413
414   switch (myOperation) {
415     case BooleanGUI::FUSE:
416       anObj = anOper->MakeFuseList(myObjects, isCheckSelfInte);
417     break;
418     case BooleanGUI::COMMON:
419       anObj = anOper->MakeCommonList(myObjects, isCheckSelfInte);
420     break;
421   case BooleanGUI::CUT:
422       anObj =
423         anOper->MakeCutList(myObject1.get(), myObjects, isCheckSelfInte);
424     break;
425   case BooleanGUI::SECTION:
426       anObj = anOper->MakeBoolean
427         (myObject1.get(), myObjects[0], myOperation, isCheckSelfInte);
428     break;
429   default:
430     break;
431   }
432
433   if (!anObj->_is_nil())
434     objects.push_back(anObj._retn());
435
436   return true;
437 }
438
439 //=================================================================================
440 // function : restoreSubShapes
441 // purpose  :
442 //=================================================================================
443 void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
444                                           SALOMEDS::SObject_ptr theSObject)
445 {
446   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
447     // empty list of arguments means that all arguments should be restored
448     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
449                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
450                                          /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT,
451                                          mainFrame()->CheckBoxAddPrefix->isChecked()); // ? false
452   }
453 }