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