Back to index

scribus-ng  1.3.4.dfsg+svn20071115
cmdpage.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 "cmdpage.h"
00008 #include "cmdutil.h"
00009 #include "page.h"
00010 #include "scribuscore.h"
00011 #include "commonstrings.h"
00012 
00013 
00014 PyObject *scribus_actualpage(PyObject* /* self */)
00015 {
00016        if(!checkHaveDocument())
00017               return NULL;
00018        return PyInt_FromLong(static_cast<long>(ScCore->primaryMainWindow()->doc->currentPageNumber() + 1));
00019 }
00020 
00021 PyObject *scribus_redraw(PyObject* /* self */)
00022 {
00023        if(!checkHaveDocument())
00024               return NULL;
00025        ScCore->primaryMainWindow()->view->DrawNew();
00026  //    Py_INCREF(Py_None);
00027  //    return Py_None;
00028        Py_RETURN_NONE;
00029 }
00030 
00031 PyObject *scribus_pageposition(PyObject* /* self */, PyObject* args)
00032 {
00033        int e;
00034        if (!PyArg_ParseTuple(args, "i", &e))
00035               return NULL;
00036        if(!checkHaveDocument())
00037               return NULL;
00038        e--;
00039        if ((e < 0) || (e > static_cast<int>(ScCore->primaryMainWindow()->doc->Pages->count())-1))
00040        {
00041               PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error"));
00042               return NULL;
00043        }
00044        return PyInt_FromLong(static_cast<long>(ScCore->primaryMainWindow()->doc->locationOfPage(e)));
00045 }
00046 
00047 PyObject *scribus_savepageeps(PyObject* /* self */, PyObject* args)
00048 {
00049        char *Name;
00050        if (!PyArg_ParseTuple(args, "es", "utf-8", &Name))
00051               return NULL;
00052        if(!checkHaveDocument())
00053               return NULL;
00054        bool ret = ScCore->primaryMainWindow()->DoSaveAsEps(QString::fromUtf8(Name));
00055        if (!ret)
00056        {
00057               PyErr_SetString(ScribusException, QObject::tr("Failed to save EPS.","python error"));
00058               return NULL;
00059        }
00060 //     Py_INCREF(Py_True); // return True not None for backward compat
00061 //     return Py_True;
00062        Py_RETURN_TRUE;
00063 }
00064 
00065 PyObject *scribus_deletepage(PyObject* /* self */, PyObject* args)
00066 {
00067        int e;
00068        if (!PyArg_ParseTuple(args, "i", &e))
00069               return NULL;
00070        if(!checkHaveDocument())
00071               return NULL;
00072        e--;
00073        if ((e < 0) || (e > static_cast<int>(ScCore->primaryMainWindow()->doc->Pages->count())-1))
00074        {
00075               PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error"));
00076               return NULL;
00077        }
00078        ScCore->primaryMainWindow()->DeletePage2(e);
00079 //     Py_INCREF(Py_None);
00080 //     return Py_None;
00081        Py_RETURN_NONE;
00082 }
00083 
00084 PyObject *scribus_gotopage(PyObject* /* self */, PyObject* args)
00085 {
00086        int e;
00087        if (!PyArg_ParseTuple(args, "i", &e))
00088               return NULL;
00089        if(!checkHaveDocument())
00090               return NULL;
00091        e--;
00092        if ((e < 0) || (e > static_cast<int>(ScCore->primaryMainWindow()->doc->Pages->count())-1))
00093        {
00094               PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error"));
00095               return NULL;
00096        }
00097        ScCore->primaryMainWindow()->view->GotoPage(e);
00098 //     Py_INCREF(Py_None);
00099 //     return Py_None;
00100        Py_RETURN_NONE;
00101 }
00102 
00103 PyObject *scribus_newpage(PyObject* /* self */, PyObject* args)
00104 {
00105        int e;
00106        char *name = const_cast<char*>("");
00107        QString qName(CommonStrings::trMasterPageNormal);
00108        if (!PyArg_ParseTuple(args, "i|es", &e, "utf-8", &name))
00109               return NULL;
00110        if(!checkHaveDocument())
00111               return NULL;
00112 
00113        int loc = (e > -1) ? e : ScCore->primaryMainWindow()->doc->Pages->count();
00114        if (ScCore->primaryMainWindow()->doc->pageSets[ScCore->primaryMainWindow()->doc->currentPageLayout].Columns != 1)
00115        {
00116               switch (ScCore->primaryMainWindow()->doc->locationOfPage(loc))
00117               {
00118                      case LeftPage:
00119                             qName = CommonStrings::trMasterPageNormalLeft;
00120                             break;
00121                      case RightPage:
00122                             qName = CommonStrings::trMasterPageNormalRight;
00123                             break;
00124                      case MiddlePage:
00125                             qName = CommonStrings::trMasterPageNormalMiddle;
00126                             break;
00127               }
00128        }
00129        if (QString(name).length() != 0)
00130               qName = QString::fromUtf8(name);
00131 
00132        if (!ScCore->primaryMainWindow()->doc->MasterNames.contains(qName))
00133        {
00134               PyErr_SetString(PyExc_IndexError, QObject::tr("Given master page name does not match any existing.","python error"));
00135               return NULL;
00136        }
00137        if (e < 0)
00138               ScCore->primaryMainWindow()->slotNewPageP(loc, qName);
00139        else
00140        {
00141               e--;
00142               if ((e < 0) || (e > static_cast<int>(loc - 1)))
00143               {
00144                      PyErr_SetString(PyExc_IndexError, QObject::tr("Page number out of range.","python error"));
00145                      return NULL;
00146               }
00147               ScCore->primaryMainWindow()->slotNewPageP(e, qName);
00148        }
00149 //     Py_INCREF(Py_None);
00150  //    return Py_None;
00151        Py_RETURN_NONE;
00152 }
00153 
00154 PyObject *scribus_pagecount(PyObject* /* self */)
00155 {
00156        if(!checkHaveDocument())
00157               return NULL;
00158        return PyInt_FromLong(static_cast<long>(ScCore->primaryMainWindow()->doc->Pages->count()));
00159 }
00160 
00161 PyObject *scribus_pagedimension(PyObject* /* self */)
00162 {
00163        if(!checkHaveDocument())
00164               return NULL;
00165        PyObject *t;
00166        t = Py_BuildValue(
00167                      "(dd)",
00168                      PointToValue(ScCore->primaryMainWindow()->doc->pageWidth), // it's just view scale... * ScCore->primaryMainWindow()->doc->Scale),
00169                      PointToValue(ScCore->primaryMainWindow()->doc->pageHeight)  // * ScCore->primaryMainWindow()->doc->Scale)
00170               );
00171        return t;
00172 }
00173 
00174 PyObject *scribus_getpageitems(PyObject* /* self */)
00175 {
00176        if(!checkHaveDocument())
00177               return NULL;
00178        if (ScCore->primaryMainWindow()->doc->Items->count() == 0)
00179               return Py_BuildValue((char*)"[]");
00180        uint counter = 0;
00181        int pageNr = ScCore->primaryMainWindow()->doc->currentPageNumber();
00182        for (uint lam2 = 0; lam2 < ScCore->primaryMainWindow()->doc->Items->count(); ++lam2)
00183        {
00184               if (pageNr == ScCore->primaryMainWindow()->doc->Items->at(lam2)->OwnPage)
00185                      counter++;
00186        }
00187        PyObject *l = PyList_New(counter);
00188        PyObject *row;
00189        counter = 0;
00190        for (uint i = 0; i<ScCore->primaryMainWindow()->doc->Items->count(); ++i)
00191        {
00192               if (pageNr == ScCore->primaryMainWindow()->doc->Items->at(i)->OwnPage)
00193               {
00194                      row = Py_BuildValue((char*)"(sii)",
00195                                          ScCore->primaryMainWindow()->doc->Items->at(i)->itemName().ascii(),
00196                                          ScCore->primaryMainWindow()->doc->Items->at(i)->itemType(),
00197                                          ScCore->primaryMainWindow()->doc->Items->at(i)->ItemNr
00198                                         );
00199                      PyList_SetItem(l, counter, row);
00200                      counter++;
00201               }
00202        } // for
00203        return l;
00204 }
00205 
00206 PyObject *scribus_getHguides(PyObject* /* self */)
00207 {
00208        if(!checkHaveDocument())
00209               return NULL;
00210        Guides g = ScCore->primaryMainWindow()->doc->currentPage()->guides.horizontals(GuideManagerCore::Standard);
00211        int n = g.count();//ScCore->primaryMainWindow()->doc->currentPage->YGuides.count();
00212        if (n == 0)
00213               return Py_BuildValue((char*)"[]");
00214        int i;
00215        double tmp;
00216        PyObject *l, *guide;
00217        l = PyList_New(0);
00218        for (i=0; i<n; i++)
00219        {
00220               tmp = g[i];
00221               guide = Py_BuildValue("d", PointToValue(tmp));
00222               PyList_Append(l, guide);
00223        }
00224        return l;
00225 }
00226 
00227 PyObject *scribus_setHguides(PyObject* /* self */, PyObject* args)
00228 {
00229        PyObject *l;
00230        if (!PyArg_ParseTuple(args, "O", &l))
00231               return NULL;
00232        if(!checkHaveDocument())
00233               return NULL;
00234        if (!PyList_Check(l))
00235        {
00236               PyErr_SetString(PyExc_TypeError, QObject::tr("argument is not list: must be list of float values.","python error"));
00237               return NULL;
00238        }
00239        int i, n;
00240        n = PyList_Size(l);
00241        double guide;
00242        ScCore->primaryMainWindow()->doc->currentPage()->guides.clearHorizontals(GuideManagerCore::Standard);
00243        for (i=0; i<n; i++)
00244        {
00245               if (!PyArg_Parse(PyList_GetItem(l, i), "d", &guide))
00246               {
00247                      PyErr_SetString(PyExc_TypeError, QObject::tr("argument contains non-numeric values: must be list of float values.","python error"));
00248                      return NULL;
00249               }
00250               ScCore->primaryMainWindow()->doc->currentPage()->guides.addHorizontal(ValueToPoint(guide), GuideManagerCore::Standard);
00251        }
00252        Py_INCREF(Py_None);
00253        return Py_None;
00254        Py_RETURN_NONE;
00255 }
00256 
00257 PyObject *scribus_getVguides(PyObject* /* self */)
00258 {
00259        if(!checkHaveDocument())
00260               return NULL;
00261        Guides g = ScCore->primaryMainWindow()->doc->currentPage()->guides.verticals(GuideManagerCore::Standard);
00262        int n = g.count();//ScCore->primaryMainWindow()->doc->currentPage->XGuides.count();
00263        if (n == 0)
00264               return Py_BuildValue((char*)"[]");
00265        int i;
00266        double tmp;
00267        PyObject *l, *guide;
00268        l = PyList_New(0);
00269        for (i=0; i<n; i++)
00270        {
00271               tmp = g[i];
00272               guide = Py_BuildValue("d", PointToValue(tmp));
00273               PyList_Append(l, guide);
00274        }
00275        return l;
00276 }
00277 
00278 PyObject *scribus_setVguides(PyObject* /* self */, PyObject* args)
00279 {
00280        PyObject *l;
00281        if (!PyArg_ParseTuple(args, "O", &l))
00282               return NULL;
00283        if(!checkHaveDocument())
00284               return NULL;
00285        if (!PyList_Check(l))
00286        {
00287               PyErr_SetString(PyExc_TypeError, QObject::tr("argument is not list: must be list of float values.","python error"));
00288               return NULL;
00289        }
00290        int i, n;
00291        n = PyList_Size(l);
00292        double guide;
00293        ScCore->primaryMainWindow()->doc->currentPage()->guides.clearVerticals(GuideManagerCore::Standard);
00294        for (i=0; i<n; i++)
00295        {
00296               if (!PyArg_Parse(PyList_GetItem(l, i), "d", &guide))
00297               {
00298                      PyErr_SetString(PyExc_TypeError, QObject::tr("argument contains no-numeric values: must be list of float values.","python error"));
00299                      return NULL;
00300               }
00301               ScCore->primaryMainWindow()->doc->currentPage()->guides.addVertical(ValueToPoint(guide), GuideManagerCore::Standard);
00302        }
00303 //     Py_INCREF(Py_None);
00304 //     return Py_None;
00305        Py_RETURN_NONE;
00306 }
00307 
00308 PyObject *scribus_getpagemargins(PyObject* /* self */)
00309 {
00310        PyObject *margins = NULL;
00311        if(!checkHaveDocument())
00312               return NULL;
00313        margins = Py_BuildValue("ffff", PointToValue(ScCore->primaryMainWindow()->doc->pageMargins.Top),
00314                                                                PointToValue(ScCore->primaryMainWindow()->doc->pageMargins.Left),
00315                                                                PointToValue(ScCore->primaryMainWindow()->doc->pageMargins.Right),
00316                                                                PointToValue(ScCore->primaryMainWindow()->doc->pageMargins.Bottom));
00317        return margins;
00318 }
00319 
00323 void cmdpagedocwarnings()
00324 {
00325     QStringList s;
00326     s << scribus_newpage__doc__ << scribus_pageposition__doc__ << scribus_actualpage__doc__ << scribus_redraw__doc__ << scribus_savepageeps__doc__ << scribus_deletepage__doc__ << scribus_gotopage__doc__ << scribus_pagecount__doc__ << scribus_getHguides__doc__ <<scribus_setHguides__doc__ <<scribus_getVguides__doc__ <<scribus_setVguides__doc__ <<scribus_pagedimension__doc__ <<scribus_getpageitems__doc__ <<scribus_getpagemargins__doc__;
00327 }