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