Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : BooleanGUI_Dialog.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "BooleanGUI_Dialog.h"
31 #include "BooleanGUI.h"
32 #include "DlgRef_2Sel_QTD.h"
33
34 #include "SUIT_Session.h"
35 #include "SalomeApp_Application.h"
36 #include "SalomeApp_SelectionMgr.h"
37
38 #include <qlabel.h>
39
40 //=================================================================================
41 // class    : BooleanGUI_Dialog()
42 // purpose  : Constructs a BooleanGUI_Dialog which is a child of 'parent', with the
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 BooleanGUI_Dialog::BooleanGUI_Dialog( const int theOperation, QWidget* parent, const char* name, bool modal, WFlags fl)
48   :GEOMBase_Skeleton(parent, name, modal, fl),
49    myOperation( theOperation )
50 {
51   QPixmap image0;
52   QString aTitle, aCaption;
53   switch ( myOperation )
54   {
55         case BooleanGUI::COMMON:
56           image0 = QPixmap( SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_COMMON")));
57                         aTitle = tr("GEOM_COMMON");
58                         aCaption = tr("GEOM_COMMON_TITLE");
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                         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                         break;
70                 case BooleanGUI::SECTION:
71           image0 = QPixmap( SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SECTION")));
72                         aTitle = tr("GEOM_SECTION");
73                         aCaption = tr("GEOM_SECTION_TITLE");
74                         break;
75         }
76   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
77
78   setCaption( aCaption );
79
80   /***************************************************************/
81   GroupConstructors->setTitle( aTitle );
82   RadioButton1->setPixmap( image0 );
83   RadioButton2->close(TRUE);
84   RadioButton3->close(TRUE);
85
86   myGroup = new DlgRef_2Sel_QTD(this, "GroupCommon");
87   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
88   if ( myOperation != BooleanGUI::CUT )
89   {
90         myGroup->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
91         myGroup->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
92   }
93   else
94   {
95         myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
96         myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
97   }
98   
99   myGroup->PushButton1->setPixmap(image1);
100   myGroup->PushButton2->setPixmap(image1);
101   myGroup->LineEdit1->setReadOnly( true );
102   myGroup->LineEdit2->setReadOnly( true );
103
104   Layout1->addWidget(myGroup, 2, 0);
105   /***************************************************************/
106
107   /* Initialisation */
108   Init();
109 }
110
111
112 //=================================================================================
113 // function : ~BooleanGUI_Dialog()
114 // purpose  : Destroys the object and frees any allocated resources
115 //=================================================================================
116 BooleanGUI_Dialog::~BooleanGUI_Dialog()
117 {
118 }
119
120
121
122 //=================================================================================
123 // function : Init()
124 // purpose  :
125 //=================================================================================
126 void BooleanGUI_Dialog::Init()
127 {
128   /* init variables */
129   myEditCurrentArgument = myGroup->LineEdit1;
130
131    /* signals and slots connections */
132   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
133   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
134
135   connect(myGroup->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
136   connect(myGroup->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
137
138   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
139   connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140
141   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
142           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
143
144   initName( GroupConstructors->title() );
145
146   globalSelection( GEOM_ALLSHAPES );
147 }
148
149
150 //=================================================================================
151 // function : ClickOnOk()
152 // purpose  :
153 //=================================================================================
154 void BooleanGUI_Dialog::ClickOnOk()
155 {
156   if ( ClickOnApply() )
157     ClickOnCancel();
158 }
159
160
161 //=================================================================================
162 // function : ClickOnApply()
163 // purpose  :
164 //=================================================================================
165 bool BooleanGUI_Dialog::ClickOnApply()
166 {
167   if ( !onAccept() )
168     return false;  
169
170   initName();
171   return true;
172 }
173
174
175 //=================================================================================
176 // function : SelectionIntoArgument()
177 // purpose  : Called when selection has changed
178 //=================================================================================
179 void BooleanGUI_Dialog::SelectionIntoArgument()
180 {
181   myEditCurrentArgument->setText( "" );
182
183   if ( IObjectCount() != 1 )
184   {
185     if      ( myEditCurrentArgument == myGroup->LineEdit1 )   myObject1 = GEOM::GEOM_Object::_nil();
186     else if ( myEditCurrentArgument == myGroup->LineEdit2 )   myObject2 = GEOM::GEOM_Object::_nil();
187     return;
188   }
189
190   // nbSel == 1
191   Standard_Boolean aRes = Standard_False;
192   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
193   if ( !CORBA::is_nil( aSelectedObject ) && aRes && GEOMBase::IsShape( aSelectedObject ) )
194   {
195     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
196     if      ( myEditCurrentArgument == myGroup->LineEdit1 )   myObject1 = aSelectedObject;
197     else if ( myEditCurrentArgument == myGroup->LineEdit2 )   myObject2 = aSelectedObject;
198   }
199 }
200
201
202 //=================================================================================
203 // function : SetEditCurrentArgument()
204 // purpose  :
205 //=================================================================================
206 void BooleanGUI_Dialog::SetEditCurrentArgument()
207 {
208   QPushButton* send = (QPushButton*)sender();
209
210   if      ( send == myGroup->PushButton1 ) myEditCurrentArgument = myGroup->LineEdit1;
211   else if ( send == myGroup->PushButton2 ) myEditCurrentArgument = myGroup->LineEdit2;
212
213   myEditCurrentArgument->setFocus();
214   SelectionIntoArgument();
215 }
216
217
218
219 //=================================================================================
220 // function : LineEditReturnPressed()
221 // purpose  :
222 //=================================================================================
223 void BooleanGUI_Dialog::LineEditReturnPressed()
224 {
225   QLineEdit* send = (QLineEdit*)sender();
226   if ( send == myGroup->LineEdit1 || send == myGroup->LineEdit2 )
227   {
228     myEditCurrentArgument = send;
229     GEOMBase_Skeleton::LineEditReturnPressed();
230   }
231 }
232
233
234 //=================================================================================
235 // function : ActivateThisDialog()
236 // purpose  :
237 //=================================================================================
238 void BooleanGUI_Dialog::ActivateThisDialog()
239 {
240   GEOMBase_Skeleton::ActivateThisDialog();
241   globalSelection( GEOM_ALLSHAPES );
242   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
243           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
244 }
245
246
247 //=================================================================================
248 // function : enterEvent()
249 // purpose  : when mouse enter onto the QWidget
250 //=================================================================================
251 void BooleanGUI_Dialog::enterEvent(QEvent * e)
252 {
253   if ( !GroupConstructors->isEnabled() )
254     ActivateThisDialog();
255 }
256
257 //=================================================================================
258 // function : createOperation
259 // purpose  :
260 //=================================================================================
261 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
262 {
263   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
264 }
265
266 //=================================================================================
267 // function : isValid
268 // purpose  :
269 //=================================================================================
270 bool BooleanGUI_Dialog::isValid( QString& msg )
271 {
272   return !CORBA::is_nil( myObject1 ) && !CORBA::is_nil( myObject2 );
273 }
274
275 //=================================================================================
276 // function : execute
277 // purpose  :
278 //=================================================================================
279 bool BooleanGUI_Dialog::execute( ObjectList& objects )
280 {
281   GEOM::GEOM_Object_var anObj;
282  
283   anObj = GEOM::GEOM_IBooleanOperations::_narrow( getOperation() )->MakeBoolean( myObject1, myObject2, myOperation );
284   if ( !anObj->_is_nil() )
285     objects.push_back( anObj._retn() );
286
287   return true;
288 }