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