Salome HOME
03145925ff81be452f060bfa901cb6b6bcb34c8b
[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 <qtextedit.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   QGridLayout* topLayout = new QGridLayout(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 = 0;
104     switch (param->GetType()) {
105     case SMESHGUI_aParameter::DOUBLE: {
106       SMESHGUI_SpinBox* spin = new SMESHGUI_SpinBox(GroupC1);
107       aSpinWidget = spin;
108       spin->setPrecision(12);
109       break;
110     }
111     case SMESHGUI_aParameter::INT: {
112       QSpinBox* spin = new QSpinBox(GroupC1);
113       aSpinWidget = spin;
114       break;
115     }
116     case SMESHGUI_aParameter::TEXT: {
117       QTextEdit* edit = new QTextEdit(GroupC1);
118       edit->setWordWrap(QTextEdit::NoWrap);
119       edit->setTextFormat(Qt::PlainText);
120       aSpinWidget = edit;
121       break;
122     }
123     default:;
124     }
125     if (aSpinWidget) {
126       GroupC1Layout->addWidget(aSpinWidget, row, 1);
127       aSpinWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
128       aSpinWidget->setMinimumSize(150, 0);
129       param->InitializeWidget(aSpinWidget);
130       mySpinList.push_back(aSpinWidget);
131     }
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,      0, 0);
159   topLayout->addWidget(GroupButtons, 1, 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 }