Back to index

scribus-ng  1.3.4.dfsg+svn20071115
cmddoc.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 "cmddoc.h"
00008 #include "cmdutil.h"
00009 #include "units.h"
00010 #include "documentinformation.h"
00011 #include "scribuscore.h"
00012 
00013 /*
00014 newDocument(size, margins, orientation, firstPageNumber,
00015             unit, pagesType, firstPageOrder)*/
00016 PyObject *scribus_newdocument(PyObject* /* self */, PyObject* args)
00017 {
00018        double topMargin, bottomMargin, leftMargin, rightMargin;
00019        double pageWidth, pageHeight;
00020        int orientation, firstPageNr, unit, pagesType, facingPages, firstPageOrder, numPages;
00021 
00022        PyObject *p, *m;
00023 
00024        if ((!PyArg_ParseTuple(args, "OOiiiiii", &p, &m, &orientation,
00025                                                                              &firstPageNr, &unit,
00026                                                                              &pagesType,
00027                                                                              &firstPageOrder,
00028                                                                              &numPages)) ||
00029                                           (!PyArg_ParseTuple(p, "dd", &pageWidth, &pageHeight)) ||
00030                                           (!PyArg_ParseTuple(m, "dddd", &leftMargin, &rightMargin,
00031                                                                                     &topMargin, &bottomMargin)))
00032               return NULL;
00033        if (numPages <= 0)
00034               numPages = 1;
00035        if (pagesType == 0)
00036        {
00037               facingPages = 0;
00038               firstPageOrder = 0;
00039        }
00040        else
00041               facingPages = 1;
00042        // checking the bounds
00043        if (pagesType < firstPageOrder)
00044        {
00045               PyErr_SetString(ScribusException, QObject::tr("firstPageOrder is bigger than allowed.","python error"));
00046               return NULL;
00047        }
00048 
00049        pageWidth = value2pts(pageWidth, unit);
00050        pageHeight = value2pts(pageHeight, unit);
00051        if (orientation == 1)
00052        {
00053               double x = pageWidth;
00054               pageWidth = pageHeight;
00055               pageHeight = x;
00056        }
00057        leftMargin = value2pts(leftMargin, unit);
00058        rightMargin = value2pts(rightMargin, unit);
00059        topMargin = value2pts(topMargin, unit);
00060        bottomMargin = value2pts(bottomMargin, unit);
00061 
00062        bool ret = ScCore->primaryMainWindow()->doFileNew(pageWidth, pageHeight,
00063                                                         topMargin, leftMargin, rightMargin, bottomMargin,
00064                                                         // autoframes. It's disabled in python
00065                                                         // columnDistance, numberCols, autoframes,
00066                                                         0, 1, false,
00067                                                         pagesType, unit, firstPageOrder,
00068                                                         orientation, firstPageNr, "Custom", true, numPages);
00069        ScCore->primaryMainWindow()->doc->pageSets[pagesType].FirstPage = firstPageOrder;
00070 
00071        return PyInt_FromLong(static_cast<long>(ret));
00072 }
00073 
00074 PyObject *scribus_newdoc(PyObject* /* self */, PyObject* args)
00075 {
00076        qDebug("WARNING: newDoc() procedure is obsolete, it will be removed in a forthcoming release. Use newDocument() instead.");
00077        double b, h, lr, tpr, btr, rr, ebr;
00078        int unit, ds, fsl, fNr, ori;
00079        PyObject *p, *m;
00080        if ((!PyArg_ParseTuple(args, "OOiiiii", &p, &m, &ori, &fNr, &unit, &ds, &fsl)) ||
00081                (!PyArg_ParseTuple(p, "dd", &b, &h)) ||
00082                (!PyArg_ParseTuple(m, "dddd", &lr, &rr, &tpr, &btr)))
00083               return NULL;
00084        b = value2pts(b, unit);
00085        h = value2pts(h, unit);
00086        if (ori == 1)
00087        {
00088               ebr = b;
00089               b = h;
00090               h = ebr;
00091        }
00096        if (ds!=1 && fsl>0)
00097               fsl = 0;
00098        // end of hack
00099        tpr = value2pts(tpr, unit);
00100        lr = value2pts(lr, unit);
00101        rr = value2pts(rr, unit);
00102        btr = value2pts(btr, unit);
00103        bool ret = ScCore->primaryMainWindow()->doFileNew(b, h, tpr, lr, rr, btr, 0, 1, false, ds, unit, fsl, ori, fNr, "Custom", true);
00104        //     qApp->processEvents();
00105        return PyInt_FromLong(static_cast<long>(ret));
00106 }
00107 
00108 PyObject *scribus_setmargins(PyObject* /* self */, PyObject* args)
00109 {
00110        double lr, tpr, btr, rr;
00111        if (!PyArg_ParseTuple(args, "dddd", &lr, &rr, &tpr, &btr))
00112               return NULL;
00113        if(!checkHaveDocument())
00114               return NULL;
00115        tpr = ValueToPoint(tpr);
00116        lr = ValueToPoint(lr);
00117        rr = ValueToPoint(rr);
00118        btr = ValueToPoint(btr);
00119        ScCore->primaryMainWindow()->doc->resetPage(tpr, lr, rr, btr, ScCore->primaryMainWindow()->doc->currentPageLayout);
00120        ScCore->primaryMainWindow()->view->reformPages();
00121        ScCore->primaryMainWindow()->doc->setModified(true);
00122        ScCore->primaryMainWindow()->view->GotoPage(ScCore->primaryMainWindow()->doc->currentPageNumber());
00123        ScCore->primaryMainWindow()->view->DrawNew();
00124 //     Py_INCREF(Py_None);
00125 //     return Py_None;
00126        Py_RETURN_NONE;
00127 }
00128 
00129 PyObject *scribus_closedoc(PyObject* /* self */)
00130 {
00131        if(!checkHaveDocument())
00132               return NULL;
00133        ScCore->primaryMainWindow()->doc->setModified(false);
00134        bool ret = ScCore->primaryMainWindow()->slotFileClose();
00135        qApp->processEvents();
00136        return PyInt_FromLong(static_cast<long>(ret));
00137 }
00138 
00139 PyObject *scribus_havedoc(PyObject* /* self */)
00140 {
00141        return PyInt_FromLong(static_cast<long>(ScCore->primaryMainWindow()->HaveDoc));
00142 }
00143 
00144 PyObject *scribus_opendoc(PyObject* /* self */, PyObject* args)
00145 {
00146        char *Name;
00147        if (!PyArg_ParseTuple(args, "es", "utf-8", &Name))
00148               return NULL;
00149        bool ret = ScCore->primaryMainWindow()->loadDoc(QString::fromUtf8(Name));
00150        if (!ret)
00151        {
00152               PyErr_SetString(ScribusException, QObject::tr("Failed to open document.","python error"));
00153               return NULL;
00154        }
00155 //     Py_INCREF(Py_True); // compatibility: return true, not none, on success
00156 //     return Py_True;
00157        Py_RETURN_TRUE;
00158 }
00159 
00160 PyObject *scribus_savedoc(PyObject* /* self */)
00161 {
00162        if(!checkHaveDocument())
00163               return NULL;
00164        ScCore->primaryMainWindow()->slotFileSave();
00165 //     Py_INCREF(Py_None);
00166 //     return Py_None;
00167        Py_RETURN_NONE;
00168 }
00169 
00170 PyObject *scribus_savedocas(PyObject* /* self */, PyObject* args)
00171 {
00172        char *Name;
00173        if (!PyArg_ParseTuple(args, "es", "utf-8", &Name))
00174               return NULL;
00175        if(!checkHaveDocument())
00176               return NULL;
00177        bool ret = ScCore->primaryMainWindow()->DoFileSave(QString::fromUtf8(Name));
00178        if (!ret)
00179        {
00180               PyErr_SetString(ScribusException, QObject::tr("Failed to save document.","python error"));
00181               return NULL;
00182        }
00183 //     Py_INCREF(Py_True); // compatibility: return true, not none, on success
00184 //     return Py_True;
00185        Py_RETURN_TRUE;
00186 }
00187 
00188 PyObject *scribus_setinfo(PyObject* /* self */, PyObject* args)
00189 {
00190        char *Author;
00191        char *Title;
00192        char *Desc;
00193        // z means string, but None becomes a NULL value. QString()
00194        // will correctly handle NULL.
00195        if (!PyArg_ParseTuple(args, "zzz", &Author, &Title, &Desc))
00196               return NULL;
00197        if(!checkHaveDocument())
00198               return NULL;
00199        ScCore->primaryMainWindow()->doc->documentInfo.setAuthor(QString::fromUtf8(Author));
00200        ScCore->primaryMainWindow()->doc->documentInfo.setTitle(QString::fromUtf8(Title));
00201        ScCore->primaryMainWindow()->doc->documentInfo.setComments(QString::fromUtf8(Desc));
00202        ScCore->primaryMainWindow()->slotDocCh();
00203 //     Py_INCREF(Py_None);
00204 //     return Py_None;
00205        Py_RETURN_NONE;
00206 }
00207 
00208 PyObject *scribus_setunit(PyObject* /* self */, PyObject* args)
00209 {
00210        int e;
00211        if (!PyArg_ParseTuple(args, "i", &e))
00212               return NULL;
00213        if(!checkHaveDocument())
00214               return NULL;
00215        if ((e < 0) || (e > 3))
00216        {
00217               PyErr_SetString(PyExc_ValueError, QObject::tr("Unit out of range. Use one of the scribus.UNIT_* constants.","python error"));
00218               return NULL;
00219        }
00220        ScCore->primaryMainWindow()->slotChangeUnit(e);
00221 //     Py_INCREF(Py_None);
00222 //     return Py_None;
00223        Py_RETURN_NONE;
00224 }
00225 
00226 PyObject *scribus_getunit(PyObject* /* self */)
00227 {
00228        if(!checkHaveDocument())
00229               return NULL;
00230        return PyInt_FromLong(static_cast<long>(ScCore->primaryMainWindow()->doc->unitIndex()));
00231 }
00232 
00233 PyObject *scribus_loadstylesfromfile(PyObject* /* self */, PyObject *args)
00234 {
00235        char *fileName;
00236        if (!PyArg_ParseTuple(args, "es", "utf-8", &fileName))
00237               return NULL;
00238        if(!checkHaveDocument())
00239               return NULL;
00240        ScCore->primaryMainWindow()->doc->loadStylesFromFile(QString::fromUtf8(fileName));
00241 //     Py_INCREF(Py_None);
00242 //     return Py_None;
00243        Py_RETURN_NONE;
00244 }
00245 
00246 PyObject *scribus_setdoctype(PyObject* /* self */, PyObject* args)
00247 {
00248        int fp, fsl;
00249        if (!PyArg_ParseTuple(args, "ii", &fp, &fsl))
00250               return NULL;
00251        if(!checkHaveDocument())
00252               return NULL;
00253        if (ScCore->primaryMainWindow()->doc->currentPageLayout == fp)
00254               ScCore->primaryMainWindow()->doc->pageSets[ScCore->primaryMainWindow()->doc->currentPageLayout].FirstPage = fsl;
00255        ScCore->primaryMainWindow()->view->reformPages();
00256        ScCore->primaryMainWindow()->view->GotoPage(ScCore->primaryMainWindow()->doc->currentPageNumber()); // is this needed?
00257        ScCore->primaryMainWindow()->view->DrawNew();   // is this needed?
00258        //CB TODO ScCore->primaryMainWindow()->pagePalette->RebuildPage(); // is this needed?
00259        ScCore->primaryMainWindow()->slotDocCh();
00260 //     Py_INCREF(Py_None);
00261 //     return Py_None;
00262        Py_RETURN_NONE;
00263 }
00264 
00265 PyObject *scribus_closemasterpage(PyObject* /* self */)
00266 {
00267        if(!checkHaveDocument())
00268               return NULL;
00269        ScCore->primaryMainWindow()->view->hideMasterPage();
00270 //     Py_INCREF(Py_None);
00271 //     return Py_None;
00272        Py_RETURN_NONE;
00273 }
00274 
00275 PyObject *scribus_masterpagenames(PyObject* /* self */)
00276 {
00277        if(!checkHaveDocument())
00278               return NULL;
00279        PyObject* names = PyList_New(ScCore->primaryMainWindow()->doc->MasterPages.count());
00280        QMap<QString,int>::const_iterator it(ScCore->primaryMainWindow()->doc->MasterNames.constBegin());
00281        QMap<QString,int>::const_iterator itEnd(ScCore->primaryMainWindow()->doc->MasterNames.constEnd());
00282        int n = 0;
00283        for ( ; it != itEnd; ++it )
00284        {
00285               PyList_SET_ITEM(names, n++, PyString_FromString(it.key().utf8().data()) );
00286        }
00287        return names;
00288 }
00289 
00290 PyObject *scribus_editmasterpage(PyObject* /* self */, PyObject* args)
00291 {
00292        char* name = 0;
00293        if (!PyArg_ParseTuple(args, "es", const_cast<char*>("utf-8"), &name))
00294               return NULL;
00295        if(!checkHaveDocument())
00296               return NULL;
00297        const QString masterPageName(name);
00298        const QMap<QString,int>& masterNames(ScCore->primaryMainWindow()->doc->MasterNames);
00299        const QMap<QString,int>::const_iterator it(masterNames.find(masterPageName));
00300        if ( it == masterNames.constEnd() )
00301        {
00302               PyErr_SetString(PyExc_ValueError, "Master page not found");
00303               return NULL;
00304        }
00305        ScCore->primaryMainWindow()->view->showMasterPage(*it);
00306 //     Py_INCREF(Py_None);
00307 //     return Py_None;
00308        Py_RETURN_NONE;
00309 }
00310 
00311 PyObject* scribus_createmasterpage(PyObject* /* self */, PyObject* args)
00312 {
00313        char* name = 0;
00314        if (!PyArg_ParseTuple(args, "es", const_cast<char*>("utf-8"), &name))
00315               return NULL;
00316        if(!checkHaveDocument())
00317               return NULL;
00318        const QString masterPageName(name);
00319        if (ScCore->primaryMainWindow()->doc->MasterNames.contains(masterPageName))
00320        {
00321               PyErr_SetString(PyExc_ValueError, "Master page already exists");
00322               return NULL;
00323        }
00324        ScCore->primaryMainWindow()->doc->addMasterPage(ScCore->primaryMainWindow()->doc->MasterPages.count(), masterPageName);
00325 //     Py_INCREF(Py_None);
00326 //     return Py_None;
00327        Py_RETURN_NONE;
00328 }
00329 
00330 PyObject* scribus_deletemasterpage(PyObject* /* self */, PyObject* args)
00331 {
00332        char* name = 0;
00333        if (!PyArg_ParseTuple(args, "es", const_cast<char*>("utf-8"), &name))
00334               return NULL;
00335        if(!checkHaveDocument())
00336               return NULL;
00337        const QString masterPageName(name);
00338        if (!ScCore->primaryMainWindow()->doc->MasterNames.contains(masterPageName))
00339        {
00340               PyErr_SetString(PyExc_ValueError, "Master page does not exist");
00341               return NULL;
00342        }
00343        if (masterPageName == "Normal")
00344        {
00345               PyErr_SetString(PyExc_ValueError, "Can not delete the Normal master page");
00346               return NULL;
00347        }
00348        bool oldMode = ScCore->primaryMainWindow()->doc->masterPageMode();
00349        ScCore->primaryMainWindow()->doc->setMasterPageMode(true);
00350        ScCore->primaryMainWindow()->DeletePage2(ScCore->primaryMainWindow()->doc->MasterNames[masterPageName]);
00351        ScCore->primaryMainWindow()->doc->setMasterPageMode(oldMode);
00352 //     Py_INCREF(Py_None);
00353 //     return Py_None;
00354        Py_RETURN_NONE;
00355 }
00356 
00360 void cmddocdocwarnings()
00361 {
00362     QStringList s;
00363     s << scribus_newdocument__doc__ << scribus_newdoc__doc__ <<  scribus_closedoc__doc__ << scribus_havedoc__doc__ << scribus_opendoc__doc__ << scribus_savedoc__doc__ << scribus_savedocas__doc__ << scribus_setinfo__doc__ <<scribus_setmargins__doc__ <<scribus_setunit__doc__ <<scribus_getunit__doc__ <<scribus_loadstylesfromfile__doc__ <<scribus_setdoctype__doc__ <<scribus_closemasterpage__doc__ <<scribus_masterpagenames__doc__ <<scribus_editmasterpage__doc__ <<scribus_createmasterpage__doc__ <<scribus_deletemasterpage__doc__;
00364 }