Salome HOME
SMH: 3.0.0 preparation - merged and adopted version (POLYWORK+HEAD)
[modules/visu.git] / src / VISUGUI / VisuGUI_InitMeshDlg.cxx
1 z//  VISU VISUGUI : GUI of VISU 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   : VisuGUI_InitMeshDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : VISU
27 //  $Header$
28
29 using namespace std;
30 #include "VisuGUI_InitMeshDlg.h"
31 #include "VisuGUI.h"
32
33 #include "SUIT_Application.h"
34 #include "SUIT_Desktop.h"
35 #include "utilities.h"
36
37 // QT Includes
38 #include <qbuttongroup.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
51
52 //=================================================================================
53 // class    : VisuGUI_InitMeshDlg()
54 // purpose  : Constructs a VisuGUI_InitMeshDlg which is a child of 'parent', with the 
55 //            name 'name' and widget flags set to 'f'.
56 //            The dialog will by default be modeless, unless you set 'modal' to
57 //            TRUE to construct a modal dialog.
58 //=================================================================================
59 VisuGUI_InitMeshDlg::VisuGUI_InitMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel = 0, bool modal, WFlags fl )
60     : QDialog( parent, name, modal, fl )
61 {
62     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "VISU",tr("ICON_DLG_INIT_MESH")));
63     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "VISU",tr("ICON_SELECT")));
64     if ( !name )
65         setName( "VisuGUI_InitMeshDlg" );
66     resize( 303, 175 ); 
67     setCaption( tr( "Visu_INIT_MESH"  ) );
68     VisuGUI_InitMeshDlgLayout = new QGridLayout( this ); 
69     VisuGUI_InitMeshDlgLayout->setSpacing( 6 );
70     VisuGUI_InitMeshDlgLayout->setMargin( 11 );
71     
72     /***************************************************************/
73     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
74     GroupConstructors->setTitle( tr( "Visu_INIT"  ) );
75     GroupConstructors->setExclusive( TRUE );
76     GroupConstructors->setColumnLayout(0, Qt::Vertical );
77     GroupConstructors->layout()->setSpacing( 0 );
78     GroupConstructors->layout()->setMargin( 0 );
79     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
80     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
81     GroupConstructorsLayout->setSpacing( 6 );
82     GroupConstructorsLayout->setMargin( 11 );
83     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
84     Constructor1->setText( tr( ""  ) );
85     Constructor1->setPixmap( image1 );
86     Constructor1->setChecked( TRUE );
87     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
88     Constructor1->setMinimumSize( QSize( 50, 0 ) );
89     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
90     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
91     GroupConstructorsLayout->addItem( spacer, 0, 1 );
92     VisuGUI_InitMeshDlgLayout->addWidget( GroupConstructors, 0, 0 );
93     
94     /***************************************************************/
95     GroupButtons = new QGroupBox( this, "GroupButtons" );
96     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
97     GroupButtons->setTitle( tr( ""  ) );
98     GroupButtons->setColumnLayout(0, Qt::Vertical );
99     GroupButtons->layout()->setSpacing( 0 );
100     GroupButtons->layout()->setMargin( 0 );
101     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
102     GroupButtonsLayout->setAlignment( Qt::AlignTop );
103     GroupButtonsLayout->setSpacing( 6 );
104     GroupButtonsLayout->setMargin( 11 );
105     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
106     buttonCancel->setText( tr( "Visu_BUT_CLOSE"  ) );
107     buttonCancel->setAutoDefault( TRUE );
108     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
109     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
110     buttonApply->setText( tr( "Visu_BUT_APPLY"  ) );
111     buttonApply->setAutoDefault( TRUE );
112     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
113     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
114     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
115     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
116     buttonOk->setText( tr( "Visu_BUT_OK"  ) );
117     buttonOk->setAutoDefault( TRUE );
118     buttonOk->setDefault( TRUE );
119     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
120     VisuGUI_InitMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
121
122     /***************************************************************/
123     GroupC1 = new QGroupBox( this, "GroupC1" );
124     GroupC1->setTitle( tr( "Visu_ARGUMENTS"  ) );
125     GroupC1->setMinimumSize( QSize( 0, 0 ) );
126     GroupC1->setFrameShape( QGroupBox::Box );
127     GroupC1->setFrameShadow( QGroupBox::Sunken );
128     GroupC1->setColumnLayout(0, Qt::Vertical );
129     GroupC1->layout()->setSpacing( 0 );
130     GroupC1->layout()->setMargin( 0 );
131     GroupC1Layout = new QGridLayout( GroupC1->layout() );
132     GroupC1Layout->setAlignment( Qt::AlignTop );
133     GroupC1Layout->setSpacing( 6 );
134     GroupC1Layout->setMargin( 11 );
135
136     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
137     TextLabelC1A1->setText( tr( "Visu_OBJECT_GEOM"  ) );
138     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
139     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
140     TextLabelC1A1->setFrameShadow( QLabel::Plain );
141     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
142     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
143     SelectButtonC1A1->setText( tr( ""  ) );
144     SelectButtonC1A1->setPixmap( image0 );
145     SelectButtonC1A1->setToggleButton( FALSE );
146     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
147     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
148     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
149
150     TextLabel_NameMesh = new QLabel( GroupC1, "TextLabel_NameMesh" );
151     TextLabel_NameMesh->setText( tr( "Visu_NAME"  ) );
152     GroupC1Layout->addWidget( TextLabel_NameMesh, 1, 0 );
153     LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
154     GroupC1Layout->addWidget( LineEdit_NameMesh, 1, 2 );
155
156     VisuGUI_InitMeshDlgLayout->addWidget( GroupC1, 1, 0 );
157     /***************************************************************/
158
159     Init(Sel) ;
160 }
161
162
163 //=================================================================================
164 // function : ~VisuGUI_InitMeshDlg()
165 // purpose  : Destroys the object and frees any allocated resources
166 //=================================================================================
167 VisuGUI_InitMeshDlg::~VisuGUI_InitMeshDlg()
168 {
169     // no need to delete child widgets, Qt does it all for us
170 }
171
172
173 //=================================================================================
174 // function : Init()
175 // purpose  :
176 //=================================================================================
177 void VisuGUI_InitMeshDlg::Init( SALOME_Selection* Sel )
178 {
179   GroupC1->show();
180   myConstructorId = 0 ;
181   Constructor1->setChecked( TRUE );
182   myEditCurrentArgument = LineEditC1A1 ;        
183   mySelection = Sel;
184   myVisuGUI = VisuGUI::GetVisuGUI() ;
185   myVisuGUI->SetActiveDialogBox( (QDialog*)this ) ;
186
187   myGeomFilter = new SALOME_TypeFilter( "GEOM" );
188   mySelection->AddFilter( myGeomFilter );
189
190   myNameMesh = "Mesh";
191
192   /* signals and slots connections */
193   connect( buttonOk, SIGNAL( pressed() ),     this, SLOT( ClickOnOk() ) );
194   connect( buttonApply, SIGNAL( pressed() ), this, SLOT(ClickOnApply() ) );
195   connect( buttonCancel, SIGNAL( pressed() ), this, SLOT( ClickOnCancel() ) ) ;
196   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
197
198   connect( SelectButtonC1A1, SIGNAL (pressed() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
199   connect( LineEdit_NameMesh, SIGNAL (textChanged(const QString&)  ), this, SLOT( TextChangedInLineEdit(const QString&) ) ) ;
200
201   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
202   connect( myVisuGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
203   connect( myVisuGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
204   
205 //  int x, y ;
206 //  myVisuGUI->DefineDlgPosition( this, x, y ) ;
207 //  this->move( x, y ) ;
208   this->show() ; 
209
210   return ;
211 }
212
213
214 //=================================================================================
215 // function : ConstructorsClicked()
216 // purpose  : Radio button management
217 //=================================================================================
218 void VisuGUI_InitMeshDlg::ConstructorsClicked(int constructorId)
219 {
220   return ;
221 }
222
223 //=================================================================================
224 // function : ClickOnOk()
225 // purpose  :
226 //=================================================================================
227 void VisuGUI_InitMeshDlg::ClickOnOk()
228 {
229   this->ClickOnApply() ;
230   this->ClickOnCancel() ;
231
232   return ;
233 }
234
235 //=================================================================================
236 // function : ClickOnApply()
237 // purpose  :
238 //=================================================================================
239 void VisuGUI_InitMeshDlg::ClickOnApply()
240 {
241   switch(myConstructorId)
242     { 
243     case 0 :
244       { 
245         if ( !myNameMesh.isEmpty() && !myNameMesh.isNull() && !myGeomShape->_is_nil() )
246           myVisuGUI->InitMesh( mySMeshMesh, myNameMesh ) ;
247         break ;
248       }
249     }
250 }
251
252
253 //=================================================================================
254 // function : ClickOnCancel()
255 // purpose  :
256 //=================================================================================
257 void VisuGUI_InitMeshDlg::ClickOnCancel()
258 {
259   mySelection->ClearFilters() ;
260   disconnect( mySelection, 0, this, 0 );
261   myVisuGUI->ResetState() ;
262   reject() ;
263   return ;
264 }
265
266
267 //=================================================================================
268 // function : SelectionIntoArgument()
269 // purpose  : Called when selection as changed or other case
270 //=================================================================================
271 void VisuGUI_InitMeshDlg::SelectionIntoArgument()
272 {
273   myEditCurrentArgument->setText("") ;
274   QString aString = ""; 
275   myGeomShape = GEOM::GEOM_Shape::_nil();
276
277   int nbSel = myVisuGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
278   if ( nbSel != 1 ) {
279     return ;
280   } else {
281     Standard_Boolean testResult ;
282     Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
283     myGeomShape = myVisuGUI->ConvertIOinGEOMShape(IO, testResult) ;
284     if( !testResult ) return ;
285   }
286   
287   myEditCurrentArgument->setText(aString) ;
288 }
289
290
291 //=================================================================================
292 // function : SetEditCurrentArgument()
293 // purpose  :
294 //=================================================================================
295 void VisuGUI_InitMeshDlg::SetEditCurrentArgument()
296 {
297   QPushButton* send = (QPushButton*)sender();
298   switch (myConstructorId)
299     {
300     case 0: /* default constructor */
301       { 
302         if(send == SelectButtonC1A1) {
303           LineEditC1A1->setFocus() ;
304           myEditCurrentArgument = LineEditC1A1;
305         }
306         SelectionIntoArgument() ;
307         break;
308       }
309     }
310   return ;
311 }
312
313 //=================================================================================
314 // function : DeactivateActiveDialog()
315 // purpose  :
316 //=================================================================================
317 void VisuGUI_InitMeshDlg::DeactivateActiveDialog()
318 {
319   if ( GroupConstructors->isEnabled() ) {
320     disconnect( mySelection, 0, this, 0 );
321     GroupConstructors->setEnabled(false) ;
322     GroupC1->setEnabled(false) ;
323     GroupButtons->setEnabled(false) ;
324   }
325 }
326
327
328 //=================================================================================
329 // function : ActivateThisDialog()
330 // purpose  :
331 //=================================================================================
332 void VisuGUI_InitMeshDlg::ActivateThisDialog()
333 {
334   myVisuGUI->EmitSignalDeactivateDialog() ;   
335   GroupConstructors->setEnabled(true) ;
336   GroupC1->setEnabled(true) ;
337   GroupButtons->setEnabled(true) ;
338   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
339   return ;
340 }
341
342
343 //=================================================================================
344 // function : enterEvent()
345 // purpose  :
346 //=================================================================================
347 void VisuGUI_InitMeshDlg::enterEvent(QEvent* e)
348 {
349   if ( GroupConstructors->isEnabled() )
350     return ;  
351   ActivateThisDialog() ;
352   return ;
353 }
354
355
356 //=================================================================================
357 // function : closeEvent()
358 // purpose  :
359 //=================================================================================
360 void VisuGUI_InitMeshDlg::closeEvent( QCloseEvent* e )
361 {
362   this->ClickOnCancel() ;
363   return ;
364 }
365
366 //=================================================================================
367 // function : TextChangedInLineEdit()
368 // purpose  :
369 //=================================================================================
370 void VisuGUI_InitMeshDlg::TextChangedInLineEdit(const QString& newText)
371 {  
372   QLineEdit* send = (QLineEdit*)sender();
373   
374   if (send == LineEdit_NameMesh) {
375     myNameMesh = newText;
376   }
377   return ;
378 }