Salome HOME
sources v1.2c
[modules/geom.git] / src / GEOMGUI / GeometryGUI_CutDlg.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_CutDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "GeometryGUI_CutDlg.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_CutDlg()
55 // purpose  : Constructs a GeometryGUI_CutDlg 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_CutDlg::GeometryGUI_CutDlg( 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( "GeometryGUI",tr("ICON_DLG_CUT")));
64     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_SELECT")));
65
66     if ( !name )
67       setName( "GeometryGUI_CutDlg" );
68     resize( 322, 220 ); 
69     setCaption( tr( "GEOM_CUT_TITLE" ) );
70     setSizeGripEnabled( TRUE );
71
72     GeometryGUI_CutDlgLayout = new QGridLayout( this ); 
73     GeometryGUI_CutDlgLayout->setSpacing( 6 );
74     GeometryGUI_CutDlgLayout->setMargin( 11 );
75
76     /***************************************************************/
77     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
78     GroupConstructors->setTitle( tr( "GEOM_CUT"  ) );
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_CutDlgLayout->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_TOOL_OBJECT"  ) );
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_MAIN_OBJECT"  ) );
129     TextLabelC1A1Shape->setMinimumSize( QSize( 50, 0 ) );
130     TextLabelC1A1Shape->setFrameShape( QLabel::NoFrame );
131     TextLabelC1A1Shape->setFrameShadow( QLabel::Plain );
132     GroupConstructor1Layout->addWidget( TextLabelC1A1Shape, 0, 0 );
133     GeometryGUI_CutDlgLayout->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_CutDlgLayout->addWidget( GroupButtons, 2, 0 ); 
162
163     /* Initialisation */
164     Init( Sel ) ; 
165 }
166
167
168 //=================================================================================
169 // function : ~GeometryGUI_CutDlg()
170 // purpose  : Destroys the object and frees any allocated resources
171 //=================================================================================
172 GeometryGUI_CutDlg::~GeometryGUI_CutDlg()
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_CutDlg::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", "Geometry");
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   connect( SelectButtonC1A1Shape, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
212   connect( SelectButtonC1A2Shape, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
213
214   connect( LineEditC1A1Shape, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
215   connect( LineEditC1A2Shape, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
216   
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_CutDlg::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_CutDlg::ClickOnOk()
262 {
263   this->ClickOnApply() ;
264   this->ClickOnCancel() ;
265
266   return ;
267 }
268
269 //=================================================================================
270 // function : ClickOnApply()
271 // purpose  :
272 //=================================================================================
273 void GeometryGUI_CutDlg::ClickOnApply()
274 {
275   myGeomGUI->GetDesktop()->putInfo( tr("") ) ; 
276   switch(myConstructorId)
277     { 
278     case 0 :
279       {
280         if(myOkShape1 && myOkShape2) {    
281           myGeomGUI->MakeBooleanAndDisplay(myGeomShape1 ,myGeomShape2, 2 ) ;
282         }
283         break ;
284       }
285     }
286
287   // accept();
288   return ;
289 }
290
291
292 //=================================================================================
293 // function : ClickOnCancel()
294 // purpose  :
295 //=================================================================================
296 void GeometryGUI_CutDlg::ClickOnCancel()
297 {
298   disconnect( mySelection, 0, this, 0 );
299   myGeomGUI->ResetState() ;
300   reject() ;
301   return ;
302 }
303
304
305
306 //=================================================================================
307 // function : SelectionIntoArgument()
308 // purpose  : Called when selection has changed
309 //=================================================================================
310 void GeometryGUI_CutDlg::SelectionIntoArgument()
311 {
312   myEditCurrentArgument->setText("") ;
313   QString aString = ""; /* name of selection */
314
315   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
316   if ( nbSel != 1 ) {
317     switch (myConstructorId) 
318       {
319       case 0:
320         {
321           if ( myEditCurrentArgument == LineEditC1A1Shape ) {
322             myOkShape1 = false ;
323           }
324           else if ( myEditCurrentArgument == LineEditC1A2Shape ) {
325             myOkShape2 = false ;
326           }
327           break ;
328         } 
329       }
330     return ;
331   }
332
333   /*  nbSel == 1  */ 
334   TopoDS_Shape S;
335   Standard_Boolean testResult ;
336   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
337
338   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
339     return ;
340
341   if ( myEditCurrentArgument == LineEditC1A1Shape ) {
342     myGeomShape1 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
343     if( !testResult )
344       return ;
345     myShape1 = S ;
346     LineEditC1A1Shape->setText(aString) ;
347     myOkShape1 = true ;
348   }    
349   else if ( myEditCurrentArgument == LineEditC1A2Shape ) {
350     myGeomShape2 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
351     if( !testResult )
352       return ;
353     myShape2 = S ;
354     LineEditC1A2Shape->setText(aString) ;
355     myOkShape2 = true ;
356   }
357
358   return ; 
359 }
360
361
362 //=================================================================================
363 // function : SetEditCurrentArgument()
364 // purpose  :
365 //=================================================================================
366 void GeometryGUI_CutDlg::SetEditCurrentArgument()
367 {
368   QPushButton* send = (QPushButton*)sender();
369   
370   switch (myConstructorId)
371     {
372     case 0: /* default constructor */
373       { 
374         if( send == SelectButtonC1A1Shape ) {
375           LineEditC1A1Shape->setFocus() ;
376           myEditCurrentArgument = LineEditC1A1Shape ;
377         }
378         else if(send == SelectButtonC1A2Shape) {
379           LineEditC1A2Shape->setFocus() ;
380           myEditCurrentArgument = LineEditC1A2Shape;
381         }
382         SelectionIntoArgument() ;
383         break;
384       }
385     }
386   return ;
387 }
388
389
390
391 //=================================================================================
392 // function : LineEditReturnPressed()
393 // purpose  :
394 //=================================================================================
395 void GeometryGUI_CutDlg::LineEditReturnPressed()
396 {
397   QLineEdit* send = (QLineEdit*)sender();  
398   if( send == LineEditC1A1Shape )
399     myEditCurrentArgument = LineEditC1A1Shape ;
400   else if ( send == LineEditC1A2Shape )
401     myEditCurrentArgument = LineEditC1A2Shape ; 
402   else
403     return ;
404   
405   /* User name of object input management                          */
406   /* If successfull the selection is changed and signal emitted... */
407   /* so SelectionIntoArgument() is automatically called.           */
408   const QString objectUserName = myEditCurrentArgument->text() ;
409   QWidget* thisWidget = (QWidget*)this ;
410   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
411     myEditCurrentArgument->setText( objectUserName ) ;
412   }
413   return ;
414 }
415
416
417
418 //=================================================================================
419 // function : DeactivateActiveDialog()
420 // purpose  :
421 //=================================================================================
422 void GeometryGUI_CutDlg::DeactivateActiveDialog()
423 {
424   if ( GroupConstructors->isEnabled() ) {
425
426     GroupConstructors->setEnabled(false) ;
427     GroupConstructor1->setEnabled(false) ;
428     GroupButtons->setEnabled(false) ;  
429     disconnect( mySelection, 0, this, 0 );
430   }
431   return ;
432 }
433
434
435
436 //=================================================================================
437 // function : closeEvent()
438 // purpose  :
439 //=================================================================================
440 void GeometryGUI_CutDlg::closeEvent( QCloseEvent* e )
441 {
442   this->ClickOnCancel() ; /* same than click on cancel button */
443 }
444
445
446 //=================================================================================
447 // function : enterEvent()
448 // purpose  : when mouse enter onto the QWidget
449 //=================================================================================
450 void GeometryGUI_CutDlg::enterEvent( QEvent *  )
451 {
452   if ( GroupConstructors->isEnabled() )
453     return ;
454   ActivateThisDialog() ;
455 }
456
457
458
459 //=================================================================================
460 // function : ActivateThisDialog()
461 // purpose  :
462 //=================================================================================
463 void GeometryGUI_CutDlg::ActivateThisDialog()
464 {
465   /* Emit a signal to deactivate any active dialog */
466   myGeomGUI->EmitSignalDeactivateDialog() ;
467   GroupConstructors->setEnabled(true) ;
468   GroupConstructor1->setEnabled(true) ;
469   GroupButtons->setEnabled(true) ;  
470   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
471   return ;
472 }