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