KDEUI
kcolorcollection.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kcolorcollection.h"
00023
00024 #include <QtCore/QFile>
00025 #include <QtCore/QTextIStream>
00026 #include <kstandarddirs.h>
00027 #include <kglobal.h>
00028 #include <ksavefile.h>
00029 #include <kstringhandler.h>
00030
00031
00032 class KColorCollectionPrivate
00033 {
00034 public:
00035 KColorCollectionPrivate(const QString&);
00036 KColorCollectionPrivate(const KColorCollectionPrivate&);
00037 ~KColorCollectionPrivate() {}
00038 struct ColorNode
00039 {
00040 ColorNode(const QColor &c, const QString &n)
00041 : color(c), name(n) {}
00042 QColor color;
00043 QString name;
00044 };
00045 QList<ColorNode> colorList;
00046
00047 QString name;
00048 QString desc;
00049 KColorCollection::Editable editable;
00050 };
00051
00052 KColorCollectionPrivate::KColorCollectionPrivate(const QString &_name)
00053 : name(_name)
00054 {
00055 if (name.isEmpty()) return;
00056
00057 QString filename = KStandardDirs::locate("config", "colors/"+name);
00058 if (filename.isEmpty()) return;
00059
00060 QFile paletteFile(filename);
00061 if (!paletteFile.exists()) return;
00062 if (!paletteFile.open(QIODevice::ReadOnly)) return;
00063
00064
00065
00066 QString line = QString::fromLocal8Bit(paletteFile.readLine());
00067 if (line.indexOf(" Palette") == -1) return;
00068
00069 while( !paletteFile.atEnd() )
00070 {
00071 line = QString::fromLocal8Bit(paletteFile.readLine());
00072 if (line[0] == '#')
00073 {
00074
00075 line = line.mid(1);
00076 line = line.trimmed();
00077 if (!line.isEmpty())
00078 {
00079 desc += line+'\n';
00080 }
00081 }
00082 else
00083 {
00084
00085 line = line.trimmed();
00086 if (line.isEmpty()) continue;
00087 int r, g, b;
00088 int pos = 0;
00089 if (sscanf(line.toAscii(), "%d %d %d%n", &r, &g, &b, &pos) >= 3)
00090 {
00091 r = qBound(0, r, 255);
00092 g = qBound(0, g, 255);
00093 b = qBound(0, b, 255);
00094 QString name = line.mid(pos).trimmed();
00095 colorList.append(ColorNode(QColor(r, g, b), name));
00096 }
00097 }
00098 }
00099 }
00100
00101 KColorCollectionPrivate::KColorCollectionPrivate(const KColorCollectionPrivate& p)
00102 : colorList(p.colorList), name(p.name), desc(p.desc), editable(p.editable)
00103 {
00104 }
00105
00106
00107 QStringList
00108 KColorCollection::installedCollections()
00109 {
00110 QStringList paletteList;
00111 KGlobal::dirs()->findAllResources("config", "colors/*", KStandardDirs::NoDuplicates, paletteList);
00112
00113 int strip = strlen("colors/");
00114 for(QStringList::Iterator it = paletteList.begin();
00115 it != paletteList.end();
00116 ++it)
00117 {
00118 (*it) = (*it).mid(strip);
00119 }
00120
00121 return paletteList;
00122 }
00123
00124 KColorCollection::KColorCollection(const QString &name)
00125 {
00126 d = new KColorCollectionPrivate(name);
00127 }
00128
00129 KColorCollection::KColorCollection(const KColorCollection &p)
00130 {
00131 d = new KColorCollectionPrivate(*p.d);
00132 }
00133
00134 KColorCollection::~KColorCollection()
00135 {
00136
00137 delete d;
00138 }
00139
00140 bool
00141 KColorCollection::save()
00142 {
00143 QString filename = KStandardDirs::locateLocal("config", "colors/" + d->name);
00144 KSaveFile sf(filename);
00145 if (!sf.open()) return false;
00146
00147 QTextStream str ( &sf );
00148
00149 QString description = d->desc.trimmed();
00150 description = '#'+description.split( '\n', QString::KeepEmptyParts).join("\n#");
00151
00152 str << "KDE RGB Palette\n";
00153 str << description << "\n";
00154 foreach (const KColorCollectionPrivate::ColorNode &node, d->colorList)
00155 {
00156 int r,g,b;
00157 node.color.getRgb(&r, &g, &b);
00158 str << r << " " << g << " " << b << " " << node.name << "\n";
00159 }
00160
00161 sf.flush();
00162 return sf.finalize();
00163 }
00164
00165 QString KColorCollection::description() const
00166 {
00167 return d->desc;
00168 }
00169
00170 void KColorCollection::setDescription(const QString &desc)
00171 {
00172 d->desc = desc;
00173 }
00174
00175 QString KColorCollection::name() const
00176 {
00177 return d->name;
00178 }
00179
00180 void KColorCollection::setName(const QString &name)
00181 {
00182 d->name = name;
00183 }
00184
00185 KColorCollection::Editable KColorCollection::editable() const
00186 {
00187 return d->editable;
00188 }
00189
00190 void KColorCollection::setEditable(Editable editable)
00191 {
00192 d->editable = editable;
00193 }
00194
00195 int KColorCollection::count() const
00196 {
00197 return (int) d->colorList.count();
00198 }
00199
00200 KColorCollection&
00201 KColorCollection::operator=( const KColorCollection &p)
00202 {
00203 if (&p == this) return *this;
00204 d->colorList = p.d->colorList;
00205 d->name = p.d->name;
00206 d->desc = p.d->desc;
00207 d->editable = p.d->editable;
00208 return *this;
00209 }
00210
00211 QColor
00212 KColorCollection::color(int index) const
00213 {
00214 if ((index < 0) || (index >= count()))
00215 return QColor();
00216
00217 return d->colorList[index].color;
00218 }
00219
00220 int
00221 KColorCollection::findColor(const QColor &color) const
00222 {
00223 for (int i = 0; i < d->colorList.size(); ++i)
00224 {
00225 if (d->colorList[i].color == color)
00226 return i;
00227 }
00228 return -1;
00229 }
00230
00231 QString
00232 KColorCollection::name(int index) const
00233 {
00234 if ((index < 0) || (index >= count()))
00235 return QString();
00236
00237 return d->colorList[index].name;
00238 }
00239
00240 QString KColorCollection::name(const QColor &color) const
00241 {
00242 return name(findColor(color));
00243 }
00244
00245 int
00246 KColorCollection::addColor(const QColor &newColor, const QString &newColorName)
00247 {
00248 d->colorList.append(KColorCollectionPrivate::ColorNode(newColor, newColorName));
00249 return count() - 1;
00250 }
00251
00252 int
00253 KColorCollection::changeColor(int index,
00254 const QColor &newColor,
00255 const QString &newColorName)
00256 {
00257 if ((index < 0) || (index >= count()))
00258 return -1;
00259
00260 KColorCollectionPrivate::ColorNode& node = d->colorList[index];
00261 node.color = newColor;
00262 node.name = newColorName;
00263
00264 return index;
00265 }
00266
00267 int KColorCollection::changeColor(const QColor &oldColor,
00268 const QColor &newColor,
00269 const QString &newColorName)
00270 {
00271 return changeColor( findColor(oldColor), newColor, newColorName);
00272 }
00273