Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPalette.h
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 
16 /*
17  *
18  *
19  *
20  *
21  */
22 
23 #ifndef __TY_PALETTE__
24 #define __TY_PALETTE__
25 
26 #include <map>
27 
28 #include "Tympan/core/color.h"
30 
40 class TYPalette : public TYElement
41 {
45 
46  // Methodes
47 public:
49  typedef float values_type;
50 
51  typedef std::map<values_type, OColor> color_map_type;
52  typedef color_map_type::iterator color_map_iter;
53  typedef color_map_type::const_iterator color_map_const_iter;
54  typedef color_map_type::value_type color_map_item;
58  TYPalette();
59 
64  TYPalette(const TYPalette& other);
65 
70  virtual ~TYPalette();
71 
72  TYPalette& operator=(const TYPalette& other);
73 
74  bool operator==(const TYPalette& other) const;
75 
76  bool operator!=(const TYPalette& other) const;
77 
86  virtual bool deepCopy(const TYElement* pOther, bool copyId = true, bool pUseCopyTag = false);
87 
88  virtual std::string toString() const;
89 
90  virtual DOM_Element toXML(DOM_Element& domElement) /* const */;
91 
92  virtual int fromXML(DOM_Element domElement);
93 
94  void clear();
95 
96  static const values_type infinity;
97 
103  {
104  assert(getNbColors() >= 2);
105  return _colorMap.begin()->first;
106  }
107 
113  {
114  assert(getNbColors() >= 2);
115  return (++_colorMap.rbegin())->first;
116  }
117 
122  size_t getNbColors() const
123  {
124  return _colorMap.size();
125  }
126 
131  size_t getNbValues() const
132  {
133  return _colorMap.size() - 1;
134  }
135 
140  const OColor& getInfColor() const
141  {
142  assert(getNbColors() >= 2);
143  return _colorMap.begin()->second;
144  }
145 
149  void setInfColor(const OColor& color)
150  {
151  assert(getNbColors() >= 2);
152  _colorMap.begin()->second = color;
153  }
154 
159  const OColor& getSupColor() const
160  {
161  assert(_colorMap.size() >= 2);
162  return _colorMap.rbegin()->second;
163  }
164 
168  void setSupColor(const OColor& color)
169  {
170  assert(_colorMap.size() >= 2);
171  return _colorMap.rbegin()->second = color;
172  }
173 
181  bool insertColor(values_type value, const OColor& color);
182 
191  bool moveValue(values_type old_value, values_type new_value);
192 
198  bool removeValue(values_type value);
199 
203  void resetcolorMapFromColors(values_type valueMin, values_type valueMax,
204  const std::vector<OColor>& lookupTable);
205 
209  void resetToDefault();
210 
214  bool resetFromPreferences();
215 
219  void saveToPreferences();
220 
225  // OLookupTable& getLookupTable() { return _colorMap; } // XXX update callers
227  {
228  return _colorMap;
229  } // Should be replace be an smart range
230 
234  // void setColors(const OLookupTable& lookupTable) { _colorMap = lookupTable; } // XXX update callers
235 
239  // void setColorFromPercent(values_type percent, const OColor& color); //XXX merge into setColorFromValue
240 
244  const OColor& getColorFromValue(values_type value) const;
245 
249  const OColor& getColorFromIndex(unsigned i) const;
250 
254  TYPalette::values_type getValueFromIndex(unsigned i) const;
255 
259  // void setColorFromValue(values_type value, const OColor& color); // Unused : to be deleted ?
260 
265  {
266  return getValueMin() + (getValueMax() - getValueMin()) * percent;
267  }
268 
272  values_type normalize(values_type value) const;
273 
278  static LPTYPalette loadPaletteFromFile(const QString& qFileName);
279 
284  bool savePaletteToFile(const QString& qFileName);
285 
289  void makeLinearPalette(unsigned int nb_colors, float value_min, float value_max);
290 
291 protected:
293 
297  int load_legacy_XML(const DOM_Element& domElement);
298 
303 };
304 
305 #endif // __TY_PALETTE__
QDomElement DOM_Element
Definition: QT2DOM.h:30
#define TY_EXT_GRAPHIC_DECL_ONLY(classname)
Definition: TYElement.h:432
#define OPROTODECL(classname)
Definition: TYElement.h:65
#define TY_EXTENSION_DECL_ONLY(classname)
Definition: TYElement.h:386
Definition: color.h:31
Classe de definition d'une palette.
Definition: TYPalette.h:41
virtual std::string toString() const
Definition: TYPalette.cpp:200
size_t getNbValues() const
Getter for the number of values.
Definition: TYPalette.h:131
bool removeValue(values_type value)
Removes an existing bound.
Definition: TYPalette.cpp:253
void makeLinearPalette(unsigned int nb_colors, float value_min, float value_max)
Make a linear palette from a min/max couple.
Definition: TYPalette.cpp:551
const OColor & getColorFromValue(values_type value) const
Set de la lookup table.
Definition: TYPalette.cpp:491
bool resetFromPreferences()
Reset the colormap from the preferences XXX.
Definition: TYPalette.cpp:72
const OColor & getInfColor() const
Get de la couleur pour la borne inf.
Definition: TYPalette.h:140
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
void resetToDefault()
Reset the colormap to a hard-coded default.
Definition: TYPalette.cpp:49
const OColor & getSupColor() const
Get/Set de la couleur pour la borne sup.
Definition: TYPalette.h:159
bool operator!=(const TYPalette &other) const
const OColor & getColorFromIndex(unsigned i) const
Get de la couleur par index.
Definition: TYPalette.cpp:499
DOM_Element save_legacy_XML(DOM_Element &domElement)
implement loading legacy (pre v4.6) XML format for TYPalettes
Definition: TYPalette.cpp:302
TYPalette()
A TYPalette represents a mapping from values to colors.
Definition: TYPalette.cpp:42
static LPTYPalette loadPaletteFromFile(const QString &qFileName)
Create a new TYPalette from an XML file Holding only one Palette.
Definition: TYPalette.cpp:524
color_map_type _colorMap
Definition: TYPalette.h:292
bool operator==(const TYPalette &other) const
Definition: TYPalette.cpp:170
bool savePaletteToFile(const QString &qFileName)
Save the Palette into a file with the name given.
Definition: TYPalette.cpp:541
values_type getValueMax() const
Get de la valeur max.
Definition: TYPalette.h:112
TYPalette & operator=(const TYPalette &other)
Definition: TYPalette.cpp:160
void clear()
Definition: TYPalette.cpp:144
color_map_type::const_iterator color_map_const_iter
Definition: TYPalette.h:53
TYPalette::values_type getValueFromIndex(unsigned i) const
Get de la valeur par index.
Definition: TYPalette.cpp:511
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYPalette.cpp:273
bool moveValue(values_type old_value, values_type new_value)
Moves an existing bound,.
Definition: TYPalette.cpp:216
float values_type
The real number type used to store values (typically double or float)
Definition: TYPalette.h:49
void saveToPreferences()
Saves the colormap to the preferences XXX.
Definition: TYPalette.cpp:103
void setSupColor(const OColor &color)
Set de la couleur pour la borne sup.
Definition: TYPalette.h:168
virtual ~TYPalette()
Destructeur. Destructeur de la classe TYPalette.
Definition: TYPalette.cpp:158
color_map_type::value_type color_map_item
Definition: TYPalette.h:54
void resetcolorMapFromColors(values_type valueMin, values_type valueMax, const std::vector< OColor > &lookupTable)
Reset the colormap as a linear scale with the given colors between minValue and maxValue.
Definition: TYPalette.cpp:126
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
static const values_type infinity
Definition: TYPalette.h:96
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYPalette.cpp:189
std::map< values_type, OColor > color_map_type
Definition: TYPalette.h:51
virtual int fromXML(DOM_Element domElement)
Definition: TYPalette.cpp:392
values_type denormalize(values_type percent) const
Set de la couleur pour une valeur.
Definition: TYPalette.h:264
void setInfColor(const OColor &color)
Set de la couleur pour la borne inf.
Definition: TYPalette.h:149
values_type getValueMin() const
Get de la valeur min.
Definition: TYPalette.h:102
bool insertColor(values_type value, const OColor &color)
Insert a new color or update an existing one.
Definition: TYPalette.cpp:205
color_map_type::iterator color_map_iter
Definition: TYPalette.h:52
int load_legacy_XML(const DOM_Element &domElement)
implement loading legacy (pre v4.6) XML format for TYPalettes
Definition: TYPalette.cpp:337