Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Classes | Functions | Variables
objprinter.cpp File Reference
#include "objprinter.h"
#include "cmdutil.h"
#include "prefsmanager.h"
#include <structmember.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <vector>
#include "pslib.h"
#include "gsutil.h"
#include "scribuscore.h"
#include "util.h"

Go to the source code of this file.

Classes

struct  Printer

Functions

bool SCRIBUS_API loadText (QString nam, QString *Buffer)
void SCRIBUS_API ReOrderText (ScribusDoc *doc, ScribusView *view)
static void Printer_dealloc (Printer *self)
static PyObject * Printer_new (PyTypeObject *type, PyObject *, PyObject *)
static int Printer_init (Printer *self, PyObject *, PyObject *)
static PyObject * Printer_getallPrinters (Printer *self, void *)
static int Printer_setallPrinters (Printer *, PyObject *, void *)
static PyObject * Printer_getprinter (Printer *self, void *)
static int Printer_setprinter (Printer *self, PyObject *value, void *)
static PyObject * Printer_getfile (Printer *self, void *)
static int Printer_setfile (Printer *self, PyObject *value, void *)
static PyObject * Printer_getcmd (Printer *self, void *)
static int Printer_setcmd (Printer *self, PyObject *value, void *)
static PyObject * Printer_getpages (Printer *self, void *)
static int Printer_setpages (Printer *self, PyObject *value, void *)
static PyObject * Printer_getseparation (Printer *self, void *)
static int Printer_setseparation (Printer *self, PyObject *value, void *)
static PyObject * Printer_print (Printer *self)

Variables

static PyMemberDef Printer_members []
static PyGetSetDef Printer_getseters []
static PyMethodDef Printer_methods []
PyTypeObject Printer_Type

Class Documentation

struct Printer

Definition at line 29 of file objprinter.cpp.

Class Members
PyObject_HEAD PyObject * allPrinters
PyObject * cmd
int color
int copies
PyObject * file
int mph
int mpv
PyObject * pages
PyObject * printer
int pslevel
PyObject * separation
int ucr
int useICC

Function Documentation

bool SCRIBUS_API loadText ( QString  nam,
QString *  Buffer 
)

Definition at line 275 of file util.cpp.

{
       QFile f(filename);
       QFileInfo fi(f);
       if (!fi.exists())
              return false;
       bool ret;
       QByteArray bb(f.size());
       if (f.open(IO_ReadOnly))
       {
              f.readBlock(bb.data(), f.size());
              f.close();
              for (uint posi = 0; posi < bb.size(); ++posi)
                     *Buffer += QChar(bb[posi]);
              /*
              int len = bb.size();
              int oldLen = Buffer->length();
              Buffer->setLength( oldLen + len + 1);
              // digged into Qt 3.3 sources to find that. Might break in Qt 4 -- AV
              unsigned short * ucsString = const_cast<unsigned short *>(Buffer->ucs2()) + oldLen;
              char * data = bb.data();
              for (uint posi = 0; posi < len; ++posi)
                     *ucsString++ = *data++;
              *ucsString = 0;
               */
              ret = true;
       }
       else
              ret = false;
       return ret;
}

Here is the caller graph for this function:

static void Printer_dealloc ( Printer self) [static]

Definition at line 48 of file objprinter.cpp.

{
       Py_XDECREF(self->allPrinters);
       Py_XDECREF(self->printer);
       Py_XDECREF(self->file);
       Py_XDECREF(self->cmd);
       Py_XDECREF(self->pages);
       Py_XDECREF(self->separation);
       self->ob_type->tp_free((PyObject *)self);
}
static PyObject* Printer_getallPrinters ( Printer self,
void *   
) [static]

Definition at line 260 of file objprinter.cpp.

{
       Py_INCREF(self->allPrinters);
       return self->allPrinters;
}
static PyObject* Printer_getcmd ( Printer self,
void *   
) [static]

Definition at line 325 of file objprinter.cpp.

