Back to index

scribus-ng  1.3.4.dfsg+svn20071115
colorsetmanager.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008        copyright            : (C) 2006 by Craig Bradney
00009        email                : cbradney@zip.com.au
00010 ***************************************************************************/
00011 
00012 #include "colorsetmanager.h"
00013 #include "scpaths.h"
00014 #include "util.h"
00015 #include "commonstrings.h"
00016 #include <qdom.h>
00017 
00018 ColorSetManager::ColorSetManager()
00019 {
00020 }
00021 
00022 ColorSetManager::~ColorSetManager()
00023 {
00024 }
00025 
00026 void ColorSetManager::initialiseDefaultPrefs(struct ApplicationPrefs& appPrefs)
00027 {
00028        QString pfadC = ScPaths::instance().libDir()+"swatches/";
00029        QString pfadC2 = pfadC + "Scribus_X11.txt";
00030        QFile fiC(pfadC2);
00031        if (!fiC.exists())
00032        {
00033               appPrefs.DColors.insert("White", ScColor(0, 0, 0, 0));
00034               appPrefs.DColors.insert("Black", ScColor(0, 0, 0, 255));
00035               appPrefs.DColors.insert("Blue", ScColor(255, 255, 0, 0));
00036               appPrefs.DColors.insert("Cyan", ScColor(255, 0, 0, 0));
00037               appPrefs.DColors.insert("Green", ScColor(255, 0, 255, 0));
00038               appPrefs.DColors.insert("Red", ScColor(0, 255, 255, 0));
00039               appPrefs.DColors.insert("Yellow", ScColor(0, 0, 255, 0));
00040               appPrefs.DColors.insert("Magenta", ScColor(0, 255, 0, 0));
00041               appPrefs.DColorSet = "Scribus-Small";
00042        }
00043        else
00044        {
00045               if (fiC.open(IO_ReadOnly))
00046               {
00047                      QString ColorEn, Cname;
00048                      int Rval, Gval, Bval;
00049                      QTextStream tsC(&fiC);
00050                      ColorEn = tsC.readLine();
00051                      if (ColorEn.startsWith("<?xml version="))
00052                      {
00053                             QCString docBytes("");
00054                             loadRawText(pfadC2, docBytes);
00055                             QString docText("");
00056                             docText = QString::fromUtf8(docBytes);
00057                             QDomDocument docu("scridoc");
00058                             docu.setContent(docText);
00059                             ScColor lf = ScColor();
00060                             QDomElement elem = docu.documentElement();
00061                             QDomNode PAGE = elem.firstChild();
00062                             while(!PAGE.isNull())
00063                             {
00064                                    QDomElement pg = PAGE.toElement();
00065                                    if(pg.tagName()=="COLOR" && pg.attribute("NAME")!=CommonStrings::None)
00066                                    {
00067                                           if (pg.hasAttribute("CMYK"))
00068                                                  lf.setNamedColor(pg.attribute("CMYK"));
00069                                           else
00070                                                  lf.fromQColor(QColor(pg.attribute("RGB")));
00071                                           if (pg.hasAttribute("Spot"))
00072                                                  lf.setSpotColor(static_cast<bool>(pg.attribute("Spot").toInt()));
00073                                           else
00074                                                  lf.setSpotColor(false);
00075                                           if (pg.hasAttribute("Register"))
00076                                                  lf.setRegistrationColor(static_cast<bool>(pg.attribute("Register").toInt()));
00077                                           else
00078                                                  lf.setRegistrationColor(false);
00079                                           appPrefs.DColors.insert(pg.attribute("NAME"), lf);
00080                                    }
00081                                    PAGE=PAGE.nextSibling();
00082                             }
00083                      }
00084                      else
00085                      {
00086                             while (!tsC.atEnd())
00087                             {
00088                                    ColorEn = tsC.readLine();
00089                                    QTextStream CoE(&ColorEn, IO_ReadOnly);
00090                                    CoE >> Rval;
00091                                    CoE >> Gval;
00092                                    CoE >> Bval;
00093                                    CoE >> Cname;
00094                                    ScColor tmp;
00095                                    tmp.setColorRGB(Rval, Gval, Bval);
00096                                    appPrefs.DColors.insert(Cname, tmp);
00097                             }
00098                      }
00099                      fiC.close();
00100               }
00101               appPrefs.DColorSet = "X11 RGB-Set";
00102        }
00103 }
00104 
00105 void ColorSetManager::findPaletteLocations()
00106 {
00107        paletteLocations.clear();
00108        QStringList locations=ScPaths::instance().getSystemCreateSwatchesDirs();
00109        locations << ScPaths::instance().libDir()+"swatches/";
00110        for ( QStringList::Iterator it = locations.begin(); it != locations.end(); ++it )
00111        {
00112               QFile createDir(*it);
00113               if (createDir.exists())
00114                      paletteLocations << (*it);
00115     }
00116 }
00117 
00118 void ColorSetManager::findPalettes()
00119 {
00120        palettes.clear();
00121        QString path;
00122        for ( QStringList::Iterator it = paletteLocations.begin(); it != paletteLocations.end(); ++it )
00123        {
00124               path=(*it);
00125               QDir dir(path , "*", QDir::Name, QDir::Files | QDir::NoSymLinks);
00126               if (dir.exists() && (dir.count() != 0))
00127                      for (uint i = 0; i < dir.count(); ++i) 
00128                      {
00129                             QFileInfo file(path + dir[i]);
00130                             QString setName=file.baseName();
00131                             setName.replace("_", " ");
00132                             palettes.insert(setName, file.absFilePath());
00133                      }
00134        }
00135 }
00136 
00137 QStringList ColorSetManager::paletteNames( )
00138 {
00139        QStringList nameList;
00140        for ( QMap<QString, QString>::Iterator it = palettes.begin(); it != palettes.end(); ++it )
00141               nameList << it.key();
00142        return nameList;
00143 }
00144 
00145 const QString& ColorSetManager::paletteFileFromName(const QString& paletteName)
00146 {
00147        if (palettes.contains(paletteName))
00148               return palettes[paletteName];
00149        return QString::null;
00150 }