Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYCourbeWidget.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 
25 #include "TYCourbeWidget.h"
26 
27 #include <math.h>
28 #include "TYSpectreWidget.h"
29 #include <QPainter>
31 #include "TYPrintDialog.h"
32 #include <QtPrintSupport/QPrinter>
33 #include <QtPrintSupport/QPrintDialog>
34 
35 #define TR(id) OLocalizator::getString("TYSpectreWidget", (id))
36 
37 using namespace Qt;
38 
39 TYCourbeWidget::TYCourbeWidget(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 
89  paint.translate(midX - (w / 2), midY - (h / 2));
90  paintInto(&paint, w, h);
91  }
92  }
93  delete printer;
94 }
95 
96 void TYCourbeWidget::paintEvent(QPaintEvent* event)
97 {
98  int w = 0, h = 0;
99  QPainter* p = new QPainter(this);
100  w = width();
101  h = height();
102 
103  paintInto(p, w, h);
104 
105  delete p;
106 }
107 
108 void TYCourbeWidget::paintInto(QPainter* painter, int w, int h)
109 {
110 
111  LPTYSpectre pSpectre = new TYSpectre();
112  _pSpectreWidget->spectre(pSpectre);
114 
115  // RNU
116  size_t nbFreq = tabFreq.size();
117 
118  // Initialisation des parametres
119  double width = double(w);
120  double height = double(h);
121  double value[50], hauteur = NAN, L = width * 2 / 3, hzero = NAN;
122  double hmax = 0, largeur = L / (nbFreq + 1), x = width * 1 / 6 + 10;
123  int val = -1;
124  size_t i = 0;
125  double hmin = 0;
126  QString num;
127  double textPos = NAN;
128  double midFreq = nbFreq / 2.0;
129  bool valid = false;
130 
131  for (i = 0; i < nbFreq; i++)
132  {
133  value[i] = pSpectre->getTabValReel()[i];
134 
135  if (value[i] != TY_SPECTRE_DEFAULT_VALUE)
136  {
137  valid = true;
138  }
139 
140  if (value[i] >= 0)
141  {
142  if (value[i] > hmax)
143  {
144  hmax = value[i];
145  }
146  }
147  else if (value[i] < hmin)
148  {
149  hmin = value[i];
150  }
151  }
152 
153  double echelle = height * 7 / 10 / (-hmin + hmax);
154  hzero = height * 8.5 / 10 + hmin * echelle;
155  if ((hmin == 0) && (hmax == 0))
156  {
157  echelle = 1;
158  hzero = height / 2;
159  }
160 
161  // Courbe
162  if (valid)
163  {
164  double valMax = 0.0;
165  hauteur = value[0] * echelle;
166  double hprev = hzero - hauteur;
167  x += largeur;
168  painter->setPen(QPen(red, 2));
169 
170  for (i = 1; i < nbFreq; i++)
171  {
172  hauteur = value[i] * echelle;
173  painter->drawLine((int)x, (int)hprev, (int)(x + largeur), (int)(hzero - hauteur));
174  hprev = hzero - hauteur;
175 
176  if (val != ROUND(value[i]))
177  {
178  val = ROUND(value[i]);
179  valMax = MAX(val, valMax);
180  }
181 
182  if ((i == int(midFreq)) || (i == (nbFreq - 1)))
183  {
184  if (hauteur < 0)
185  {
186  textPos = hzero - 5;
187  }
188  else
189  {
190  textPos = hzero + 17;
191  }
192  painter->drawLine((int)x, (int)(hzero + 5), (int)x, (int)(hzero - 5));
193  painter->drawText((int)(x - 10), (int)textPos, num.setNum(tabFreq[i]));
194  }
195 
196  x += largeur;
197  }
198 
199  // Val Max
200  painter->drawLine((int)(width * 1 / 6 + 5), (int)(hzero - valMax * echelle),
201  (int)(width * 1 / 6 + 15), (int)(hzero - valMax * echelle));
202  painter->drawText((int)(width * 1 / 6 - 25), (int)(hzero - valMax * echelle), num.setNum(valMax));
203  // Val Max / 2
204  painter->drawLine((int)(width * 1 / 6 + 5), (int)(hzero - (valMax / 2) * echelle),
205  (int)(width * 1 / 6 + 15), (int)(hzero - (valMax / 2) * echelle));
206  painter->drawText((int)(width * 1 / 6 - 25), (int)(hzero - (valMax / 2) * echelle),
207  num.setNum((valMax / 2)));
208 
209  painter->setPen(QPen(black, 2));
210  painter->drawLine((int)(width * 1 / 6), (int)(hzero), (int)(width * 5 / 6), (int)hzero);
211  painter->drawLine((int)(width * 1 / 6 + 10), (int)(height * 18 / 20), (int)(width * 1 / 6 + 10),
212  (int)(hzero - hmax * echelle - 10));
213  painter->drawText((int)(width * 1 / 6), (int)(hzero - hmax * echelle - 20), "dB");
214  painter->drawText((int)(width * 5 / 6), (int)hzero, TR("id_abscisse"));
215  }
216  else
217  {
218  painter->setPen(QPen(red, 3));
219  painter->drawText((int)(width / 10), (int)(height / 10), TR("id_invalid_spectre"));
220  }
221 }
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 une courbe (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 * getTabValReel() override
Definition: spectre.h:356
TYSpectreWidget * _pSpectreWidget
void paintInto(QPainter *painter, int w, int h)
virtual ~TYCourbeWidget()
virtual void paintEvent(QPaintEvent *event)
TYCourbeWidget(TYSpectreWidget *pSpectreWidget, bool dbType, QWidget *_pParent=NULL)
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