{
       Py_INCREF(self->cmd);
       return self->cmd;
}
static PyObject* Printer_getfile ( Printer self,
void *   
) [static]

Definition at line 303 of file objprinter.cpp.

{
       Py_INCREF(self->file);
       return self->file;
}
static PyObject* Printer_getpages ( Printer self,
void *   
) [static]

Definition at line 347 of file objprinter.cpp.

{
       Py_INCREF(self->pages);
       return self->pages;
}
static PyObject* Printer_getprinter ( Printer self,
void *   
) [static]

Definition at line 272 of file objprinter.cpp.

{
       Py_INCREF(self->printer);
       return self->printer;
}
static PyObject* Printer_getseparation ( Printer self,
void *   
) [static]

Definition at line 381 of file objprinter.cpp.

{
       Py_INCREF(self->separation);
       return self->separation;
}
static int Printer_init ( Printer self,
PyObject *  ,
PyObject *   
) [static]

Definition at line 124 of file objprinter.cpp.

{
// pool system for installed printers
// most code is stolen and little adopted from druck.cpp
       PyObject *allPrinters = PyList_New(0);
       if (allPrinters){
              Py_DECREF(self->allPrinters);
              self->allPrinters = allPrinters;
       }
#ifdef HAVE_CUPS
       cups_dest_t *dests;
       int num_dests = cupsGetDests(&dests);
       for (int i = 0; i < num_dests; ++i) {
              if (dests[i].name != NULL) {
                     PyObject *tmp = PyString_FromString(dests[i].name);
                     if (tmp) {
                            PyList_Append(self->allPrinters, tmp);
                            Py_DECREF(tmp);
                     }
              }
       }
       cupsFreeDests(num_dests, dests);
#else
       QString Pcap;
       // loadText is defined in utils.cpp
       if (loadText("/etc/printcap", &Pcap))
       {
              QTextStream ts(&Pcap, IO_ReadOnly);
              while(!ts.atEnd())
              {
                     QStringList wt;
                     QString tmp = ts.readLine();
                     if (tmp.isEmpty())
                            continue;
                     if ((tmp[0] != '#') && (tmp[0] != ' ') && (tmp[0] != '\n') && (tmp[0] != '\t'))
                     {
                            tmp = tmp.stripWhiteSpace();
                            tmp = tmp.left(tmp.length() - (tmp.right(2) == ":\\" ? 2 : 1));
                            wt = QStringList::split("|", tmp);
                            PyObject *tmppr = PyString_FromString(wt[0]);
                            if (tmppr){
                                   PyList_Append(self->allPrinters, tmppr);
                                   Py_DECREF(tmppr);
                            }
                     }
              }
       }
#endif
       PyObject *tmp2 = PyString_FromString("File");
       PyList_Append(self->allPrinters, tmp2);
       Py_DECREF(tmp2);
// as defaut set to print into file
       PyObject *printer = NULL;
       printer = PyString_FromString("File");
       if (printer){
              Py_DECREF(self->printer);
              self->printer = printer;
       }
// set defaul name of file to print into
       QString tf = ScCore->primaryMainWindow()->doc->PDF_Options.Datei;
       if (tf.isEmpty()) {
              QFileInfo fi = QFileInfo(ScCore->primaryMainWindow()->doc->DocName);
              tf = fi.dirPath()+"/"+fi.baseName()+".pdf";
       }
       PyObject *file = NULL;
       file = PyString_FromString(tf.ascii());
       if (file){
              Py_DECREF(self->file);
              self->file = file;
       } else {
              PyErr_SetString(PyExc_SystemError, "Can not initialize 'file' attribute");
              return -1;
       }
// alternative printer commands default to ""
       PyObject *cmd = NULL;
       cmd = PyString_FromString("");
       if (cmd){
              Py_DECREF(self->cmd);
              self->cmd = cmd;
       }
// if document exist when created Printer instance
// set to print all pages
       PyObject *pages = NULL;
       int num = 0;
       if (ScCore->primaryMainWindow()->HaveDoc)
              // which one should I use ???
              // new = ScCore->primaryMainWindow()->view->Pages.count()
              num = ScCore->primaryMainWindow()->doc->Pages->count();
       pages = PyList_New(num);
       if (pages){
              Py_DECREF(self->pages);
              self->pages = pages;
       }
       for (int i = 0; i<num; i++) {
              PyObject *tmp=NULL;
              tmp = PyInt_FromLong((long)i+1L); // instead of 1 put here first page number
              if (tmp)
                     PyList_SetItem(self->pages, i, tmp);
       }
// do not print separation
       PyObject *separation = NULL;
       separation = PyString_FromString("No");
       if (separation){
              Py_DECREF(self->separation);
              self->separation = separation;
       }
// print in color
       self->color = 1;
// do not use ICC Profile
       self->useICC = 0;
// use PostScrip level 3
       self->pslevel = 3;
// do not mirror pages
       self->mph = 0;
// do not mirror pages
       self->mpv = 0;
// apply Under Color Removal as default
       self->ucr = 1;
// number of copies
       self->copies = 1;
       return 0;
}

