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