kpalette.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kpalette.h"
00023
00024 #include <qfile.h>
00025 #include <qtextstream.h>
00026 #include <kstandarddirs.h>
00027 #include <kglobal.h>
00028 #include <ksavefile.h>
00029 #include <kstringhandler.h>
00030
00031 template class QPtrList<KPalette::kolor>;
00032
00033 QStringList
00034 KPalette::getPaletteList()
00035 {
00036 QStringList paletteList;
00037 KGlobal::dirs()->findAllResources("config", "colors/*", false, true, paletteList);
00038
00039 int strip = strlen("colors/");
00040 for(QStringList::Iterator it = paletteList.begin();
00041 it != paletteList.end();
00042 it++)
00043 {
00044 (*it) = (*it).mid(strip);
00045 }
00046
00047 return paletteList;
00048 }
00049
00050 KPalette::KPalette(const QString &name)
00051 : mName(name)
00052 {
00053 mKolorList.setAutoDelete(true);
00054 if (mName.isEmpty()) return;
00055
00056 QString filename = locate("config", "colors/"+mName);
00057 if (filename.isEmpty()) return;
00058
00059 QFile paletteFile(filename);
00060 if (!paletteFile.exists()) return;
00061 if (!paletteFile.open(IO_ReadOnly)) return;
00062
00063 uint maxLength = 1024;
00064 QString line;
00065
00066
00067
00068 if (paletteFile.readLine(line, maxLength) == -1) return;
00069 if (line.find(" Palette") == -1) return;
00070
00071 char *buffer = new char[maxLength];
00072
00073 while( paletteFile.readLine(line, maxLength) != -1)
00074 {
00075 if (line[0] == '#')
00076 {
00077
00078 line = line.mid(1);
00079 line = line.stripWhiteSpace();
00080 if (!line.isEmpty())
00081 {
00082 mDesc += line+"\n";
00083 }
00084 }
00085 else
00086 {
00087
00088 line = line.stripWhiteSpace();
00089 if (line.isEmpty()) continue;
00090 int red, green, blue;
00091 int pos = 0;
00092 buffer[0] = '\0';
00093 if (sscanf(line.ascii(), "%d %d %d%n", &red, &green, &blue, &pos) >= 3)
00094 {
00095 if (red > 255) red = 255;
00096 if (red < 0) red = 0;
00097 if (green > 255) green = 255;
00098 if (green < 0) green = 0;
00099 if (blue > 255) blue = 255;
00100 if (blue < 0) blue = 0;
00101 kolor *node = new kolor();
00102 node->color.setRgb(red, green, blue);
00103 node->name = line.mid(pos).stripWhiteSpace();
00104 if (node->name.isNull()) node->name = "";
00105 mKolorList.append( node );
00106 }
00107 }
00108 }
00109 delete [] buffer;
00110 }
00111
00112 KPalette::KPalette(const KPalette &p)
00113 : mName(p.mName), mDesc(p.mDesc), mEditable(p.mEditable)
00114 {
00115 mKolorList.setAutoDelete(true);
00116
00117
00118
00119 QPtrList<kolor> *nonConstList = (QPtrList<kolor> *) &p.mKolorList;
00120 for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
00121 {
00122 mKolorList.append(new kolor(*node));
00123 }
00124 }
00125
00126 KPalette::~KPalette()
00127 {
00128
00129 }
00130
00131 bool
00132 KPalette::save()
00133 {
00134 QString filename = locateLocal("config", "colors/"+mName);
00135 KSaveFile sf(filename);
00136 if (sf.status() != 0) return false;
00137
00138 QTextStream *str = sf.textStream();
00139
00140 QString description = mDesc.stripWhiteSpace();
00141 description = "#"+QStringList::split("\n", description, true).join("\n#");
00142
00143 (*str) << "KDE RGB Palette\n";
00144 (*str) << description << "\n";
00145
00146
00147 QPtrList<kolor> *nonConstList = (QPtrList<kolor> *) (&mKolorList);
00148 for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
00149 {
00150 int r,g,b;
00151 node->color.rgb(&r, &g, &b);
00152 (*str) << r << " " << g << " " << b << " " << node->name << "\n";
00153 }
00154 return sf.close();
00155 }
00156
00157
00158 KPalette&
00159 KPalette::operator=( const KPalette &p)
00160 {
00161 if (&p == this) return *this;
00162 mKolorList.clear();
00163
00164
00165
00166 QPtrList<kolor> *nonConstList = (QPtrList<kolor> *) &p.mKolorList;
00167 for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
00168 {
00169 mKolorList.append(new kolor(*node));
00170 }
00171 mName = p.mName;
00172 mDesc = p.mDesc;
00173 mEditable = p.mEditable;
00174 return *this;
00175 }
00176
00177 QColor
00178 KPalette::color(int index)
00179 {
00180 if ((index < 0) || (index >= nrColors()))
00181 return QColor();
00182
00183 kolor *node = mKolorList.at(index);
00184 if (!node)
00185 return QColor();
00186
00187 return node->color;
00188 }
00189
00190 int
00191 KPalette::findColor(const QColor &color) const
00192 {
00193 int index;
00194 QPtrListIterator<kolor> it( mKolorList );
00195 for (index = 0; it.current(); ++it, ++index)
00196 {
00197 if (it.current()->color == color)
00198 return index;
00199 }
00200 return -1;
00201 }
00202
00203 QString
00204 KPalette::colorName(int index)
00205 {
00206 if ((index < 0) || (index >= nrColors()))
00207 return QString::null;
00208
00209 kolor *node = mKolorList.at(index);
00210 if (!node)
00211 return QString::null;
00212
00213 return node->name;
00214 }
00215
00216 int
00217 KPalette::addColor(const QColor &newColor, const QString &newColorName)
00218 {
00219 kolor *node = new kolor();
00220 node->color = newColor;
00221 node->name = newColorName;
00222 mKolorList.append( node );
00223 return nrColors()-1;
00224 }
00225
00226 int
00227 KPalette::changeColor(int index,
00228 const QColor &newColor,
00229 const QString &newColorName)
00230 {
00231 if ((index < 0) || (index >= nrColors()))
00232 return -1;
00233
00234 kolor *node = mKolorList.at(index);
00235 if (!node)
00236 return -1;
00237
00238 node->color = newColor;
00239 node->name = newColorName;
00240 return index;
00241 }
This file is part of the documentation for kdelibs Version 3.1.0.