Salome HOME
d8a7743d84f00d0a5ebc2c3ca271ca129db36965
[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
31 #include "GeometryGUI.h"
32 #include "GEOMBase.h"
33
34 #include "SUIT_ResourceMgr.h"
35 #include "SUIT_Session.h"
36 #include "SalomeApp_Application.h"
37 #include "LightApp_SelectionMgr.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, Qt::WindowFlags 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.htm");
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.htm");
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.htm");
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.htm");
80                         break;
81   }
82   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
83
84   setWindowTitle( aCaption );
85
86   /***************************************************************/
87   GroupConstructors->setTitle( aTitle );
88   RadioButton1->setIcon( image0 );
89   RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
90   RadioButton2->close();
91   RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
92   RadioButton3->close();
93
94   myGroup = new Ui::DlgRef_2Sel_QTD();
95   QWidget* aMyGroupWidget = new QWidget(this);
96   myGroup->setupUi(aMyGroupWidget);
97   aMyGroupWidget->setObjectName("GroupCommon");
98
99   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
100   if ( myOperation != BooleanGUI::CUT )
101   {
102         myGroup->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
103         myGroup->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
104   }
105   else
106   {
107         myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
108         myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
109   }
110   
111   myGroup->PushButton1->setIcon(image1);
112   myGroup->PushButton2->setIcon(image1);
113   myGroup->LineEdit1->setReadOnly( true );
114   myGroup->LineEdit2->setReadOnly( true );
115
116   gridLayout1->addWidget(aMyGroupWidget, 2, 0);
117   /***************************************************************/
118
119   /* Initialisation */
120   Init();
121 }
122
123
124 //=================================================================================
125 // function : ~BooleanGUI_Dialog()
126 // purpose  : Destroys the object and frees any allocated resources
127 //=================================================================================
128 BooleanGUI_Dialog::~BooleanGUI_Dialog()
129 {
130 }
131
132
133 //=================================================================================
134 // function : Init()
135 // purpose  :
136 //=================================================================================
137 void BooleanGUI_Dialog::Init()
138 {
139   /* init variables */
140   myEditCurrentArgument = myGroup->LineEdit1;
141
142    /* signals and slots connections */
143   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
144   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
145
146   connect(myGroup->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
147   connect(myGroup->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
148
149   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
150   connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
151
152   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
153           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
154
155   initName( GroupConstructors->title().toLatin1().constData() );
156
157   globalSelection( GEOM_ALLSHAPES );
158 }
159
160
161 //=================================================================================
162 // function : ClickOnOk()
163 // purpose  :
164 //=================================================================================
165 void BooleanGUI_Dialog::ClickOnOk()
166 {
167   if ( ClickOnApply() )
168     ClickOnCancel();
169 }
170
171
172 //=================================================================================
173 // function : ClickOnApply()
174 // purpose  :
175 //=================================================================================
176 bool BooleanGUI_Dialog::ClickOnApply()
177 {
178   if ( !onAccept() )
179     return false;  
180
181   initName();
182   return true;
183 }
184
185
186 //=================================================================================
187 // function : SelectionIntoArgument()
188 // purpose  : Called when selection has changed
189 //=================================================================================
190 void BooleanGUI_Dialog::SelectionIntoArgument()
191 {
192   myEditCurrentArgument->setText( "" );
193
194   if ( IObjectCount() != 1 )
195   {
196     if      ( myEditCurrentArgument == myGroup->LineEdit1 )   myObject1 = GEOM::GEOM_Object::_nil();
197     else if ( myEditCurrentArgument == myGroup->LineEdit2 )   myObject2 = GEOM::GEOM_Object::_nil();
198     return;
199   }
200
201   // nbSel == 1
202   Standard_Boolean aRes = Standard_False;
203   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
204   if ( !CORBA::is_nil( aSelectedObject ) && aRes && GEOMBase::IsShape( aSelectedObject ) )
205   {
206     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
207     if      ( myEditCurrentArgument == myGroup->LineEdit1 )   myObject1 = aSelectedObject;
208     else if ( myEditCurrentArgument == myGroup->LineEdit2 )   myObject2 = aSelectedObject;
209   }
210 }
211
212
213 //=================================================================================
214 // function : SetEditCurrentArgument()
215 // purpose  :
216 //=================================================================================
217 void BooleanGUI_Dialog::SetEditCurrentArgument()
218 {
219   QPushButton* send = (QPushButton*)sender();
220
221   if      ( send == myGroup->PushButton1 ) myEditCurrentArgument = myGroup->LineEdit1;
222   else if ( send == myGroup->PushButton2 ) myEditCurrentArgument = myGroup->LineEdit2;
223
224   myEditCurrentArgument->setFocus();
225   SelectionIntoArgument();
226 }
227
228
229 //=================================================================================
230 // function : LineEditReturnPressed()
231 // purpose  :
232 //=================================================================================
233 void BooleanGUI_Dialog::LineEditReturnPressed()
234 {
235   QLineEdit* send = (QLineEdit*)sender();
236   if ( send == myGroup->LineEdit1 || send == myGroup->LineEdit2 )
237   {
238     myEditCurrentArgument = send;
239     GEOMBase_Skeleton::LineEditReturnPressed();
240   }
241 }
242
243
244 //=================================================================================
245 // function : ActivateThisDialog()
246 // purpose  :
247 //=================================================================================
248 void BooleanGUI_Dialog::ActivateThisDialog()
249 {
250   GEOMBase_Skeleton::ActivateThisDialog();
251   globalSelection( GEOM_ALLSHAPES );
252   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
253           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
254 }
255
256
257 //=================================================================================
258 // function : enterEvent()
259 // purpose  : when mouse enter onto the QWidget
260 //=================================================================================
261 void BooleanGUI_Dialog::enterEvent(QEvent * e)
262 {
263   if ( !GroupConstructors->isEnabled() )
264     ActivateThisDialog();
265 }
266
267 //=================================================================================
268 // function : createOperation
269 // purpose  :
270 //=================================================================================
271 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
272 {
273   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
274 }
275
276 //=================================================================================
277 // function : isValid
278 // purpose  :
279 //=================================================================================
280 bool BooleanGUI_Dialog::isValid( QString& msg )
281 {
282   return !CORBA::is_nil( myObject1 ) && !CORBA::is_nil( myObject2 );
283 }
284
285 //=================================================================================
286 // function : execute
287 // purpose  :
288 //=================================================================================
289 bool BooleanGUI_Dialog::execute( ObjectList& objects )
290 {
291   GEOM::GEOM_Object_var anObj;
292  
293   anObj = GEOM::GEOM_IBooleanOperations::_narrow( getOperation() )->MakeBoolean( myObject1, myObject2, myOperation );
294   if ( !anObj->_is_nil() )
295     objects.push_back( anObj._retn() );
296
297   return true;
298 }