00001
00002
00003
00004
00005
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* )
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().toUtf8()));
00025 cc++;
00026 }
00027 return l;
00028 }
00029
00030 PyObject *scribus_getcolor(PyObject* , 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").toLocal8Bit().constData());
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").toLocal8Bit().constData());
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* , 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").toLocal8Bit().constData());
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").toLocal8Bit().constData());
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* , 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").toLocal8Bit().constData());
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").toLocal8Bit().constData());
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").toLocal8Bit().constData());
00106 return NULL;
00107 }
00108 (*colorList)[col].setColor(c, m, y, k);
00109 }
00110
00111
00112 Py_RETURN_NONE;
00113 }
00114
00115 PyObject *scribus_newcolor(PyObject* , 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").toLocal8Bit().constData());
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
00133
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
00143
00144 (*colorList)[col].setColor(c, m, y, k);
00145 }
00146
00147
00148 Py_RETURN_NONE;
00149 }
00150
00151 PyObject *scribus_delcolor(PyObject* , PyObject* args)
00152 {
00153 char *Name = const_cast<char*>("");
00154 char *Repl = const_cast<char*>(CommonStrings::None.toLatin1().constData());
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").toLocal8Bit().constData());
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").toLocal8Bit().constData());
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").toLocal8Bit().constData());
00185 return NULL;
00186 }
00187 }
00188
00189
00190 Py_RETURN_NONE;
00191 }
00192
00193 PyObject *scribus_replcolor(PyObject* , PyObject* args)
00194 {
00195 char *Name = const_cast<char*>("");
00196 char *Repl = const_cast<char*>(CommonStrings::None.toLatin1().constData());
00197
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").toLocal8Bit().constData());
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").toLocal8Bit().constData());
00214 return NULL;
00215 }
00216
00217
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 }