Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYHistoWidget.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 
24 #include "TYHistoWidget.h"
25 
26 #include <math.h>
27 #include "TYSpectreWidget.h"
28 #include <QPainter>
30 #include "TYPrintDialog.h"
31 #include <QtPrintSupport/QPrinter>
32 #include <QtPrintSupport/QPrintDialog>
33 #include <QtWidgets>
34 
35 #define TR(id) OLocalizator::getString("TYSpectreWidget", (id))
36 
37 // using namespace Qt;
38 
39 TYHistoWidget::TYHistoWidget(TYSpectreWidget* pSpectreWidget, bool dbType, QWidget* _pParent /*=NULL*/)
40  : QWidget(_pParent)
41 {
42  _pSpectreWidget = pSpectreWidget;
43  _dbType = dbType;
44 }
45 
47 
49 {
50  TYPrintDialog* pDialog = new TYPrintDialog(this);
51  pDialog->_groupBoxProjet->setEnabled(false);
52  pDialog->_groupBoxCalcul->setEnabled(false);
53  QPrinter* printer = new QPrinter();
54  if (pDialog->exec() == QDialog::Accepted)
55  {
56  QPrintDialog dialog(printer, this);
57  if (dialog.exec())
58  {
59  QPainter paint(printer);
60  QPixmap tmpPix;
61 
62  int mid = int(printer->width() / 2) -
63  int(paint.fontMetrics().horizontalAdvance(pDialog->_lineEditTete->text()) / 2);
64  int midX = int(printer->width() / 2);
65  int midY = int(printer->height() / 2);
66  paint.drawText(mid, 20, pDialog->_lineEditTete->text());
67  mid = int(printer->width() / 2) -
68  int(paint.fontMetrics().horizontalAdvance(pDialog->_lineEditPied->text()) / 2);
69  paint.drawText(mid, printer->height() - 20, pDialog->_lineEditPied->text());
70  paint.setFont(QFont("Helvetica", 15, QFont::Bold));
71  mid = int(printer->width() / 2) -
72  int(paint.fontMetrics().horizontalAdvance(pDialog->_lineEditTitre->text()) / 2);
73  paint.drawText(mid, printer->height() * 14 / 15, pDialog->_lineEditTitre->text());
74  paint.setFont(QFont());
75 
76  double Wmetric = printer->width() * 9 / 10;
77  double Hmetric = printer->height() * 8 / 9;
78  double x = width();
79  double y = height();
80  double w = Wmetric;
81  double h = Wmetric * (y / x);
82 
83  if (h > Hmetric)
84  {
85  h = Hmetric;
86  w = Hmetric * (x / y);
87  }
88  paint.translate(midX - (w / 2), midY - (h / 2));
89  paintInto(&paint, w, h);
90  }
91  }
92  delete printer;
93 }
94 
95 void TYHistoWidget::paintEvent(QPaintEvent* event)
96 {
97  int w = 0, h = 0;
98  QPainter* p = new QPainter(this);
99  w = width();
100  h = height();
101 
102  paintInto(p, w, h);
103 
104  delete p;
105 }
106 
107 void TYHistoWidget::paintInto(QPainter* painter, int w, int h)
108 {
109  LPTYSpectre pSpectre = new TYSpectre();
110  _pSpectreWidget->spectre(pSpectre);
112 
113  size_t nbFreq = tabFreq.size();
114 
115  // Initialisation des parametres
116  double width = double(w);
117  double height = double(h);
118  double value[50], hauteur = NAN, L = width * 2 / 3, hzero = NAN;
119  double hmax = 0, largeur = NAN, x = NAN;
120  size_t i = 0;
121  int val = -1;
122  double hmin = 0;
123  QString num;
124  double textPos = NAN;
125  double midFreq = double(nbFreq) / 2;
126 
127  for (i = 0; i < nbFreq; i++)
128  {
129  value[i] = pSpectre->getTabValReel()[i];
130  if (value[i] >= 0)
131  {
132  if (value[i] > hmax)
133  {
134  hmax = value[i];
135  }
136  }
137  else if (value[i] < hmin)
138  {
139  hmin = value[i];
140  }
141  }
142 
143  double echelle = height * 7 / 10 / (-hmin + hmax);
144  hzero = height * 8.5 / 10 + hmin * echelle;
145 
146  if ((hmin == 0) && (hmax == 0))
147  {
148  echelle = 1;
149  hzero = height / 2;
150  }
151 
152  L = width / 2;
153  x = width * 1 / 12;
154  largeur = L / (nbFreq + 1);
155  val = -1;
156 
157  // Histogramme
158  painter->setPen(Qt::blue);
159 
160  {
161  double valMax = 0.0;
162 
163  for (i = 0; i < nbFreq; i++)
164  {
165  hauteur = value[i] * echelle;
166  painter->fillRect((int)x, (int)(hzero - hauteur), (int)largeur, (int)hauteur,
167  QColor(100, 255, 110));
168  painter->drawRect((int)x, (int)hzero - hauteur, (int)largeur, (int)hauteur);
169 
170  if (val != ROUND(value[i]))
171  {
172  val = ROUND(value[i]);
173  valMax = MAX(val, valMax);
174  }
175 
176  if ((i == int(midFreq)) || (i == (nbFreq - 1)))
177  {
178  if (hauteur < 0)
179  {
180  textPos = hzero - 5;
181  }
182  else
183  {
184  textPos = hzero + 17;
185  }
186  painter->drawLine((int)x, (int)(hzero + 5), (int)x, (int)(hzero - 5));
187  painter->drawText((int)(x - 10), (int)textPos, num.setNum(tabFreq[i]));
188  }
189 
190  x += largeur;
191  }
192 
193  // Val Max
194  painter->setPen(QPen(Qt::blue, 2));
195  painter->drawLine((int)(width * 1 / 12 - 5), (int)(hzero - valMax * echelle),
196  (int)(width * 1 / 12 + 5), (int)(hzero - valMax * echelle));
197  painter->drawText((int)(width * 1 / 12 - 25), (int)(hzero - valMax * echelle), num.setNum(valMax));
198  // Val Max / 2
199  painter->drawLine((int)(width * 1 / 12 - 5), (int)(hzero - (valMax / 2) * echelle),
200  (int)(width * 1 / 12 + 5), (int)(hzero - (valMax / 2) * echelle));
201  painter->drawText((int)(width * 1 / 12 - 25), (int)(hzero - (valMax / 2) * echelle),
202  num.setNum((valMax / 2)));
203 
204  painter->setPen(QPen(Qt::black, 2));
205  painter->drawLine((int)(width * 1 / 12 - 2), (int)hzero, (int)(width * 11 / 12), (int)hzero);
206  painter->drawLine((int)(width * 1 / 12), (int)(height * 18 / 20), (int)(width * 1 / 12),
207  (int)(hzero - hmax * echelle - 10));
208  painter->drawText((int)(width * 1 / 12), (int)(hzero - hmax * echelle - 20), "dB");
209  painter->drawText((int)(x + 2 * largeur), (int)(height * 18 / 20), TR("id_abscisse"));
210 
211  x += width / 3 - 4 * largeur;
212 
213  // Reglage de l'echelle pour dBLin
214  double value = pSpectre->valGlobDBLin();
215  if (value < 0)
216  {
217  hmin = value;
218  hmax = 0;
219  }
220  else
221  {
222  hmax = value;
223  hmin = 0;
224  }
225  echelle = height * 7 / 10 / (-hmin + hmax);
226  hzero = height * 8.5 / 10 + hmin * echelle;
227  hauteur = value * echelle;
228 
229  // Construction d'un nouvel axe pour dBLin
230  painter->drawLine((int)x, (int)(height * 18 / 20), (int)x, (int)(hzero - hmax * echelle - 10));
231  painter->drawLine((int)(x - 5), (int)(hzero - value * echelle), (int)(x + 5),
232  (int)(hzero - value * echelle));
233  painter->drawText((int)(x - 15), (int)(hzero - hmax * echelle - 20), num.setNum(value, 'f', 2));
234 
235  // Dessin de dbLin
236  painter->fillRect((int)x, (int)(hzero - hauteur), (int)largeur, (int)hauteur, QColor(100, 255, 110));
237  painter->setPen(Qt::blue);
238  painter->drawRect((int)x, (int)(hzero - hauteur), (int)largeur, (int)hauteur);
239  painter->setPen(Qt::black);
240  painter->drawText((int)(x - 20), (int)(height * 19 / 20), TR("id_spectre_histo_dbZ"));
241 
242  x += 3 * largeur;
243 
244  // Reglage de l'echelle pour dBA
245  value = pSpectre->valGlobDBA();
246  if (value < 0)
247  {
248  hmin = value;
249  hmax = 0;
250  }
251  else
252  {
253  hmax = value;
254  hmin = 0;
255  }
256  echelle = height * 7 / 10 / (-hmin + hmax);
257  hzero = height * 8.5 / 10 + hmin * echelle;
258  hauteur = value * echelle;
259 
260  painter->setPen(QPen(Qt::black, 2));
261  // Construction d'un nouvel axe pour dBA
262  painter->drawLine((int)x, (int)(height * 18 / 20), (int)x, (int)(hzero - hmax * echelle - 10));
263  painter->drawLine((int)(x - 5), (int)(hzero - value * echelle), (int)(x + 5),
264  (int)(hzero - value * echelle));
265  painter->drawText((int)x, (int)(hzero - hmax * echelle - 20), num.setNum(value, 'f', 2));
266 
267  // Dessin de dBA
268  painter->fillRect((int)x, (int)(hzero - hauteur), (int)largeur, (int)hauteur, QColor(100, 255, 110));
269  painter->setPen(Qt::blue);
270  painter->drawRect((int)x, (int)(hzero - hauteur), (int)largeur, (int)hauteur);
271  painter->setPen(Qt::black);
272  painter->drawText(x, height * 19 / 20, TR("id_spectre_histo_dbA"));
273  }
274 }
int ROUND(double a)
Compute the rounded value of a number.
Definition: 3d.h:192
double MAX(double a, double b)
Return the biggest number of two ones.
Definition: 3d.h:90
#define TR(id)
outil IHM pour un histogramme (fichier header)
Boite de dialogue des parametres d'impression (fichier header)
const std::vector< double > tabFreq
outil IHM pour un spectre (fichier header)
OTabFreq TYTabFreq
Collection des frequences.
Definition: TYSpectre.h:27
double valGlobDBA() const
Compute the global value dB[A] of a one-third Octave spectrum.
Definition: spectre.cpp:691
double valGlobDBLin() const
Compute the global value dB[Lin] of a one-third Octave spectrum.
Definition: spectre.cpp:679
double * getTabValReel() override
Definition: spectre.h:357
TYSpectreWidget * _pSpectreWidget
Definition: TYHistoWidget.h:79
void paintInto(QPainter *painter, int w, int h)
TYHistoWidget(TYSpectreWidget *pSpectreWidget, bool dbType, QWidget *_pParent=NULL)
virtual void paintEvent(QPaintEvent *event)
virtual ~TYHistoWidget()
classe pour une boite de dialogue des parametres d'impression.
Definition: TYPrintDialog.h:51
QGroupBox * _groupBoxCalcul
Definition: TYPrintDialog.h:66
QLineEdit * _lineEditPied
Definition: TYPrintDialog.h:83
QLineEdit * _lineEditTete
Definition: TYPrintDialog.h:84
QGroupBox * _groupBoxProjet
Definition: TYPrintDialog.h:61
QLineEdit * _lineEditTitre
Definition: TYPrintDialog.h:86
classe de l'objet IHM pour un spectre
void spectre(TYSpectre *pSpectre)
Construit un spectre a partir du contenu du widget.
virtual TYSpectreForm getForm()
Set/Get de la forme du spectre.
Definition: TYSpectre.h:107
static const TYTabFreq getTabFreqNorm(TYSpectreForm form=SPECTRE_FORM_TIERS)
Definition: TYSpectre.cpp:419