Here is the call graph for this function:

static PyObject* Printer_new ( PyTypeObject *  type,
PyObject *  ,
PyObject *   
) [static]

Definition at line 59 of file objprinter.cpp.

{
// do not create new object if there is no opened document
       if (!ScCore->primaryMainWindow()->HaveDoc) {
              PyErr_SetString(PyExc_SystemError, "Need to open document first");
              return NULL;
       }

       Printer *self;
       self = (Printer *)type->tp_alloc(type, 0);
       if (self != NULL) {
// set allPrinters attribute
              self->allPrinters = PyList_New(0);
              if (self->allPrinters == NULL){
                     Py_DECREF(self);
                     return NULL;
              }
// set printer attribute
              self->printer = PyString_FromString("");
              if (self->printer == NULL){
                     Py_DECREF(self);
                     return NULL;
              }
// set file attribute
              self->file = PyString_FromString("");
              if (self->file == NULL){
                     Py_DECREF(self);
                     return NULL;
              }
// set cmd attribute
              self->cmd = PyString_FromString("");
              if (self->cmd == NULL){
                     Py_DECREF(self);
                     return NULL;
              }
// set pages attribute
              self->pages = PyList_New(0);
              if (self->pages == NULL){
                     Py_DECREF(self);
                     return NULL;
              }
// set separation attribute
              self->separation = PyString_FromString("No");
              if (self->separation == NULL){
                     Py_DECREF(self);
                     return NULL;
              }
// set color attribute
              self->color = 1;
// set useICC attribute
              self->useICC = 0;
// set pslevel attribute
              self->pslevel = 3;
// set mph attribute
              self->mph = 0;
// set mpv attribute
              self->mpv = 0;
// set ucr attribute
              self->ucr = 1;
// set copies attribute
              self->copies = 1;
       }
       return (PyObject *) self;
}

Here is the call graph for this function:

static PyObject* Printer_print ( Printer self) [static]

Definition at line 415 of file objprinter.cpp.

