Back to index

scribus-ng  1.3.4.dfsg+svn20071115
cmdcolor.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 #include "cmdcolor.h"
00008 #include "cmdutil.h"
00009 #include "prefsmanager.h"
00010 #include "commonstrings.h"
00011 #include "scribuscore.h"
00012 #include "sccolorengine.h"
00013 
00014 PyObject *scribus_colornames(PyObject* /* self */)
00015 {
00016        ColorList edc;
00017        PyObject *l;
00018        int cc = 0;
00019        edc = ScCore->primaryMainWindow()->HaveDoc ? ScCore->primaryMainWindow()->doc->PageColors : PrefsManager::instance()->colorSet();
00020        ColorList::Iterator it;
00021        l = PyList_New(edc.count());
00022        for (it = edc.begin(); it != edc.end(); ++it)
00023        {
00024               PyList_SetItem(l, cc, PyString_FromString(it.key().utf8()));
00025               cc++;
00026        }
00027        return l;
00028 }
00029 
00030 PyObject *scribus_getcolor(PyObject* /* self */, PyObject* args)
00031 {
00032        ColorList edc;
00033        char *Name = const_cast<char*>("");
00034        int c, m, y, k;
00035        if (!PyArg_ParseTuple(args, "es", "utf-8", &Name))
00036               return NULL;
00037        if (strcmp(Name, "") == 0)
00038        {
00039               PyErr_SetString(PyExc_ValueError, QObject::tr("Cannot get a color with an empty name.","python error"));
00040               return NULL;
00041        }
00042        edc = ScCore->primaryMainWindow()->HaveDoc ? ScCore->primaryMainWindow()->doc->PageColors : PrefsManager::instance()->colorSet();
00043        ScribusDoc* currentDoc = ScCore->primaryMainWindow()->HaveDoc ? ScCore->primaryMainWindow()->doc : NULL;
00044        QString col = QString::fromUtf8(Name);
00045        if (!edc.contains(col))
00046        {
00047               PyErr_SetString(NotFoundError, QObject::tr("Color not found.","python error"));
00048               return NULL;
00049        }
00050        CMYKColor cmykValues;
00051        ScColorEngine::getCMYKValues(edc[col], currentDoc, cmykValues);
00052        cmykValues.getValues(c, m, y, k);
00053        return Py_BuildValue("(iiii)", static_cast<long>(c), static_cast<long>(m), static_cast<long>(y), static_cast<long>(k));
00054 }
00055 
00056 PyObject *scribus_getcolorasrgb(PyObject* /* self */, PyObject* args)
00057 {
00058        ColorList edc;
00059        char *Name = const_cast<char*>("");
00060        if (!PyArg_ParseTuple(args, "es", "utf-8", &Name))
00061               return NULL;
00062        if (strcmp(Name, "") == 0)
00063        {
00064               PyErr_SetString(PyExc_ValueError, QObject::tr("Cannot get a color with an empty name.","python error"));
00065               return NULL;
00066        }
00067        edc = ScCore->primaryMainWindow()->HaveDoc ? ScCore->primaryMainWindow()->doc->PageColors : PrefsManager::instance()->colorSet();
00068        ScribusDoc* currentDoc = ScCore->primaryMainWindow()->HaveDoc ? ScCore->primaryMainWindow()->doc : NULL;
00069        QString col = QString::fromUtf8(Name);
00070        if (!edc.contains(col))
00071        {
00072               PyErr_SetString(NotFoundError, QObject::tr("Color not found.","python error"));
00073               return NULL;
00074        }
00075        QColor rgb = ScColorEngine::getRGBColor(edc[col], currentDoc);
00076        return Py_BuildValue("(iii)", static_cast<long>(rgb.red()), static_cast<long>(rgb.green()), static_cast<long>(rgb.blue()));
00077 }
00078 
00079 PyObject *scribus_setcolor(PyObject* /* self */, PyObject* args)
00080 {
00081        char *Name = const_cast<char*>("");
00082        int c, m, y, k;
00083        if (!PyArg_ParseTuple(args, "esiiii", "utf-8", &Name, &c, &m, &y, &k))
00084               return NULL;
00085        if (strcmp(Name, "") == 0)
00086        {
00087               PyErr_SetString(PyExc_ValueError, QObject::tr("Cannot change a color with an empty name.","python error"));
00088               return NULL;
00089        }
00090        QString col = QString::fromUtf8(Name);
00091        if (ScCore->primaryMainWindow()->HaveDoc)
00092        {
00093               if (!ScCore->primaryMainWindow()->doc->PageColors.contains(col))
00094               {
00095                      PyErr_SetString(NotFoundError, QObject::tr("Color not found in document.","python error"));
00096                      return NULL;
00097               }
00098               ScCore->primaryMainWindow()->doc->PageColors[col].setColor(c, m, y, k);
00099        }
00100        else
00101        {
00102               ColorList* colorList=PrefsManager::instance()->colorSetPtr();
00103               if (!colorList->contains(col))
00104               {
00105                      PyErr_SetString(NotFoundError, QObject::tr("Color not found in default colors.","python error"));
00106                      return NULL;
00107               }
00108               (*colorList)[col].setColor(c, m, y, k);
00109        }
00110 //     Py_INCREF(Py_None);
00111 //     return Py_None;
00112        Py_RETURN_NONE;
00113 }
00114 
00115 PyObject *scribus_newcolor(PyObject* /* self */, PyObject* args)
00116 {
00117        char *Name = const_cast<char*>("");
00118        int c, m, y, k;
00119        if (!PyArg_ParseTuple(args, "esiiii", "utf-8", &Name, &c, &m, &y, &k))
00120               return NULL;
00121        if (strcmp(Name, "") == 0)
00122        {
00123               PyErr_SetString(PyExc_ValueError, QObject::tr("Cannot create a color with an empty name.","python error"));
00124               return NULL;
00125        }
00126        QString col = QString::fromUtf8(Name);
00127        if (ScCore->primaryMainWindow()->HaveDoc)
00128               {
00129                      if (!ScCore->primaryMainWindow()->doc->PageColors.contains(col))
00130                             ScCore->primaryMainWindow()->doc->PageColors.insert(col, ScColor(c, m, y, k));
00131                      else
00132                             // FIXME: Given that we have a changeColour function, should we really be
00133                             // silently changing colours in newColour?
00134                             ScCore->primaryMainWindow()->doc->PageColors[col].setColor(c, m, y, k);
00135               }
00136        else
00137               {
00138                      ColorList* colorList=PrefsManager::instance()->colorSetPtr();
00139                      if (!colorList->contains(col))
00140                             colorList->insert(col, ScColor(c, m, y, k));
00141                      else
00142                             // FIXME: Given that we have a changeColour function, should we really be
00143                             // silently changing colours in newColour?
00144                             (*colorList)[col].setColor(c, m, y, k);
00145               }
00146  //    Py_INCREF(Py_None);
00147  //    return Py_None;
00148        Py_RETURN_NONE;
00149 }
00150 
00151 PyObject *scribus_delcolor(PyObject* /* self */, PyObject* args)
00152 {
00153        char *Name = const_cast<char*>("");
00154        char *Repl = const_cast<char*>(CommonStrings::None.latin1());
00155        if (!PyArg_ParseTuple(args, "es|es", "utf-8", &Name, "utf-8", &Repl))
00156               return NULL;
00157        if (strcmp(Name, "") == 0)
00158        {
00159               PyErr_SetString(PyExc_ValueError, QObject::tr("Cannot delete a color with an empty name.","python error"));
00160               return NULL;
00161        }
00162        QString col = QString::fromUtf8(Name);
00163        QString rep = QString::fromUtf8(Repl);
00164        if (ScCore->primaryMainWindow()->HaveDoc)
00165        {
00166               if (ScCore->primaryMainWindow()->doc->PageColors.contains(col) && (ScCore->primaryMainWindow()->doc->PageColors.contains(rep) || (rep == CommonStrings::None)))
00167                      {
00168                             ScCore->primaryMainWindow()->doc->PageColors.remove(col);
00169                             ReplaceColor(col, rep);
00170                      }
00171               else
00172               {
00173                      PyErr_SetString(NotFoundError, QObject::tr("Color not found in document.","python error"));
00174                      return NULL;
00175               }
00176        }
00177        else
00178        {
00179               ColorList* colorList=PrefsManager::instance()->colorSetPtr();
00180               if (colorList->contains(col))
00181                      colorList->remove(col);
00182               else
00183               {
00184                      PyErr_SetString(NotFoundError, QObject::tr("Color not found in default colors.","python error"));
00185                      return NULL;
00186               }
00187        }
00188 //     Py_INCREF(Py_None);
00189 //     return Py_None;
00190        Py_RETURN_NONE;
00191 }
00192 
00193 PyObject *scribus_replcolor(PyObject* /* self */, PyObject* args)
00194 {
00195        char *Name = const_cast<char*>("");
00196        char *Repl = const_cast<char*>(CommonStrings::None.latin1());
00197        //FIXME: this should definitely use keyword arguments
00198        if (!PyArg_ParseTuple(args, "es|es", "utf-8", &Name, "utf-8", &Repl))
00199               return NULL;
00200        if(!checkHaveDocument())
00201               return NULL;
00202        if (strcmp(Name, "") == 0)
00203        {
00204               PyErr_SetString(PyExc_ValueError, QObject::tr("Cannot replace a color with an empty name.","python error"));
00205               return NULL;
00206        }
00207        QString col = QString::fromUtf8(Name);
00208        QString rep = QString::fromUtf8(Repl);
00209        if (ScCore->primaryMainWindow()->doc->PageColors.contains(col) && (ScCore->primaryMainWindow()->doc->PageColors.contains(rep) || (rep == CommonStrings::None)))
00210               ReplaceColor(col, rep);
00211        else
00212        {
00213               PyErr_SetString(NotFoundError, QObject::tr("Color not found.","python error"));
00214               return NULL;
00215        }
00216 //     Py_INCREF(Py_None);
00217 //     return Py_None;
00218        Py_RETURN_NONE;
00219 }
00220 
00224 void cmdcolordocswarnings()
00225 {
00226     QStringList s;
00227     s << scribus_colornames__doc__ << scribus_getcolor__doc__ << scribus_getcolorasrgb__doc__;
00228     s << scribus_setcolor__doc__ << scribus_newcolor__doc__ << scribus_delcolor__doc__;
00229     s << scribus_replcolor__doc__;
00230 }