Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYLookupTableWidget.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 
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 
37 
38 TYLookupTableWidget::TYLookupTableWidget(const TYPalette* palette, QWidget* parent, const char* name)
39  : QWidget(parent), _palette(palette), _rectPal(0)
40 {
41  setObjectName(name);
42 
43  setFixedHeight(30);
44  _rectPal = new QRect(0, 0, width(), height());
45 }
46 
48 
50 {
51  QPainter paint(this);
52  drawPalette(&paint);
53 }
54 
55 void TYLookupTableWidget::resizeEvent(QResizeEvent* e)
56 {
57  _rectPal->setSize(e->size());
58 }
59 
60 void TYLookupTableWidget::drawPalette(QPainter* painter)
61 {
62  size_t nbColors = _palette->getNbColors();
63  if (nbColors == 0)
64  {
65  return;
66  }
67 
68  const int bounds_width = 0.1 * _rectPal->width();
69  const int width_but_bounds = _rectPal->width() - 2 * bounds_width;
70 
71  int x = 0;
72  TYPalette::values_type prev_value = -std::numeric_limits<float>::infinity();
73 
74  TYPalette::values_type value = NAN;
75  OColor color;
76  BOOST_FOREACH (boost::tie(value, color), _palette->getColorMap())
77  {
78  // Handle specially first and last iteration
79  const int dx =
80  boost::math::isinf(value - prev_value)
81  ? bounds_width
82  : (_palette->normalize(value) - _palette->normalize(prev_value)) * width_but_bounds;
83  prev_value = value;
84  painter->fillRect(x, _rectPal->top(), x + dx, _rectPal->bottom(), toQColor(color));
85  x += dx;
86  }
87 }
88 
90  const char* name)
91  : QWidget(parent)
92 {
93  if (!parent)
94  {
95  parent = this;
96  }
97 
98  QGridLayout* pPreviewLayout = new QGridLayout();
99  parent->setLayout(pPreviewLayout);
100 
101  p_scale = new TYLookupTableWidget(palette, this, name);
102  p_minBound = new QDoubleSpinBox();
103  p_maxBound = new QDoubleSpinBox();
104 
105  p_minBound->setDecimals(1);
106  p_minBound->setReadOnly(true);
107  p_minBound->setButtonSymbols(QAbstractSpinBox::NoButtons);
108  p_maxBound->setDecimals(1);
109  p_maxBound->setReadOnly(true);
110  p_maxBound->setButtonSymbols(QAbstractSpinBox::NoButtons);
111 
112  pPreviewLayout->addWidget(p_scale, 1, 0, 1, 3);
113  pPreviewLayout->addWidget(p_minBound, 0, 0, Qt::AlignLeft);
114  pPreviewLayout->addWidget(p_maxBound, 0, 2, Qt::AlignRight);
115 
116  update(palette);
117 }
118 
120 
122 {
123  assert(p_scale->_palette == palette && "Inconsistent use of the update slot");
124  p_minBound->setValue(p_scale->_palette->getValueMin());
125  p_maxBound->setValue(p_scale->_palette->getValueMax());
126  p_scale->repaint();
127  QWidget::update();
128 }
Outil IHM utile a l'affichage de la palette de couleur (fichier header)
QColor toQColor(const OColor &color)
const char * name
Definition: color.h:31
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