Salome HOME
Revert "Synchronize adm files"
[modules/geom.git] / src / BooleanGUI / BooleanGUI_Dialog.cxx
1 // Copyright (C) 2007-2014  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_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_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()));
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   myGroup->PushButton1->click();
203   SelectionIntoArgument();
204   resize(100,100);
205 }
206
207 //=================================================================================
208 // function : ClickOnOk()
209 // purpose  :
210 //=================================================================================
211 void BooleanGUI_Dialog::ClickOnOk()
212 {
213   setIsApplyAndClose( true );
214   if (ClickOnApply())
215     ClickOnCancel();
216 }
217
218 //=================================================================================
219 // function : ClickOnApply()
220 // purpose  :
221 //=================================================================================
222 bool BooleanGUI_Dialog::ClickOnApply()
223 {
224   if (!onAccept())
225     return false;
226
227   initName();
228   // activate selection and connect selection manager
229   myGroup->PushButton1->click();
230   return true;
231 }
232
233 //=================================================================================
234 // function : reset()
235 // purpose  : 
236 //=================================================================================
237 void BooleanGUI_Dialog::reset()
238 {
239   myObjects = new GEOM::ListOfGO;
240   myObjects->length( 0 );       
241 }
242
243 //=================================================================================
244 // function : singleSelection
245 // purpose  : Performs single selection. Called from SelectionIntoArgument()
246 //=================================================================================
247 void BooleanGUI_Dialog::singleSelection()
248 {
249   myEditCurrentArgument->setText("");
250
251   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_SHAPE );
252   TopoDS_Shape aShape;
253   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
254     QString aName = GEOMBase::GetName( aSelectedObject.get() );
255     myEditCurrentArgument->setText( aName );
256
257     // clear selection
258     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
259     myGeomGUI->getApp()->selectionMgr()->clearSelected();
260     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
261             this, SLOT(SelectionIntoArgument()));
262
263     if (myEditCurrentArgument == myGroup->LineEdit1) {
264       myObject1 = aSelectedObject;
265       if (!myGroup->PushButton2->isHidden() && !myObjects->length())
266         myGroup->PushButton2->click();
267     }
268     else if (myEditCurrentArgument == myGroup->LineEdit2) {
269       myObjects->length(1);
270       myObjects[0] = aSelectedObject.get();
271       if (!myObject1)
272         myGroup->PushButton1->click();
273     }
274   }
275   else {
276     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
277     else if (myEditCurrentArgument == myGroup->LineEdit2) reset();
278   }
279 }
280
281 //=================================================================================
282 // function : multipleSelection
283 // purpose  : Performs multiple selection. Called from SelectionIntoArgument()
284 //=================================================================================
285 void BooleanGUI_Dialog::multipleSelection()
286 {
287   myEditCurrentArgument->setText( "" );
288   reset();
289         
290   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
291   SALOME_ListIO aSelList;
292   aSelMgr->selectedObjects(aSelList);
293   myObjects->length(aSelList.Extent());
294
295   int i = 0;
296   for (SALOME_ListIteratorOfListIO anIt (aSelList); anIt.More(); anIt.Next()) {
297     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value() );
298
299     if ( !CORBA::is_nil( aSelectedObject ) ) {
300       myObjects[i++] = aSelectedObject;
301     }
302   }
303
304   myObjects->length( i );
305   if ( i == 1 ) {
306     myEditCurrentArgument->setText( GEOMBase::GetName( myObjects[0] ) );
307   } else if ( i > 0 ) {
308     myEditCurrentArgument->setText( QString::number( i ) + "_" + tr( "GEOM_OBJECTS" ) );
309   }
310 }
311
312 //=================================================================================
313 // function : SelectionIntoArgument()
314 // purpose  : Called when selection is changed or on dialog initialization or activation
315 //=================================================================================
316 void BooleanGUI_Dialog::SelectionIntoArgument()
317 {
318   if ( myOperation == BooleanGUI::SECTION ||
319       (myOperation == BooleanGUI::CUT &&
320        myEditCurrentArgument == myGroup->LineEdit1)) {
321     singleSelection();
322   } else {
323     multipleSelection();
324   }
325
326   processPreview();
327 }
328
329 //=================================================================================
330 // function : SetEditCurrentArgument()
331 // purpose  :
332 //=================================================================================
333 void BooleanGUI_Dialog::SetEditCurrentArgument()
334 {
335   QPushButton* send = (QPushButton*)sender();
336
337   if (send == myGroup->PushButton1) {
338     myEditCurrentArgument = myGroup->LineEdit1;
339
340     if (!myGroup->PushButton2->isHidden()) {
341       myGroup->PushButton2->setDown(false);
342       myGroup->LineEdit2->setEnabled(false);
343     }
344   }
345   else if (send == myGroup->PushButton2) {
346     myEditCurrentArgument = myGroup->LineEdit2;
347
348     myGroup->PushButton1->setDown(false);
349     myGroup->LineEdit1->setEnabled(false);
350   }
351
352   // enable line edit
353   myEditCurrentArgument->setEnabled(true);
354   myEditCurrentArgument->setFocus();
355   // after setFocus(), because it will be setDown(false) when loses focus
356   send->setDown(true);
357 }
358
359 //=================================================================================
360 // function : ActivateThisDialog()
361 // purpose  :
362 //=================================================================================
363 void BooleanGUI_Dialog::ActivateThisDialog()
364 {
365   GEOMBase_Skeleton::ActivateThisDialog();
366
367   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
368            this, SLOT( SelectionIntoArgument() ) );
369   processPreview();
370 }
371
372 //=================================================================================
373 // function : enterEvent()
374 // purpose  : when mouse enter onto the QWidget
375 //=================================================================================
376 void BooleanGUI_Dialog::enterEvent (QEvent*)
377 {
378   if (!mainFrame()->GroupConstructors->isEnabled())
379     ActivateThisDialog();
380 }
381
382 //=================================================================================
383 // function : createOperation
384 // purpose  :
385 //=================================================================================
386 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
387 {
388   return getGeomEngine()->GetIBooleanOperations(getStudyId());
389 }
390
391 //=================================================================================
392 // function : isValid
393 // purpose  :
394 //=================================================================================
395 bool BooleanGUI_Dialog::isValid (QString&)
396 {
397   bool isOK = false;
398
399   switch (myOperation) {
400     case BooleanGUI::FUSE:
401     case BooleanGUI::COMMON:
402       isOK = myObjects->length() > 1;
403     break;
404   case BooleanGUI::CUT:
405       isOK = myObject1 && myObjects->length();
406     break;
407   case BooleanGUI::SECTION:
408       isOK = myObject1 && (myObjects->length() == 1);
409     break;
410   default:
411     break;
412   }
413
414   return isOK;
415 }
416
417 //=================================================================================
418 // function : execute
419 // purpose  :
420 //=================================================================================
421 bool BooleanGUI_Dialog::execute (ObjectList& objects)
422 {
423   GEOM::GEOM_Object_var anObj;
424
425   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
426   const bool isCheckSelfInte = myGroup->CheckBox1->isChecked();
427
428   switch (myOperation) {
429     case BooleanGUI::FUSE:
430       {
431         const bool isRmExtraEdges = myGroup->CheckBox2->isChecked();
432
433         anObj = anOper->MakeFuseList
434           (myObjects, isCheckSelfInte, isRmExtraEdges);
435       }
436     break;
437     case BooleanGUI::COMMON:
438       anObj = anOper->MakeCommonList(myObjects, isCheckSelfInte);
439     break;
440   case BooleanGUI::CUT:
441       anObj =
442         anOper->MakeCutList(myObject1.get(), myObjects, isCheckSelfInte);
443     break;
444   case BooleanGUI::SECTION:
445       anObj = anOper->MakeBoolean
446         (myObject1.get(), myObjects[0], myOperation, isCheckSelfInte);
447     break;
448   default:
449     break;
450   }
451
452   if (!anObj->_is_nil())
453     objects.push_back(anObj._retn());
454
455   return true;
456 }
457
458 //=================================================================================
459 // function : restoreSubShapes
460 // purpose  :
461 //=================================================================================
462 void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
463                                           SALOMEDS::SObject_ptr theSObject)
464 {
465   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
466     // empty list of arguments means that all arguments should be restored
467     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
468                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
469                                          /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT,
470                                          mainFrame()->CheckBoxAddPrefix->isChecked()); // ? false
471   }
472 }