Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYSolverParamsWidget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
23 #include <QRadioButton>
24 #include <QHBoxLayout>
25 #include <QSpacerItem>
26 #include <QSizePolicy>
27 #include <QIntValidator>
28 #include <QDoubleValidator>
29 #include <QFontMetrics>
30 
32 #include "TYSolverParamsWidget.h"
33 
34 #define TR(id) OLocalizator::getString("TYSolverParamsWidget", (id))
35 
36 using namespace std;
37 
38 TYSolverParamsDataModel::TYSolverParamsDataModel(QString paramName, QString type, QStringList valuesLabel,
39  QString defaultValue)
40 {
41 
42  this->paramName = paramName;
43  this->type = type;
44  this->valueLabels = valuesLabel;
45  this->defaultValue = defaultValue;
46 }
47 
48 TYSolverParamsDataModel* TYSolverParamsDataModel::fromJsonObject(QString paramName, QJsonObject dataModelJson)
49 {
50  QString defaultValue = dataModelJson["default"].toVariant().toString();
51  if (dataModelJson["type"] == "bool")
52  if (defaultValue == "true")
53  defaultValue = "1";
54  else if (defaultValue == "false")
55  defaultValue = "0";
56 
57  return new TYSolverParamsDataModel(paramName, dataModelJson["type"].toString(),
58  dataModelJson["labels"].toVariant().toStringList(), defaultValue);
59 }
60 
62 {
63  this->dataModel = dataModel;
64  this->setValue(dataModel->defaultValue);
65  this->setToolTip(TR(dataModel->paramName + "_help"));
66 }
67 
68 void TYSolverParamsWidget::setValue(QString value)
69 {
70  if (m_value != value)
71  {
72  m_value = value;
73  emit valueChanged(value);
74  }
75 }
76 
78 {
79  setValue(QString::number(value));
80 }
81 
83 {
84  setValue(int(value));
85 }
86 
88  : TYSolverParamsWidget(dataModel)
89 {
90  this->setLayout(new QHBoxLayout(this));
91  this->layout()->setContentsMargins(-1, 0, -1, 0);
92 
93  label = new QLabel(this);
94  label->setText(TR(dataModel->paramName));
95  // definition de la longueur du label en nombre de characteres 'M'
96  label->setMinimumWidth(QFontMetrics(label->font()).horizontalAdvance("M") * 15);
97  label->setMaximumWidth(QFontMetrics(label->font()).horizontalAdvance("M") * 15);
98  this->layout()->addWidget(label);
99 
100  lineEdit = new QLineEdit(this);
101  // definition de la longueur du lineEdit en nombre de characteres 'M'
102  lineEdit->setMinimumWidth(QFontMetrics(lineEdit->font()).horizontalAdvance("M") * 10);
103  lineEdit->setMaximumWidth(QFontMetrics(lineEdit->font()).horizontalAdvance("M") * 10);
104 
105  // validation des entrees en fonction du type de donnees
106  if (dataModel->isInt())
107  {
108  lineEdit->setValidator(new QIntValidator());
109  }
110  else
111  {
112  // nombres decimals avec separateur '.' ou ','
113  lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]*[\\.,][0-9]*")));
114  }
115 
116  this->layout()->addWidget(lineEdit);
117  this->layout()->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
118 
119  // connection du widget a la valeur du parametre
120  auto setValueStr = static_cast<void (TYSolverParamsWidget::*)(QString)>(&TYSolverParamsWidget::setValue);
121  QObject::connect(lineEdit, &QLineEdit::textChanged, this, setValueStr);
122  QObject::connect(this, &TYSolverParamsWidget::valueChanged, lineEdit, &QLineEdit::setText);
123 }
124 
126  : TYSolverParamsWidget(dataModel)
127 {
128 
129  // on met un buttonGroup dans une groupBox
130  groupBox = new QGroupBox(this);
131  groupBox->setLayout(new QHBoxLayout(groupBox));
132  groupBox->setTitle(TR(dataModel->paramName));
133  buttonGroup = new QButtonGroup(groupBox);
134 
135  // ajout d'un bouton radio pour chaque valeur possible
136  int id = 0;
137  for (QString label : dataModel->valueLabels)
138  {
139  QRadioButton* radioButton = new QRadioButton(groupBox);
140  radioButton->setText(TR(dataModel->paramName + "_" + label));
141  buttonGroup->addButton(radioButton, id);
142  id++;
143  groupBox->layout()->addWidget(radioButton);
144  }
145  groupBox->layout()->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
146 
147  // on place la groupBox dans le layout du widget principal
148  this->setLayout(new QHBoxLayout(this));
149  this->layout()->addWidget(groupBox);
150 
151  // connection du widget a la valeur du parametre
152  auto buttonClickedInt = static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::idClicked);
153  auto setValueInt = static_cast<void (TYSolverParamsWidget::*)(int)>(&TYSolverParamsWidget::setValue);
154  QObject::connect(buttonGroup, buttonClickedInt, this, setValueInt);
155 
156  auto toggle_button = [this](QString value) { buttonGroup->button(value.toInt())->setChecked(true); };
157  QObject::connect(this, &TYSolverParamsWidget::valueChanged, buttonGroup, toggle_button);
158 }
159 
161  : TYSolverParamsWidget(dataModel)
162 {
163  this->setLayout(new QHBoxLayout(this));
164  this->layout()->setContentsMargins(-1, 0, -1, 0);
165 
166  checkBox = new QCheckBox(this);
167  checkBox->setText(TR(dataModel->paramName));
168  this->layout()->addWidget(checkBox);
169  checkBox->setChecked(false);
170 
171  // connection du widget a la valeur du parametre
172  auto setValueBool = static_cast<void (TYSolverParamsWidget::*)(bool)>(&TYSolverParamsWidget::setValue);
173  QObject::connect(checkBox, &QCheckBox::toggled, this, setValueBool);
174 
175  auto checkTheBox = [this](QString value) { checkBox->setChecked(value == "1"); };
176  QObject::connect(this, &TYSolverParamsWidget::valueChanged, checkBox, checkTheBox);
177 }
#define TR(id)
Widgets permettant de controler les parametres du solveur.
TYSolverParamsCheckBoxWidget(TYSolverParamsDataModel *dataModel)
Objet contenant les informations concernant les parametres du solveur.
TYSolverParamsDataModel(QString paramName, QString type, QStringList valuesLabel=QStringList(), QString defaultValue="0")
static TYSolverParamsDataModel * fromJsonObject(QString paramName, QJsonObject dataModelJson)
TYSolverParamsInputValueWidget(TYSolverParamsDataModel *dataModel)
TYSolverParamsRadioButtonsWidget(TYSolverParamsDataModel *dataModel)
Objet de base dont doivent heriter les widgets utilises pour controler les parametres du solveur.
void valueChanged(QString)
void setValue(QString value)
TYSolverParamsWidget(TYSolverParamsDataModel *dataModel)
TYSolverParamsDataModel * dataModel