Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYCourbeWidget.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 
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 #include <QtWidgets>
35 
36 #define TR(id) OLocalizator::getString("TYSpectreWidget", (id))
37 
38 // using namespace Qt;
39 
40 TYCourbeWidget::TYCourbeWidget(TYSpectreWidget* pSpectreWidget, bool dbType, QWidget* _pParent /*=NULL*/)
41  : QWidget(_pParent)
42 {
43  _pSpectreWidget = pSpectreWidget;
44  _dbType = dbType;
45 }
46 
48 
50 {
51  TYPrintDialog* pDialog = new TYPrintDialog(this);
52  pDialog->_groupBoxProjet->setEnabled(false);
53  pDialog->_groupBoxCalcul->setEnabled(false);
54  QPrinter* printer = new QPrinter();
55  if (pDialog->exec() == QDialog::Accepted)
56  {
57  QPrintDialog dialog(printer, this);
58  if (dialog.exec())
59  {
60  QPainter paint(printer);
61  QPixmap tmpPix;
62 
63  int mid = int(printer->width() / 2) -
64  int(paint.fontMetrics().horizontalAdvance(pDialog->_lineEditTete->text()) / 2);
65  int midX = int(printer->width() / 2);
66  int midY = int(printer->height() / 2);
67  paint.drawText(mid, 20, pDialog->_lineEditTete->text());
68  mid = int(printer->width() / 2) -
69  int(paint.fontMetrics().horizontalAdvance(pDialog->_lineEditPied->text()) / 2);
70  paint.drawText(mid, printer->height() - 20, pDialog->_lineEditPied->text());
71  paint.setFont(QFont("Helvetica", 15, QFont::Bold));
72  mid = int(printer->width() / 2) -
73  int(paint.fontMetrics().horizontalAdvance(pDialog->_lineEditTitre->text()) / 2);
74  paint.drawText(mid, printer->height() * 14 / 15, pDialog->_lineEditTitre->text());
75  paint.setFont(QFont());
76 
77  double Wmetric = printer->width() * 9 / 10;
78  double Hmetric = printer->height() * 8 / 9;
79  double x = width();
80  double y = height();
81  double w = Wmetric;
82  double h = Wmetric * (y / x);
83 
84  if (h > Hmetric)
85  {
86  h = Hmetric;
87  w = Hmetric * (x / y);
88  }
89 
90  paint.translate(midX - (w / 2), midY - (h / 2));
91  paintInto(&paint, w, h);
92  }
93  }
94  delete printer;
95 }
96 
97 void TYCourbeWidget::paintEvent(QPaintEvent* event)
98 {
99  int w = 0, h = 0;
100  QPainter* p = new QPainter(this);
101  w = width();
102  h = height();
103 
104  paintInto(p, w, h);
105 
106  delete p;
107 }
108 
109 void TYCourbeWidget::paintInto(QPainter* painter, int w, int h)
110 {
111 
112  LPTYSpectre pSpectre = new TYSpectre();
113  _pSpectreWidget->spectre(pSpectre);
115 
116  // RNU
117  size_t nbFreq = tabFreq.size();
118 
119  // Initialisation des parametres
120  double width = double(w);
121  double height = double(h);
122  double value[50], hauteur = NAN, L = width * 2 / 3, hzero = NAN;
123  double hmax = 0, largeur = L / (nbFreq + 1), x = width * 1 / 6 + 10;
124  int val = -1;
125  size_t i = 0;
126  double hmin = 0;
127  QString num;
128  double textPos = NAN;
129  double midFreq = nbFreq / 2.0;
130 
131  for (i = 0; i < nbFreq; i++)
132  {
133  value[i] = pSpectre->getTabValReel()[i];
134 
135  if (value[i] >= 0)
136  {
137  if (value[i] > hmax)
138  {
139  hmax = value[i];
140  }
141  }
142  else if (value[i] < hmin)
143  {
144  hmin = value[i];
145  }
146  }
147 
148  double echelle = height * 7 / 10 / (-hmin + hmax);
149  hzero = height * 8.5 / 10 + hmin * echelle;
150  if ((hmin == 0) && (hmax == 0))
151  {
152  echelle = 1;
153  hzero = height / 2;
154  }
155 
156  // Courbe
157  {
158  double valMax = 0.0;
159  hauteur = value[0] * echelle;
160  double hprev = hzero - hauteur;
161  x += largeur;
162  painter->setPen(QPen(Qt::red, 2));
163 
164  for (i = 1; i < nbFreq; i++)
165  {
166  hauteur = value[i] * echelle;
167  painter->drawLine((int)x, (int)hprev, (int)(x + largeur), (int)(hzero - hauteur));
168  hprev = hzero - 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->drawLine((int)(width * 1 / 6 + 5), (int)(hzero - valMax * echelle),
195  (int)(width * 1 / 6 + 15), (int)(hzero - valMax * echelle));
196  painter->drawText((int)(width * 1 / 6 - 25), (int)(hzero - valMax * echelle), num.setNum(valMax));
197  // Val Max / 2
198  painter->drawLine((int)(width * 1 / 6 + 5), (int)(hzero - (valMax / 2) * echelle),
199  (int)(width * 1 / 6 + 15), (int)(hzero - (valMax / 2) * echelle));
200  painter->drawText((int)(width * 1 / 6 - 25), (int)(hzero - (valMax / 2) * echelle),
201  num.setNum((valMax / 2)));
202 
203  painter->setPen(QPen(Qt::black, 2));
204  painter->drawLine((int)(width * 1 / 6), (int)(hzero), (int)(width * 5 / 6), (int)hzero);
205  painter->drawLine((int)(width * 1 / 6 + 10), (int)(height * 18 / 20), (int)(width * 1 / 6 + 10),
206  (int)(hzero - hmax * echelle - 10));
207  painter->drawText((int)(width * 1 / 6), (int)(hzero - hmax * echelle - 20), "dB");
208  painter->drawText((int)(width * 5 / 6), (int)hzero, TR("id_abscisse"));
209  }
210 }
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:357
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: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