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