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 "";
61 return d->palette->comment();
62}
63
65{
66 if (!d->palette) return;
67 return d->palette->setComment(comment);
68}
69
71{
72 if (!d->palette) return QStringList();
73 return d->palette->swatchGroupNames();
74}
75
77{
78 if (!d->palette) return;
79 d->palette->addGroup(name);
80}
81
82void Palette::removeGroup(QString name, bool keepColors)
83{
84 if (!d->palette) return;
85 return d->palette->removeGroup(name, keepColors);
86}
87
89{
90 if (!d->palette) return 0;
91 return d->palette->colorCount();
92}
93
95{
96 warnScript << "DEPRECATED Palette.colorSetEntryByIndex() - use Palette.entryByIndex() instead";
97 return entryByIndex(index);
98}
99
101{
102 if (!d->palette || columnCount() == 0) {
103 return new Swatch();
104 }
105 int col = index % columnCount();
106 int row = (index - col) / columnCount();
107 return new Swatch(d->palette->getColorGlobal(col, row));
108}
109
110Swatch *Palette::colorSetEntryFromGroup(int index, const QString &groupName)
111{
112 warnScript << "DEPRECATED Palette.colorSetEntryFromGroup() - use Palette.entryByIndexFromGroup() instead";
113 return entryByIndexFromGroup(index, groupName);
114}
115
116Swatch *Palette::entryByIndexFromGroup(int index, const QString &groupName)
117{
118 if (!d->palette || columnCount() == 0) {
119 return new Swatch();
120 }
121 int row = index % columnCount();
122 return new Swatch(d->palette->getSwatchFromGroup((index - row) / columnCount(), row, groupName));
123}
124
125void Palette::addEntry(Swatch entry, QString groupName)
126{
127 d->palette->addSwatch(entry.kisSwatch(), groupName);
128}
129
130void Palette::removeEntry(int index, const QString &/*groupName*/)
131{
132 int col = index % columnCount();
133 int tmp = index;
134 int row = (index - col) / columnCount();
135 KisSwatchGroupSP groupFoundIn;
136 Q_FOREACH(const QString &name, groupNames()) {
137 KisSwatchGroupSP g = d->palette->getGroup(name);
138 tmp -= g->rowCount() * columnCount();
139 if (tmp < 0) {
140 groupFoundIn = g;
141 break;
142 }
143 row -= g->rowCount();
144
145 }
146 if (!groupFoundIn) { return; }
147 d->palette->removeSwatch(col, row, groupFoundIn);
148}
149
150void Palette::changeGroupName(QString oldGroupName, QString newGroupName)
151{
152 warnScript << "DEPRECATED Palette.changeGroupName() - use Palette.renameGroup() instead";
153 return renameGroup(oldGroupName, newGroupName);
154}
155
156void Palette::renameGroup(QString oldGroupName, QString newGroupName)
157{
158 d->palette->changeGroupName(oldGroupName, newGroupName);
159}
160
161void Palette::moveGroup(const QString &groupName, const QString &groupNameInsertBefore)
162{
163 return d->palette->moveGroup(groupName, groupNameInsertBefore);
164}
165
167{
168 return false;
169}
170
171KoColorSetSP Palette::colorSet()
172{
173 return d->palette;
174}
The Palette class Palette is a resource object that stores organised color data.
Definition Palette.h:45
void moveGroup(const QString &groupName, const QString &groupNameInsertBefore=QString())
Move the group groupName to position before group groupNameInsertBefore.
Definition Palette.cpp:161
bool save()
save the palette
Definition Palette.cpp:166
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:116
void removeEntry(int index, const QString &groupName)
remove from defined group a color entry designed by given index.
Definition Palette.cpp:130
Q_DECL_DEPRECATED void changeGroupName(QString oldGroupName, QString newGroupName)
changeGroupName change the group name.
Definition Palette.cpp:150
Swatch * entryByIndex(int index)
get color (swatch) from the global index.
Definition Palette.cpp:100
void addGroup(QString name)
Palette content can be organized in groups.
Definition Palette.cpp:76
int numberOfEntries() const
number of 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:110
Q_DECL_DEPRECATED Swatch * colorSetEntryByIndex(int index)
colorSetEntryByIndex get the colorsetEntry from the global index.
Definition Palette.cpp:94
QString comment()
the comment or description associated with the palette.
Definition Palette.cpp:58
int colorsCountTotal()
number of colors (swatches) in palette NOTE: same as numberOfEntries()
Definition Palette.cpp:88
void renameGroup(QString oldGroupName, QString newGroupName)
rename a group
Definition Palette.cpp:156
int columnCount()
Palettes are defined in grids.
Definition Palette.cpp:46
void removeGroup(QString name, bool keepColors=true)
Palette content can be organized in groups.
Definition Palette.cpp:82
void setComment(QString comment)
the comment or description associated with the palette.
Definition Palette.cpp:64
void addEntry(Swatch entry, QString groupName=QString())
add a color entry to a group.
Definition Palette.cpp:125
QStringList groupNames() const
Palette content can be organized in groups.
Definition Palette.cpp:70
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-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.