Salome HOME
PAL10665 - unnecessary Plot2d in CreateHypothesis dialog
[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   myPreview->hide();
123   GroupC1Layout->addWidget( myPreview, row, 1 );
124
125   paramIt = myParamList.begin();
126   std::list<QWidget*>::const_iterator anIt = mySpinList.begin();
127   for( ; paramIt!=myParamList.end(); paramIt++, anIt++ )
128   {
129     (*paramIt)->TakeValue( *anIt );
130     UpdateShown( *paramIt, *anIt );
131     FunctionPreview( *paramIt, *anIt );
132   }
133
134   /***************************************************************/
135   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
136   GroupButtons->setColumnLayout(0, Qt::Vertical);
137   GroupButtons->layout()->setSpacing(0);
138   GroupButtons->layout()->setMargin(0);
139   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
140   GroupButtonsLayout->setAlignment(Qt::AlignTop);
141   GroupButtonsLayout->setSpacing(6);
142   GroupButtonsLayout->setMargin(11);
143   /* Ok button */
144   myButtonOk = new QPushButton(GroupButtons, "buttonOk");
145   myButtonOk->setText(tr("SMESH_BUT_OK"));
146   myButtonOk->setAutoDefault(TRUE);
147   myButtonOk->setDefault(TRUE);
148   GroupButtonsLayout->addWidget(myButtonOk, 0, 0);
149   /* add spacer between buttons */
150   GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
151   /* Cancel button */
152   myButtonCancel = new QPushButton(GroupButtons, "buttonCancel");
153   myButtonCancel->setText(tr("SMESH_BUT_CANCEL"));
154   myButtonCancel->setAutoDefault(TRUE);
155   GroupButtonsLayout->addWidget(myButtonCancel, 0, 2);
156
157   /***************************************************************/
158   topLayout->addWidget(GroupC1,      1 );
159   topLayout->addWidget(GroupButtons, 0 );
160
161   /* signals and slots connections */
162   connect(myButtonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
163   connect(myButtonCancel, SIGNAL(clicked()), this, SLOT(reject()));
164 }
165
166 //======================================================================================
167 // function : ~SMESHGUI_aParameterDlg()
168 // purpose  : Destructor
169 //======================================================================================
170 SMESHGUI_aParameterDlg::~SMESHGUI_aParameterDlg()
171 {
172 }
173
174 //=======================================================================
175 //function : ClickOnOk
176 //purpose  :
177 //=======================================================================
178 void SMESHGUI_aParameterDlg::ClickOnOk()
179 {
180   if (!mySMESHGUI->isActiveStudyLocked()) {
181     list<SMESHGUI_aParameterPtr>::iterator paramIt  = myParamList.begin();
182     list<QWidget*>::iterator               widgetIt = mySpinList.begin();
183     for (;
184          paramIt != myParamList.end() && widgetIt != mySpinList.end();
185          paramIt++ , widgetIt++)
186       (*paramIt)->TakeValue(*widgetIt);
187
188     accept();
189   }
190 }
191
192 //=======================================================================
193 // function : Parameters()
194 // purpose  : return a list of parameters from a dialog box
195 //=======================================================================
196 bool SMESHGUI_aParameterDlg::Parameters( SMESHGUI* theModule, 
197                                          list<SMESHGUI_aParameterPtr> params,
198                                          const char *aTitle)
199 {
200   if (!params.empty()) {
201     SMESHGUI_aParameterDlg *Dialog =
202       new SMESHGUI_aParameterDlg( theModule, params, aTitle, TRUE);
203     return (Dialog->exec() == QDialog::Accepted);
204   }
205   return false;
206 }
207
208 //=======================================================================
209 // function : FunctionPreview
210 // purpose  : 
211 //=======================================================================
212 void SMESHGUI_aParameterDlg::FunctionPreview( const SMESHGUI_aParameterPtr p, QWidget* w )
213 {
214   if( !w || !w->isShown() )
215     return;
216
217   SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( p.operator->() );
218   SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( p.operator->() );
219   SMESHGUI_boolParameter* bool_param = dynamic_cast<SMESHGUI_boolParameter*>( p.operator->() );
220   if( str_param && str_param->needPreview() )
221   {
222     QString val; str_param->GetNewText( val );
223     if( !val.isNull() )
224       myPreview->setParams( val );
225   }
226   else if( tab_param && tab_param->needPreview() )
227   {
228     SMESH::double_array d;
229     tab_param->data( d );
230     myPreview->setParams( d );
231   }
232   else if( bool_param && bool_param->needPreview() )
233   {
234     int exp=0;
235     bool_param->GetNewInt( exp );
236     myPreview->setIsExp( exp );
237   }
238 }
239
240 //=======================================================================
241 // function : onValueChanged
242 // purpose  : 
243 //=======================================================================
244 void SMESHGUI_aParameterDlg::onValueChanged()
245 {
246   if( sender()->inherits( "QWidget" ) )
247   {
248     QWidget* w = ( QWidget* )sender();
249
250
251     std::list<QWidget*>::const_iterator anIt = mySpinList.begin(),
252                                         aLast = mySpinList.end();
253     std::list<SMESHGUI_aParameterPtr>::const_iterator aPIt = myParamList.begin();
254     for( ; anIt!=aLast; anIt++, aPIt++ )
255       if( *anIt == w )
256       {
257         (*aPIt)->TakeValue( w );
258         UpdateShown( *aPIt, w );
259         FunctionPreview( *aPIt, w );
260         break;
261       }
262   }
263 }
264
265 //=======================================================================
266 // function : onValueChanged
267 // purpose  :
268 //=======================================================================
269 void SMESHGUI_aParameterDlg::UpdateShown( const SMESHGUI_aParameterPtr param, QWidget* w )
270 {
271   SMESHGUI_dependParameter* depPar = dynamic_cast<SMESHGUI_enumParameter*>( param.get() );
272   if( !depPar )
273     depPar = dynamic_cast<SMESHGUI_boolParameter*>( param.get() );
274
275   if( !depPar )
276     return;
277
278   SMESHGUI_dependParameter::ShownMap& map = depPar->shownMap();
279   if( map.isEmpty() )
280     return;
281
282   int val;
283   depPar->TakeValue( w );
284   depPar->GetNewInt( val );
285   bool hasValue = map.contains( val );
286
287   std::list<QWidget*>::const_iterator anIt = mySpinList.begin(),
288                                       aLast = mySpinList.end(),
289                                       aLIt = myLabelList.begin();
290   std::list<SMESHGUI_aParameterPtr>::iterator aPIt = myParamList.begin();
291   bool preview = false;
292   for( int i=0; anIt!=aLast; anIt++, aLIt++, i++, aPIt++ )
293   {
294     bool shown = hasValue && map[ val ].contains( i );
295     (*anIt)->setShown( shown );
296     (*aLIt)->setShown( shown );
297     if( shown )
298     {
299       SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( (*aPIt).operator->() );
300       SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( (*aPIt).operator->() );
301       preview = preview || ( str_param && str_param->needPreview() ) || ( tab_param && tab_param->needPreview() );
302     }
303   }
304   myPreview->setShown( preview );
305 }