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