{
       if (!ScCore->primaryMainWindow()->HaveDoc) {
              PyErr_SetString(PyExc_SystemError, "Need to open documetnt first");
              return NULL;
       }
// copied from void ScribusMainWindow::slotFilePrint() in file scribus.cpp
       QString fna, prn, cmd, scmd, cc, data, SepName;
       QString printcomm;
       int Nr, PSLevel;
       bool fil, PSfile;
       PSfile = false;

//    ReOrderText(ScCore->primaryMainWindow()->doc, ScCore->primaryMainWindow()->view);
       prn = QString(PyString_AsString(self->printer));
       fna = QString(PyString_AsString(self->file));
       fil = (QString(PyString_AsString(self->printer)) == QString("File")) ? true : false;
       std::vector<int> pageNs;
       PrintOptions options;
       for (int i = 0; i < PyList_Size(self->pages); ++i) {
              options.pageNumbers.push_back((int)PyInt_AsLong(PyList_GetItem(self->pages, i)));
       }
       Nr = (self->copies < 1) ? 1 : self->copies;
       SepName = QString(PyString_AsString(self->separation));
       options.separationName = SepName;
       options.outputSeparations =(SepName == QString("No")) ?  false : true;
       options.useColor = self->color;
       options.mirrorH = self->mph;
       options.mirrorV = self->mpv;
       options.useICC = self->useICC;
       options.doGCR = self->ucr;
       options.cropMarks = false;
       options.bleedMarks = false;
       options.registrationMarks = false;
       options.colorMarks = false;
       options.markOffset = 0.0;
       options.bleeds.Top = 0.0;
       options.bleeds.Left = 0.0;
       options.bleeds.Right = 0.0;
       options.bleeds.Bottom = 0.0;
       int psl = self->pslevel;
       if (psl < 1)
              psl = 1;
       else if (psl > 3)
              psl = 3;
       PSLevel = psl;
       printcomm = QString(PyString_AsString(self->cmd));
       QMap<QString, QMap<uint, FPointArray> > ReallyUsed;
       ReallyUsed.clear();
       ScCore->primaryMainWindow()->doc->getUsedFonts(ReallyUsed);
       PrefsManager *prefsManager=PrefsManager::instance();
       PSLib *dd = new PSLib(options, true, prefsManager->appPrefs.AvailFonts, ReallyUsed, ScCore->primaryMainWindow()->doc->PageColors, false, true);
       if (dd != NULL)
       {
              if (!fil)
                     fna = QDir::convertSeparators(prefsManager->preferencesLocation()+"/tmp.ps");
              PSfile = dd->PS_set_file(fna);
              fna = QDir::convertSeparators(fna);
              if (PSfile)
              {
                     options.setDevParam = false;
                     options.doClip = false;
                     options.doOverprint = false;
                     dd->CreatePS(ScCore->primaryMainWindow()->doc, options);
                     if (PSLevel != 3)
                     {
                            QString tmp;
                            QStringList opts;
                            opts.append( QString("-dDEVICEWIDTHPOINTS=%1").arg(tmp.setNum(ScCore->primaryMainWindow()->doc->pageWidth)) );
                            opts.append( QString("-dDEVICEHEIGHTPOINTS=%1").arg(tmp.setNum(ScCore->primaryMainWindow()->doc->pageHeight)) );
                            convertPS2PS(fna, fna+".tmp", opts, PSLevel);
                            moveFile( fna + ".tmp", fna );
                     }

                     if (!fil)
                     {
                            if (!printcomm.isEmpty())
                                   cmd = printcomm + " "+fna;
                            else
                            {
                                   cmd = "lpr -P" + prn;
                                   if (Nr > 1)
                                          cmd += " -#" + cc.setNum(Nr);
#ifdef HAVE_CUPS
// This need yet to be implemented by object Printer
//                                 cmd += printer->PrinterOpts;
#endif
                                   cmd += " "+fna;
                            }
                            system(cmd);
                            unlink(fna);
                     }
              }
              else {
                     delete dd;
                     PyErr_SetString(PyExc_SystemError, "Printing failed");
                     return NULL;
              }
              delete dd;
       }
//     Py_INCREF(Py_None);
//     return Py_None;
       Py_RETURN_NONE;
}

Here is the call graph for this function:

static int Printer_setallPrinters ( Printer ,
PyObject *  ,
void *   
) [static]

Definition at line 266 of file objprinter.cpp.

{
       PyErr_SetString(PyExc_ValueError, "'allPrinters' attribute is READ-ONLY");
       return -1;
}
static int Printer_setcmd ( Printer self,
PyObject *  value,
void *   
) [static]

Definition at line 331 of file objprinter.cpp.

