DISPLACE  1.0
A spatial model of fisheries to help sustainable fishing and maritime spatial planning
palettemanager.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // DISPLACE: DYNAMIC INDIVIDUAL VESSEL-BASED SPATIAL PLANNING
3 // AND EFFORT DISPLACEMENT
4 // Copyright (c) 2012-2019 Francois Bastardie <fba@aqua.dtu.dk>
5 
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // --------------------------------------------------------------------------
20 
21 /* --------------------------------------------------------------------------
22  * DISPLACE: DYNAMIC INDIVIDUAL VESSEL-BASED SPATIAL PLANNING
23  * AND EFFORT DISPLACEMENT
24  * Copyright (c) 2012, 2013, 2014 Francois Bastardie <fba@aqua.dtu.dk>
25  *
26  * This program is free software; you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation; either version 2 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License along
37  * with this program; if not, write to the Free Software Foundation, Inc.,
38  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39  * --------------------------------------------------------------------------
40  */
41 #ifndef PALETTEMANAGER_H
42 #define PALETTEMANAGER_H
43 
44 #include <QString>
45 #include <QVector>
46 #include <QColor>
47 #include <QMap>
48 #include <QList>
49 #include <QIODevice>
50 #include <QVariant>
51 #include <cmath>
52 #include <memory>
53 
57 
59 };
60 
61 
62 class Palette {
63 public:
64  typedef QMap<double,QColor> Container;
65  typedef Container::const_iterator Iterator;
66 
67  Palette();
68  explicit Palette(PaletteRole role, const QString &name);
69  Palette(const Palette &);
70  ~Palette();
71 
72  // Role
73  PaletteRole role() const { return mRole; }
74  void setRole (PaletteRole r) { mRole = r; }
75 
76  // Name
77  void setName (const QString &n) { m_name = n; }
78  QString name() const { return m_name; }
79 
80  // Palette Colors
81  int colorCount() const { return m_palette.count(); }
82 
83  Iterator begin() const { return m_palette.cbegin(); }
84  Iterator end() const { return m_palette.cend(); }
85  void addColor (double value, const QColor &col) { m_palette.insert(value, col); }
86 
87  const QColor &color(double val) const {
88  Iterator it = m_palette.lowerBound(val);
89  if (it != m_palette.begin())
90  --it;
91  return *it;
92  }
93 
94  const QColor &colorByIndex(int idx) const {
95  Iterator it = m_palette.begin();
96  it += (idx % m_palette.size());
97  return *it;
98  }
99 
100  // Palette Parameters
101 
102  // Palette Special Color
103  int specialColorCount() const { return mSpecials.size(); }
104  QColor specialColor(int i) const { return mSpecials[i]; }
105  const QVector<QColor> &specialColors() const { return mSpecials; }
106 
107  void setSpecialColor (int i, const QColor &col) { mSpecials[i] = col; }
108  void addSpecialColor (const QColor &col) { mSpecials.push_back(col); }
109  void setNumSpecialColor (int i);
110 
111  // Reset
112  void clear() {
113  m_palette.clear();
114  mSpecials.clear();
115  }
116 
117  // I/O
118  bool loadFromFile(QIODevice *device);
119  bool saveToFile (QIODevice *device);
120 
121 protected:
123  QString m_name;
125  QVector<QColor> mSpecials;
126 
127  double m_min, m_max;
128 };
129 
131 {
132 public:
133  PaletteManager();
134  ~PaletteManager();
135 
136  const Palette &palette(PaletteRole) const;
137  std::shared_ptr<Palette> palette(const QString &n) const;
138  void setPalette(PaletteRole n, const Palette &palette);
139 
140  int paletteCount() const;
141 
142  bool isPalettePresent (const QString &name) const;
143  void removePalette (const QString &name);
144  void removePalette (const Palette &view);
145 
147  if (mInstance == 0)
148  mInstance = new PaletteManager();
149  return mInstance;
150  }
151 
152 protected:
153  typedef QMap<QString, std::shared_ptr<Palette> > PaletteMapContainer;
154  typedef QList<std::shared_ptr<Palette> > PaletteListContainer;
155 
158 
160  static const QString defaultPaletteFileNames[];
161 };
162 
163 #endif // PALETTEMANAGER_H
void removePalette(const QString &name)
Definition: palettemanager.cpp:225
PaletteRole role() const
Definition: palettemanager.h:73
PaletteRole
Definition: palettemanager.h:54
Definition: palettemanager.h:56
QString name() const
Definition: palettemanager.h:78
void setRole(PaletteRole r)
Definition: palettemanager.h:74
Definition: palettemanager.h:55
Definition: palettemanager.h:56
PaletteRole mRole
Definition: palettemanager.h:122
Definition: palettemanager.h:55
bool isPalettePresent(const QString &name) const
Definition: palettemanager.cpp:220
Definition: palettemanager.h:55
void setName(const QString &n)
Definition: palettemanager.h:77
Iterator end() const
Definition: palettemanager.h:84
bool saveToFile(QIODevice *device)
Definition: palettemanager.cpp:136
double m_max
Definition: palettemanager.h:127
QColor specialColor(int i) const
Definition: palettemanager.h:104
Definition: palettemanager.h:56
bool loadFromFile(QIODevice *device)
Definition: palettemanager.cpp:79
int paletteCount() const
Definition: palettemanager.cpp:215
~PaletteManager()
Definition: palettemanager.cpp:189
void addColor(double value, const QColor &col)
Definition: palettemanager.h:85
Palette()
Definition: palettemanager.cpp:48
Definition: palettemanager.h:56
void addSpecialColor(const QColor &col)
Definition: palettemanager.h:108
Definition: palettemanager.h:56
PaletteMapContainer m_map
Definition: palettemanager.h:156
const QVector< QColor > & specialColors() const
Definition: palettemanager.h:105
Definition: palettemanager.h:56
QVector< QColor > mSpecials
Definition: palettemanager.h:125
Definition: palettemanager.h:55
QString m_name
Definition: palettemanager.h:123
PaletteManager()
Definition: palettemanager.cpp:178
const QColor & color(double val) const
Definition: palettemanager.h:87
const Palette & palette(PaletteRole) const
Definition: palettemanager.cpp:194
std::pair< box, unsigned > value
Definition: diffusion.cpp:30
static PaletteManager * instance()
Definition: palettemanager.h:146
Definition: palettemanager.h:56
static PaletteManager * mInstance
Definition: palettemanager.h:159
const QColor & colorByIndex(int idx) const
Definition: palettemanager.h:94
void setNumSpecialColor(int i)
Definition: palettemanager.cpp:71
~Palette()
Definition: palettemanager.cpp:67
Definition: palettemanager.h:55
Container::const_iterator Iterator
Definition: palettemanager.h:65
void setSpecialColor(int i, const QColor &col)
Definition: palettemanager.h:107
PaletteListContainer m_list
Definition: palettemanager.h:157
int colorCount() const
Definition: palettemanager.h:81
QList< std::shared_ptr< Palette > > PaletteListContainer
Definition: palettemanager.h:154
Definition: palettemanager.h:58
int specialColorCount() const
Definition: palettemanager.h:103
QMap< QString, std::shared_ptr< Palette > > PaletteMapContainer
Definition: palettemanager.h:153
static const QString defaultPaletteFileNames[]
Definition: palettemanager.h:160
Iterator begin() const
Definition: palettemanager.h:83
Definition: palettemanager.h:55
Definition: palettemanager.h:56
double m_min
Definition: palettemanager.h:127
Container m_palette
Definition: palettemanager.h:124
Definition: palettemanager.h:56
void setPalette(PaletteRole n, const Palette &palette)
Definition: palettemanager.cpp:208
Definition: palettemanager.h:130
void clear()
Definition: palettemanager.h:112
Definition: palettemanager.h:55
QMap< double, QColor > Container
Definition: palettemanager.h:64
Definition: palettemanager.h:62