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