Salome HOME
NRI : Merge from V1_2.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_WorkingPlaneDlg.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   : GeometryGUI_WorkingPlaneDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27
28 using namespace std;
29 #include "GeometryGUI_WorkingPlaneDlg.h"
30
31 #include "GeometryGUI.h"
32 #include "QAD_Application.h"
33 #include "QAD_Desktop.h"
34 #include "utilities.h"
35
36 #include <BRepAdaptor_Surface.hxx>
37 #include <gp_Pln.hxx>
38 #include <gp_Dir.hxx>
39 #include <gp_Ax1.hxx>
40
41 #include <qbuttongroup.h>
42 #include <qgroupbox.h>
43 #include <qlabel.h>
44 #include <qlineedit.h>
45 #include <qpushbutton.h>
46 #include <qradiobutton.h>
47 #include <qlayout.h>
48 #include <qvariant.h>
49 #include <qtooltip.h>
50 #include <qwhatsthis.h>
51 #include <qimage.h>
52 #include <qpixmap.h>
53
54 //=================================================================================
55 // class    : GeometryGUI_WorkingPlaneDlg()
56 // purpose  : Constructs a GeometryGUI_WorkingPlaneDlg which is a child of 'parent', with the 
57 //            name 'name' and widget flags set to 'f'.
58 //            The dialog will by default be modeless, unless you set 'modal' to
59 //            TRUE to construct a modal dialog.
60 //=================================================================================
61 GeometryGUI_WorkingPlaneDlg::GeometryGUI_WorkingPlaneDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
62     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
63 {
64     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_WPLANE_FACE")));
65     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
66
67     if ( !name )
68         setName( "GeometryGUI_WorkingPlaneDlg" );
69     resize( 303, 185 ); 
70     setCaption( tr( "GEOM_WPLANE_TITLE"  ) );
71     setSizeGripEnabled( TRUE );
72     GeometryGUI_WorkingPlaneDlgLayout = new QGridLayout( this ); 
73     GeometryGUI_WorkingPlaneDlgLayout->setSpacing( 6 );
74     GeometryGUI_WorkingPlaneDlgLayout->setMargin( 11 );
75     
76     /***************************************************************/
77     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
78     GroupConstructors->setTitle( tr( "GEOM_WPLANE"  ) );
79     GroupConstructors->setExclusive( TRUE );
80     GroupConstructors->setColumnLayout(0, Qt::Vertical );
81     GroupConstructors->layout()->setSpacing( 0 );
82     GroupConstructors->layout()->setMargin( 0 );
83     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
84     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
85     GroupConstructorsLayout->setSpacing( 6 );
86     GroupConstructorsLayout->setMargin( 11 );
87     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
88     Constructor1->setText( tr( ""  ) );
89     Constructor1->setPixmap( image0 );
90     Constructor1->setChecked( TRUE );
91     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
92     Constructor1->setMinimumSize( QSize( 50, 0 ) );
93     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
94     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
95     GroupConstructorsLayout->addItem( spacer, 0, 1 );
96     GeometryGUI_WorkingPlaneDlgLayout->addWidget( GroupConstructors, 0, 0 );
97
98     /***************************************************************/
99     GroupButtons = new QGroupBox( this, "GroupButtons" );
100     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
101     GroupButtons->setTitle( tr( ""  ) );
102     GroupButtons->setColumnLayout(0, Qt::Vertical );
103     GroupButtons->layout()->setSpacing( 0 );
104     GroupButtons->layout()->setMargin( 0 );
105     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
106     GroupButtonsLayout->setAlignment( Qt::AlignTop );
107     GroupButtonsLayout->setSpacing( 6 );
108     GroupButtonsLayout->setMargin( 11 );
109     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
110     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
111     buttonCancel->setAutoDefault( TRUE );
112     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
113     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
114     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
115     buttonApply->setAutoDefault( TRUE );
116     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
117     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
118     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
119     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
120     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
121     buttonOk->setAutoDefault( TRUE );
122     buttonOk->setDefault( TRUE );
123     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
124     GeometryGUI_WorkingPlaneDlgLayout->addWidget( GroupButtons, 2, 0 );
125     
126     /***************************************************************/
127     GroupC1 = new QGroupBox( this, "GroupC1" );
128     GroupC1->setTitle( tr( "GEOM_WPLANE_FACE"  ) );
129     GroupC1->setMinimumSize( QSize( 0, 0 ) );
130     GroupC1->setFrameShape( QGroupBox::Box );
131     GroupC1->setFrameShadow( QGroupBox::Sunken );
132     GroupC1->setColumnLayout(0, Qt::Vertical );
133     GroupC1->layout()->setSpacing( 0 );
134     GroupC1->layout()->setMargin( 0 );
135     GroupC1Layout = new QGridLayout( GroupC1->layout() );
136     GroupC1Layout->setAlignment( Qt::AlignTop );
137     GroupC1Layout->setSpacing( 6 );
138     GroupC1Layout->setMargin( 11 );
139     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
140     TextLabelC1A1->setText( tr( "GEOM_SELECTION"  ) );
141     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
142     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
143     TextLabelC1A1->setFrameShadow( QLabel::Plain );
144     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
145     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
146     SelectButtonC1A1->setText( tr( ""  ) );
147     SelectButtonC1A1->setPixmap( image1 );
148     SelectButtonC1A1->setToggleButton( FALSE );
149     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
150     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
151     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
152     GeometryGUI_WorkingPlaneDlgLayout->addWidget( GroupC1, 1, 0 );
153     /***************************************************************/
154
155     Init(Sel) ; /* Initialisations */
156 }
157
158 //=================================================================================
159 // function : ~GeometryGUI_WorkingPlaneDlg()
160 // purpose  : Destroys the object and frees any allocated resources
161 //=================================================================================
162 GeometryGUI_WorkingPlaneDlg::~GeometryGUI_WorkingPlaneDlg()
163 {
164     // no need to delete child widgets, Qt does it all for us
165 }
166
167
168 //=================================================================================
169 // function : Init()
170 // purpose  :
171 //=================================================================================
172 void GeometryGUI_WorkingPlaneDlg::Init( SALOME_Selection* Sel )
173 {
174
175   GroupC1->show();
176   myConstructorId = 0 ;
177   Constructor1->setChecked( TRUE );
178   myEditCurrentArgument = LineEditC1A1 ;        
179   mySelection = Sel;  
180   this->myOkPlane = false ;
181   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
182   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
183
184   // TODO : previous selection into argument ?
185
186   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
187   myGeom = GEOM::GEOM_Gen::_narrow(comp);
188   /* Filter definition */
189   myFaceFilter = new GEOM_FaceFilter( StdSelect_Plane, myGeom );
190   mySelection->AddFilter( myFaceFilter ); /* filter for next selection */
191
192   /* signals and slots connections */
193   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
194   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
195   connect( buttonApply, SIGNAL( clicked() ),  this, SLOT(ClickOnApply() ) );
196   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
197
198   connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
199
200   connect( SelectButtonC1A1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
201   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
202   /* to close dialog if study change */
203   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
204
205   /* Move widget on the botton right corner of main widget */
206   int x, y ;
207   myGeomGUI->DefineDlgPosition( this, x, y ) ;
208   this->move( x, y ) ;
209   this->show() ; /* displays Dialog */
210   
211   return ;
212 }
213
214
215 //=================================================================================
216 // function : ConstructorsClicked()
217 // purpose  : Radio button management
218 //=================================================================================
219 void GeometryGUI_WorkingPlaneDlg::ConstructorsClicked(int constructorId)
220 {
221   return ;
222 }
223
224 //=================================================================================
225 // function : ClickOnOk()
226 // purpose  :
227 //=================================================================================
228 void GeometryGUI_WorkingPlaneDlg::ClickOnOk()
229 {
230   this->ClickOnApply() ;
231   this->ClickOnCancel() ;
232
233   return ;
234 }
235
236 //=================================================================================
237 // function : ClickOnApply()
238 // purpose  :
239 //=================================================================================
240 void GeometryGUI_WorkingPlaneDlg::ClickOnApply()
241 {
242   mySelection->ClearFilters() ;
243   myGeomGUI->GetDesktop()->putInfo( tr("") ) ;
244   switch(myConstructorId)
245     { 
246     case 0 :
247       { 
248         if(myOkPlane) {   
249           myGeomGUI->MakeWorkingPlane( this->myLoc, this->myDir ) ;
250         }
251         break ;
252       }
253     }
254   // accept();
255   return ;
256 }
257
258
259 //=================================================================================
260 // function : ClickOnCancel()
261 // purpose  :
262 //=================================================================================
263 void GeometryGUI_WorkingPlaneDlg::ClickOnCancel()
264 {
265   mySelection->ClearFilters() ;
266   disconnect( mySelection, 0, this, 0 );
267   myGeomGUI->ResetState() ;
268   reject() ;
269   return ;
270 }
271
272
273 //=================================================================================
274 // function : SelectionIntoArgument()
275 // purpose  : Called when selection as changed or other case
276 //=================================================================================
277 void GeometryGUI_WorkingPlaneDlg::SelectionIntoArgument()
278 {
279   /* All this for first constructor */
280   // if(myEditCurrentArgument == LineEditC1A1 )
281
282   myEditCurrentArgument->setText("") ;
283   QString aString = ""; /* future the name of selection */
284
285   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
286   if ( nbSel != 1 ) {
287     switch (myConstructorId) 
288       {
289       case 0:
290         {
291           if ( myEditCurrentArgument == LineEditC1A1 ) {
292             myOkPlane = false ;
293           }
294           break ;
295         } 
296       }
297     return ;
298   }
299   
300   /*  nbSel == 1  */ 
301    TopoDS_Shape S;
302    if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
303      return ;
304    
305   if ( myEditCurrentArgument == LineEditC1A1 ) {
306     BRepAdaptor_Surface surf(TopoDS::Face(S));
307     gp_Pln Plane = surf.Plane();
308     myLoc = Plane.Location();
309     myDir = Plane.Axis().Direction();
310
311     LineEditC1A1->setText(aString) ;
312     myOkPlane = true ;
313   }
314
315   /* no simulation */
316   return ;
317 }
318
319
320 //=================================================================================
321 // function : SetEditCurrentArgument()
322 // purpose  :
323 //=================================================================================
324 void GeometryGUI_WorkingPlaneDlg::SetEditCurrentArgument()
325 {
326   QPushButton* send = (QPushButton*)sender();
327   switch (myConstructorId)
328     {
329     case 0: /* default constructor */
330       { 
331         if(send == SelectButtonC1A1) {
332           LineEditC1A1->setFocus() ;
333           myEditCurrentArgument = LineEditC1A1;
334         }
335         mySelection->AddFilter(myFaceFilter) ;
336         SelectionIntoArgument() ;
337         break;
338       }
339     }
340   return ;
341 }
342
343
344
345 //=================================================================================
346 // function : LineEditReturnPressed()
347 // purpose  :
348 //=================================================================================
349 void GeometryGUI_WorkingPlaneDlg::LineEditReturnPressed()
350 {
351   QLineEdit* send = (QLineEdit*)sender();  
352   if( send == LineEditC1A1 )
353     myEditCurrentArgument = LineEditC1A1 ;
354   return ;
355   
356   /* User name of object input management                          */
357   /* If successfull the selection is changed and signal emitted... */
358   /* so SelectionIntoArgument() is automatically called.           */
359   const QString objectUserName = myEditCurrentArgument->text() ;
360   QWidget* thisWidget = (QWidget*)this ;
361   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
362     myEditCurrentArgument->setText( objectUserName ) ;
363   }
364   return ;
365 }
366
367
368 //=================================================================================
369 // function : DeactivateActiveDialog()
370 // purpose  :
371 //=================================================================================
372 void GeometryGUI_WorkingPlaneDlg::DeactivateActiveDialog()
373 {
374   if ( GroupConstructors->isEnabled() ) {
375     mySelection->ClearFilters() ;
376     disconnect( mySelection, 0, this, 0 );
377     GroupConstructors->setEnabled(false) ;
378     GroupC1->setEnabled(false) ;
379     GroupButtons->setEnabled(false) ;
380   }
381   return ;
382 }
383
384
385 //=================================================================================
386 // function : ActivateThisDialog()
387 // purpose  :
388 //=================================================================================
389 void GeometryGUI_WorkingPlaneDlg::ActivateThisDialog()
390 {
391   /* Emit a signal to deactivate the active dialog */
392   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
393   myGeomGUI->EmitSignalDeactivateDialog() ;   
394   GroupConstructors->setEnabled(true) ;
395   GroupC1->setEnabled(true) ;
396   GroupButtons->setEnabled(true) ;
397   return ;
398 }
399
400
401 //=================================================================================
402 // function : enterEvent()
403 // purpose  :
404 //=================================================================================
405 void GeometryGUI_WorkingPlaneDlg::enterEvent(QEvent* e)
406 {
407   if ( GroupConstructors->isEnabled() )
408     return ;  
409   ActivateThisDialog() ;
410   return ;
411 }
412
413
414 //=================================================================================
415 // function : closeEvent()
416 // purpose  :
417 //=================================================================================
418 void GeometryGUI_WorkingPlaneDlg::closeEvent( QCloseEvent* e )
419 {
420   /* same than click on cancel button */
421   this->ClickOnCancel() ;
422   return ;
423 }