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