Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[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
34 #include "SUIT_Tools.h"
35 #include "SUIT_Desktop.h"
36
37 // QT Includes
38 #include <qgroupbox.h>
39 #include <qlabel.h>
40 #include <qpushbutton.h>
41 #include <qlayout.h>
42 #include <qspinbox.h>
43 #include <qvalidator.h>
44 #include <qtextedit.h>
45
46 using namespace std;
47
48 //======================================================================================
49 // function : SMESHGUI_aParameterDlg()
50 //
51 //  The dialog will by default be modal, unless you set 'modal' to
52 //  false when constructing dialog
53 //
54 //======================================================================================
55 SMESHGUI_aParameterDlg::SMESHGUI_aParameterDlg
56                                         (std::list<SMESHGUI_aParameterPtr> params,
57                                          QWidget*                          parent,
58                                          QString                           title,
59                                          bool                              modal)
60 : QDialog(parent, "MyParameterDialog", modal, WStyle_Customize |
61           WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
62   myParamList(params)
63 {
64   /* creating widgets */
65   init();
66   /* title */
67   setCaption(title);
68
69   /* Move widget on the botton right corner of main widget */
70   SUIT_Tools::centerWidget(this, parent);
71 }
72
73 //======================================================================================
74 // function : SMESHGUI_aParameterDlg::init()
75 // purpose  : creates dialog's layout
76 //======================================================================================
77 void SMESHGUI_aParameterDlg::init()
78 {
79   setSizeGripEnabled(TRUE);
80
81   QGridLayout* topLayout = new QGridLayout(this);
82   topLayout->setMargin(11); topLayout->setSpacing(6);
83
84   /***************************************************************/
85   QGroupBox* GroupC1 = new QGroupBox(this, "GroupC1");
86   GroupC1->setColumnLayout(0, Qt::Vertical);
87   GroupC1->layout()->setSpacing(0);
88   GroupC1->layout()->setMargin(0);
89   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1->layout());
90   GroupC1Layout->setAlignment(Qt::AlignTop);
91   GroupC1Layout->setSpacing(6);
92   GroupC1Layout->setMargin(11);
93   /* Spin boxes with labels */
94   list<SMESHGUI_aParameterPtr>::iterator paramIt = myParamList.begin();
95   for (int row = 0; paramIt != myParamList.end(); paramIt++ , row++)
96   {
97     SMESHGUI_aParameterPtr param = (*paramIt);
98     QLabel * label = new QLabel(GroupC1, "TextLabel");
99     GroupC1Layout->addWidget(label, row, 0);
100     label->setText(param->Label());
101     QWidget* aSpinWidget = 0;
102     switch (param->GetType()) {
103     case SMESHGUI_aParameter::DOUBLE: {
104       SMESHGUI_SpinBox* spin = new SMESHGUI_SpinBox(GroupC1);
105       aSpinWidget = spin;
106       spin->setPrecision(12);
107       break;
108     }
109     case SMESHGUI_aParameter::INT: {
110       QSpinBox* spin = new QSpinBox(GroupC1);
111       aSpinWidget = spin;
112       break;
113     }
114     case SMESHGUI_aParameter::TEXT: {
115       QTextEdit* edit = new QTextEdit(GroupC1);
116       edit->setWordWrap(QTextEdit::NoWrap);
117       edit->setTextFormat(Qt::PlainText);
118       aSpinWidget = edit;
119       break;
120     }
121     default:;
122     }
123     if (aSpinWidget) {
124       GroupC1Layout->addWidget(aSpinWidget, row, 1);
125       aSpinWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
126       aSpinWidget->setMinimumSize(150, 0);
127       param->InitializeWidget(aSpinWidget);
128       mySpinList.push_back(aSpinWidget);
129     }
130   }
131
132   /***************************************************************/
133   QGroupBox* GroupButtons = new QGroupBox(this, "GroupButtons");
134   GroupButtons->setColumnLayout(0, Qt::Vertical);
135   GroupButtons->layout()->setSpacing(0);
136   GroupButtons->layout()->setMargin(0);
137   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
138   GroupButtonsLayout->setAlignment(Qt::AlignTop);
139   GroupButtonsLayout->setSpacing(6);
140   GroupButtonsLayout->setMargin(11);
141   /* Ok button */
142   myButtonOk = new QPushButton(GroupButtons, "buttonOk");
143   myButtonOk->setText(tr("SMESH_BUT_OK"));
144   myButtonOk->setAutoDefault(TRUE);
145   myButtonOk->setDefault(TRUE);
146   GroupButtonsLayout->addWidget(myButtonOk, 0, 0);
147   /* add spacer between buttons */
148   GroupButtonsLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
149   /* Cancel button */
150   myButtonCancel = new QPushButton(GroupButtons, "buttonCancel");
151   myButtonCancel->setText(tr("SMESH_BUT_CANCEL"));
152   myButtonCancel->setAutoDefault(TRUE);
153   GroupButtonsLayout->addWidget(myButtonCancel, 0, 2);
154
155   /***************************************************************/
156   topLayout->addWidget(GroupC1,      0, 0);
157   topLayout->addWidget(GroupButtons, 1, 0);
158
159   /* signals and slots connections */
160   connect(myButtonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
161   connect(myButtonCancel, SIGNAL(clicked()), this, SLOT(reject()));
162
163   /* Retrieve SMESHGUI */
164   mySMESHGUI = SMESHGUI::GetSMESHGUI();
165 }
166
167 //======================================================================================
168 // function : ~SMESHGUI_aParameterDlg()
169 // purpose  : Destructor
170 //======================================================================================
171 SMESHGUI_aParameterDlg::~SMESHGUI_aParameterDlg()
172 {
173 }
174
175 //=======================================================================
176 //function : ClickOnOk
177 //purpose  :
178 //=======================================================================
179 void SMESHGUI_aParameterDlg::ClickOnOk()
180 {
181   if (!mySMESHGUI->isActiveStudyLocked()) {
182     list<SMESHGUI_aParameterPtr>::iterator paramIt  = myParamList.begin();
183     list<QWidget*>::iterator               widgetIt = mySpinList.begin();
184     for (;
185          paramIt != myParamList.end() && widgetIt != mySpinList.end();
186          paramIt++ , widgetIt++)
187       (*paramIt)->TakeValue(*widgetIt);
188
189     accept();
190   }
191 }
192
193 //=======================================================================
194 // function : Parameters()
195 // purpose  : return a list of parameters from a dialog box
196 //=======================================================================
197 bool SMESHGUI_aParameterDlg::Parameters (list<SMESHGUI_aParameterPtr> params,
198                                          const char *aTitle)
199 {
200   if (!params.empty()) {
201     SMESHGUI_aParameterDlg *Dialog =
202       new SMESHGUI_aParameterDlg(params, SMESHGUI::desktop(), aTitle, TRUE);
203     return (Dialog->exec() == QDialog::Accepted);
204   }
205   return false;
206 }