Salome HOME
NRI : Merge from V1_2.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_FaceDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_FaceDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10
11 #include "GeometryGUI_FaceDlg.h"
12 #include "GeometryGUI.h"
13
14 #include "QAD_Application.h"
15 #include "QAD_Desktop.h"
16 #include "utilities.h"
17
18 #include <qbuttongroup.h>
19 #include <qcheckbox.h>
20 #include <qgroupbox.h>
21 #include <qlabel.h>
22 #include <qlineedit.h>
23 #include <qpushbutton.h>
24 #include <qradiobutton.h>
25 #include <qlayout.h>
26 #include <qvariant.h>
27 #include <qtooltip.h>
28 #include <qwhatsthis.h>
29 #include <qimage.h>
30 #include <qpixmap.h>
31
32
33
34 //=================================================================================
35 // class    : GeometryGUI_FaceDlg()
36 // purpose  : Constructs a GeometryGUI_FaceDlg which is a child of 'parent', with the 
37 //            name 'name' and widget flags set to 'f'.
38 //            The dialog will by default be modeless, unless you set 'modal' to
39 //            TRUE to construct a modal dialog.
40 //=================================================================================
41 GeometryGUI_FaceDlg::GeometryGUI_FaceDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
42     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
43 {
44     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_BUILD_FACE")));
45     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
46
47     if ( !name )
48         setName( "GeometryGUI_FaceDlg" );
49     resize( 303, 208 ); 
50     setCaption( tr( "GEOM_FACE_TITLE"  ) );
51     setSizeGripEnabled( TRUE );
52     GeometryGUI_FaceDlgLayout = new QGridLayout( this ); 
53     GeometryGUI_FaceDlgLayout->setSpacing( 6 );
54     GeometryGUI_FaceDlgLayout->setMargin( 11 );
55
56     /***************************************************************/
57     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
58     GroupConstructors->setTitle( tr( "GEOM_FACE"  ) );
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     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
68     Constructor1->setText( tr( ""  ) );
69     Constructor1->setPixmap( image0 );
70     Constructor1->setChecked( TRUE );
71     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
72     Constructor1->setMinimumSize( QSize( 50, 0 ) );
73     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
74     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
75     GroupConstructorsLayout->addItem( spacer, 0, 1 );
76     GeometryGUI_FaceDlgLayout->addWidget( GroupConstructors, 0, 0 );
77
78     /***************************************************************/
79     GroupC1 = new QGroupBox( this, "GroupC1" );
80     GroupC1->setTitle( tr( "GEOM_FACE_FFW"  ) );
81     GroupC1->setMinimumSize( QSize( 0, 0 ) );
82     GroupC1->setFrameShape( QGroupBox::Box );
83     GroupC1->setFrameShadow( QGroupBox::Sunken );
84     GroupC1->setColumnLayout(0, Qt::Vertical );
85     GroupC1->layout()->setSpacing( 0 );
86     GroupC1->layout()->setMargin( 0 );
87     GroupC1Layout = new QGridLayout( GroupC1->layout() );
88     GroupC1Layout->setAlignment( Qt::AlignTop );
89     GroupC1Layout->setSpacing( 6 );
90     GroupC1Layout->setMargin( 11 );
91     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
92     TextLabelC1A1->setText( tr( "GEOM_WIRE"  ) );
93     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
94     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
95     TextLabelC1A1->setFrameShadow( QLabel::Plain );
96     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
97     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
98     SelectButtonC1A1->setText( tr( ""  ) );
99     SelectButtonC1A1->setPixmap( image1 );
100     SelectButtonC1A1->setToggleButton( FALSE );
101     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
102     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
103     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
104     CheckBox1 = new QCheckBox( GroupC1, "CheckBox1" );
105     CheckBox1->setText( tr( "GEOM_FACE_OPT"  ) );
106     CheckBox1->setChecked( TRUE );
107     GroupC1Layout->addWidget( CheckBox1, 1, 2);
108     GeometryGUI_FaceDlgLayout->addWidget( GroupC1, 1, 0 );
109
110     /***************************************************************/
111     GroupButtons = new QGroupBox( this, "GroupButtons" );
112     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
113     GroupButtons->setTitle( tr( ""  ) );
114     GroupButtons->setColumnLayout(0, Qt::Vertical );
115     GroupButtons->layout()->setSpacing( 0 );
116     GroupButtons->layout()->setMargin( 0 );
117     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
118     GroupButtonsLayout->setAlignment( Qt::AlignTop );
119     GroupButtonsLayout->setSpacing( 6 );
120     GroupButtonsLayout->setMargin( 11 );
121     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
122     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
123     buttonCancel->setAutoDefault( TRUE );
124     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
125     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
126     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
127     buttonApply->setAutoDefault( TRUE );
128     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
129     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
130     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
131     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
132     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
133     buttonOk->setAutoDefault( TRUE );
134     buttonOk->setDefault( TRUE );
135     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
136     GeometryGUI_FaceDlgLayout->addWidget( GroupButtons, 2, 0 );
137
138     Init(Sel) ; /* Initialisations */
139 }
140
141
142 //=================================================================================
143 // function : ~GeometryGUI_FaceDlg()
144 // purpose  : Destroys the object and frees any allocated resources
145 //=================================================================================
146 GeometryGUI_FaceDlg::~GeometryGUI_FaceDlg()
147 {
148     // no need to delete child widgets, Qt does it all for us
149 }
150
151
152
153 //=================================================================================
154 // function : Init()
155 // purpose  :
156 //=================================================================================
157 void GeometryGUI_FaceDlg::Init( SALOME_Selection* Sel )
158 {
159
160   GroupC1->show();
161   myConstructorId = 0 ;
162   Constructor1->setChecked( TRUE );
163   CheckBox1->setChecked( TRUE );
164
165   myEditCurrentArgument = LineEditC1A1 ;        
166   mySelection = Sel;  
167   this->myOkShape = false ;
168   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
169   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
170
171   // TODO previous selection into argument ?
172
173   /* Filter definitions */
174   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
175   myGeom = GEOM::GEOM_Gen::_narrow(comp);
176   myWireFilter = new GEOM_ShapeTypeFilter( TopAbs_WIRE, myGeom );
177   mySelection->AddFilter(myWireFilter) ; /* first filter used */
178
179   /* signals and slots connections */
180   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
181   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
182   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
183   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
184
185   connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
186
187   connect( SelectButtonC1A1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
188   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
189   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
190   /* to close dialog if study change */
191   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
192  
193   /* Move widget on the botton right corner of main widget */
194   int x, y ;
195   myGeomGUI->DefineDlgPosition( this, x, y ) ;
196   this->move( x, y ) ;
197   this->show() ; /* displays Dialog */
198
199   return ;
200 }
201
202
203 //=================================================================================
204 // function : ConstructorsClicked()
205 // purpose  : Radio button management
206 //=================================================================================
207 void GeometryGUI_FaceDlg::ConstructorsClicked(int constructorId)
208 {
209   return ;
210 }
211
212 //=================================================================================
213 // function : ClickOnOk()
214 // purpose  :
215 //=================================================================================
216 void GeometryGUI_FaceDlg::ClickOnOk()
217 {
218   this->ClickOnApply() ;
219   this->ClickOnCancel() ;
220
221   return ;
222 }
223
224 //=================================================================================
225 // function : ClickOnApply()
226 // purpose  :
227 //=================================================================================
228 void GeometryGUI_FaceDlg::ClickOnApply()
229 {
230   myGeomGUI->GetDesktop()->putInfo( tr("") ) ; 
231   switch(myConstructorId)
232     { 
233     case 0 :
234       { 
235         if(myOkShape) {   
236           myGeomGUI->MakeFaceAndDisplay(this->myGeomShape, this->CheckBox1->isChecked() ) ;
237         }
238         break ;
239       }
240     }
241   // accept();
242   return ;
243 }
244
245
246 //=================================================================================
247 // function : ClickOnCancel()
248 // purpose  :
249 //=================================================================================
250 void GeometryGUI_FaceDlg::ClickOnCancel()
251 {
252   mySelection->ClearFilters() ;
253   myGeomGUI->ResetState() ;
254   disconnect( mySelection, 0, this, 0 );
255   reject() ;
256   return ;
257 }
258
259
260 //=================================================================================
261 // function : SelectionIntoArgument()
262 // purpose  : Called when selection as changed or other case
263 //=================================================================================
264 void GeometryGUI_FaceDlg::SelectionIntoArgument()
265 {
266   /* All this for first constructor */
267   // if(myEditCurrentArgument == LineEditC1A1 )
268   
269   myOkShape = false;
270   myEditCurrentArgument->setText("") ;
271   QString aString = ""; /* future the name of selection */
272
273   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
274   if ( nbSel != 1 )
275     return ;
276
277   // nbSel == 1 !
278   Standard_Boolean testResult ;
279   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
280   if( !myGeomGUI->GetTopoFromSelection(this->mySelection, this->myShape) )
281     return ;
282   
283   myGeomShape = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
284   if( !testResult )
285     return ;
286
287   myEditCurrentArgument->setText(aString) ;
288   this->myOkShape = true ;
289
290   /* no simulation */
291   return ;
292 }
293
294
295 //=================================================================================
296 // function : SetEditCurrentArgument()
297 // purpose  :
298 //=================================================================================
299 void GeometryGUI_FaceDlg::SetEditCurrentArgument()
300 {
301   QPushButton* send = (QPushButton*)sender();
302   mySelection->ClearFilters() ;
303
304   switch (myConstructorId)
305     {
306     case 0: /* default constructor */
307       { 
308         if(send == SelectButtonC1A1) {
309           LineEditC1A1->setFocus() ;
310           myEditCurrentArgument = LineEditC1A1;
311           mySelection->AddFilter(myWireFilter) ;
312         }
313         SelectionIntoArgument() ;
314         break;
315       }
316     }
317   return ;
318 }
319
320
321
322 //=================================================================================
323 // function : LineEditReturnPressed()
324 // purpose  :
325 //=================================================================================
326 void GeometryGUI_FaceDlg::LineEditReturnPressed()
327 {
328    QLineEdit* send = (QLineEdit*)sender();  
329    if( send == LineEditC1A1 )
330      myEditCurrentArgument = LineEditC1A1 ;
331    else
332      return ;
333   
334   /* User name of object input management                          */
335   /* If successfull the selection is changed and signal emitted... */
336   /* so SelectionIntoArgument() is automatically called.           */
337   const QString objectUserName = myEditCurrentArgument->text() ;
338   QWidget* thisWidget = (QWidget*)this ;
339   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
340     myEditCurrentArgument->setText( objectUserName ) ;
341   } 
342   return ;
343 }
344
345
346 //=================================================================================
347 // function : DeactivateActiveDialog()
348 // purpose  :
349 //=================================================================================
350 void GeometryGUI_FaceDlg::DeactivateActiveDialog()
351 {
352   if ( GroupConstructors->isEnabled() ) {
353     disconnect( mySelection, 0, this, 0 );
354     mySelection->ClearFilters() ;
355     GroupConstructors->setEnabled(false) ;
356     GroupC1->setEnabled(false) ;
357     GroupButtons->setEnabled(false) ;
358   }
359   return ;
360 }
361
362
363 //=================================================================================
364 // function : ActivateThisDialog()
365 // purpose  :
366 //=================================================================================
367 void GeometryGUI_FaceDlg::ActivateThisDialog()
368 {
369   /* Emit a signal to deactivate the active dialog */
370   myGeomGUI->EmitSignalDeactivateDialog() ;   
371   GroupConstructors->setEnabled(true) ;
372   GroupC1->setEnabled(true) ;
373   GroupButtons->setEnabled(true) ;
374   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
375   return ;
376 }
377
378
379 //=================================================================================
380 // function : enterEvent()
381 // purpose  :
382 //=================================================================================
383 void GeometryGUI_FaceDlg::enterEvent(QEvent* e)
384 {
385   if ( GroupConstructors->isEnabled() )
386     return ;  
387   ActivateThisDialog() ;
388   return ;
389 }
390
391
392 //=================================================================================
393 // function : closeEvent()
394 // purpose  :
395 //=================================================================================
396 void GeometryGUI_FaceDlg::closeEvent( QCloseEvent* e )
397 {
398   /* same than click on cancel button */
399   this->ClickOnCancel() ;
400   return ;
401 }