KProperty

KColorCollection.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19//-----------------------------------------------------------------------------
20// KDE color collection
21
22#include "KColorCollection.h"
23
24#include <QtCore/QFile>
25#include <QtCore/QDir>
26#include <QtCore/QTextStream>
27#include <qsavefile.h>
28#include <qstandardpaths.h>
29
30//BEGIN KColorCollectionPrivate
31class KColorCollectionPrivate
32{
33public:
34 KColorCollectionPrivate(const QString &);
35 KColorCollectionPrivate(const KColorCollectionPrivate &);
36 ~KColorCollectionPrivate() {}
37 struct ColorNode {
38 ColorNode(const QColor &c, const QString &n)
39 : color(c), name(n) {}
40 QColor color;
41 QString name;
42 };
43 QList<ColorNode> colorList;
44
45 QString name;
46 QString desc;
48};
49
50KColorCollectionPrivate::KColorCollectionPrivate(const QString &_name)
51 : name(_name)
52{
53 if (name.isEmpty()) {
54 return;
55 }
56
58 if (filename.isEmpty()) {
59 return;
60 }
61
62 QFile paletteFile(filename);
63 if (!paletteFile.exists()) {
64 return;
65 }
66 if (!paletteFile.open(QIODevice::ReadOnly)) {
67 return;
68 }
69
70 // Read first line
71 // Expected "GIMP Palette"
72 QString line = QString::fromLocal8Bit(paletteFile.readLine());
73 if (line.contains(QLatin1String(" Palette"))) {
74 return;
75 }
76
77 while (!paletteFile.atEnd()) {
78 line = QString::fromLocal8Bit(paletteFile.readLine());
79 if (line[0] == QLatin1Char('#')) {
80 // This is a comment line
81 line = line.mid(1); // Strip '#'
82 line = line.trimmed(); // Strip remaining white space..
83 if (!line.isEmpty()) {
84 desc += line + QLatin1Char('\n'); // Add comment to description
85 }
86 } else {
87 // This is a color line, hopefully
88 line = line.trimmed();
89 if (line.isEmpty()) {
90 continue;
91 }
92 int r, g, b;
93 int pos = 0;
94 if (sscanf(line.toLatin1().constData(), "%d %d %d%n", &r, &g, &b, &pos) >= 3) {
95 r = qBound(0, r, 255);
96 g = qBound(0, g, 255);
97 b = qBound(0, b, 255);
98 QString name = line.mid(pos).trimmed();
99 colorList.append(ColorNode(QColor(r, g, b), name));
100 }
101 }
102 }
103}
104
105KColorCollectionPrivate::KColorCollectionPrivate(const KColorCollectionPrivate &p)
106 : colorList(p.colorList), name(p.name), desc(p.desc), editable(p.editable)
107{
108}
109//END KColorCollectionPrivate
110
112{
114 QStringLiteral("colors"),
116
117 QStringList paletteList;
118 Q_FOREACH (const QString &dir, paletteDirs) {
119 paletteList += QDir(dir).entryList(QDir::Files | QDir::NoDotAndDotDot);
120 }
121 paletteList.removeDuplicates();
122
123 return paletteList;
124}
125
127{
128 d = new KColorCollectionPrivate(name);
129}
130
132{
133 d = new KColorCollectionPrivate(*p.d);
134}
135
137{
138 // Need auto-save?
139 delete d;
140}
141
142bool
144{
146 + d->name;
147 QSaveFile sf(filename);
148 if (!sf.open(QIODevice::WriteOnly)) {
149 return false;
150 }
151
152 QTextStream str(&sf);
153
154 QString description = d->desc.trimmed();
155 description = QLatin1Char('#') + description.split(QLatin1Char('\n'), QString::KeepEmptyParts).join(QStringLiteral("\n#"));
156
157 str << QLatin1String("KDE RGB Palette\n");
158 str << description << QLatin1Char('\n');
159 foreach (const KColorCollectionPrivate::ColorNode &node, d->colorList) {
160 int r, g, b;
161 node.color.getRgb(&r, &g, &b);
162 str << r << " " << g << " " << b << " " << node.name << "\n";
163 }
164
165 return sf.commit();
166}
167
169{
170 return d->desc;
171}
172
174{
175 d->desc = desc;
176}
177
179{
180 return d->name;
181}
182
184{
185 d->name = name;
186}
187
189{
190 return d->editable;
191}
192
194{
195 d->editable = editable;
196}
197
199{
200 return (int) d->colorList.count();
201}
202
205{
206 if (&p == this) {
207 return *this;
208 }
209 d->colorList = p.d->colorList;
210 d->name = p.d->name;
211 d->desc = p.d->desc;
212 d->editable = p.d->editable;
213 return *this;
214}
215
216QColor
218{
219 if ((index < 0) || (index >= count())) {
220 return QColor();
221 }
222
223 return d->colorList[index].color;
224}
225
226int
228{
229 for (int i = 0; i < d->colorList.size(); ++i) {
230 if (d->colorList[i].color == color) {
231 return i;
232 }
233 }
234 return -1;
235}
236
239{
240 if ((index < 0) || (index >= count())) {
241 return QString();
242 }
243
244 return d->colorList[index].name;
245}
246
248{
249 return name(findColor(color));
250}
251
252int
253KColorCollection::addColor(const QColor &newColor, const QString &newColorName)
254{
255 d->colorList.append(KColorCollectionPrivate::ColorNode(newColor, newColorName));
256 return count() - 1;
257}
258
259int
261 const QColor &newColor,
262 const QString &newColorName)
263{
264 if ((index < 0) || (index >= count())) {
265 return -1;
266 }
267
268 KColorCollectionPrivate::ColorNode &node = d->colorList[index];
269 node.color = newColor;
270 node.name = newColorName;
271
272 return index;
273}
274
276 const QColor &newColor,
277 const QString &newColorName)
278{
279 return changeColor(findColor(oldColor), newColor, newColorName);
280}
281
282#include "KColorCollection.moc"
Class for handling color collections ("palettes").
int count() const
Return the number of colors in the collection.
KColorCollection & operator=(const KColorCollection &)
KColorCollection assignment operator.
void setName(const QString &name)
Set the name of the collection.
Editable editable() const
Returns whether the collection may be edited.
QString name() const
Get the name of the collection.
int changeColor(int index, const QColor &newColor, const QString &newColorName=QString())
Change a color.
void setDescription(const QString &desc)
Set the description of the collection.
~KColorCollection()
KColorCollection destructor.
QString description() const
Get the description of the collection.
QColor color(int index) const
Find color by index.
void setEditable(Editable editable)
Change whether the collection may be edited.
KColorCollection(const QString &name=QString())
KColorCollection constructor.
static QStringList installedCollections()
Query which KDE color collections are installed.
int findColor(const QColor &color) const
Find index by color.
int addColor(const QColor &newColor, const QString &newColorName=QString())
Add a color.
bool save()
Save the collection.
Editable
Used to specify whether a collection may be edited.
QString name(StandardAction id)
void getRgb(int *r, int *g, int *b, int *a) const const
QStringList entryList(Filters filters, SortFlags sort) const const
void append(QList< T > &&value)
qsizetype count() const const
qsizetype size() const const
bool commit()
virtual bool open(OpenMode mode) override
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
QString writableLocation(StandardLocation type)
QString fromLocal8Bit(QByteArrayView str)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString trimmed() const const
QString join(QChar separator) const const
qsizetype removeDuplicates()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.