Krita

Palette.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "Palette.h"
8#include <KoColorSet.h>
9#include <KisSwatch.h>
10#include <KisSwatchGroup.h>
11#include <ManagedColor.h>
12#include <KisPaletteModel.h>
13
14struct Palette::Private {
15 KoColorSetSP palette {0};
16};
17
18Palette::Palette(Resource *resource, QObject *parent)
19 : QObject(parent)
20 , d(new Private()) {
21 d->palette = resource->resource().dynamicCast<KoColorSet>();
22}
23
24Palette::~Palette()
25{
26 delete d;
27}
28
29bool Palette::operator==(const Palette &other) const
30{
31 return (d->palette == other.d->palette);
32}
33
34bool Palette::operator!=(const Palette &other) const
35{
36 return !(operator==(other));
37}
38
39
41{
42 if (!d->palette) return 0;
43 return d->palette->colorCount();
44}
45
47{
48 if (!d->palette) return 0;
49 return d->palette->columnCount();
50}
51
52void Palette::setColumnCount(int columns)
53{
54 if (d->palette && columns > 0)
55 d->palette->setColumnCount(columns);
56}
57
59{
60 if (!d->palette) return 0;
61 return d->palette->rowCount();
62}
63
65{
66 if (!d->palette) return 0;
67 KisSwatchGroupSP group = d->palette->getGroup(name);
68 if (!group) return 0;
69 return group->rowCount();
70}
71
73{
74 if (!d->palette) return;
75 KisSwatchGroupSP group = d->palette->getGroup(name);
76 if (!group) return;
77 if (rows > 0)
78 group->setRowCount(rows);
79}
80
82{
83 if (!d->palette) return "";
84 return d->palette->comment();
85}
86
88{
89 if (!d->palette) return;
90 return d->palette->setComment(comment);
91}
92
94{
95 if (!d->palette) return QStringList();
96 return d->palette->swatchGroupNames();
97}
98
100{
101 if (!d->palette) return;
102 d->palette->addGroup(name);
103}
104
105void Palette::removeGroup(QString name, bool keepColors)
106{
107 if (!d->palette) return;
108 return d->palette->removeGroup(name, keepColors);
109}
110
112{
113 if (!d->palette) return 0;
114 return d->palette->colorCount();
115}
116
118{
119 if (!d->palette) return 0;
120 KisSwatchGroupSP group = d->palette->getGroup(name);
121 if (!group) return 0;
122 return group->colorCount();
123}
124
126{
127 if (!d->palette) return 0;
128 return d->palette->slotCount();
129}
130
132{
133 if (!d->palette) return 0;
134 KisSwatchGroupSP group = d->palette->getGroup(name);
135 if (!group) return 0;
136 return group->slotCount();
137}
138
140{
141 warnScript << "DEPRECATED Palette.colorSetEntryByIndex() - use Palette.entryByIndex() instead";
142 return entryByIndex(index);
143}
144
146{
147 if (!d->palette || columnCount() == 0) {
148 return new Swatch();
149 }
150 int col = index % columnCount();
151 int row = (index - col) / columnCount();
152 return new Swatch(d->palette->getColorGlobal(col, row));
153}
154
155Swatch *Palette::colorSetEntryFromGroup(int index, const QString &groupName)
156{
157 warnScript << "DEPRECATED Palette.colorSetEntryFromGroup() - use Palette.entryByIndexFromGroup() instead";
158 return entryByIndexFromGroup(index, groupName);
159}
160
161Swatch *Palette::entryByIndexFromGroup(int index, const QString &groupName)
162{
163 if (!d->palette || columnCount() == 0) {
164 return new Swatch();
165 }
166 int col = index % columnCount();
167 return new Swatch(d->palette->getSwatchFromGroup(col, (index - col) / columnCount(), groupName));
168}
169
170void Palette::addEntry(Swatch entry, QString groupName)
171{
172 d->palette->addSwatch(entry.kisSwatch(), groupName);
173}
174
175void Palette::removeEntry(int index)
176{
177 int col = index % columnCount();
178 int tmp = index;
179 int row = (index - col) / columnCount();
180 KisSwatchGroupSP groupFoundIn;
181 Q_FOREACH(const QString &name, groupNames()) {
182 KisSwatchGroupSP g = d->palette->getGroup(name);
183 tmp -= g->rowCount() * columnCount();
184 if (tmp < 0) {
185 groupFoundIn = g;
186 break;
187 }
188 row -= g->rowCount();
189 }
190 if (!groupFoundIn) { return; }
191 d->palette->removeSwatch(col, row, groupFoundIn);
192}
193void Palette::removeEntryFromGroup(int index, const QString &groupName)
194{
195 if (!d->palette) return;
196 KisSwatchGroupSP group = d->palette->getGroup(groupName);
197 if (!group) return;
198 int col = index % columnCount();
199 int row = (index - col) / columnCount();
200 d->palette->removeSwatch(col, row, group);
201}
202
203void Palette::changeGroupName(QString oldGroupName, QString newGroupName)
204{
205 warnScript << "DEPRECATED Palette.changeGroupName() - use Palette.renameGroup() instead";
206 return renameGroup(oldGroupName, newGroupName);
207}
208
209void Palette::renameGroup(QString oldGroupName, QString newGroupName)
210{
211 d->palette->changeGroupName(oldGroupName, newGroupName);
212}
213
214void Palette::moveGroup(const QString &groupName, const QString &groupNameInsertBefore)
215{
216 return d->palette->moveGroup(groupName, groupNameInsertBefore);
217}
218
220{
221 return false;
222}
223
224KoColorSetSP Palette::colorSet()
225{
226 return d->palette;
227}
The Palette class Palette is a resource object that stores organised color data.
Definition Palette.h:45
int colorsCountGroup(QString name)
colorsCountGroup
Definition Palette.cpp:117
void moveGroup(const QString &groupName, const QString &groupNameInsertBefore=QString())
Move the group groupName to position before group groupNameInsertBefore.
Definition Palette.cpp:214
bool save()
save the palette
Definition Palette.cpp:219
void setColumnCount(int columns)
Palettes are defined in grids.
Definition Palette.cpp:52
Swatch * entryByIndexFromGroup(int index, const QString &groupName)
get color (swatch) from the given group index.
Definition Palette.cpp:161
void removeEntry(int index)
Remove the color entry at the given index in this palette.
Definition Palette.cpp:175
Q_DECL_DEPRECATED void changeGroupName(QString oldGroupName, QString newGroupName)
changeGroupName change the group name.
Definition Palette.cpp:203
Swatch * entryByIndex(int index)
get color (swatch) from the global index.
Definition Palette.cpp:145
void addGroup(QString name)
Palette content can be organized in groups.
Definition Palette.cpp:99
int slotCountGroup(QString name)
number of slots for swatches in group This includes any empty slots not filled by a color.
Definition Palette.cpp:131
int numberOfEntries() const
number of filled colors (swatches) in palette NOTE: same as colorsCountTotal()
Definition Palette.cpp:40
Q_DECL_DEPRECATED Swatch * colorSetEntryFromGroup(int index, const QString &groupName)
colorSetEntryFromGroup
Definition Palette.cpp:155
Q_DECL_DEPRECATED Swatch * colorSetEntryByIndex(int index)
colorSetEntryByIndex get the colorsetEntry from the global index.
Definition Palette.cpp:139
QString comment()
the comment or description associated with the palette.
Definition Palette.cpp:81
int colorsCountTotal()
number of filled colors (swatches) in palette NOTE: same as numberOfEntries()
Definition Palette.cpp:111
void renameGroup(QString oldGroupName, QString newGroupName)
rename a group
Definition Palette.cpp:209
int columnCount()
Palettes are defined in grids.
Definition Palette.cpp:46
void setRowCountGroup(int rows, QString name)
Set the number of rows defined in the given group.
Definition Palette.cpp:72
int slotCount()
number of slots for swatches in palette This includes any empty slots not filled by a color.
Definition Palette.cpp:125
void removeGroup(QString name, bool keepColors=true)
Palette content can be organized in groups.
Definition Palette.cpp:105
int rowCountGroup(QString name)
The number of rows defined in the given group.
Definition Palette.cpp:64
void removeEntryFromGroup(int index, const QString &groupName)
Remove the color entry at the given index in the given group.
Definition Palette.cpp:193
void setComment(QString comment)
the comment or description associated with the palette.
Definition Palette.cpp:87
void addEntry(Swatch entry, QString groupName=QString())
add a color entry to a group.
Definition Palette.cpp:170
int rowCount()
The number of rows in the palette grid.
Definition Palette.cpp:58
QStringList groupNames() const
Palette content can be organized in groups.
Definition Palette.cpp:93
A Resource represents a gradient, pattern, brush tip, brush preset, palette or workspace definition.
Definition Resource.h:31
The Swatch class is a thin wrapper around the KisSwatch class.
Definition Swatch.h:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 18 2025 12:12:35 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.