Changeset View
Changeset View
Standalone View
Standalone View
libs/pigment/resources/KisSwatchGroup.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org> | ||||
3 | Copyright (c) 2016 L. E. Segovia <leo.segovia@siggraph.org> | ||||
4 | Copyright (c) 2018 Michael Zhou <simerixh@gmail.com> | ||||
5 | | ||||
6 | This library is free software; you can redistribute it and/or | ||||
7 | modify it under the terms of the GNU Lesser General Public | ||||
8 | License as published by the Free Software Foundation; either | ||||
9 | version 2.1 of the License, or (at your option) any later version. | ||||
10 | | ||||
11 | This library is distributed in the hope that it will be useful, | ||||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
14 | Lesser General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU Lesser General Public | ||||
17 | License along with this library; if not, write to the Free Software | ||||
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
19 | | ||||
20 | */ | ||||
21 | | ||||
22 | #include "KisSwatchGroup.h" | ||||
23 | | ||||
24 | struct KisSwatchGroup::Private { | ||||
25 | typedef QMap<int, KisSwatch> Column; | ||||
26 | | ||||
27 | Private() | ||||
28 | : name(QString()) | ||||
29 | , colorMatrix(DEFAULT_COLUMN_COUNT) | ||||
30 | , colorCount(0) | ||||
31 | , rowCount(DEFAULT_ROW_COUNT) | ||||
32 | { } | ||||
33 | | ||||
34 | static int DEFAULT_COLUMN_COUNT; | ||||
35 | static int DEFAULT_ROW_COUNT; | ||||
36 | | ||||
37 | QString name; | ||||
38 | QVector<Column> colorMatrix; | ||||
39 | int colorCount; | ||||
40 | int rowCount; | ||||
41 | }; | ||||
42 | | ||||
43 | int KisSwatchGroup::Private::DEFAULT_COLUMN_COUNT = 16; | ||||
44 | int KisSwatchGroup::Private::DEFAULT_ROW_COUNT = 20; | ||||
45 | | ||||
46 | KisSwatchGroup::KisSwatchGroup() | ||||
47 | : d(new Private) | ||||
48 | { } | ||||
49 | | ||||
50 | KisSwatchGroup::~KisSwatchGroup() | ||||
51 | { } | ||||
52 | | ||||
53 | KisSwatchGroup::KisSwatchGroup(const KisSwatchGroup &rhs) | ||||
54 | : d(new Private(*rhs.d)) | ||||
55 | { } | ||||
56 | | ||||
57 | KisSwatchGroup &KisSwatchGroup::operator =(const KisSwatchGroup &rhs) | ||||
58 | { | ||||
59 | if (&rhs == this) { | ||||
60 | return *this; | ||||
61 | } | ||||
62 | d.reset(new Private(*rhs.d)); | ||||
63 | return *this; | ||||
64 | } | ||||
65 | | ||||
66 | void KisSwatchGroup::setEntry(const KisSwatch &e, int column, int row) | ||||
67 | { | ||||
68 | Q_ASSERT(column < d->colorMatrix.size() && column >= 0 && row >= 0); | ||||
69 | if (row >= d->rowCount) { | ||||
70 | setRowCount(row + 1); | ||||
71 | } | ||||
72 | if (!checkEntry(column, row)) { | ||||
73 | d->colorCount++; | ||||
74 | } | ||||
75 | d->colorMatrix[column][row] = e; | ||||
76 | } | ||||
77 | | ||||
78 | bool KisSwatchGroup::checkEntry(int column, int row) const | ||||
79 | { | ||||
80 | if (row >= d->rowCount || column >= d->colorMatrix.size() || column < 0) { | ||||
81 | return false; | ||||
82 | } | ||||
83 | if (!d->colorMatrix[column].contains(row)) { | ||||
84 | return false; | ||||
85 | } | ||||
86 | return true; | ||||
87 | } | ||||
88 | | ||||
89 | bool KisSwatchGroup::removeEntry(int column, int row) | ||||
90 | { | ||||
91 | if (d->colorCount == 0) { | ||||
92 | return false; | ||||
93 | } | ||||
94 | | ||||
95 | if (row >= d->rowCount || column >= d->colorMatrix.size() || column < 0) { | ||||
96 | return false; | ||||
97 | } | ||||
98 | | ||||
99 | // QMap::remove returns 1 if key found else 0 | ||||
100 | if (d->colorMatrix[column].remove(row)) { | ||||
101 | d->colorCount -= 1; | ||||
102 | return true; | ||||
103 | } else { | ||||
104 | return false; | ||||
105 | } | ||||
106 | } | ||||
107 | | ||||
108 | void KisSwatchGroup::setColumnCount(int columnCount) | ||||
109 | { | ||||
110 | Q_ASSERT(columnCount >= 0); | ||||
111 | if (columnCount < d->colorMatrix.size()) { | ||||
112 | for (int i = d->colorMatrix.size() - 1; i <= columnCount; i-- ) { | ||||
113 | d->colorCount -= d->colorMatrix[i].size(); | ||||
114 | } | ||||
115 | } | ||||
116 | d->colorMatrix.resize(columnCount); | ||||
117 | } | ||||
118 | | ||||
119 | int KisSwatchGroup::columnCount() const { | ||||
120 | return d->colorMatrix.size(); | ||||
121 | } | ||||
122 | | ||||
123 | KisSwatch KisSwatchGroup::getEntry(int column, int row) const | ||||
124 | { | ||||
125 | Q_ASSERT(checkEntry(column, row)); | ||||
126 | return d->colorMatrix[column][row]; | ||||
127 | } | ||||
128 | | ||||
129 | void KisSwatchGroup::addEntry(const KisSwatch &e) | ||||
130 | { | ||||
131 | if (columnCount() == 0) { | ||||
132 | setColumnCount(Private::DEFAULT_COLUMN_COUNT); | ||||
133 | } | ||||
134 | | ||||
135 | if (d->colorCount == 0) { | ||||
136 | setEntry(e, 0, 0); | ||||
137 | return; | ||||
138 | } | ||||
139 | | ||||
140 | int y = 0; | ||||
141 | for (const Private::Column &c : d->colorMatrix) { | ||||
142 | if (c.isEmpty()) { continue; } | ||||
143 | if (y < c.lastKey()) { | ||||
144 | y = c.lastKey(); | ||||
145 | } | ||||
146 | } | ||||
147 | for (int x = d->colorMatrix.size() - 1; x >= 0; x--) { | ||||
148 | if (checkEntry(x, y)) { | ||||
149 | // if the last entry's at the rightmost column, | ||||
150 | // add e to the leftmost column of the next row | ||||
151 | // and increase row count | ||||
152 | if (++x == d->colorMatrix.size()) { | ||||
153 | x = 0; | ||||
154 | y++; | ||||
155 | } | ||||
156 | // else just add it to the right | ||||
157 | setEntry(e, x, y); | ||||
158 | break; | ||||
159 | } | ||||
160 | } | ||||
161 | } | ||||
162 | | ||||
163 | void KisSwatchGroup::clear() | ||||
164 | { | ||||
165 | d->colorMatrix.clear(); | ||||
166 | } | ||||
167 | | ||||
168 | void KisSwatchGroup::setRowCount(int newRowCount) | ||||
169 | { | ||||
170 | d->rowCount = newRowCount; | ||||
171 | for (Private::Column &c : d->colorMatrix) { | ||||
172 | for (int k : c.keys()) { | ||||
173 | if (k >= newRowCount) { | ||||
174 | c.remove(k); | ||||
175 | d->colorCount--; | ||||
176 | } | ||||
177 | } | ||||
178 | } | ||||
179 | } | ||||
180 | | ||||
181 | int KisSwatchGroup::rowCount() const | ||||
182 | { | ||||
183 | return d->rowCount; | ||||
184 | } | ||||
185 | | ||||
186 | int KisSwatchGroup::colorCount() const | ||||
187 | { | ||||
188 | return d->colorCount; | ||||
189 | } | ||||
190 | | ||||
191 | QList<KisSwatchGroup::SwatchInfo> KisSwatchGroup::infoList() const | ||||
192 | { | ||||
193 | QList<SwatchInfo> res; | ||||
194 | int column = 0; | ||||
195 | for (const Private::Column &c : d->colorMatrix) { | ||||
196 | int i = 0; | ||||
197 | for (const KisSwatch &s : c.values()) { | ||||
198 | SwatchInfo info = {d->name, s, c.keys()[i++], column}; | ||||
199 | res.append(info); | ||||
200 | } | ||||
201 | column++; | ||||
202 | } | ||||
203 | return res; | ||||
204 | } | ||||
205 | | ||||
206 | void KisSwatchGroup::setName(const QString &name) | ||||
207 | { | ||||
208 | d->name = name; | ||||
209 | } | ||||
210 | | ||||
211 | QString KisSwatchGroup::name() const | ||||
212 | { | ||||
213 | return d->name; | ||||
214 | } |