itom  4.1.0
paletteOrganizer.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef PALETTEORGANIZER_H
24 #define PALETTEORGANIZER_H
25 
26 #include "../../common/sharedStructures.h"
27 #include "../../common/sharedStructuresQt.h"
28 #include "../../common/sharedStructuresGraphics.h"
29 
30 #include <qhash.h>
31 #include <qsharedpointer.h>
32 #include <qbrush.h>
33 #include <qsettings.h>
34 
35 namespace ito
36 {
37 
39 {
40  public:
42  {
43  m_paletteData.name = "";
44  m_paletteData.type = 0;
45  m_paletteData.inverseColorOne = QColor();
46  m_paletteData.inverseColorTwo = QColor();
47  m_paletteData.invalidColor = QColor();
48  }
49  ItomPaletteBase(const QString &name, const char type)
50  {
51  m_paletteData.name = name;
52  m_paletteData.type = type;
53  m_paletteData.inverseColorOne = QColor();
54  m_paletteData.inverseColorTwo = QColor();
55  m_paletteData.invalidColor = QColor();
56  }
57  ItomPaletteBase(const QString &name, const char type, const QColor &invCol1, const QColor &invCol2, const QColor &invalCol, const QVector<QGradientStop> &colStops)
58  {
59  m_paletteData.name = name;
60  m_paletteData.type = type;
61  m_paletteData.inverseColorOne = invCol1;
62  m_paletteData.inverseColorTwo = invCol2;
63  m_paletteData.invalidColor = invalCol;
64  m_paletteData.colorStops = colStops;
65  }
66  ItomPaletteBase(const QString &name, const char type, const QColor &start, const QColor &stop)
67  {
68  m_paletteData.name = name;
69  m_paletteData.type = type;
70  m_paletteData.colorStops.append(QPair<double, QColor>(0.0, start));
71  m_paletteData.colorStops.append(QPair<double, QColor>(1.0, stop));
72  }
73 
74  ItomPaletteBase(const ItomPaletteBase & src) : m_paletteData(src.m_paletteData)
75  { }
76 
77  ~ItomPaletteBase() {
78  m_paletteData.colorStops.clear();
79  }
80 
81  QString getName() const { return m_paletteData.name; }
82  bool setName(const QString &name);
83  inline int getNumColorStops() const { return m_paletteData.colorStops.size(); }
84  inline int getType() const { return m_paletteData.type; }
85 
86  double getPosFirst() const { return m_paletteData.colorStops[0].first; }
87  double getPosLast() const { return m_paletteData.colorStops[m_paletteData.colorStops.size()-1].first; }
88  double getPos(unsigned int color) const;
89 
90  bool setInverseColorOne(const QColor &color);
91  QColor getInverseColorOne() const { return m_paletteData.inverseColorOne; }
92  bool setInverseColorTwo(const QColor &color);
93  QColor getInverseColorTwo() const { return m_paletteData.inverseColorTwo; }
94 
95  bool setInvalidColor(const QColor &color);
96  QColor getInvalidColor() const;
97 
98  int findUpper(double pos) const;
99  inline QVector<QPair<double, QColor> > getColorStops(void) const { return m_paletteData.colorStops; }
100  bool setColorStops(const QVector<QPair<double, QColor> > &colorStops);
101  QColor getColor(unsigned int index) const;
102 
103  ItomPalette getPalette() const;
104 
105  bool isWriteProtected() const { return m_paletteData.type & ito::tPaletteReadOnly; }
106  inline void setWriteProtection() { m_paletteData.type = m_paletteData.type | ito::tPaletteReadOnly; }
107  void removeWriteProtection() { m_paletteData.type = m_paletteData.type & (~ito::tPaletteReadOnly); }
108  bool insertColorStop( double pos, const QColor &color );
109 
110  QVector<ito::uint32> get256Colors(bool includeAlpha = false) const;
111 
112  private:
113  ItomPalette m_paletteData;
114 };
115 
116 //----------------------------------------------------------------------------------
117 class PaletteOrganizer : public QObject
118 {
119  Q_OBJECT
120 
121  public:
123  ~PaletteOrganizer(){};
124 
125  /* save the given color palette to the settings. Settings must already be opened in the group,
126  where the palette should be saved. Each palette is stored as a subgroup of the current group. */
127  ito::RetVal saveColorPaletteToSettings(const ItomPaletteBase &palette, QSettings &settings) const;
128 
129  /* load a color palette from the settings. Settings must already be opened in the group,
130  where the palette should be loaded from. The current group must hereby consist of a subgroup with the group name 'name'. */
131  ito::RetVal loadColorPaletteFromSettings(const QString &paletteName, ItomPaletteBase &palette, QSettings &settings) const;
132 
133  ItomPaletteBase getColorPalette(const int index) const;
134  ItomPaletteBase getColorPalette(const QString &name, bool *found = NULL) const;
135  int getColorBarIndex(const QString& name, bool *found = NULL) const;
136 
137  QList<QString> getColorPaletteList(const int type = ito::tPaletteNoType) const;
138  QList<QString> getBuiltInPaletteNames() const { return m_builtInPalettes; }
139  int numberOfColorPalettes() const { return m_colorPalettes.length(); }
140  bool removeColorPalette(const int index);
141 
142  private:
143  void calcColorPaletteLut();
144 
145  QList<QString> m_restrictedKeyWords;
146  QList<ItomPaletteBase> m_colorPalettes;
147  QList<QString> m_builtInPalettes;
148  QHash<QString,int> m_colorPaletteLUT;
149 
150  ItomPaletteBase noPalette;
151 
152  public slots:
153  ito::RetVal setColorBarThreaded(QString name, ito::ItomPaletteBase newPalette, ItomSharedSemaphore *waitCond = NULL);
154  ito::RetVal getColorBarThreaded(QString name, QSharedPointer<ito::ItomPaletteBase> palette, ItomSharedSemaphore *waitCond = NULL);
155  ito::RetVal getColorBarListThreaded(QSharedPointer<QStringList> palettes, ItomSharedSemaphore *waitCond = NULL);
156 };
157 } //namespace ito
158 #endif
Definition: paletteOrganizer.h:38
int findUpper(double pos) const
Find the next color stop and its values within this palette.
Definition: paletteOrganizer.cpp:48
bool setInverseColorOne(const QColor &color)
Set the first inverse color for this color bar.
Definition: paletteOrganizer.cpp:83
Definition: paletteOrganizer.h:117
Class for managing status values (like errors or warning)
Definition: retVal.h:54
double getPos(unsigned int color) const
Get the position of the n-th color-stop in the palette.
Definition: paletteOrganizer.cpp:224
PaletteOrganizer()
shortdesc
Definition: paletteOrganizer.cpp:354
ItomPalette getPalette() const
This function returns the internal structur of the palette.
Definition: paletteOrganizer.cpp:249
QColor getColor(unsigned int index) const
Get the RGBA-Color of the n-th color-stop in the palette.
Definition: paletteOrganizer.cpp:238
QVector< ito::uint32 > get256Colors(bool includeAlpha=false) const
Transform the color stops to a 256 color vector.
Definition: paletteOrganizer.cpp:262
Definition: apiFunctionsGraph.cpp:39
semaphore which can be used for asychronous thread communication. By using this class it is possible ...
Definition: sharedStructuresQt.h:57
bool setInvalidColor(const QColor &color)
Set the invalid color for this color bar.
Definition: paletteOrganizer.cpp:134
int getColorBarIndex(const QString &name, bool *found=NULL) const
shortdesc
Definition: paletteOrganizer.cpp:626
ItomPaletteBase getColorPalette(const int index) const
shortdesc
Definition: paletteOrganizer.cpp:587
QList< QString > getColorPaletteList(const int type=ito::tPaletteNoType) const
shortdesc
Definition: paletteOrganizer.cpp:644
bool insertColorStop(double pos, const QColor &color)
Insert a new color stop into the palette defined by color and position.
Definition: paletteOrganizer.cpp:168
bool setInverseColorTwo(const QColor &color)
Set the second inverse color for this color bar.
Definition: paletteOrganizer.cpp:102
Definition: sharedStructuresGraphics.h:113
QColor getInvalidColor() const
Get the invalid color for this color bar.
Definition: paletteOrganizer.cpp:153