• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • colors
kcolorcollection.cpp
Go to the documentation of this file.
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/QTextIStream>
26#include <kstandarddirs.h>
27#include <kglobal.h>
28#include <ksavefile.h>
29#include <kstringhandler.h>
30
31//BEGIN KColorCollectionPrivate
32class KColorCollectionPrivate
33{
34public:
35 KColorCollectionPrivate(const QString&);
36 KColorCollectionPrivate(const KColorCollectionPrivate&);
37 ~KColorCollectionPrivate() {}
38 struct ColorNode
39 {
40 ColorNode(const QColor &c, const QString &n)
41 : color(c), name(n) {}
42 QColor color;
43 QString name;
44 };
45 QList<ColorNode> colorList;
46
47 QString name;
48 QString desc;
49 KColorCollection::Editable editable;
50};
51
52KColorCollectionPrivate::KColorCollectionPrivate(const QString &_name)
53 : name(_name)
54{
55 if (name.isEmpty()) return;
56
57 QString filename = KStandardDirs::locate("config", "colors/"+name);
58 if (filename.isEmpty()) return;
59
60 QFile paletteFile(filename);
61 if (!paletteFile.exists()) return;
62 if (!paletteFile.open(QIODevice::ReadOnly)) return;
63
64 // Read first line
65 // Expected "GIMP Palette"
66 QString line = QString::fromLocal8Bit(paletteFile.readLine());
67 if (line.indexOf(" Palette") == -1) return;
68
69 while( !paletteFile.atEnd() )
70 {
71 line = QString::fromLocal8Bit(paletteFile.readLine());
72 if (line[0] == '#')
73 {
74 // This is a comment line
75 line = line.mid(1); // Strip '#'
76 line = line.trimmed(); // Strip remaining white space..
77 if (!line.isEmpty())
78 {
79 desc += line+'\n'; // Add comment to description
80 }
81 }
82 else
83 {
84 // This is a color line, hopefully
85 line = line.trimmed();
86 if (line.isEmpty()) continue;
87 int r, g, b;
88 int pos = 0;
89 if (sscanf(line.toLatin1(), "%d %d %d%n", &r, &g, &b, &pos) >= 3)
90 {
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
101KColorCollectionPrivate::KColorCollectionPrivate(const KColorCollectionPrivate& p)
102 : colorList(p.colorList), name(p.name), desc(p.desc), editable(p.editable)
103{
104}
105//END KColorCollectionPrivate
106
107QStringList
108KColorCollection::installedCollections()
109{
110 QStringList paletteList;
111 KGlobal::dirs()->findAllResources("config", "colors/*", KStandardDirs::NoDuplicates, paletteList);
112
113 int strip = strlen("colors/");
114 for(QStringList::Iterator it = paletteList.begin();
115 it != paletteList.end();
116 ++it)
117 {
118 (*it) = (*it).mid(strip);
119 }
120
121 return paletteList;
122}
123
124KColorCollection::KColorCollection(const QString &name)
125{
126 d = new KColorCollectionPrivate(name);
127}
128
129KColorCollection::KColorCollection(const KColorCollection &p)
130{
131 d = new KColorCollectionPrivate(*p.d);
132}
133
134KColorCollection::~KColorCollection()
135{
136 // Need auto-save?
137 delete d;
138}
139
140bool
141KColorCollection::save()
142{
143 QString filename = KStandardDirs::locateLocal("config", "colors/" + d->name);
144 KSaveFile sf(filename);
145 if (!sf.open()) return false;
146
147 QTextStream str ( &sf );
148
149 QString description = d->desc.trimmed();
150 description = '#'+description.split( '\n', QString::KeepEmptyParts).join("\n#");
151
152 str << "KDE RGB Palette\n";
153 str << description << "\n";
154 foreach (const KColorCollectionPrivate::ColorNode &node, d->colorList)
155 {
156 int r,g,b;
157 node.color.getRgb(&r, &g, &b);
158 str << r << " " << g << " " << b << " " << node.name << "\n";
159 }
160
161 sf.flush();
162 return sf.finalize();
163}
164
165QString KColorCollection::description() const
166{
167 return d->desc;
168}
169
170void KColorCollection::setDescription(const QString &desc)
171{
172 d->desc = desc;
173}
174
175QString KColorCollection::name() const
176{
177 return d->name;
178}
179
180void KColorCollection::setName(const QString &name)
181{
182 d->name = name;
183}
184
185KColorCollection::Editable KColorCollection::editable() const
186{
187 return d->editable;
188}
189
190void KColorCollection::setEditable(Editable editable)
191{
192 d->editable = editable;
193}
194
195int KColorCollection::count() const
196{
197 return (int) d->colorList.count();
198}
199
200KColorCollection&
201KColorCollection::operator=( const KColorCollection &p)
202{
203 if (&p == this) return *this;
204 d->colorList = p.d->colorList;
205 d->name = p.d->name;
206 d->desc = p.d->desc;
207 d->editable = p.d->editable;
208 return *this;
209}
210
211QColor
212KColorCollection::color(int index) const
213{
214 if ((index < 0) || (index >= count()))
215 return QColor();
216
217 return d->colorList[index].color;
218}
219
220int
221KColorCollection::findColor(const QColor &color) const
222{
223 for (int i = 0; i < d->colorList.size(); ++i)
224 {
225 if (d->colorList[i].color == color)
226 return i;
227 }
228 return -1;
229}
230
231QString
232KColorCollection::name(int index) const
233{
234 if ((index < 0) || (index >= count()))
235 return QString();
236
237 return d->colorList[index].name;
238}
239
240QString KColorCollection::name(const QColor &color) const
241{
242 return name(findColor(color));
243}
244
245int
246KColorCollection::addColor(const QColor &newColor, const QString &newColorName)
247{
248 d->colorList.append(KColorCollectionPrivate::ColorNode(newColor, newColorName));
249 return count() - 1;
250}
251
252int
253KColorCollection::changeColor(int index,
254 const QColor &newColor,
255 const QString &newColorName)
256{
257 if ((index < 0) || (index >= count()))
258 return -1;
259
260 KColorCollectionPrivate::ColorNode& node = d->colorList[index];
261 node.color = newColor;
262 node.name = newColorName;
263
264 return index;
265}
266
267int KColorCollection::changeColor(const QColor &oldColor,
268 const QColor &newColor,
269 const QString &newColorName)
270{
271 return changeColor( findColor(oldColor), newColor, newColorName);
272}
273
KColorCollection
Class for handling color collections ("palettes").
Definition: kcolorcollection.h:43
KColorCollection::count
int count() const
Return the number of colors in the collection.
Definition: kcolorcollection.cpp:195
KColorCollection::operator=
KColorCollection & operator=(const KColorCollection &)
KColorCollection assignment operator.
Definition: kcolorcollection.cpp:201
KColorCollection::setName
void setName(const QString &name)
Set the name of the collection.
Definition: kcolorcollection.cpp:180
KColorCollection::editable
Editable editable() const
Returns whether the collection may be edited.
Definition: kcolorcollection.cpp:185
KColorCollection::name
QString name() const
Get the name of the collection.
Definition: kcolorcollection.cpp:175
KColorCollection::changeColor
int changeColor(int index, const QColor &newColor, const QString &newColorName=QString())
Change a color.
Definition: kcolorcollection.cpp:253
KColorCollection::setDescription
void setDescription(const QString &desc)
Set the description of the collection.
Definition: kcolorcollection.cpp:170
KColorCollection::~KColorCollection
~KColorCollection()
KColorCollection destructor.
Definition: kcolorcollection.cpp:134
KColorCollection::description
QString description() const
Get the description of the collection.
Definition: kcolorcollection.cpp:165
KColorCollection::color
QColor color(int index) const
Find color by index.
Definition: kcolorcollection.cpp:212
KColorCollection::setEditable
void setEditable(Editable editable)
Change whether the collection may be edited.
Definition: kcolorcollection.cpp:190
KColorCollection::KColorCollection
KColorCollection(const QString &name=QString())
KColorCollection constructor.
Definition: kcolorcollection.cpp:124
KColorCollection::installedCollections
static QStringList installedCollections()
Query which KDE color collections are installed.
Definition: kcolorcollection.cpp:108
KColorCollection::findColor
int findColor(const QColor &color) const
Find index by color.
Definition: kcolorcollection.cpp:221
KColorCollection::addColor
int addColor(const QColor &newColor, const QString &newColorName=QString())
Add a color.
Definition: kcolorcollection.cpp:246
KColorCollection::save
bool save()
Save the collection.
Definition: kcolorcollection.cpp:141
KColorCollection::Editable
Editable
Used to specify whether a collection may be edited.
Definition: kcolorcollection.h:110
KSaveFile
KSaveFile::finalize
bool finalize()
KSaveFile::open
virtual bool open(OpenMode flags=QIODevice::ReadWrite)
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter, SearchOptions options, QStringList &relPaths) const
KStandardDirs::NoDuplicates
NoDuplicates
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, bool createDir, const KComponentData &cData=KGlobal::mainComponent())
QList
kcolorcollection.h
kglobal.h
ksavefile.h
kstandarddirs.h
kstringhandler.h
KGlobal::dirs
KStandardDirs * dirs()
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal