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