• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KDECore

kpalette.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, 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   while( paletteFile.readLine(line, maxLength) != -1)
00072   {
00073      if (line[0] == '#') 
00074      {
00075         // This is a comment line
00076         line = line.mid(1); // Strip '#' 
00077         line = line.stripWhiteSpace(); // Strip remaining white space..
00078         if (!line.isEmpty())
00079         {
00080            mDesc += line+"\n"; // Add comment to description
00081         }
00082      }
00083      else
00084      {
00085         // This is a color line, hopefully
00086         line = line.stripWhiteSpace();
00087         if (line.isEmpty()) continue;
00088         int red, green, blue;
00089         int pos = 0;
00090         if (sscanf(line.ascii(), "%d %d %d%n", &red, &green, &blue, &pos) >= 3)
00091         {
00092            if (red > 255) red = 255;
00093            if (red < 0) red = 0;    
00094            if (green > 255) green = 255;
00095            if (green < 0) green = 0;    
00096            if (blue > 255) blue = 255;
00097            if (blue < 0) blue = 0;  
00098            kolor *node = new kolor();
00099            node->color.setRgb(red, green, blue);
00100            node->name = line.mid(pos).stripWhiteSpace();
00101            if (node->name.isNull()) node->name = "";
00102            mKolorList.append( node );
00103         }
00104      }
00105   }
00106 }
00107 
00108 KPalette::KPalette(const KPalette &p)
00109  : mName(p.mName), mDesc(p.mDesc), mEditable(p.mEditable)
00110 {
00111    mKolorList.setAutoDelete(true);
00112    // Make a deep copy of the color list
00113    // We can't iterate a const list :(
00114    // DF: yes you can - use the proper iterator, not first/next
00115    QPtrList<kolor> *nonConstList = (QPtrList<kolor> *) &p.mKolorList;
00116    for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
00117    {
00118        mKolorList.append(new kolor(*node));
00119    }
00120 }
00121 
00122 KPalette::~KPalette()
00123 {
00124   // Need auto-save?
00125 }
00126 
00127 bool
00128 KPalette::save()
00129 {
00130    QString filename = locateLocal("config", "colors/"+mName);
00131    KSaveFile sf(filename);
00132    if (sf.status() != 0) return false;
00133 
00134    QTextStream *str = sf.textStream();
00135 
00136    QString description = mDesc.stripWhiteSpace();
00137    description = "#"+QStringList::split("\n", description, true).join("\n#");
00138 
00139    (*str) << "KDE RGB Palette\n";   
00140    (*str) << description << "\n";
00141    // We can't iterate a const list :(
00142    // DF: yes you can - use the proper iterator, not first/next
00143    QPtrList<kolor> *nonConstList = (QPtrList<kolor> *) (&mKolorList);
00144    for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
00145    {
00146        int r,g,b;
00147        node->color.rgb(&r, &g, &b);
00148        (*str) << r << " " << g << " " << b << " " << node->name << "\n";
00149    }
00150    return sf.close();
00151 }
00152 
00153 
00154 KPalette&
00155 KPalette::operator=( const KPalette &p)
00156 {
00157   if (&p == this) return *this;
00158   mKolorList.clear();
00159   // Make a deep copy of the color list
00160   // We can't iterate a const list :(
00161    // DF: yes you can - use the proper iterator, not first/next
00162   QPtrList<kolor> *nonConstList = (QPtrList<kolor> *) &p.mKolorList;
00163   for(kolor *node = nonConstList->first(); node; node = nonConstList->next())
00164   {
00165      mKolorList.append(new kolor(*node));
00166   }
00167   mName = p.mName;
00168   mDesc = p.mDesc;
00169   mEditable = p.mEditable; 
00170   return *this;
00171 }
00172 
00173 QColor
00174 KPalette::color(int index) 
00175 {
00176   if ((index < 0) || (index >= nrColors()))
00177     return QColor();
00178 
00179   kolor *node = mKolorList.at(index);
00180   if (!node)
00181     return QColor();
00182 
00183   return node->color;
00184 }
00185 
00186 int
00187 KPalette::findColor(const QColor &color) const
00188 {
00189   int index;
00190   QPtrListIterator<kolor> it( mKolorList );
00191   for (index = 0; it.current(); ++it, ++index)
00192   {
00193      if (it.current()->color == color)
00194          return index;
00195   }
00196   return -1;
00197 }
00198 
00199 QString
00200 KPalette::colorName(int index) 
00201 {
00202   if ((index < 0) || (index >= nrColors()))
00203     return QString::null;
00204 
00205   kolor *node = mKolorList.at(index);
00206   if (!node)
00207     return QString::null;
00208 
00209   return node->name;
00210 }
00211 
00212 int
00213 KPalette::addColor(const QColor &newColor, const QString &newColorName)
00214 {
00215   kolor *node = new kolor();
00216   node->color = newColor;
00217   node->name = newColorName;
00218   mKolorList.append( node );
00219   return nrColors()-1;
00220 }
00221 
00222 int
00223 KPalette::changeColor(int index, 
00224                       const QColor &newColor, 
00225                       const QString &newColorName)
00226 {
00227   if ((index < 0) || (index >= nrColors()))
00228     return -1;
00229 
00230   kolor *node = mKolorList.at(index);
00231   if (!node)
00232     return -1;
00233 
00234   node->color = newColor;
00235   node->name = newColorName;
00236   return index;
00237 }

KDECore

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal