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