Salome HOME
Bug 9282: Order of commands
[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->setColumnLayout(0, Qt::Vertical);
98     GroupButtons->layout()->setSpacing(0);
99     GroupButtons->layout()->setMargin(0);
100     GroupButtonsLayout = new QGridLayout (GroupButtons->layout());
101     GroupButtonsLayout->setAlignment(Qt::AlignTop);
102     GroupButtonsLayout->setSpacing(6);
103     GroupButtonsLayout->setMargin(11);
104
105     buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
106     buttonCancel->setText(tr("BUT_CLOSE"));
107     buttonCancel->setAutoDefault(TRUE);
108     GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
109
110     buttonApply = new QPushButton (GroupButtons, "buttonApply");
111     buttonApply->setText(tr("BUT_APPLY"));
112     buttonApply->setAutoDefault(TRUE);
113     GroupButtonsLayout->addWidget(buttonApply, 0, 1);
114
115     QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
116     GroupButtonsLayout->addItem(spacer_9, 0, 2);
117
118     buttonOk = new QPushButton(GroupButtons, "buttonOk");
119     buttonOk->setText(tr("BUT_OK"));
120     buttonOk->setAutoDefault(TRUE);
121     buttonOk->setDefault(TRUE);
122     GroupButtonsLayout->addWidget(buttonOk, 0, 0);
123
124     VisuGUI_InitMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
125
126     /***************************************************************/
127     GroupC1 = new QGroupBox( this, "GroupC1" );
128     GroupC1->setTitle( tr( "Visu_ARGUMENTS"  ) );
129     GroupC1->setMinimumSize( QSize( 0, 0 ) );
130     GroupC1->setFrameShape( QGroupBox::Box );
131     GroupC1->setFrameShadow( QGroupBox::Sunken );
132     GroupC1->setColumnLayout(0, Qt::Vertical );
133     GroupC1->layout()->setSpacing( 0 );
134     GroupC1->layout()->setMargin( 0 );
135     GroupC1Layout = new QGridLayout( GroupC1->layout() );
136     GroupC1Layout->setAlignment( Qt::AlignTop );
137     GroupC1Layout->setSpacing( 6 );
138     GroupC1Layout->setMargin( 11 );
139
140     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
141     TextLabelC1A1->setText( tr( "Visu_OBJECT_GEOM"  ) );
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( image0 );
149     SelectButtonC1A1->setToggleButton( FALSE );
150     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
151     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
152     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
153
154     TextLabel_NameMesh = new QLabel( GroupC1, "TextLabel_NameMesh" );
155     TextLabel_NameMesh->setText( tr( "Visu_NAME"  ) );
156     GroupC1Layout->addWidget( TextLabel_NameMesh, 1, 0 );
157     LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
158     GroupC1Layout->addWidget( LineEdit_NameMesh, 1, 2 );
159
160     VisuGUI_InitMeshDlgLayout->addWidget( GroupC1, 1, 0 );
161     /***************************************************************/
162
163     Init(Sel);
164 }
165
166 //=================================================================================
167 // function : ~VisuGUI_InitMeshDlg()
168 // purpose  : Destroys the object and frees any allocated resources
169 //=================================================================================
170 VisuGUI_InitMeshDlg::~VisuGUI_InitMeshDlg()
171 {
172     // no need to delete child widgets, Qt does it all for us
173 }
174
175
176 //=================================================================================
177 // function : Init()
178 // purpose  :
179 //=================================================================================
180 void VisuGUI_InitMeshDlg::Init( SALOME_Selection* Sel )
181 {
182   GroupC1->show();
183   myConstructorId = 0 ;
184   Constructor1->setChecked( TRUE );
185   myEditCurrentArgument = LineEditC1A1 ;        
186   mySelection = Sel;
187   myVisuGUI = VisuGUI::GetVisuGUI() ;
188   myVisuGUI->SetActiveDialogBox( (QDialog*)this ) ;
189
190   myGeomFilter = new SALOME_TypeFilter( "GEOM" );
191   mySelection->AddFilter( myGeomFilter );
192
193   myNameMesh = "Mesh";
194
195   /* signals and slots connections */
196   connect( buttonOk, SIGNAL( pressed() ),     this, SLOT( ClickOnOk() ) );
197   connect( buttonApply, SIGNAL( pressed() ), this, SLOT(ClickOnApply() ) );
198   connect( buttonCancel, SIGNAL( pressed() ), this, SLOT( ClickOnCancel() ) ) ;
199   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
200
201   connect( SelectButtonC1A1, SIGNAL (pressed() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
202   connect( LineEdit_NameMesh, SIGNAL (textChanged(const QString&)  ), this, SLOT( TextChangedInLineEdit(const QString&) ) ) ;
203
204   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
205   connect( myVisuGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
206   connect( myVisuGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
207   
208 //  int x, y ;
209 //  myVisuGUI->DefineDlgPosition( this, x, y ) ;
210 //  this->move( x, y ) ;
211   this->show() ; 
212
213   return ;
214 }
215
216
217 //=================================================================================
218 // function : ConstructorsClicked()
219 // purpose  : Radio button management
220 //=================================================================================
221 void VisuGUI_InitMeshDlg::ConstructorsClicked(int constructorId)
222 {
223   return ;
224 }
225
226 //=================================================================================
227 // function : ClickOnOk()
228 // purpose  :
229 //=================================================================================
230 void VisuGUI_InitMeshDlg::ClickOnOk()
231 {
232   this->ClickOnApply() ;
233   this->ClickOnCancel() ;
234
235   return ;
236 }
237
238 //=================================================================================
239 // function : ClickOnApply()
240 // purpose  :
241 //=================================================================================
242 void VisuGUI_InitMeshDlg::ClickOnApply()
243 {
244   switch(myConstructorId)
245     { 
246     case 0 :
247       { 
248         if ( !myNameMesh.isEmpty() && !myNameMesh.isNull() && !myGeomShape->_is_nil() )
249           myVisuGUI->InitMesh( mySMeshMesh, myNameMesh ) ;
250         break ;
251       }
252     }
253 }
254
255
256 //=================================================================================
257 // function : ClickOnCancel()
258 // purpose  :
259 //=================================================================================
260 void VisuGUI_InitMeshDlg::ClickOnCancel()
261 {
262   mySelection->ClearFilters() ;
263   disconnect( mySelection, 0, this, 0 );
264   myVisuGUI->ResetState() ;
265   reject() ;
266   return ;
267 }
268
269
270 //=================================================================================
271 // function : SelectionIntoArgument()
272 // purpose  : Called when selection as changed or other case
273 //=================================================================================
274 void VisuGUI_InitMeshDlg::SelectionIntoArgument()
275 {
276   myEditCurrentArgument->setText("") ;
277   QString aString = ""; 
278   myGeomShape = GEOM::GEOM_Shape::_nil();
279
280   int nbSel = myVisuGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
281   if ( nbSel != 1 ) {
282     return ;
283   } else {
284     Standard_Boolean testResult ;
285     Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
286     myGeomShape = myVisuGUI->ConvertIOinGEOMShape(IO, testResult) ;
287     if( !testResult ) return ;
288   }
289   
290   myEditCurrentArgument->setText(aString) ;
291 }
292
293
294 //=================================================================================
295 // function : SetEditCurrentArgument()
296 // purpose  :
297 //=================================================================================
298 void VisuGUI_InitMeshDlg::SetEditCurrentArgument()
299 {
300   QPushButton* send = (QPushButton*)sender();
301   switch (myConstructorId)
302     {
303     case 0: /* default constructor */
304       { 
305         if(send == SelectButtonC1A1) {
306           LineEditC1A1->setFocus() ;
307           myEditCurrentArgument = LineEditC1A1;
308         }
309         SelectionIntoArgument() ;
310         break;
311       }
312     }
313   return ;
314 }
315
316 //=================================================================================
317 // function : DeactivateActiveDialog()
318 // purpose  :
319 //=================================================================================
320 void VisuGUI_InitMeshDlg::DeactivateActiveDialog()
321 {
322   if ( GroupConstructors->isEnabled() ) {
323     disconnect( mySelection, 0, this, 0 );
324     GroupConstructors->setEnabled(false) ;
325     GroupC1->setEnabled(false) ;
326     GroupButtons->setEnabled(false) ;
327   }
328 }
329
330
331 //=================================================================================
332 // function : ActivateThisDialog()
333 // purpose  :
334 //=================================================================================
335 void VisuGUI_InitMeshDlg::ActivateThisDialog()
336 {
337   myVisuGUI->EmitSignalDeactivateDialog() ;   
338   GroupConstructors->setEnabled(true) ;
339   GroupC1->setEnabled(true) ;
340   GroupButtons->setEnabled(true) ;
341   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
342   return ;
343 }
344
345
346 //=================================================================================
347 // function : enterEvent()
348 // purpose  :
349 //=================================================================================
350 void VisuGUI_InitMeshDlg::enterEvent(QEvent* e)
351 {
352   if ( GroupConstructors->isEnabled() )
353     return ;  
354   ActivateThisDialog() ;
355   return ;
356 }
357
358
359 //=================================================================================
360 // function : closeEvent()
361 // purpose  :
362 //=================================================================================
363 void VisuGUI_InitMeshDlg::closeEvent( QCloseEvent* e )
364 {
365   this->ClickOnCancel() ;
366   return ;
367 }
368
369 //=================================================================================
370 // function : TextChangedInLineEdit()
371 // purpose  :
372 //=================================================================================
373 void VisuGUI_InitMeshDlg::TextChangedInLineEdit(const QString& newText)
374 {  
375   QLineEdit* send = (QLineEdit*)sender();
376   
377   if (send == LineEdit_NameMesh) {
378     myNameMesh = newText;
379   }
380   return ;
381 }