kdecore Library API Documentation

kpalette.cpp

00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Waldo Bastian (bastian@kde.org) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 //----------------------------------------------------------------------------- 00020 // KDE color palette 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 // Read first line 00067 // Expected "GIMP Palette" 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 // This is a comment line 00078 line = line.mid(1); // Strip '#' 00079 line = line.stripWhiteSpace(); // Strip remaining white space.. 00080 if (!line.isEmpty()) 00081 { 00082 mDesc += line+"\n"; // Add comment to description 00083 } 00084 } 00085 else 00086 { 00087 // This is a color line, hopefully 00088 line = line.stripWhiteSpace(); 00089 if (line.isEmpty()) continue; 00090 int red, green, blue; 00091 int pos = 0; 00092 buffer[0] = '\0'; // Make string empty 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 // Make a deep copy of the color list 00117 // We can't iterate a const list :( 00118 // DF: yes you can - use the proper iterator, not first/next 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 // Need auto-save? 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 // We can't iterate a const list :( 00146 // DF: yes you can - use the proper iterator, not first/next 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 // Make a deep copy of the color list 00164 // We can't iterate a const list :( 00165 // DF: yes you can - use the proper iterator, not first/next 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 }
KDE Logo
This file is part of the documentation for kdecore Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 14 00:03:31 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003