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