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