Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYSolverParamsWidgetManager.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 <QCoreApplication>
24 #include <QDir>
25 #include <QJsonParseError>
26 #include <QJsonDocument>
27 
28 #include "Tympan/core/config.h"
29 #include "Tympan/core/exceptions.h"
31 
33 #include <iostream>
34 #define TR(id) OLocalizator::getString("TYSolverParamsWidgetManager", (id))
35 
37 {
38  QString test(QCoreApplication::applicationDirPath() + "/" + SOLVER_PARAMS_JSON);
39  readDataModel(test);
40 }
41 
43 {
44  readDataModel(dataModelPath);
45 }
46 
47 void TYSolverParamsWidgetManager::readDataModel(QString dataModelPath)
48 {
49 
50  // Lecture du modele de donnees des parametres du solveur
51  dataModelPath = QDir::toNativeSeparators(dataModelPath);
52 
53  QFile file(dataModelPath);
54  bool opened = file.open(QIODevice::ReadOnly | QIODevice::Text);
55 
56  if (!opened)
57  throw tympan::invalid_data(TR("id_invalid_datamodel_filepath").arg(dataModelPath).toStdString())
59 
60  QJsonParseError JsonParseError;
61  QJsonObject dataModel = QJsonDocument::fromJson(file.readAll(), &JsonParseError).object();
62  file.close();
63 
64  // Map<nom_parametre, data_model>
65  for (QString sectionName : dataModel.keys())
66  {
67  QJsonObject section = dataModel[sectionName].toObject();
68  for (QString paramName : section.keys())
69  {
70  _dataModelMap.insert(
71  paramName, TYSolverParamsDataModel::fromJsonObject(paramName, section[paramName].toObject()));
72  }
73  }
74 }
75 
77 {
78  if (!_dataModelMap.contains(paramName))
79  throw tympan::invalid_data((paramName).toStdString()) << tympan_source_loc;
80  return _dataModelMap[paramName];
81 }
82 
84 {
85  if (!_solverParamsWidgets.contains(paramName))
86  throw tympan::invalid_data((paramName).toStdString()) << tympan_source_loc;
87  return _solverParamsWidgets[paramName];
88 }
89 
91 {
92  TYSolverParamsDataModel* dataModel = this->getDataModel(paramName);
93  return makeWidget(dataModel);
94 }
95 
97 {
98 
99  // creation d'un widget en fonction du modele de donnees
100  TYSolverParamsWidget* widget = nullptr;
101  if (dataModel->isLabeledValues())
102  {
103  widget = new TYSolverParamsRadioButtonsWidget(dataModel);
104  }
105  else if (dataModel->isBool())
106  {
107  widget = new TYSolverParamsCheckBoxWidget(dataModel);
108  }
109  else
110  {
111  widget = new TYSolverParamsInputValueWidget(dataModel);
112  }
113 
114  // enregister le widget
115  registerWidget(widget);
116 
117  return widget;
118 }
119 
121 {
122 
123  // enregistrement du widget et connection de la valeur de son parametre au gestionnaire de widgets
124  _solverParamsWidgets.insert(widget->dataModel->paramName, widget);
125  QObject::connect(widget, &TYSolverParamsWidget::valueChanged, this,
127 }
128 
129 void TYSolverParamsWidgetManager::updateWidgets(QString newParamValues)
130 {
131 
132  _solverParams = newParamValues;
133 
134  // separations de la chaine de characteres en lignes
135  QStringList lines = _solverParams.split(QRegExp("[\r\n]"), Qt::SkipEmptyParts);
136  for (QString line : lines)
137  {
138 
139  // recuperation du nom et de la valeur du parametre
140  QStringList splitted_line = line.split(QRegExp("\\s?=\\s?"));
141  if (splitted_line.size() == 2)
142  {
143  QString paramName = splitted_line[0];
144  QString value = splitted_line[1];
145  if (_solverParamsWidgets.contains(paramName))
146  {
147 
148  // on remplace des True/False par des nombres
149  if (value == "True")
150  value = "1";
151  if (value == "False")
152  value = "0";
153 
154  // mise a jours de la valeur du widget
155  getWidget(paramName)->setValue(value);
156  }
157  }
158  }
159 }
160 
162 {
163  const TYSolverParamsWidget* editedWidget = (TYSolverParamsWidget*)QObject::sender();
164  QString paramName = editedWidget->dataModel->paramName;
165  newValue = newValue.replace(',', '.');
166  // on remplace les nombres utilises pour representer les booleens par True/False
167  if (editedWidget->dataModel->isBool())
168  {
169  newValue = (newValue == "1" ? "True" : "False");
170  }
171  const QRegExp paramRegex{QRegExp::escape(paramName) + "\\s?=\\s?([[0-9]*\\.?[0-9]*|(True|False))"};
172  _solverParams = _solverParams.replace(paramRegex, paramName + "=" + newValue);
173 }
174 
176 {
177  // on force l'emission du signal valueChanged pour chaque widget enregistre
178  // pour s'assurer que 'IHM est bien à jours
179  for (QString paramName : _solverParamsWidgets.keys())
180  {
181  TYSolverParamsWidget* widget = _solverParamsWidgets[paramName];
182  emit widget->valueChanged(widget->value());
183  }
184 }
#define TR(id)
Objet permettant de gerer les differents widgets servant a parametriser le solveur.
Objet contenant les informations concernant les parametres du solveur.
static TYSolverParamsDataModel * fromJsonObject(QString paramName, QJsonObject dataModelJson)
QMap< QString, TYSolverParamsDataModel * > _dataModelMap
void registerWidget(TYSolverParamsWidget *widget)
TYSolverParamsDataModel * getDataModel(QString paramName)
void readDataModel(QString dataModelPath)
void updateWidgets(QString newParamValues)
TYSolverParamsWidget * makeWidget(QString paramName)
QMap< QString, TYSolverParamsWidget * > _solverParamsWidgets
TYSolverParamsWidget * getWidget(QString paramName)
Objet de base dont doivent heriter les widgets utilises pour controler les parametres du solveur.
void valueChanged(QString)
void setValue(QString value)
TYSolverParamsDataModel * dataModel
Utilities to handle exceptions and to pretty-print value.
#define tympan_source_loc
This macro build a source_loc object to be attached to a tympan::Exception.
Definition: exceptions.h:76
The base exception class for errors due to invalid data.
Definition: exceptions.h:60