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