Salome HOME
b9c9f1d3b3091c97c6a3e10fa96fe12e66cf32b7
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_aParameterDlg.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_aParameterDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_aParameterDlg.h"
30 #include "SMESHGUI_aParameter.h"
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_FunctionPreview.h"
35
36 #include "SUIT_Tools.h"
37 #include "SUIT_Desktop.h"
38
39 // QT Includes
40 #include <qgroupbox.h>
41 #include <qlabel.h>
42 #include <qpushbutton.h>
43 #include <qlayout.h>
44 #include <qspinbox.h>
45 #include <qvalidator.h>
46 #include <qlineedit.h>
47
48 using namespace std;
49
50 //======================================================================================
51 // function : SMESHGUI_aParameterDlg()
52 //
53 //  The dialog will by default be modal, unless you set 'modal' to
54 //  false when constructing dialog
55 //
56 //======================================================================================
57 SMESHGUI_aParameterDlg::SMESHGUI_aParameterDlg
58                                        ( SMESHGUI* theModule,
59                                          std::list<SMESHGUI_aParameterPtr> params,
60                                          QString                           title,
61                                          bool                              modal)
62 : QDialog( SMESH::GetDesktop( theModule ), "MyParameterDialog", modal, WStyle_Customize |
63            WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),           
64   myParamList(params),
65   mySMESHGUI(theModule)
66 {
67   /* creating widgets */
68   init();
69   /* title */
70   setCaption(title);
71
72   /* Move widget on the botton right corner of main widget */
73   SUIT_Tools::centerWidget(this, SMESH::GetDesktop( theModule ) );
74 }
75
76 //======================================================================================
77 // function : SMESHGUI_aParameterDlg::init()
78 // purpose  : creates dialog's layout
79 //======================================================================================
80 void SMESHGUI_aParameterDlg::init()
81 {
82   setSizeGripEnabled(TRUE);
83
84   QVBoxLayout* topLayout = new QVBoxLayout(this);
85   topLayout->setMargin(11); topLayout->setSpacing(6);
86
87   /***************************************************************/
88   QGroupBox* GroupC1 = new QGroupBox(this, "GroupC1");
89   GroupC1->setColumnLayout(0, Qt::Vertical);
90   GroupC1->layout()->setSpacing(0);
91   GroupC1->layout()->setMargin(0);
92   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1->layout());
93   GroupC1Layout->setAlignment(Qt::AlignTop);
94   GroupC1Layout->setSpacing(6);
95   GroupC1Layout->setMargin(11);
96   /* Spin boxes with labels */
97   list<SMESHGUI_aParameterPtr>::iterator paramIt = myParamList.begin();
98   int row;
99   for( row = 0; paramIt != myParamList.end(); paramIt++ , row++)
100   {
101     SMESHGUI_aParameterPtr param = (*paramIt);
102     QLabel * label = new QLabel(GroupC1, "TextLabel");
103     GroupC1Layout->addWidget(label, row, 0);
104     label->setText(param->Label());
105     QWidget* aSpinWidget = param->CreateWidget( GroupC1 );
106     if (aSpinWidget) {
107       GroupC1Layout->addWidget(aSpinWidget, row, 1);
108       aSpinWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
109       aSpinWidget->setMinimumSize(150, 0);
110
111       QString sig = param->sigValueChanged();
112       if( !sig.isEmpty() /*&& param->GetType()!=SMESHGUI_aParameter::TABLE*/ )
113         connect( aSpinWidget, sig.latin1(), this, SLOT( onValueChanged() ) );
114       
115       param->InitializeWidget(aSpinWidget);
116       mySpinList.push_back(aSpinWidget);
117       myLabelList.push_back(label);
118     }
119   }
120
121   myPreview = new SMESHGUI_FunctionPreview( GroupC1 );
122   GroupC1Layout->addWidget( myPreview, row, 1 );
123
124   paramIt = myParamList.begin();
125   std::list<QWidget*>::const_iterator anIt = mySpinList.begin();
126   for( ; paramIt!=myParamList.end(); paramIt++, anIt++ )
127   {
128     (*paramIt)->TakeValue( *anIt );
129     UpdateShown( *paramIt, *anIt );
130     FunctionPreview( *paramIt, *anIt );
131   }
132
133   /***************************************************************/
134   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
135   GroupButtons->setColumnLayout(0, Qt::Vertical);
136   GroupButtons->layout()->setSpacing(0);
137   GroupButtons->layout()->setMargin(0);
138   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
139   GroupButtonsLayout->setAlignment(Qt::AlignTop);
140   GroupButtonsLayout->setSpacing(6);
141   GroupButtonsLayout->setMargin(11);
142   /* Ok button */
143   myButtonOk = new QPushButton(GroupButtons, "buttonOk");
144   myButtonOk->setText(tr("SMESH_BUT_OK"));
145   myButtonOk->setAutoDefault(TRUE);
146   myButtonOk->setDefault(TRUE);
147   GroupButtonsLayout->addWidget(myButtonOk, 0, 0);
148   /* add spacer between buttons */
149   GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
150   /* Cancel button */
151   myButtonCancel = new QPushButton(GroupButtons, "buttonCancel");
152   myButtonCancel->setText(tr("SMESH_BUT_CANCEL"));
153   myButtonCancel->setAutoDefault(TRUE);
154   GroupButtonsLayout->addWidget(myButtonCancel, 0, 2);
155
156   /***************************************************************/
157   topLayout->addWidget(GroupC1,      1 );
158   topLayout->addWidget(GroupButtons, 0 );
159
160   /* signals and slots connections */
161   connect(myButtonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
162   connect(myButtonCancel, SIGNAL(clicked()), this, SLOT(reject()));
163 }
164
165 //======================================================================================
166 // function : ~SMESHGUI_aParameterDlg()
167 // purpose  : Destructor
168 //======================================================================================
169 SMESHGUI_aParameterDlg::~SMESHGUI_aParameterDlg()
170 {
171 }
172
173 //=======================================================================
174 //function : ClickOnOk
175 //purpose  :
176 //=======================================================================
177 void SMESHGUI_aParameterDlg::ClickOnOk()
178 {
179   if (!mySMESHGUI->isActiveStudyLocked()) {
180     list<SMESHGUI_aParameterPtr>::iterator paramIt  = myParamList.begin();
181     list<QWidget*>::iterator               widgetIt = mySpinList.begin();
182     for (;
183          paramIt != myParamList.end() && widgetIt != mySpinList.end();
184          paramIt++ , widgetIt++)
185       (*paramIt)->TakeValue(*widgetIt);
186
187     accept();
188   }
189 }
190
191 //=======================================================================
192 // function : Parameters()
193 // purpose  : return a list of parameters from a dialog box
194 //=======================================================================
195 bool SMESHGUI_aParameterDlg::Parameters( SMESHGUI* theModule, 
196                                          list<SMESHGUI_aParameterPtr> params,
197                                          const char *aTitle)
198 {
199   if (!params.empty()) {
200     SMESHGUI_aParameterDlg *Dialog =
201       new SMESHGUI_aParameterDlg( theModule, params, aTitle, TRUE);
202     return (Dialog->exec() == QDialog::Accepted);
203   }
204   return false;
205 }
206
207 //=======================================================================
208 // function : FunctionPreview
209 // purpose  : 
210 //=======================================================================
211 void SMESHGUI_aParameterDlg::FunctionPreview( const SMESHGUI_aParameterPtr p, QWidget* w )
212 {
213   if( !w || !w->isShown() )
214     return;
215
216   SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( p.operator->() );
217   SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( p.operator->() );
218   SMESHGUI_boolParameter* bool_param = dynamic_cast<SMESHGUI_boolParameter*>( p.operator->() );
219   if( str_param && str_param->needPreview() )
220   {
221     QString val; str_param->GetNewText( val );
222     if( !val.isNull() )
223       myPreview->setParams( val );
224   }
225   else if( tab_param && tab_param->needPreview() )
226   {
227     SMESH::double_array d;
228     tab_param->data( d );
229     myPreview->setParams( d );
230   }
231   else if( bool_param && bool_param->needPreview() )
232   {
233     int exp=0;
234     bool_param->GetNewInt( exp );
235     myPreview->setIsExp( exp );
236   }
237 }
238
239 //=======================================================================
240 // function : onValueChanged
241 // purpose  : 
242 //=======================================================================
243 void SMESHGUI_aParameterDlg::onValueChanged()
244 {
245   if( sender()->inherits( "QWidget" ) )
246   {
247     QWidget* w = ( QWidget* )sender();
248
249
250     std::list<QWidget*>::const_iterator anIt = mySpinList.begin(),
251                                         aLast = mySpinList.end();
252     std::list<SMESHGUI_aParameterPtr>::const_iterator aPIt = myParamList.begin();
253     for( ; anIt!=aLast; anIt++, aPIt++ )
254       if( *anIt == w )
255       {
256         (*aPIt)->TakeValue( w );
257         UpdateShown( *aPIt, w );
258         FunctionPreview( *aPIt, w );
259         break;
260       }
261   }
262 }
263
264 //=======================================================================
265 // function : onValueChanged
266 // purpose  :
267 //=======================================================================
268 void SMESHGUI_aParameterDlg::UpdateShown( const SMESHGUI_aParameterPtr param, QWidget* w )
269 {
270   SMESHGUI_dependParameter* depPar = dynamic_cast<SMESHGUI_enumParameter*>( param.get() );
271   if( !depPar )
272     depPar = dynamic_cast<SMESHGUI_boolParameter*>( param.get() );
273
274   if( !depPar )
275     return;
276
277   SMESHGUI_dependParameter::ShownMap& map = depPar->shownMap();
278   if( map.isEmpty() )
279     return;
280
281   int val;
282   depPar->TakeValue( w );
283   depPar->GetNewInt( val );
284   bool hasValue = map.contains( val );
285
286   std::list<QWidget*>::const_iterator anIt = mySpinList.begin(),
287                                       aLast = mySpinList.end(),
288                                       aLIt = myLabelList.begin();
289   std::list<SMESHGUI_aParameterPtr>::iterator aPIt = myParamList.begin();
290   bool preview = false;
291   for( int i=0; anIt!=aLast; anIt++, aLIt++, i++, aPIt++ )
292   {
293     bool shown = hasValue && map[ val ].contains( i );
294     (*anIt)->setShown( shown );
295     (*aLIt)->setShown( shown );
296     if( shown )
297     {
298       SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( (*aPIt).operator->() );
299       SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( (*aPIt).operator->() );
300       preview = preview || ( str_param && str_param->needPreview() ) || ( tab_param && tab_param->needPreview() );
301     }
302   }
303   myPreview->setShown( preview );
304 }