Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYLookupTableWidget.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 
23 #include "TYLookupTableWidget.h"
24 
25 #include <cassert>
26 #include <limits>
27 
28 #include <boost/foreach.hpp>
29 #include <boost/tuple/tuple.hpp>
30 #include <boost/math/special_functions/fpclassify.hpp>
31 
32 #include <math.h>
33 #include <qpainter.h>
34 #include <qrect.h>
35 #include <QtWidgets>
36 
38 
39 TYLookupTableWidget::TYLookupTableWidget(const TYPalette* palette, QWidget* parent, const char* name)
40  : QWidget(parent), _palette(palette), _rectPal(0)
41 {
42  setObjectName(name);
43 
44  setFixedHeight(30);
45  _rectPal = new QRect(0, 0, width(), height());
46 }
47 
49 
51 {
52  QPainter paint(this);
53  drawPalette(&paint);
54 }
55 
56 void TYLookupTableWidget::resizeEvent(QResizeEvent* e)
57 {
58  _rectPal->setSize(e->size());
59 }
60 
61 void TYLookupTableWidget::drawPalette(QPainter* painter)
62 {
63  size_t nbColors = _palette->getNbColors();
64  if (nbColors == 0)
65  {
66  return;
67  }
68 
69  const int bounds_width = 0.1 * _rectPal->width();
70  const int width_but_bounds = _rectPal->width() - 2 * bounds_width;
71 
72  int x = 0;
73  TYPalette::values_type prev_value = -std::numeric_limits<float>::infinity();
74 
75  TYPalette::values_type value = NAN;
76  OColor color;
77  BOOST_FOREACH (boost::tie(value, color), _palette->getColorMap())
78  {
79  // Handle specially first and last iteration
80  const int dx =
81  boost::math::isinf(value - prev_value)
82  ? bounds_width
83  : (_palette->normalize(value) - _palette->normalize(prev_value)) * width_but_bounds;
84  prev_value = value;
85  painter->fillRect(x, _rectPal->top(), x + dx, _rectPal->bottom(), toQColor(color));
86  x += dx;
87  }
88 }
89 
91  const char* name)
92  : QWidget(parent)
93 {
94  if (!parent)
95  {
96  parent = this;
97  }
98 
99  QGridLayout* pPreviewLayout = new QGridLayout();
100  parent->setLayout(pPreviewLayout);
101 
102  p_scale = new TYLookupTableWidget(palette, this, name);
103  p_minBound = new QDoubleSpinBox();
104  p_maxBound = new QDoubleSpinBox();
105 
106  p_minBound->setDecimals(1);
107  p_minBound->setReadOnly(true);
108  p_minBound->setButtonSymbols(QAbstractSpinBox::NoButtons);
109  p_maxBound->setDecimals(1);
110  p_maxBound->setReadOnly(true);
111  p_maxBound->setButtonSymbols(QAbstractSpinBox::NoButtons);
112 
113  pPreviewLayout->addWidget(p_scale, 1, 0, 1, 3);
114  pPreviewLayout->addWidget(p_minBound, 0, 0, Qt::AlignLeft);
115  pPreviewLayout->addWidget(p_maxBound, 0, 2, Qt::AlignRight);
116 
117  update(palette);
118 }
119 
121 
123 {
124  assert(p_scale->_palette == palette && "Inconsistent use of the update slot");
125  p_minBound->setValue(p_scale->_palette->getValueMin());
126  p_maxBound->setValue(p_scale->_palette->getValueMax());
127  p_scale->repaint();
128  QWidget::update();
129 }
Outil IHM utile a l'affichage de la palette de couleur (fichier header)
QColor toQColor(const OColor &color)
const char * name
Definition: color.h:36
TYLabeledLookupTableWidget(const TYPalette *palette, QWidget *parent=0, const char *name=0)
TYLookupTableWidget * p_scale
void update(const TYPalette *palette)
Outil IHM utile a l'affichage de la palette de couleur.
TYLookupTableWidget(const TYPalette *palette, QWidget *parent=0, const char *name=0)
void drawPalette(QPainter *painter)
void paintEvent(QPaintEvent *)
void resizeEvent(QResizeEvent *)
const TYPalette * _palette
Classe de definition d'une palette.
Definition: TYPalette.h:41
const color_map_type & getColorMap() const
Get de la lookup table.
Definition: TYPalette.h:226
size_t getNbColors() const
Getter for the number of colors.
Definition: TYPalette.h:122
values_type getValueMax() const
Get de la valeur max.
Definition: TYPalette.h:112
float values_type
The real number type used to store values (typically double or float)
Definition: TYPalette.h:49
values_type normalize(values_type value) const
Normalize a value, ie convert a value between min and max to a percentage.
Definition: TYPalette.cpp:483
values_type getValueMin() const
Get de la valeur min.
Definition: TYPalette.h:102