• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kpalette.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 palette
21
22#include "kpalette.h"
23
24#include <tqfile.h>
25#include <tqtextstream.h>
26#include <tdestandarddirs.h>
27#include <tdeglobal.h>
28#include <ksavefile.h>
29#include <kstringhandler.h>
30
31template class TQPtrList<KPalette::kolor>;
32
33TQStringList
34KPalette::getPaletteList()
35{
36 TQStringList paletteList;
37 TDEGlobal::dirs()->findAllResources("config", "colors/*", false, true, paletteList);
38
39 int strip = strlen("colors/");
40 for(TQStringList::Iterator it = paletteList.begin();
41 it != paletteList.end();
42 it++)
43 {
44 (*it) = (*it).mid(strip);
45 }
46
47 return paletteList;
48}
49
50KPalette::KPalette(const TQString &name)
51 : mName(name)
52{
53 mKolorList.setAutoDelete(true);
54 if (mName.isEmpty()) return;
55
56 TQString filename = locate("config", "colors/"+mName);
57 if (filename.isEmpty()) return;
58
59 TQFile paletteFile(filename);
60 if (!paletteFile.exists()) return;
61 if (!paletteFile.open(IO_ReadOnly)) return;
62
63 uint maxLength = 1024;
64 TQString line;
65
66 // Read first line
67 // Expected "GIMP Palette"
68 if (paletteFile.readLine(line, maxLength) == -1) return;
69 if (line.find(" Palette") == -1) return;
70
71 while( paletteFile.readLine(line, maxLength) != -1)
72 {
73 if (line[0] == '#')
74 {
75 // This is a comment line
76 line = line.mid(1); // Strip '#'
77 line = line.stripWhiteSpace(); // Strip remaining white space..
78 if (!line.isEmpty())
79 {
80 mDesc += line+"\n"; // Add comment to description
81 }
82 }
83 else
84 {
85 // This is a color line, hopefully
86 line = line.stripWhiteSpace();
87 if (line.isEmpty()) continue;
88 int red, green, blue;
89 int pos = 0;
90 if (sscanf(line.ascii(), "%d %d %d%n", &red, &green, &blue, &pos) >= 3)
91 {
92 if (red > 255) red = 255;
93 if (red < 0) red = 0;
94 if (green > 255) green = 255;
95 if (green < 0) green = 0;
96 if (blue > 255) blue = 255;
97 if (blue < 0) blue = 0;
98 kolor *node = new kolor();
99 node->color.setRgb(red, green, blue);
100 node->name = line.mid(pos).stripWhiteSpace();
101 if (node->name.isNull()) node->name = "";
102 mKolorList.append( node );
103 }
104 }
105 }
106}
107
108KPalette::KPalette(const KPalette &p)
109 : mName(p.mName), mDesc(p.mDesc), mEditable(p.mEditable)
110{
111 mKolorList.setAutoDelete(true);
112 // Make a deep copy of the color list
113 // We can't iterate a const list :(
114 // DF: yes you can - use the proper iterator, not first/next
115 TQPtrList<kolor> *nonConstList = (TQPtrList<kolor> *) &p.mKolorList;
116 for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
117 {
118 mKolorList.append(new kolor(*node));
119 }
120}
121
122KPalette::~KPalette()
123{
124 // Need auto-save?
125}
126
127bool
128KPalette::save()
129{
130 TQString filename = locateLocal("config", "colors/"+mName);
131 KSaveFile sf(filename);
132 if (sf.status() != 0) return false;
133
134 TQTextStream *str = sf.textStream();
135
136 TQString description = mDesc.stripWhiteSpace();
137 description = "#"+TQStringList::split("\n", description, true).join("\n#");
138
139 (*str) << "KDE RGB Palette\n";
140 (*str) << description << "\n";
141 // We can't iterate a const list :(
142 // DF: yes you can - use the proper iterator, not first/next
143 TQPtrList<kolor> *nonConstList = (TQPtrList<kolor> *) (&mKolorList);
144 for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
145 {
146 int r,g,b;
147 node->color.rgb(&r, &g, &b);
148 (*str) << r << " " << g << " " << b << " " << node->name << "\n";
149 }
150 return sf.close();
151}
152
153
154KPalette&
155KPalette::operator=( const KPalette &p)
156{
157 if (&p == this) return *this;
158 mKolorList.clear();
159 // Make a deep copy of the color list
160 // We can't iterate a const list :(
161 // DF: yes you can - use the proper iterator, not first/next
162 TQPtrList<kolor> *nonConstList = (TQPtrList<kolor> *) &p.mKolorList;
163 for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
164 {
165 mKolorList.append(new kolor(*node));
166 }
167 mName = p.mName;
168 mDesc = p.mDesc;
169 mEditable = p.mEditable;
170 return *this;
171}
172
173TQColor
174KPalette::color(int index)
175{
176 if ((index < 0) || (index >= nrColors()))
177 return TQColor();
178
179 kolor *node = mKolorList.at(index);
180 if (!node)
181 return TQColor();
182
183 return node->color;
184}
185
186int
187KPalette::findColor(const TQColor &color) const
188{
189 int index;
190 TQPtrListIterator<kolor> it( mKolorList );
191 for (index = 0; it.current(); ++it, ++index)
192 {
193 if (it.current()->color == color)
194 return index;
195 }
196 return -1;
197}
198
199TQString
200KPalette::colorName(int index)
201{
202 if ((index < 0) || (index >= nrColors()))
203 return TQString::null;
204
205 kolor *node = mKolorList.at(index);
206 if (!node)
207 return TQString::null;
208
209 return node->name;
210}
211
212int
213KPalette::addColor(const TQColor &newColor, const TQString &newColorName)
214{
215 kolor *node = new kolor();
216 node->color = newColor;
217 node->name = newColorName;
218 mKolorList.append( node );
219 return nrColors()-1;
220}
221
222int
223KPalette::changeColor(int index,
224 const TQColor &newColor,
225 const TQString &newColorName)
226{
227 if ((index < 0) || (index >= nrColors()))
228 return -1;
229
230 kolor *node = mKolorList.at(index);
231 if (!node)
232 return -1;
233
234 node->color = newColor;
235 node->name = newColorName;
236 return index;
237}
KPalette
Class for handling Palettes.
Definition: kpalette.h:47
KPalette::color
TQColor color(int index)
Find color by index.
Definition: kpalette.cpp:174
KPalette::addColor
int addColor(const TQColor &newColor, const TQString &newColorName=TQString::null)
Add a color.
Definition: kpalette.cpp:213
KPalette::save
bool save()
Save the palette.
Definition: kpalette.cpp:128
KPalette::nrColors
int nrColors() const
Return the number of colors in the palette.
Definition: kpalette.h:141
KPalette::colorName
TQString colorName(int index)
Find color name by index.
Definition: kpalette.cpp:200
KPalette::getPaletteList
static TQStringList getPaletteList()
Query which KDE palettes are installed.
Definition: kpalette.cpp:34
KPalette::findColor
int findColor(const TQColor &color) const
Find index by color.
Definition: kpalette.cpp:187
KPalette::~KPalette
virtual ~KPalette()
KPalette destructor.
Definition: kpalette.cpp:122
KPalette::description
TQString description() const
Get the description of the palette.
Definition: kpalette.h:89
KPalette::changeColor
int changeColor(int index, const TQColor &newColor, const TQString &newColorName=TQString::null)
Change a color.
Definition: kpalette.cpp:223
KPalette::operator=
KPalette & operator=(const KPalette &)
KPalette assignment operator.
Definition: kpalette.cpp:155
KPalette::KPalette
KPalette(const TQString &name=TQString::null)
KPalette constructor.
Definition: kpalette.cpp:50
KSaveFile
The KSaveFile class has been made to write out changes to an existing file atomically.
Definition: ksavefile.h:42
KSaveFile::status
int status() const
Returns the status of the file based on errno.
Definition: ksavefile.h:68
KSaveFile::close
bool close()
Closes the file and makes the changes definitive.
Definition: ksavefile.cpp:107
KSaveFile::textStream
TQTextStream * textStream()
A TQTextStream* open for writing to the file.
Definition: ksavefile.h:107
TDEGlobal::dirs
static TDEStandardDirs * dirs()
Returns the application standard dirs object.
Definition: tdeglobal.cpp:58
TDEStandardDirs::findAllResources
TQStringList findAllResources(const char *type, const TQString &filter=TQString::null, bool recursive=false, bool unique=false) const
Tries to find all resources with the specified type.
Definition: tdestandarddirs.cpp:679

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.