{
       if (value == NULL) {
              PyErr_SetString(PyExc_TypeError, "Cannot delete 'cmd' attribute.");
              return -1;
       }
       if (!PyString_Check(value)) {
              PyErr_SetString(PyExc_TypeError, "The 'cmd' attribute value must be string.");
              return -1;
       }
       Py_DECREF(self->cmd);
       Py_INCREF(value);
       self->cmd = value;
       return 0;
}
static int Printer_setfile ( Printer self,
PyObject *  value,
void *   
) [static]

Definition at line 309 of file objprinter.cpp.

{
       if (value == NULL) {
              PyErr_SetString(PyExc_TypeError, "Cannot delete 'file' attribute.");
              return -1;
       }
       if (!PyString_Check(value)) {
              PyErr_SetString(PyExc_TypeError, "The 'file' attribute value must be string.");
              return -1;
       }
       Py_DECREF(self->file);
       Py_INCREF(value);
       self->file = value;
       return 0;
}
static int Printer_setpages ( Printer self,
PyObject *  value,
void *   
) [static]

Definition at line 353 of file objprinter.cpp.

{
       if (value == NULL) {
              PyErr_SetString(PyExc_TypeError, "Cannot delete 'pages' attribute.");
              return -1;
       }
       if (!PyList_Check(value)) {
              PyErr_SetString(PyExc_TypeError, "'pages' attribute value must be list of integers.");
              return -1;
       }
       int len = PyList_Size(value);
       for (int i = 0; i<len; i++){
              PyObject *tmp = PyList_GetItem(value, i);
              if (!PyInt_Check(tmp)){
                     PyErr_SetString(PyExc_TypeError, "'pages' attribute must be list containing only integers.");
                     return -1;
              }
              if (PyInt_AsLong(tmp) > static_cast<int>(ScCore->primaryMainWindow()->doc->Pages->count()) || PyInt_AsLong(tmp) < 1) {
                     PyErr_SetString(PyExc_ValueError, "'pages' value out of range.");
                     return -1;
              }
       }
       Py_DECREF(self->pages);
       Py_INCREF(value);
       self->pages = value;
       return 0;
}

Here is the call graph for this function:

static int Printer_setprinter ( Printer self,
PyObject *  value,
void *   
) [static]

Definition at line 278 of file objprinter.cpp.

{
       if (value == NULL) {
              PyErr_SetString(PyExc_TypeError, "Cannot delete 'printer' attribute.");
              return -1;
       }
       if (!PyString_Check(value)) {
              PyErr_SetString(PyExc_TypeError, "The 'printer' attribute value must be string.");
              return -1;
       }
       int n = PyList_Size(self->allPrinters);
       bool same = 0;
       for (int i = 0; i<n; i++)
              if (PyObject_RichCompareBool(value, PyList_GetItem(self->allPrinters, i), Py_EQ) == 1)
                     same = true;
       if (!same) {
              PyErr_SetString(PyExc_ValueError, "'printer' value can be only one of string in 'allPrinters' attribute ");
              return -1;
       }
       Py_DECREF(self->printer);
       Py_INCREF(value);
       self->printer = value;
       return 0;
}
static int Printer_setseparation ( Printer self,
PyObject *  value,
void *   
) [static]

Definition at line 387 of file objprinter.cpp.

{
       if (value == NULL) {
              PyErr_SetString(PyExc_TypeError, "Cannot delete 'separation' attribute.");
              return -1;
       }
       if (!PyString_Check(value)) {
              PyErr_SetString(PyExc_TypeError, "The 'separation' attribute value must be string.");
              return -1;
       }
       Py_DECREF(self->separation);
       Py_INCREF(value);
       self->separation = value;
       return 0;
}
void SCRIBUS_API ReOrderText ( ScribusDoc *  doc,
ScribusView *  view 
)

Definition at line 714 of file util.cpp.

