Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSwatchGroup.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2005..2022 Halla Rempt <halla@valdyas.org>
3 SPDX-FileCopyrightText: 2016 L. E. Segovia <amy@amyspark.me>
4 SPDX-FileCopyrightText: 2018 Michael Zhou <simerixh@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8 */
9
10#include "KisSwatchGroup.h"
11
14
16 typedef QMap<int, KisSwatch> Column;
17
18 QString name {QString()};
20 int colorCount {0};
22};
23
24
28
31
33 : d(new Private(*rhs.d))
34{ }
35
37{
38 if (&rhs == this) {
39 return *this;
40 }
41 *d = *rhs.d;
42 return *this;
43}
44
45void KisSwatchGroup::setSwatch(const KisSwatch &e, int column, int row)
46{
47 Q_ASSERT(column < d->colorMatrix.size() && column >= 0 && row >= 0);
48
49 if (row + 1 >= d->rowCount) {
50 setRowCount(row + 1);
51 }
53 d->colorCount++;
54 }
55 d->colorMatrix[column][row] = e;
56}
57
58bool KisSwatchGroup::checkSwatchExists(int column, int row) const
59{
60 if (row >= d->rowCount) {
61 return false;
62 }
63
64 if (column >= d->colorMatrix.size()){
65 return false;
66 }
67
68 if (column < 0) {
69 return false;
70 }
71
72 if (!d->colorMatrix[column].contains(row)) {
73 return false;
74 }
75
76 if (!d->colorMatrix[column][row].isValid()) {
77 return false;
78 }
79
80 return true;
81}
82
83bool KisSwatchGroup::removeSwatch(int column, int row)
84{
85 if (d->colorCount == 0) {
86 return false;
87 }
88
89 if (row >= d->rowCount || column >= d->colorMatrix.size() || column < 0) {
90 return false;
91 }
92
93 // QMap::remove returns 1 if key found else 0
94 if (d->colorMatrix[column].remove(row)) {
95 d->colorCount -= 1;
96 return true;
97 } else {
98 return false;
99 }
100}
101
103{
105
106
107 // Move 'removed' swatches into new row
108 QVector <KisSwatch> movedSwatches;
109
110 for (int r = 0; r < rowCount(); r++ ) {
111 for (int c = 0; c < d->colorMatrix.size(); c++ ) {
112
113 if (c >= columnCount && checkSwatchExists(c, r)) {
114 movedSwatches.push_back(getSwatch(c, r));
115 }
116 }
117 }
118 if ( !movedSwatches.isEmpty()) {
119 for (int i = 0; i< movedSwatches.size(); i++) {
120 int r = (i/columnCount) + d->rowCount;
121 int c = (i%columnCount);
122 d->colorMatrix[c][r] = movedSwatches.at(i);
123 }
124 d->rowCount += (movedSwatches.size()/columnCount);
125 if (movedSwatches.size()%columnCount > 0) {
126 d->rowCount += 1;
127 }
128 }
129
130 if (columnCount < d->colorMatrix.size()) {
131 int newColorCount = 0;
132 for (int i = 0; i < columnCount; i++ ) {
133 newColorCount += d->colorMatrix[i].size();
134 }
135 d->colorCount = newColorCount;
136 }
137 d->colorMatrix.resize(columnCount);
138}
139
141 return d->colorMatrix.size();
142}
143
144KisSwatch KisSwatchGroup::getSwatch(int column, int row) const
145{
146 // This is perfectly normal when Krita gets initialized, so it needs an if, not an assert.
147 // Getting -1, -1 is not a coding error.
148 if (row < 0 || column < 0) return KisSwatch();
149
150 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(column >= 0 && column < d->colorMatrix.size(), KisSwatch());
152
153 return d->colorMatrix[column][row];
154}
155
156QPair<int, int> KisSwatchGroup::addSwatch(const KisSwatch &swatch)
157{
158 if (columnCount() == 0) {
160 }
161
162 int y = 0;
163 int x = 0;
164 while (checkSwatchExists(x, y))
165 {
166 if(++x == d->colorMatrix.size())
167 {
168 x = 0;
169 ++y;
170 }
171 }
172 // clear color metadata for now.
174
175 setSwatch(swatch, x, y);
176
177 return QPair<int, int> (x, y);
178}
179
181{
182 d->colorMatrix.clear();
183}
184
185void KisSwatchGroup::setRowCount(int newRowCount)
186{
187 d->rowCount = newRowCount;
188 for (Private::Column &c : d->colorMatrix) {
189 for (int k : c.keys()) {
190 if (k >= newRowCount) {
191 c.remove(k);
192 d->colorCount--;
193 }
194 }
195 }
196}
197
199{
200 return d->rowCount;
201}
202
204{
205 return d->colorCount;
206}
207
209{
210 return columnCount() * rowCount();
211}
212
214{
216 int column = 0;
217 for (const Private::Column &c : d->colorMatrix) {
218 int i = 0;
219 for (const KisSwatch &s : c.values()) {
220 SwatchInfo info = {d->name, s, c.keys()[i++], column};
221 res.append(info);
222 }
223 column++;
224 }
225 return res;
226}
227
228void KisSwatchGroup::setName(const QString &name)
229{
230 d->name = name;
231}
232
233QString KisSwatchGroup::name() const
234{
235 return d->name;
236}
The KisSwatchGroup class stores a matrix of color swatches swatches can accessed using (x,...
std::experimental::propagate_const< std::unique_ptr< Private > > d
KisSwatchGroup & operator=(const KisSwatchGroup &rhs)
static int DEFAULT_ROW_COUNT
int slotCount() const
bool checkSwatchExists(int column, int row) const
checkSwatch checks if position column and row has a valid entry both column and row start from 0
QList< SwatchInfo > infoList() const
getColors
int rowCount() const
void setName(const QString &name)
void setSwatch(const KisSwatch &e, int column, int row)
setSwatch sets the entry at position (column, row) to be e
int columnCount() const
KisSwatch getSwatch(int column, int row) const
getSwatch used to get the swatch entry at position (column, row) there is an assertion to make sure t...
void setColumnCount(int columnCount)
int colorCount() const
static int DEFAULT_COLUMN_COUNT
QPair< int, int > addSwatch(const KisSwatch &e)
addEntry adds the entry e to the right of the rightmost entry in the last row if the rightmost entry ...
QString name() const
void setRowCount(int newRowCount)
bool removeSwatch(int column, int row)
removeSwatch removes the entry at position (column, row)
KoColor color() const
Definition KisSwatch.h:30
void clearMetadata()
clearMetadata clear th metadata map inside the KoColor.
Definition KoColor.cpp:676
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
QVector< Column > colorMatrix
QMap< int, KisSwatch > Column