File: color_palette.hpp

package info (click to toggle)
qt-color-widgets 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 916 kB
  • sloc: cpp: 6,923; sh: 236; makefile: 13
file content (229 lines) | stat: -rw-r--r-- 6,706 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
 * \file
 *
 * \author Mattia Basaglia
 *
 * \copyright Copyright (C) 2013-2020 Mattia Basaglia
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#ifndef COLOR_WIDGETS_COLOR_PALETTE_HPP
#define COLOR_WIDGETS_COLOR_PALETTE_HPP

#include <QColor>
#include <QString>
#include <QVector>
#include <QObject>
#include <QPair>
#include <QPixmap>
#include "colorwidgets_global.hpp"

namespace color_widgets {

class QCP_EXPORT ColorPalette : public QObject
{
    Q_OBJECT

    /**
     * \brief The list of colors
     */
    Q_PROPERTY(QVector<value_type> colors READ colors WRITE setColors NOTIFY colorsChanged)
    /**
     * \brief Name of the palette
     */
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    /**
     * \brief Number of colors to display in a row, if 0 unspecified
     */
    Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged)
    /**
     * \brief Number of colors
     */
    Q_PROPERTY(int count READ count)
    /**
     * \brief Name of the file the palette has been read from
     */
    Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
    /**
     * \brief Whether it has been modified and it might be advisable to save it
     */
    Q_PROPERTY(bool dirty READ dirty WRITE setDirty NOTIFY dirtyChanged)

public:
    typedef QPair<QColor,QString> value_type;

    ColorPalette(const QVector<QColor>& colors, const QString& name = QString(), int columns = 0);
    ColorPalette(const QVector<QPair<QColor,QString> >& colors, const QString& name = QString(), int columns = 0);
    explicit ColorPalette(const QString& name = QString());
    ColorPalette(const ColorPalette& other);
    ColorPalette& operator=(const ColorPalette& other);
    ~ColorPalette();
    ColorPalette(ColorPalette&& other);
    ColorPalette& operator=(ColorPalette&& other);

    /**
     * \brief Color at the given index
     */
    Q_INVOKABLE QColor colorAt(int index) const;

    /**
     * \brief Color name at the given index
     */
    Q_INVOKABLE QString nameAt(int index) const;

    QVector<QPair<QColor,QString> > colors() const;
    QVector<QColor> onlyColors() const;

    int count() const;
    int columns();

    QString name() const;

    /**
     * \brief Use a color table to set the colors
     */
    Q_INVOKABLE void loadColorTable(const QVector<QRgb>& color_table);

    /**
     * \brief Convert to a color table
     */
    Q_INVOKABLE QVector<QRgb> colorTable() const;
    
    /**
     * \brief Creates a ColorPalette from a color table
     */
    static ColorPalette fromColorTable(const QVector<QRgb>& table);

    /**
     * \brief Use the pixels on an image to set the palette colors
     */
    Q_INVOKABLE bool loadImage(const QImage& image);

    /**
     * \brief Creates a ColorPalette from a Gimp palette (gpl) file
     */
    static ColorPalette fromImage(const QImage& image);

    /**
     * \brief Load contents from a Gimp palette (gpl) file
     * \returns \b true On Success
     * \note If this function returns \b false, the palette will become empty
     */
    Q_INVOKABLE bool load(const QString& name);

    /**
     * \brief Creates a ColorPalette from a Gimp palette (gpl) file
     */
    static ColorPalette fromFile(const QString& name);

    QString fileName() const;

    bool dirty() const;

    /**
     * \brief Returns a preview image of the colors in the palette
     */
    QPixmap preview(const QSize& size, const QColor& background=Qt::transparent) const;

public Q_SLOTS:
    void setColumns(int columns);

    void setColors(const QVector<QColor>& colors);
    void setColors(const QVector<QPair<QColor,QString> >& colors);

    /**
     * \brief Change the color at the given index
     */
    void setColorAt(int index, const QColor& color);
    /**
     * \brief Change the color at the given index
     */
    void setColorAt(int index, const QColor& color, const QString& name);
    /**
     * \brief Change the name of a color
     */
    void setNameAt(int index, const QString& name = QString());
    /**
     * \brief Append a color at the end
     */
    void appendColor(const QColor& color, const QString& name = QString());
    /**
     * \brief Insert a color in an arbitrary location
     */
    void insertColor(int index, const QColor& color, const QString& name = QString());
    /**
     * \brief Remove the color at the given index
     */
    void eraseColor(int index);

    /**
     * \brief Change file name and save
     * \returns \b true on success
     */
    bool save(const QString& filename);
    /**
     * \brief save to file, the filename is \c fileName or determined automatically
     * \returns \b true on success
     */
    bool save();

    void setName(const QString& name);
    void setFileName(const QString& name);
    void setDirty(bool dirty);

Q_SIGNALS:
    /**
     * \brief Emitted when all the colors have changed
     */
    void colorsChanged(const QVector<QPair<QColor,QString> >&);
    void columnsChanged(int);
    void nameChanged(const QString&);
    void fileNameChanged(const QString&);
    void dirtyChanged(bool);
    /**
     * \brief Emitted when the color or the name at the given index has been modified
     */
    void colorChanged(int index);
    /**
     * \brief Emitted when the color at the given index has been removed
     */
    void colorRemoved(int index);
    /**
     * \brief Emitted when a single color has been added
     */
    void colorAdded(int index);
    /**
     * \brief Emitted when the colors have been modified with a simple operation (set, append etc.)
     */
    void colorsUpdated(const QVector<QPair<QColor,QString>>&);

private:
    /**
     * \brief Returns \c name if it isn't null, otherwise a default value
     */
    QString unnamed(const QString& name = QString()) const;

    /**
     * \brief Emit all the necessary signals when the palette has been completely overwritten
     */
    void emitUpdate();

    class Private;
    Private *p;
};

} // namespace color_widgets

#endif // COLOR_WIDGETS_COLOR_PALETTE_HPP