{
       double savScale = view->scale();
       view->setScale(1.0);
       currentDoc->RePos = true;
       QPixmap pgPix(10, 10);
       QRect rd = QRect(0,0,9,9);
       ScPainter *painter = new ScPainter(&pgPix, pgPix.width(), pgPix.height());
       for (uint azz=0; azz<currentDoc->MasterItems.count(); ++azz)
       {
              PageItem *currItem = currentDoc->MasterItems.at(azz);
              if (currItem->itemType() == PageItem::PathText)
                     currItem->DrawObj(painter, rd);
       }
       for (uint azz=0; azz<currentDoc->Items->count(); ++azz)
       {
              PageItem *currItem = currentDoc->Items->at(azz);
              if (currItem->itemType() == PageItem::TextFrame)
                     currItem->asTextFrame()->layout();
              else if (currItem->itemType() == PageItem::PathText)
                     currItem->DrawObj(painter, rd);
       }
       currentDoc->RePos = false;
       view->setScale(savScale);
       delete painter;
}

Variable Documentation

PyGetSetDef Printer_getseters[] [static]
Initial value:
 {
       {const_cast<char*>("allPrinters"), (getter)Printer_getallPrinters, (setter)Printer_setallPrinters, const_cast<char*>("List of installed printers  --  read only"), NULL},
       {const_cast<char*>("printer"), (getter)Printer_getprinter, (setter)Printer_setprinter, const_cast<char*>("Name of printer to use.\nDefault is 'File' for printing into file"), NULL},
       {const_cast<char*>("file"), (getter)Printer_getfile, (setter)Printer_setfile, const_cast<char*>("Name of file to print into"), NULL},
       {const_cast<char*>("cmd"), (getter)Printer_getcmd, (setter)Printer_setcmd, const_cast<char*>("Alternative Printer Command"), NULL},
       {const_cast<char*>("pages"), (getter)Printer_getpages, (setter)Printer_setpages, const_cast<char*>("List of pages to be printed"), NULL},
       {const_cast<char*>("separation"), (getter)Printer_getseparation, (setter)Printer_setseparation, const_cast<char*>("Print separationl\n\t 'No'  -- Default\n\t 'All'\n\t 'Cyan'\n\t 'Magenta'\n\t 'Yellow'\n\t 'Black'\nBeware of misspelling because check is not performed"), NULL},
       {NULL, NULL, NULL, NULL, NULL}  
}

Definition at line 404 of file objprinter.cpp.

PyMemberDef Printer_members[] [static]
Initial value:
 {
       {const_cast<char*>("copies"), T_INT, offsetof(Printer, copies), 0, const_cast<char*>("Number of copies")},
       {const_cast<char*>("color"), T_INT, offsetof(Printer, color), 0, const_cast<char*>("Print in color.\n\t True - color  --  Default\n\t False - greyscale")},
       {const_cast<char*>("useICC"), T_INT, offsetof(Printer, useICC), 0, const_cast<char*>("Use ICC Profile\n\tTrue\n\tFalse  --  Default")},
       {const_cast<char*>("pslevel"), T_INT, offsetof(Printer, pslevel), 0, const_cast<char*>("PostScript Level\nCan be 1 or 2 or 3    -- Default is 3.")},
       {const_cast<char*>("mph"), T_INT, offsetof(Printer, mph), 0, const_cast<char*>("Mirror Pages Horizontal\n\tTrue\n\tFalse  --  Default")},
       {const_cast<char*>("mpv"), T_INT, offsetof(Printer, mpv), 0, const_cast<char*>("Mirror Pages Vertical\n\t True\n\tFalse  --  Default")},
       {const_cast<char*>("ucr"), T_INT, offsetof(Printer, ucr), 0, const_cast<char*>("Apply Under Color Removal\n\tTrue  --  Default\n\tFalse")},
       {NULL, 0, 0, 0, NULL} 
}

Definition at line 247 of file objprinter.cpp.

PyMethodDef Printer_methods[] [static]
Initial value:
 {
       {const_cast<char*>("print"), (PyCFunction)Printer_print, METH_NOARGS, const_cast<char*>("Prints selected pages.")},
       {NULL, (PyCFunction)(0), 0, NULL} 
}

Definition at line 520 of file objprinter.cpp.

PyTypeObject Printer_Type

Definition at line 525 of file objprinter.cpp.