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