Salome HOME
NRI : Add QAD_Desktop.h.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddFaceDlg.cxx
1 using namespace std;
2 //  File      : SMESHGUI_AddFaceDlg.cxx
3 //  Created   : Wed Jun 26 21:00:14 2002
4 //  Author    : Nicolas REJNERI
5
6 //  Project   : SALOME
7 //  Module    : SMESH
8 //  Copyright : Open CASCADE 2002
9 //  $Header$
10
11
12 #include "SMESHGUI_AddFaceDlg.h"
13
14 #include "SMESHGUI.h"
15 #include "QAD_Application.h"
16 #include "QAD_Desktop.h"
17 #include "QAD_MessageBox.h"
18 #include "utilities.h"
19
20 // QT Includes
21 #include <qbuttongroup.h>
22 #include <qgroupbox.h>
23 #include <qlabel.h>
24 #include <qlineedit.h>
25 #include <qpushbutton.h>
26 #include <qradiobutton.h>
27 #include <qlayout.h>
28 #include <qvariant.h>
29 #include <qtooltip.h>
30 #include <qwhatsthis.h>
31 #include <qimage.h>
32 #include <qpixmap.h>
33 #include <qcheckbox.h>
34
35
36 //=================================================================================
37 // class    : SMESHGUI_AddFaceDlg()
38 // purpose  : 
39 //=================================================================================
40 SMESHGUI_AddFaceDlg::SMESHGUI_AddFaceDlg( QWidget* parent, const char* name, SALOME_Selection* Sel,
41                                           int nbNodes, bool modal, WFlags fl )
42     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
43 {
44   myNodes = nbNodes;
45   QPixmap image0;
46   if ( myNodes == 3 )
47     image0 = QPixmap(QAD_Desktop::getResourceManager()->loadPixmap( "SMESHGUI",tr("ICON_DLG_TRIANGLE")));
48   else if ( myNodes == 4 )
49     image0 = QPixmap(QAD_Desktop::getResourceManager()->loadPixmap( "SMESHGUI",tr("ICON_DLG_QUADRANGLE")));
50
51   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESHGUI",tr("ICON_SELECT")));
52
53   if ( !name )
54     setName( "SMESHGUI_AddFaceDlg" );
55   resize( 303, 185 ); 
56   if ( myNodes == 3 )
57     setCaption( tr( "SMESH_ADD_TRIANGLE_TITLE"  ) );
58   else if ( myNodes == 4 )
59     setCaption( tr( "SMESH_ADD_QUADRANGLE_TITLE"  ) );
60
61   setSizeGripEnabled( TRUE );
62   SMESHGUI_AddFaceDlgLayout = new QGridLayout( this ); 
63   SMESHGUI_AddFaceDlgLayout->setSpacing( 6 );
64   SMESHGUI_AddFaceDlgLayout->setMargin( 11 );
65
66     /***************************************************************/
67   GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
68   if ( myNodes == 3 )
69     GroupConstructors->setTitle( tr( "SMESH_TRIANGLE"  ) );
70   else if ( myNodes == 4 )
71     GroupConstructors->setTitle( tr( "SMESH_QUADRANGLE"  ) );
72   
73   GroupConstructors->setExclusive( TRUE );
74   GroupConstructors->setColumnLayout(0, Qt::Vertical );
75   GroupConstructors->layout()->setSpacing( 0 );
76   GroupConstructors->layout()->setMargin( 0 );
77   GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
78   GroupConstructorsLayout->setAlignment( Qt::AlignTop );
79   GroupConstructorsLayout->setSpacing( 6 );
80   GroupConstructorsLayout->setMargin( 11 );
81   Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
82   Constructor1->setText( tr( ""  ) );
83   Constructor1->setPixmap( image0 );
84   Constructor1->setChecked( TRUE );
85   Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
86   Constructor1->setMinimumSize( QSize( 50, 0 ) );
87   GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
88   QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
89   GroupConstructorsLayout->addItem( spacer, 0, 1 );
90   SMESHGUI_AddFaceDlgLayout->addWidget( GroupConstructors, 0, 0 );
91     
92     /***************************************************************/
93   GroupButtons = new QGroupBox( this, "GroupButtons" );
94   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
95   GroupButtons->setTitle( tr( ""  ) );
96   GroupButtons->setColumnLayout(0, Qt::Vertical );
97   GroupButtons->layout()->setSpacing( 0 );
98   GroupButtons->layout()->setMargin( 0 );
99   GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
100   GroupButtonsLayout->setAlignment( Qt::AlignTop );
101   GroupButtonsLayout->setSpacing( 6 );
102   GroupButtonsLayout->setMargin( 11 );
103   buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
104   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
105   buttonCancel->setAutoDefault( TRUE );
106   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
107   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
108   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
109   buttonApply->setAutoDefault( TRUE );
110   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
111   QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
112   GroupButtonsLayout->addItem( spacer_9, 0, 2 );
113   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
114   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
115   buttonOk->setAutoDefault( TRUE );
116   buttonOk->setDefault( TRUE );
117   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
118   SMESHGUI_AddFaceDlgLayout->addWidget( GroupButtons, 2, 0 );
119
120     /***************************************************************/
121   GroupC1 = new QGroupBox( this, "GroupC1" );
122   if ( myNodes == 3 )
123     GroupC1->setTitle( tr( "SMESH_ADD_TRIANGLE"  ) );
124   else if ( myNodes == 4 )
125     GroupC1->setTitle( tr( "SMESH_ADD_QUADRANGLE"  ) );
126
127   GroupC1->setMinimumSize( QSize( 0, 0 ) );
128   GroupC1->setFrameShape( QGroupBox::Box );
129   GroupC1->setFrameShadow( QGroupBox::Sunken );
130   GroupC1->setColumnLayout(0, Qt::Vertical );
131   GroupC1->layout()->setSpacing( 0 );
132   GroupC1->layout()->setMargin( 0 );
133   GroupC1Layout = new QGridLayout( GroupC1->layout() );
134   GroupC1Layout->setAlignment( Qt::AlignTop );
135   GroupC1Layout->setSpacing( 6 );
136   GroupC1Layout->setMargin( 11 );
137   TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
138   TextLabelC1A1->setText( tr( "SMESH_ID_NODES"  ) );
139   TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
140   TextLabelC1A1->setFrameShape( QLabel::NoFrame );
141   TextLabelC1A1->setFrameShadow( QLabel::Plain );
142   GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
143   SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
144   SelectButtonC1A1->setText( tr( ""  ) );
145   SelectButtonC1A1->setPixmap( image1 );
146   SelectButtonC1A1->setToggleButton( FALSE );
147   GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
148   LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
149   GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
150
151   Reverse = new QCheckBox( GroupC1, "Reverse" );
152   Reverse->setText( tr( "SMESH_REVERSE"  ) );
153   GroupC1Layout->addWidget( Reverse, 1, 0 );
154
155   SMESHGUI_AddFaceDlgLayout->addWidget( GroupC1, 1, 0 );
156
157   Init(Sel) ; /* Initialisations */
158 }
159
160 //=================================================================================
161 // function : ~SMESHGUI_AddFaceDlg()
162 // purpose  : Destroys the object and frees any allocated resources
163 //=================================================================================
164 SMESHGUI_AddFaceDlg::~SMESHGUI_AddFaceDlg()
165 {
166     // no need to delete child widgets, Qt does it all for us
167 }
168
169
170 //=================================================================================
171 // function : Init()
172 // purpose  :
173 //=================================================================================
174 void SMESHGUI_AddFaceDlg::Init( SALOME_Selection* Sel )
175 {
176
177   GroupC1->show();
178   myConstructorId = 0 ;
179   Constructor1->setChecked( TRUE );
180   myEditCurrentArgument = LineEditC1A1 ;        
181   mySelection = Sel;  
182   this->myOkNodes = false ;
183   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
184   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
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( SelectButtonC1A1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
193   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
194   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
195   /* to close dialog if study change */
196   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
197
198   connect( Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)) );
199
200   reverse = false;
201
202   /* Move widget on the botton right corner of main widget */
203   int x, y ;
204   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
205   this->move( x, y ) ;
206   this->show() ; /* displays Dialog */
207
208   SelectionIntoArgument();
209
210   return ;
211 }
212
213
214 //=================================================================================
215 // function : ConstructorsClicked()
216 // purpose  : Radio button management
217 //=================================================================================
218 void SMESHGUI_AddFaceDlg::ConstructorsClicked(int constructorId)
219 {
220   return ;
221 }
222
223
224 //=================================================================================
225 // function : ClickOnApply()
226 // purpose  :
227 //=================================================================================
228 void SMESHGUI_AddFaceDlg::ClickOnApply()
229 {
230   switch(myConstructorId)
231     { 
232     case 0 :
233       { 
234         if(myOkNodes) {   
235           mySMESHGUI->EraseSimulationActors();
236           mySMESHGUI->AddFace( myMesh, myMapIndex, reverse ) ;
237           mySMESHGUI->ViewNodes();
238           mySelection->ClearIObjects();
239         }
240         break ;
241       }
242     }
243   return ;
244 }
245
246 //=================================================================================
247 // function : ClickOnOk()
248 // purpose  :
249 //=================================================================================
250 void SMESHGUI_AddFaceDlg::ClickOnOk()
251 {
252   this->ClickOnApply() ;
253   this->ClickOnCancel() ;
254   return ;
255 }
256
257 //=================================================================================
258 // function : ClickOnCancel()
259 // purpose  :
260 //=================================================================================
261 void SMESHGUI_AddFaceDlg::ClickOnCancel()
262 {
263   QAD_Application::getDesktop()->SetSelectionMode( 4 );
264   disconnect( mySelection, 0, this, 0 );
265   mySMESHGUI->ResetState() ;
266   mySMESHGUI->EraseSimulationActors();
267   reject() ;
268   return ;
269 }
270
271
272 //=================================================================================
273 // function : SelectionIntoArgument()
274 // purpose  : Called when selection as changed or other case
275 //=================================================================================
276 void SMESHGUI_AddFaceDlg::SelectionIntoArgument()
277 {
278   myEditCurrentArgument->setText("") ;
279   myOkNodes = false;
280   QString aString = "";
281
282   int nbSel = mySelection->IObjectCount();
283   if(nbSel != 1)
284     return;
285
286   int nbNodes = mySMESHGUI->GetNameOfSelectedNodes(mySelection, aString) ;
287   if(nbNodes < 1)
288     return ;
289   
290   if ( mySelection->SelectionMode() != 1 ) {
291     QAD_MessageBox::warn1 ( QAD_Application::getDesktop(), tr ("SMESH_WRN_WARNING"),
292                             tr ("SMESH_WRN_SELECTIONMODE_NODES"), tr ("SMESH_BUT_YES") );
293     return;
294   }
295
296   myEditCurrentArgument->setText(aString) ;
297   Standard_Boolean res;
298   myMesh = mySMESHGUI->ConvertIOinMesh( mySelection->firstIObject(), res );
299   if (!res)
300     return ;
301
302   mySelection->GetIndex( mySelection->firstIObject(), myMapIndex );
303
304   if ( myNodes == 3 && nbNodes == 3 ) {
305     mySMESHGUI->DisplaySimulationTriangle(myMesh, myMapIndex, reverse);
306     myOkNodes = true ;
307   } else if ( myNodes == 4 && nbNodes == 4 ) {
308     mySMESHGUI->DisplaySimulationQuadrangle(myMesh, myMapIndex, reverse);
309     myOkNodes = true ;
310   } else {
311     //    mySMESHGUI->EraseSimulationActors();
312     myOkNodes = false ;
313   }
314   return ;
315 }
316
317
318 //=================================================================================
319 // function : SetEditCurrentArgument()
320 // purpose  :
321 //=================================================================================
322 void SMESHGUI_AddFaceDlg::SetEditCurrentArgument()
323 {
324   QPushButton* send = (QPushButton*)sender();
325   switch (myConstructorId)
326     {
327     case 0: /* default constructor */
328       { 
329         if(send == SelectButtonC1A1) {
330           LineEditC1A1->setFocus() ;
331           myEditCurrentArgument = LineEditC1A1;
332         }
333         SelectionIntoArgument() ;
334         break;
335       }
336     }
337   return ;
338 }
339
340 //=================================================================================
341 // function : DeactivateActiveDialog()
342 // purpose  :
343 //=================================================================================
344 void SMESHGUI_AddFaceDlg::DeactivateActiveDialog()
345 {
346   if ( GroupConstructors->isEnabled() ) {  
347     GroupConstructors->setEnabled(false) ;
348     GroupC1->setEnabled(false) ;
349     GroupButtons->setEnabled(false) ;
350     mySMESHGUI->EraseSimulationActors() ;
351     mySMESHGUI->ResetState() ;    
352     mySMESHGUI->SetActiveDialogBox(0) ;
353   }
354   return ;
355 }
356
357
358 //=================================================================================
359 // function : ActivateThisDialog()
360 // purpose  :
361 //=================================================================================
362 void SMESHGUI_AddFaceDlg::ActivateThisDialog()
363 {
364   /* Emit a signal to deactivate the active dialog */
365   mySMESHGUI->EmitSignalDeactivateDialog() ;   
366   GroupConstructors->setEnabled(true) ;
367   GroupC1->setEnabled(true) ;
368   GroupButtons->setEnabled(true) ;
369   return ;
370 }
371
372
373 //=================================================================================
374 // function : enterEvent()
375 // purpose  :
376 //=================================================================================
377 void SMESHGUI_AddFaceDlg::enterEvent(QEvent* e)
378 {
379   if ( GroupConstructors->isEnabled() )
380     return ;  
381   ActivateThisDialog() ;
382   return ;
383 }
384
385
386 //=================================================================================
387 // function : closeEvent()
388 // purpose  :
389 //=================================================================================
390 void SMESHGUI_AddFaceDlg::closeEvent( QCloseEvent* e )
391 {
392   /* same than click on cancel button */
393   this->ClickOnCancel() ;
394   return ;
395 }
396
397
398 void SMESHGUI_AddFaceDlg::CheckBox( int state )
399 {
400   if( Reverse->isOn() ) reverse = true;
401   else reverse = false;
402
403   if ( myOkNodes == false )
404     return;
405   
406   if ( state >= 0 ) {
407     mySMESHGUI->EraseSimulationActors();
408     if ( myNodes == 3 )
409       mySMESHGUI->DisplaySimulationTriangle( myMesh, myMapIndex, reverse);
410     else if ( myNodes == 4 ) {
411       mySMESHGUI->DisplaySimulationQuadrangle( myMesh, myMapIndex, reverse);
412     }
413   }
414 }