KGuiAddons

kcolorcollection.cpp
1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7// KDE color collection
8
9#include "kcolorcollection.h"
10
11#if KGUIADDONS_BUILD_DEPRECATED_SINCE(6, 3)
12#include <QDir>
13#include <QFile>
14#include <QSaveFile>
15#include <QSharedData>
16#include <QStandardPaths>
17#include <QTextStream>
18
19// BEGIN KColorCollectionPrivate
20class KColorCollectionPrivate : public QSharedData
21{
22public:
23 KColorCollectionPrivate(const QString &);
24 KColorCollectionPrivate(const KColorCollectionPrivate &) = default;
25 ~KColorCollectionPrivate()
26 {
27 }
28 struct ColorNode {
29 ColorNode(const QColor &c, const QString &n)
30 : color(c)
31 , name(n)
32 {
33 }
34 QColor color;
36 };
37 QList<ColorNode> colorList;
38
40 QString desc;
42};
43
44KColorCollectionPrivate::KColorCollectionPrivate(const QString &_name)
45 : name(_name)
46{
47 if (name.isEmpty()) {
48 return;
49 }
50
52 if (filename.isEmpty()) {
53 return;
54 }
55
56 QFile paletteFile(filename);
57 if (!paletteFile.exists()) {
58 return;
59 }
60 if (!paletteFile.open(QIODevice::ReadOnly)) {
61 return;
62 }
63
64 // Read first line
65 // Expected "GIMP Palette"
66 QString line = QString::fromLocal8Bit(paletteFile.readLine());
67 if (line.contains(QLatin1String(" Palette"))) {
68 return;
69 }
70
71 while (!paletteFile.atEnd()) {
72 line = QString::fromLocal8Bit(paletteFile.readLine());
73 if (line[0] == QLatin1Char('#')) {
74 // This is a comment line
75 line.remove(0, 1); // Strip '#'
76 line = line.trimmed(); // Strip remaining white space..
77 if (!line.isEmpty()) {
78 desc += line + QLatin1Char('\n'); // Add comment to description
79 }
80 } else {
81 // This is a color line, hopefully
82 line = line.trimmed();
83 if (line.isEmpty()) {
84 continue;
85 }
86 int r;
87 int g;
88 int b;
89 int pos = 0;
90 if (sscanf(line.toLatin1().constData(), "%d %d %d%n", &r, &g, &b, &pos) >= 3) {
91 r = qBound(0, r, 255);
92 g = qBound(0, g, 255);
93 b = qBound(0, b, 255);
94 QString name = line.mid(pos).trimmed();
95 colorList.append(ColorNode(QColor(r, g, b), name));
96 }
97 }
98 }
99}
100// END KColorCollectionPrivate
101
103{
105
106 QStringList paletteList;
107 for (const QString &dir : paletteDirs) {
108 paletteList += QDir(dir).entryList(QDir::Files | QDir::NoDotAndDotDot);
109 }
110 paletteList.removeDuplicates();
111
112 return paletteList;
113}
114
116 : d(new KColorCollectionPrivate(name))
117{
118}
119
121
122// Need auto-save?
124
126{
128 QSaveFile sf(filename);
129 if (!sf.open(QIODevice::WriteOnly)) {
130 return false;
131 }
132
133 QTextStream str(&sf);
134
135 QString description = d->desc.trimmed();
137
138 str << QLatin1String("KDE RGB Palette\n");
139 str << description << QLatin1Char('\n');
140 for (const KColorCollectionPrivate::ColorNode &node : std::as_const(d->colorList)) {
141 int r;
142 int g;
143 int b;
144 node.color.getRgb(&r, &g, &b);
145 str << r << " " << g << " " << b << " " << node.name << "\n";
146 }
147
148 return sf.commit();
149}
150
152{
153 return d->desc;
154}
155
157{
158 d->desc = desc;
159}
160
162{
163 return d->name;
164}
165
166void KColorCollection::setName(const QString &name)
167{
168 d->name = name;
169}
170
172{
173 return d->editable;
174}
175
176void KColorCollection::setEditable(Editable editable)
177{
178 d->editable = editable;
179}
180
181int KColorCollection::count() const
182{
183 return d->colorList.count();
184}
185
187
188QColor KColorCollection::color(int index) const
189{
190 if ((index < 0) || (index >= count())) {
191 return QColor();
192 }
193
194 return d->colorList[index].color;
195}
196
197int KColorCollection::findColor(const QColor &color) const
198{
199 for (int i = 0; i < d->colorList.size(); ++i) {
200 if (d->colorList[i].color == color) {
201 return i;
202 }
203 }
204 return -1;
205}
206
207QString KColorCollection::name(int index) const
208{
209 if ((index < 0) || (index >= count())) {
210 return QString();
211 }
212
213 return d->colorList[index].name;
214}
215
216QString KColorCollection::name(const QColor &color) const
217{
218 return name(findColor(color));
219}
220
221int KColorCollection::addColor(const QColor &newColor, const QString &newColorName)
222{
223 d->colorList.append(KColorCollectionPrivate::ColorNode(newColor, newColorName));
224 return count() - 1;
225}
226
227int KColorCollection::changeColor(int index, const QColor &newColor, const QString &newColorName)
228{
229 if ((index < 0) || (index >= count())) {
230 return -1;
231 }
232
233 KColorCollectionPrivate::ColorNode &node = d->colorList[index];
234 node.color = newColor;
235 node.name = newColorName;
236
237 return index;
238}
239
240int KColorCollection::changeColor(const QColor &oldColor, const QColor &newColor, const QString &newColorName)
241{
242 return changeColor(findColor(oldColor), newColor, newColorName);
243}
244
245#endif
int count() const
int changeColor(const QColor &oldColor, const QColor &newColor, const QString &newColorName=QString())
KColorCollection & operator=(const KColorCollection &)
void setName(const QString &name)
Editable editable() const
QString name() const
void setDescription(const QString &desc)
QString description() const
QColor color(int index) const
void setEditable(Editable editable)
KColorCollection(const KColorCollection &)
static QStringList installedCollections()
int findColor(const QColor &color) const
int addColor(const QColor &newColor, const QString &newColorName=QString())
QString name(GameStandardAction id)
const char * constData() const const
QString name(NameFormat format) const const
QStringList entryList(Filters filters, SortFlags sort) const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
QString writableLocation(StandardLocation type)
QString & append(QChar ch)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString fromLocal8Bit(QByteArrayView str)
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QByteArray toLatin1() const const
QString trimmed() const const
QString join(QChar separator) const const
qsizetype removeDuplicates()
KeepEmptyParts
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:54:34 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.