From 5fecc3d33c43f1315ea6c774467c88dbb5789b68 Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Thu, 5 May 2016 17:14:06 +0200 Subject: [PATCH] add buffer image for printing --- src/dialogs/orderwin.cpp | 4 +- src/dialogs/orderwin.h | 4 +- src/templates/labeldlg.cpp | 28 ++++++++++++++++++++- src/templates/labeldlg.h | 16 ++++++++++++ src/templates/ticketrender.cpp | 50 +++++++++++++++++++++++++++++++++------ src/templates/ticketrender.h | 6 ++-- 6 files changed, 91 insertions(+), 17 deletions(-) diff --git a/src/dialogs/orderwin.cpp b/src/dialogs/orderwin.cpp index 0567e10..aa8855c 100644 --- a/src/dialogs/orderwin.cpp +++ b/src/dialogs/orderwin.cpp @@ -378,7 +378,7 @@ void MOrderWindow::printTickets(QList ticketsin) if(ld.labelNeedsPageTurn(i)){ printer.newPage(); } - render.render(tickets[i],printer,&painter,p); + render.render(tickets[i],printer,&painter,p,lconf.printMode()==MLabelConfig::PrintPixmap); } } @@ -417,7 +417,7 @@ void MOrderWindow::printVouchers(QList vouchersin) if(ld.labelNeedsPageTurn(i)){ printer.newPage(); } - render.render(vouchers[i],printer,&painter,p); + render.render(vouchers[i],printer,&painter,p,lconf.printMode()==MLabelConfig::PrintPixmap); } } diff --git a/src/dialogs/orderwin.h b/src/dialogs/orderwin.h index 96b47d5..d34470c 100644 --- a/src/dialogs/orderwin.h +++ b/src/dialogs/orderwin.h @@ -95,8 +95,8 @@ class MOrderWindow:public QMainWindow void changeInvAddr(); ///change the delivery address void changeDelAddr(); - ///view/change customer data - void changeCustData(); + ///view/change customer data + void changeCustData(); /**cancel the order*/ void cancelOrder(); diff --git a/src/templates/labeldlg.cpp b/src/templates/labeldlg.cpp index b8b2e9e..324acef 100644 --- a/src/templates/labeldlg.cpp +++ b/src/templates/labeldlg.cpp @@ -382,7 +382,7 @@ void MLabelConfig::configDialog(QWidget* parent) d.setLayout(fl=new QFormLayout); fl->addRow(new QLabel(tr("Ticket Labels:"))); //each one: ask printer mode, ask label-page mode, printer/settings - QComboBox*t_prnmode,*t_pagmode; + QComboBox*t_prnmode,*t_pagmode,*t_prnbufmode; fl->addRow(tr("Print Dialog:"),t_prnmode=new QComboBox); t_prnmode->addItem(tr("Always ask for printer"),PrintAskAlways); t_prnmode->addItem(tr("Ask if unknown or not present"),PrintAskIfUnknown); @@ -411,13 +411,17 @@ void MLabelConfig::configDialog(QWidget* parent) t_prn->setText(t_printer.printerName()); }; d.connect(p,&QToolButton::clicked,settprn); + fl->addRow(tr("Print Mode:"),t_prnbufmode=new QComboBox); + t_prnbufmode->addItem(tr("Direct Print")); + t_prnbufmode->addItem(tr("Use Buffer Pixmap")); + t_prnbufmode->setCurrentIndex((int)ticket.printMode()); QFrame*frm; fl->addRow(frm=new QFrame); frm->setFrameStyle(QFrame::HLine|QFrame::Sunken); //Vouchers... fl->addRow(new QLabel(tr("Voucher Labels:"))); //each one: ask printer mode, ask label-page mode, printer/settings - QComboBox*v_prnmode,*v_pagmode; + QComboBox*v_prnmode,*v_pagmode,*v_prnbufmode; fl->addRow(tr("Print Dialog:"),v_prnmode=new QComboBox); v_prnmode->addItem(tr("Always ask for printer"),PrintAskAlways); v_prnmode->addItem(tr("Ask if unknown or not present"),PrintAskIfUnknown); @@ -444,6 +448,10 @@ void MLabelConfig::configDialog(QWidget* parent) v_prn->setText(v_printer.printerName()); }; d.connect(p,&QToolButton::clicked,setvprn); + fl->addRow(tr("Print Mode:"),v_prnbufmode=new QComboBox); + v_prnbufmode->addItem(tr("Direct Print")); + v_prnbufmode->addItem(tr("Use Buffer Pixmap")); + v_prnbufmode->setCurrentIndex((int)voucher.printMode()); //button... fl->addRow(new QLabel(" ")); fl->addRow(hl=new QHBoxLayout); @@ -461,11 +469,13 @@ void MLabelConfig::configDialog(QWidget* parent) (MLabelConfig::PrintAskMode)t_prnmode->currentIndex(), (MLabelConfig::PageAskMode)t_pagmode->currentIndex() ); + ticket.storePrintMode((MLabelConfig::PrintMode)t_prnbufmode->currentIndex()); voucher.storePrinter(v_printer); voucher.storeAskModes( (MLabelConfig::PrintAskMode)v_prnmode->currentIndex(), (MLabelConfig::PageAskMode)v_pagmode->currentIndex() ); + voucher.storePrintMode((MLabelConfig::PrintMode)v_prnbufmode->currentIndex()); } QString MLabelConfig::tr(const char* s, const char* c) @@ -606,6 +616,20 @@ void MLabelConfig::storeAskModes(MLabelConfig::PrintAskMode prm, MLabelConfig::P set.setValue("pageAskMode",(int)pgm); } +MLabelConfig::PrintMode MLabelConfig::printMode() const +{ + QSettings set; + set.beginGroup(cgroup()); + return PrintMode(set.value("printBufMode",PrintDirect).toInt()); +} + +void MLabelConfig::storePrintMode ( MLabelConfig::PrintMode pm ) +{ + QSettings set; + set.beginGroup(cgroup()); + set.setValue("printBufMode",(int)pm); +} + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ struct Paper_s { diff --git a/src/templates/labeldlg.h b/src/templates/labeldlg.h index 001ada6..5bd0e4b 100644 --- a/src/templates/labeldlg.h +++ b/src/templates/labeldlg.h @@ -97,6 +97,19 @@ class MLabelConfig ///returns the policy for asking for page layout PageAskMode pageAskMode()const; + + ///how to send data to the printer + enum PrintMode{ + //do not change the order of these entries! + //their numerical value is used as index in configDialog() + ///print directly through the printer driver + PrintDirect=0, + ///use a buffer pixmap to render fonts/graphics in memory first + PrintPixmap=1 + }; + + ///returns how to send data to the printer + PrintMode printMode()const; private: LabelType m_type; @@ -106,6 +119,8 @@ class MLabelConfig void storePrinter(QPrinter&); /// \internal helper to store ask modes void storeAskModes(PrintAskMode,PageAskMode); + /// \internal helper to store print mode + void storePrintMode(PrintMode); /// \internal return the config group QString cgroup()const; @@ -183,6 +198,7 @@ class MLabelDialog:public QDialog }; +///Dialog that allows to configure details on how to print a label. class MLabelPrintDialog:public QDialog { Q_OBJECT diff --git a/src/templates/ticketrender.cpp b/src/templates/ticketrender.cpp index 17efddf..2d31ae6 100644 --- a/src/templates/ticketrender.cpp +++ b/src/templates/ticketrender.cpp @@ -86,6 +86,9 @@ class MLabelRendererPrivate //converts a size to "natural" coordinates QSizeF tonatural(const QPaintDevice&,QSizeF); + + //converts a size to "natural" coordinates + QSizeF tonatural(int dpi,QSizeF); //returns a system font name QString getfont(QString); @@ -217,12 +220,33 @@ MLabelRendererPrivate::~MLabelRendererPrivate() QFontDatabase::removeApplicationFont(fdb[i]); } -bool MLabelRenderer::render(const MLabel&ticket,QPaintDevice&pdev,QPainter*painter,QPointF offset) +bool MLabelRenderer::render(const MLabel&label,QPaintDevice&pdev,QPainter*painter,QPointF offset,bool usePixmap) { - return d->render(ticket,pdev,painter,offset); + bool success=false; + if(usePixmap){ + // create pixmap (assume 600 DPI) + QSizeF sz=d->tonatural(600,d->tsize); + QImage map(sz.toSize(),QImage::Format_RGB32); + map.setDotsPerMeterX(23622); + map.setDotsPerMeterY(23622); + map.fill(Qt::white); + // render to pixmap + success=d->render(label,map,nullptr,QPointF()); + // render to pdev + if(success){ + QPainter*paint=painter; + if(!painter)paint=new QPainter(&pdev); + QSizeF nsize=d->tonatural(pdev,d->tsize); + paint->setClipRect(QRectF(offset,nsize)); + paint->drawImage(QRectF(offset,nsize).toRect(),map,QRectF(QPointF(),map.size())); + if(!painter)delete paint; + } + }else + success=d->render(label,pdev,painter,offset); + return success; } -bool MLabelRendererPrivate::render(const MLabel&ticket,QPaintDevice&pdev,QPainter*painter,QPointF offset) +bool MLabelRendererPrivate::render(const MLabel&label,QPaintDevice&pdev,QPainter*painter,QPointF offset) { //sanity check if(!canrender){ @@ -231,7 +255,7 @@ bool MLabelRendererPrivate::render(const MLabel&ticket,QPaintDevice&pdev,QPainte } //actually render QDomElement el=txml.documentElement(); - render(el,ticket,pdev,painter,offset); + render(el,label,pdev,painter,offset); //return result (canrender may be changed by renderer return canrender; } @@ -318,6 +342,16 @@ QSizeF MLabelRendererPrivate::tonatural(const QPaintDevice&dev,QSizeF s) return s; } +QSizeF MLabelRendererPrivate::tonatural(int dpi,QSizeF s) +{ + double fac; + if(unit=="mm")fac=25.4; + else fac=1.0; + s.setWidth(s.width()*dpi/fac); + s.setHeight(s.height()*dpi/fac); + return s; +} + void MLabelRendererPrivate::render(QDomElement&sup, const MLabel&tick, QPaintDevice&pdev, QPainter*painter, QPointF noff) { //initialize painter @@ -540,9 +574,9 @@ QString MTicketLabel::getVariable(QString var)const } MTicketRenderer::MTicketRenderer(MTemplate f):MLabelRenderer(f){} -bool MTicketRenderer::render(const MOTicket&label,QPaintDevice&pdev,QPainter*painter,QPointF offset) +bool MTicketRenderer::render(const MOTicket&label,QPaintDevice&pdev,QPainter*painter,QPointF offset,bool usePixmap) { - return MLabelRenderer::render(MTicketLabel(label),pdev,painter,offset); + return MLabelRenderer::render(MTicketLabel(label),pdev,painter,offset,usePixmap); } class MVoucherLabel:public MLabel @@ -564,7 +598,7 @@ QString MVoucherLabel::getVariable(QString var)const } MVoucherRenderer::MVoucherRenderer(MTemplate f):MLabelRenderer(f){} -bool MVoucherRenderer::render(const MOVoucher&label,QPaintDevice&pdev,QPainter*painter,QPointF offset) +bool MVoucherRenderer::render(const MOVoucher&label,QPaintDevice&pdev,QPainter*painter,QPointF offset,bool usePixmap) { - return MLabelRenderer::render(MVoucherLabel(label),pdev,painter,offset); + return MLabelRenderer::render(MVoucherLabel(label),pdev,painter,offset,usePixmap); } diff --git a/src/templates/ticketrender.h b/src/templates/ticketrender.h index fb5a928..10e2023 100644 --- a/src/templates/ticketrender.h +++ b/src/templates/ticketrender.h @@ -48,7 +48,7 @@ class MLabelRenderer virtual ~MLabelRenderer(); /**renders the ticket; returns whether the rendering was successful*/ - virtual bool render(const MLabel&label,QPaintDevice&pdev,QPainter*painter=0,QPointF offset=QPointF()); + virtual bool render(const MLabel&label,QPaintDevice&pdev,QPainter*painter=0,QPointF offset=QPointF(),bool usePixmap=false); /**returns the size of the ticket in the coordinates of the specified paint device (used for preview)*/ virtual QSizeF labelSize(const QPaintDevice&); @@ -65,7 +65,7 @@ class MVoucherRenderer:public MLabelRenderer { public: MVoucherRenderer(MTemplate f); - bool render(const MOVoucher&label,QPaintDevice&pdev,QPainter*painter=0,QPointF offset=QPointF()); + bool render(const MOVoucher&label,QPaintDevice&pdev,QPainter*painter=0,QPointF offset=QPointF(),bool usePixmap=false); }; /**convenience class: renders vouchers directly*/ @@ -73,7 +73,7 @@ class MTicketRenderer:public MLabelRenderer { public: MTicketRenderer(MTemplate f); - bool render(const MOTicket&label,QPaintDevice&pdev,QPainter*painter=0,QPointF offset=QPointF()); + bool render(const MOTicket&label,QPaintDevice&pdev,QPainter*painter=0,QPointF offset=QPointF(),bool usePixmap=false); }; -- 1.7.2.5