Back to index

scribus-ng  1.3.4.dfsg+svn20071115
objimageexport.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 "objimageexport.h"
00008 #include "cmdutil.h"
00009 #include "page.h"
00010 
00011 #include <structmember.h>
00012 #include <qfileinfo.h>
00013 #include <vector>
00014 #include "scribuscore.h"
00015 
00016 typedef struct
00017 {
00018        PyObject_HEAD
00019        PyObject *name; // string - filename
00020        PyObject *type; // string - image type (PNG, JPEG etc.)
00021        PyObject *allTypes; // list - available types
00022        int dpi; // DPI of the bitmap
00023        int scale; // how is bitmap scaled 100 = 100%
00024        int quality; // quality/compression <1; 100>
00025 } ImageExport;
00026 
00027 static void ImageExport_dealloc(ImageExport* self)
00028 {
00029        Py_XDECREF(self->name);
00030        Py_XDECREF(self->type);
00031        Py_XDECREF(self->allTypes);
00032        self->ob_type->tp_free((PyObject *)self);
00033 }
00034 
00035 static PyObject * ImageExport_new(PyTypeObject *type, PyObject */*args*/, PyObject */*kwds*/)
00036 {
00037        if(!checkHaveDocument())
00038               return NULL;
00039 
00040        ImageExport *self;
00041        self = (ImageExport *)type->tp_alloc(type, 0);
00042        if (self != NULL) {
00043               self->name = PyString_FromString("ImageExport.png");
00044               self->type = PyString_FromString("PNG");
00045               self->allTypes = PyList_New(0);
00046               self->dpi = 72;
00047               self->scale = 100;
00048               self->quality = 100;
00049        }
00050        return (PyObject *) self;
00051 }
00052 
00053 static int ImageExport_init(ImageExport */*self*/, PyObject */*args*/, PyObject */*kwds*/)
00054 {
00055        return 0;
00056 }
00057 
00058 static PyMemberDef ImageExport_members[] = {
00059        {const_cast<char*>("dpi"), T_INT, offsetof(ImageExport, dpi), 0, imgexp_dpi__doc__},
00060        {const_cast<char*>("scale"), T_INT, offsetof(ImageExport, scale), 0, imgexp_scale__doc__},
00061        {const_cast<char*>("quality"), T_INT, offsetof(ImageExport, quality), 0, imgexp_quality__doc__},
00062        {NULL, 0, 0, 0, NULL} // sentinel
00063 };
00064 
00065 static PyObject *ImageExport_getName(ImageExport *self, void */*closure*/)
00066 {
00067        Py_INCREF(self->name);
00068        return self->name;
00069 }
00070 
00071 static int ImageExport_setName(ImageExport *self, PyObject *value, void */*closure*/)
00072 {
00073        if (!PyString_Check(value)) {
00074               PyErr_SetString(PyExc_TypeError, QObject::tr("The filename must be a string.", "python error"));
00075               return -1;
00076        }
00077        if (PyString_Size(value) < 1)
00078        {
00079               PyErr_SetString(PyExc_TypeError, QObject::tr("The filename should not be empty string.", "python error"));
00080               return -1;
00081        }
00082        Py_DECREF(self->name);
00083        Py_INCREF(value);
00084        self->name = value;
00085        return 0;
00086 }
00087 
00088 static PyObject *ImageExport_getType(ImageExport *self, void */*closure*/)
00089 {
00090        Py_INCREF(self->type);
00091        return self->type;
00092 }
00093 
00094 static int ImageExport_setType(ImageExport *self, PyObject *value, void */*closure*/)
00095 {
00096        if (value == NULL) {
00097               PyErr_SetString(PyExc_TypeError, QObject::tr("Cannot delete image type settings.", "python error"));
00098               return -1;
00099        }
00100        if (!PyString_Check(value)) {
00101               PyErr_SetString(PyExc_TypeError, QObject::tr("The image type must be a string.", "python error"));
00102               return -1;
00103        }
00104        Py_DECREF(self->type);
00105        Py_INCREF(value);
00106        self->type = value;
00107        return 0;
00108 }
00109 
00110 static PyObject *ImageExport_getAllTypes(ImageExport */*self*/, void */*closure*/)
00111 {
00112        PyObject *l;
00113        int pos = 0;
00114        QStringList list = QImage::outputFormatList();
00115        l = PyList_New(list.count());
00116        for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
00117        {
00118               PyList_SetItem(l, pos, PyString_FromString((*it).latin1()));
00119               ++pos;
00120        }
00121        return l;
00122 }
00123 
00124 static int ImageExport_setAllTypes(ImageExport */*self*/, PyObject */*value*/, void */*closure*/)
00125 {
00126        PyErr_SetString(PyExc_ValueError, QObject::tr("'allTypes' attribute is READ-ONLY", "python error"));
00127        return -1;
00128 }
00129 
00130 static PyGetSetDef ImageExport_getseters [] = {
00131        {const_cast<char*>("name"), (getter)ImageExport_getName, (setter)ImageExport_setName, imgexp_filename__doc__, NULL},
00132        {const_cast<char*>("type"), (getter)ImageExport_getType, (setter)ImageExport_setType, imgexp_type__doc__, NULL},
00133        {const_cast<char*>("allTypes"), (getter)ImageExport_getAllTypes, (setter)ImageExport_setAllTypes, imgexp_alltypes__doc__, NULL},
00134        {NULL, NULL, NULL, NULL, NULL}  // sentinel
00135 };
00136 
00137 static PyObject *ImageExport_save(ImageExport *self)
00138 {
00139        if(!checkHaveDocument())
00140               return NULL;
00141 
00142        /* a little magic here - I need to compute the "maxGr" value...
00143        * We need to know the right size of the page for landscape,
00144        * portrait and user defined sizes.
00145        */
00146        double pixmapSize;
00147        (ScCore->primaryMainWindow()->doc->pageHeight > ScCore->primaryMainWindow()->doc->pageWidth)
00148                      ? pixmapSize = ScCore->primaryMainWindow()->doc->pageHeight
00149                      : pixmapSize = ScCore->primaryMainWindow()->doc->pageWidth;
00150        QImage im = ScCore->primaryMainWindow()->view->PageToPixmap(ScCore->primaryMainWindow()->doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), false);
00151        int dpi = qRound(100.0 / 2.54 * self->dpi);
00152        im.setDotsPerMeterY(dpi);
00153        im.setDotsPerMeterX(dpi);
00154        if (!im.save(PyString_AsString(self->name), PyString_AsString(self->type)))
00155        {
00156               PyErr_SetString(ScribusException, QObject::tr("Failed to export image", "python error"));
00157               return NULL;
00158        }
00159 //     Py_INCREF(Py_True); // return True not None for backward compat
00160  //    return Py_True;
00161        Py_RETURN_TRUE;
00162 }
00163 
00164 static PyObject *ImageExport_saveAs(ImageExport *self, PyObject *args)
00165 {
00166        char* value;
00167        if(!checkHaveDocument())
00168               return NULL;
00169        if (!PyArg_ParseTuple(args, const_cast<char*>("es"), "utf-8", &value))
00170               return NULL;
00171 
00172        /* a little magic here - I need to compute the "maxGr" value...
00173        * We need to know the right size of the page for landscape,
00174        * portrait and user defined sizes.
00175        */
00176        double pixmapSize;
00177        (ScCore->primaryMainWindow()->doc->pageHeight > ScCore->primaryMainWindow()->doc->pageWidth)
00178                      ? pixmapSize = ScCore->primaryMainWindow()->doc->pageHeight
00179                      : pixmapSize = ScCore->primaryMainWindow()->doc->pageWidth;
00180        QImage im = ScCore->primaryMainWindow()->view->PageToPixmap(ScCore->primaryMainWindow()->doc->currentPage()->pageNr(), qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0), false);
00181        int dpi = qRound(100.0 / 2.54 * self->dpi);
00182        im.setDotsPerMeterY(dpi);
00183        im.setDotsPerMeterX(dpi);
00184        if (!im.save(value, PyString_AsString(self->type)))
00185        {
00186               PyErr_SetString(ScribusException, QObject::tr("Failed to export image", "python error"));
00187               return NULL;
00188        }
00189 //     Py_INCREF(Py_True); // return True not None for backward compat
00190  //    return Py_True;
00191        Py_RETURN_TRUE;
00192 }
00193 
00194 static PyMethodDef ImageExport_methods[] = {
00195        {const_cast<char*>("save"), (PyCFunction)ImageExport_save, METH_NOARGS, imgexp_save__doc__},
00196        {const_cast<char*>("saveAs"), (PyCFunction)ImageExport_saveAs, METH_VARARGS, imgexp_saveas__doc__},
00197        {NULL, (PyCFunction)(0), 0, NULL} // sentinel
00198 };
00199 
00200 PyTypeObject ImageExport_Type = {
00201        PyObject_HEAD_INIT(NULL)   // PyObject_VAR_HEAD
00202        0,
00203        const_cast<char*>("ImageExport"), // char *tp_name; /* For printing, in format "<module>.<name>" */
00204        sizeof(ImageExport),   // int tp_basicsize, /* For allocation */
00205        0,  // int tp_itemsize; /* For allocation */
00206        (destructor) ImageExport_dealloc, //       destructor tp_dealloc;
00207        0, //   printfunc tp_print;
00208        0, //   getattrfunc tp_getattr;
00209        0, //   setattrfunc tp_setattr;
00210        0, //   cmpfunc tp_compare;
00211        0, //   reprfunc tp_repr;
00212        0, //   PyNumberMethods *tp_as_number;
00213        0, //   PySequenceMethods *tp_as_sequence;
00214        0, //   PyMappingMethods *tp_as_mapping;
00215        0, //   hashfunc tp_hash;
00216        0, //   ternaryfunc tp_call;
00217        0, //   reprfunc tp_str;
00218        0, //   getattrofunc tp_getattro;
00219        0, //   setattrofunc tp_setattro;
00220        0, //   PyBufferProcs *tp_as_buffer;
00221        Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,   // long tp_flags;
00222        imgexp__doc__, // char *tp_doc; /* Documentation string */
00223        0, //   traverseproc tp_traverse;
00224        0, //   inquiry tp_clear;
00225        0, //   richcmpfunc tp_richcompare;
00226        0, //   long tp_weaklistoffset;
00227        0, //   getiterfunc tp_iter;
00228        0, //   iternextfunc tp_iternext;
00229        ImageExport_methods, //      struct PyMethodDef *tp_methods;
00230        ImageExport_members, //      struct PyMemberDef *tp_members;
00231        ImageExport_getseters, //    struct PyGetSetDef *tp_getset;
00232        0, //   struct _typeobject *tp_base;
00233        0, //   PyObject *tp_dict;
00234        0, //   descrgetfunc tp_descr_get;
00235        0, //   descrsetfunc tp_descr_set;
00236        0, //   long tp_dictoffset;
00237        (initproc)ImageExport_init, //      initproc tp_init;
00238        0, //   allocfunc tp_alloc;
00239        ImageExport_new, //   newfunc tp_new;
00240        0, //   freefunc tp_free; /* Low-level free-memory routine */
00241        0, //   inquiry tp_is_gc; /* For PyObject_IS_GC */
00242        0, //   PyObject *tp_bases;
00243        0, //   PyObject *tp_mro; /* method resolution order */
00244        0, //   PyObject *tp_cache;
00245        0, //   PyObject *tp_subclasses;
00246        0, //   PyObject *tp_weaklist;
00247        0, //   destructor tp_del;
00248 
00249 #ifdef COUNT_ALLOCS
00250        /* these must be last and never explicitly initialized */
00251        //     int tp_allocs;
00252        //     int tp_frees;
00253        //     int tp_maxalloc;
00254        //     struct _typeobject *tp_next;
00255 #endif
00256 };