Salome HOME
85e1f19902c4a0b7eb08b2f5041040dd25bd91ad
[modules/geom.git] / src / BooleanGUI / BooleanGUI_Dialog.cxx
1 //  Copyright (C) 2007-2008  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : BooleanGUI_Dialog.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "BooleanGUI.h"
27 #include "BooleanGUI_Dialog.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 //=================================================================================
39 // class    : BooleanGUI_Dialog()
40 // purpose  : Constructs a BooleanGUI_Dialog which is a child of 'parent', with the
41 //            name 'name' and widget flags set to 'f'.
42 //            The dialog will by default be modeless, unless you set 'modal' to
43 //            TRUE to construct a modal dialog.
44 //=================================================================================
45 BooleanGUI_Dialog::BooleanGUI_Dialog (const int theOperation, GeometryGUI* theGeometryGUI,
46                                       QWidget* parent, bool modal, Qt::WindowFlags fl)
47   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
48     myOperation(theOperation)
49 {
50   QPixmap image0;
51   QString aTitle, aCaption;
52   switch (myOperation) {
53   case BooleanGUI::COMMON:
54     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_COMMON")));
55     aTitle = tr("GEOM_COMMON");
56     aCaption = tr("GEOM_COMMON_TITLE");
57     setHelpFileName("common_operation_page.html");
58     break;
59   case BooleanGUI::CUT:
60     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CUT")));
61     aTitle = tr("GEOM_CUT");
62     aCaption = tr("GEOM_CUT_TITLE");
63     setHelpFileName("cut_operation_page.html");
64     break;
65   case BooleanGUI::FUSE:
66     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FUSE")));
67     aTitle = tr("GEOM_FUSE");
68     aCaption = tr("GEOM_FUSE_TITLE");
69     setHelpFileName("fuse_operation_page.html");
70     break;
71   case BooleanGUI::SECTION:
72     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SECTION")));
73     aTitle = tr("GEOM_SECTION");
74     aCaption = tr("GEOM_SECTION_TITLE");
75     setHelpFileName("section_opeartion_page.html");
76     break;
77   }
78   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
79
80   setWindowTitle(aCaption);
81
82   /***************************************************************/
83   mainFrame()->GroupConstructors->setTitle(aTitle);
84   mainFrame()->RadioButton1->setIcon(image0);
85   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
86   mainFrame()->RadioButton2->close();
87   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
88   mainFrame()->RadioButton3->close();
89
90   myGroup = new DlgRef_2Sel(centralWidget());
91
92   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
93   if (myOperation != BooleanGUI::CUT) {
94     myGroup->TextLabel1->setText(tr("GEOM_OBJECT_I").arg(1));
95     myGroup->TextLabel2->setText(tr("GEOM_OBJECT_I").arg(2));
96   }
97   else {
98     myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
99     myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
100   }
101
102   myGroup->PushButton1->setIcon(image1);
103   myGroup->PushButton2->setIcon(image1);
104   myGroup->LineEdit1->setReadOnly(true);
105   myGroup->LineEdit2->setReadOnly(true);
106
107   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
108   layout->setMargin(0); layout->setSpacing(6);
109   layout->addWidget(myGroup);
110   /***************************************************************/
111
112   // Initialisation
113   Init();
114 }
115
116 //=================================================================================
117 // function : ~BooleanGUI_Dialog()
118 // purpose  : Destroys the object and frees any allocated resources
119 //=================================================================================
120 BooleanGUI_Dialog::~BooleanGUI_Dialog()
121 {
122 }
123
124 //=================================================================================
125 // function : Init()
126 // purpose  :
127 //=================================================================================
128 void BooleanGUI_Dialog::Init()
129 {
130   mainFrame()->GroupBoxPublish->show();
131
132   // init variables
133   myEditCurrentArgument = myGroup->LineEdit1;
134
135   myGroup->LineEdit1->setText("");
136   myGroup->LineEdit2->setText("");
137   myObject1 = myObject2 = GEOM::GEOM_Object::_nil();
138  
139   // signals and slots connections
140   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
141   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
142
143   connect(myGroup->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
144   connect(myGroup->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145
146   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
147   connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
148
149   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
150            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
151
152   initName(mainFrame()->GroupConstructors->title());
153
154   setTabOrder(mainFrame()->GroupConstructors, mainFrame()->GroupBoxName);
155   setTabOrder(mainFrame()->GroupBoxName, mainFrame()->GroupMedium);
156   setTabOrder(mainFrame()->GroupMedium, mainFrame()->GroupButtons);
157
158   mainFrame()->RadioButton1->setFocus();
159
160   globalSelection(GEOM_ALLSHAPES);
161
162   myGroup->PushButton1->click();
163   SelectionIntoArgument();
164 }
165
166 //=================================================================================
167 // function : ClickOnOk()
168 // purpose  :
169 //=================================================================================
170 void BooleanGUI_Dialog::ClickOnOk()
171 {
172   if (ClickOnApply())
173     ClickOnCancel();
174 }
175
176 //=================================================================================
177 // function : ClickOnApply()
178 // purpose  :
179 //=================================================================================
180 bool BooleanGUI_Dialog::ClickOnApply()
181 {
182   if (!onAccept())
183     return false;
184
185   initName();
186   // activate selection and connect selection manager
187   myGroup->PushButton1->click();
188   return true;
189 }
190
191 //=================================================================================
192 // function : SelectionIntoArgument()
193 // purpose  : Called when selection is changed or on dialog initialization or activation
194 //=================================================================================
195 void BooleanGUI_Dialog::SelectionIntoArgument()
196 {
197   myEditCurrentArgument->setText("");
198
199   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
200   SALOME_ListIO aSelList;
201   aSelMgr->selectedObjects(aSelList);
202
203   if (aSelList.Extent() != 1) {
204     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1 = GEOM::GEOM_Object::_nil();
205     else if (myEditCurrentArgument == myGroup->LineEdit2) myObject2 = GEOM::GEOM_Object::_nil();
206     return;
207   }
208
209   // nbSel == 1
210   Standard_Boolean aRes = Standard_False;
211   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aRes);
212   if (!CORBA::is_nil(aSelectedObject) && aRes && GEOMBase::IsShape(aSelectedObject))
213   {
214     myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject));
215
216     // clear selection
217     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
218     myGeomGUI->getApp()->selectionMgr()->clearSelected();
219     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
220             this, SLOT(SelectionIntoArgument()));
221
222     if (myEditCurrentArgument == myGroup->LineEdit1) {
223       myObject1 = aSelectedObject;
224       if (myObject2->_is_nil())
225         myGroup->PushButton2->click();
226     }
227     else if (myEditCurrentArgument == myGroup->LineEdit2) {
228       myObject2 = aSelectedObject;
229       if (myObject1->_is_nil())
230         myGroup->PushButton1->click();
231     }
232   }
233 }
234
235 //=================================================================================
236 // function : SetEditCurrentArgument()
237 // purpose  :
238 //=================================================================================
239 void BooleanGUI_Dialog::SetEditCurrentArgument()
240 {
241   QPushButton* send = (QPushButton*)sender();
242
243   if (send == myGroup->PushButton1) {
244     myEditCurrentArgument = myGroup->LineEdit1;
245
246     myGroup->PushButton2->setDown(false);
247     myGroup->LineEdit2->setEnabled(false);
248   }
249   else if (send == myGroup->PushButton2) {
250     myEditCurrentArgument = myGroup->LineEdit2;
251
252     myGroup->PushButton1->setDown(false);
253     myGroup->LineEdit1->setEnabled(false);
254   }
255
256   // enable line edit
257   myEditCurrentArgument->setEnabled(true);
258   myEditCurrentArgument->setFocus();
259   // after setFocus(), because it will be setDown(false) when loses focus
260   send->setDown(true);
261 }
262
263 //=================================================================================
264 // function : LineEditReturnPressed()
265 // purpose  :
266 //=================================================================================
267 void BooleanGUI_Dialog::LineEditReturnPressed()
268 {
269   QLineEdit* send = (QLineEdit*)sender();
270   if (send == myGroup->LineEdit1 || send == myGroup->LineEdit2) {
271     myEditCurrentArgument = send;
272     GEOMBase_Skeleton::LineEditReturnPressed();
273   }
274 }
275
276 //=================================================================================
277 // function : ActivateThisDialog()
278 // purpose  :
279 //=================================================================================
280 void BooleanGUI_Dialog::ActivateThisDialog()
281 {
282   GEOMBase_Skeleton::ActivateThisDialog();
283
284   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
285            this, SLOT( SelectionIntoArgument() ) );
286   displayPreview();
287 }
288
289 //=================================================================================
290 // function : enterEvent()
291 // purpose  : when mouse enter onto the QWidget
292 //=================================================================================
293 void BooleanGUI_Dialog::enterEvent (QEvent*)
294 {
295   if (!mainFrame()->GroupConstructors->isEnabled())
296     ActivateThisDialog();
297 }
298
299 //=================================================================================
300 // function : createOperation
301 // purpose  :
302 //=================================================================================
303 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
304 {
305   return getGeomEngine()->GetIBooleanOperations(getStudyId());
306 }
307
308 //=================================================================================
309 // function : isValid
310 // purpose  :
311 //=================================================================================
312 bool BooleanGUI_Dialog::isValid (QString&)
313 {
314   //Handle(SALOME_InteractiveObject) IO = firstIObject();
315   //Standard_Boolean testResult;
316   //GEOM::GEOM_Object_var anObject = GEOMBase::ConvertIOinGEOMObject(IO, testResult);
317   //if (!testResult || anObject->_is_nil())
318   //  return false;
319
320   return !CORBA::is_nil(myObject1) && !CORBA::is_nil(myObject2);
321 }
322
323 //=================================================================================
324 // function : execute
325 // purpose  :
326 //=================================================================================
327 bool BooleanGUI_Dialog::execute (ObjectList& objects)
328 {
329   GEOM::GEOM_Object_var anObj;
330
331   anObj = GEOM::GEOM_IBooleanOperations::_narrow(getOperation())->
332     MakeBoolean(myObject1, myObject2, myOperation);
333   if (!anObj->_is_nil())
334     objects.push_back(anObj._retn());
335
336   return true;
337 }
338
339 //=================================================================================
340 // function : restoreSubShapes
341 // purpose  :
342 //=================================================================================
343 void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
344                                           SALOMEDS::SObject_ptr theSObject)
345 {
346   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
347     // empty list of arguments means that all arguments should be restored
348     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
349                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
350                                          /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT); // ? false
351   }
352 }