From: Konrad Rosenbaum Date: Fri, 12 Jul 2013 14:04:52 +0000 (+0200) Subject: fix label printing X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=d1264c313c885a8ba81623f42ac62db0a9a5b543;p=web%2Fkonrad%2Fsmoke.git fix label printing --- diff --git a/src/templates/labeldlg.cpp b/src/templates/labeldlg.cpp index 41181af..b8b2e9e 100644 --- a/src/templates/labeldlg.cpp +++ b/src/templates/labeldlg.cpp @@ -19,11 +19,12 @@ #include #include #include +#include #include #include #include #include -#include +// #include #include #include #include @@ -164,6 +165,31 @@ MLabelDialog::MLabelDialog(QWidget*par,QPrinter* pn,int nl,QSizeF ls) updatePage(); } +QDataStream &operator<<(QDataStream &out, const MLabelConfig &myObj) +{ + char t=myObj.m_type; + out.writeBytes(&t,1); + return out; +} +QDataStream &operator>>(QDataStream &in, MLabelConfig &myObj) +{ + char *t; + uint l; + in.readBytes(t,l); + if(t && l>0) + myObj.m_type=(MLabelConfig::LabelType)(*t); + if(t)delete t; + return in; +} + +void MLabelConfig::initMeta() +{ + static int mt=-1; + if(mt>=0)return; + mt=qRegisterMetaType(); + qRegisterMetaTypeStreamOperators(); +} + MLabelDialog::~MLabelDialog(){} int MLabelDialog::findLabel(int n,int&row,int&col)const @@ -377,8 +403,10 @@ void MLabelConfig::configDialog(QWidget* parent) hl->addWidget(p=new QToolButton); p->setText("..."); auto settprn=[&]{ - QPrintDialog pd(&t_printer,&d); - pd.setOptions(QAbstractPrintDialog::PrintShowPageSize); + MLabelPrintDialog pd(&t_printer,&d); + pd.setWindowTitle(tr("Chose Printer for Tickets")); + pd.setButtonLabel(tr("Set Printer")); +// pd.setOptions(QAbstractPrintDialog::PrintShowPageSize); if(pd.exec()!=QDialog::Accepted)return; t_prn->setText(t_printer.printerName()); }; @@ -408,10 +436,12 @@ void MLabelConfig::configDialog(QWidget* parent) hl->addWidget(p=new QToolButton); p->setText("..."); auto setvprn=[&]{ - QPrintDialog pd(&v_printer,&d); - pd.setOptions(QAbstractPrintDialog::PrintShowPageSize); + MLabelPrintDialog pd(&v_printer,&d); + pd.setWindowTitle(tr("Chose Printer for Vouchers")); + pd.setButtonLabel(tr("Set Printer")); +// pd.setOptions(QAbstractPrintDialog::PrintShowPageSize); if(pd.exec()!=QDialog::Accepted)return; - v_prn->setText(pd.printer()->printerName()); + v_prn->setText(v_printer.printerName()); }; d.connect(p,&QToolButton::clicked,setvprn); //button... @@ -462,7 +492,7 @@ bool MLabelConfig::selectPrinter(QPrinter& printer, bool force) case PrintAskNever:/*do not override the force*/ break; } if(force){ - QPrintDialog pd(&printer); + MLabelPrintDialog pd(&printer); return pd.exec()==QDialog::Accepted; }else return gotit; @@ -470,10 +500,22 @@ bool MLabelConfig::selectPrinter(QPrinter& printer, bool force) QString MLabelConfig::cgroup() const { - QString key=m_type==Ticket?"ticket":"voucher"; + const QString key=m_type==Ticket?"ticket":"voucher"; return req->settingsGroup()+"/"+key; } +static inline void dumpPrn(QPrinter&p,QString s) +{ + qDebug()<<"printer"< paperTypes; + +void MLabelPrintDialog::initPaper() +{ + if(paperTypes.size())return; + paperTypes=QList() + << Paper_s(tr("Custom Size","paper type"),QPrinter::Custom,0,0) + << Paper_s(tr("A0","paper type"),QPrinter::A0,841,1189) + << Paper_s(tr("A1","paper type"),QPrinter::A1,594,841) + << Paper_s(tr("A2","paper type"),QPrinter::A2,420,594) + << Paper_s(tr("A3","paper type"),QPrinter::A3,297,420) + << Paper_s(tr("A4","paper type"),QPrinter::A4,210,297) + << Paper_s(tr("A5","paper type"),QPrinter::A5,148,210) + << Paper_s(tr("A6","paper type"),QPrinter::A6,105,148) + << Paper_s(tr("A7","paper type"),QPrinter::A7,74,105) + << Paper_s(tr("A8","paper type"),QPrinter::A8,52,74) + << Paper_s(tr("A9","paper type"),QPrinter::A9,37,52) + << Paper_s(tr("B0","paper type"),QPrinter::B0,1000,1414) + << Paper_s(tr("B1","paper type"),QPrinter::B1,707,1000) + << Paper_s(tr("B2","paper type"),QPrinter::B2,500,707) + << Paper_s(tr("B3","paper type"),QPrinter::B3,353,500) + << Paper_s(tr("B4","paper type"),QPrinter::B4,250,353) + << Paper_s(tr("B5","paper type"),QPrinter::B5,176,250) + << Paper_s(tr("B6","paper type"),QPrinter::B6,125,176) + << Paper_s(tr("B7","paper type"),QPrinter::B7,88,125) + << Paper_s(tr("B8","paper type"),QPrinter::B8,62,88) + << Paper_s(tr("B9","paper type"),QPrinter::B9,33,62) + << Paper_s(tr("B10","paper type"),QPrinter::B10,31,44) + << Paper_s(tr("C5E","paper type"),QPrinter::C5E,163,229) + << Paper_s(tr("U.S. Common 10 Envelope","paper type"),QPrinter::Comm10E,105,241) + << Paper_s(tr("DLE","paper type"),QPrinter::DLE,110,220) + << Paper_s(tr("Executive","paper type"),QPrinter::Executive,190.5,254) + << Paper_s(tr("Folio","paper type"),QPrinter::Folio,210,330) + << Paper_s(tr("Ledger","paper type"),QPrinter::Ledger,431.8,279.4) + << Paper_s(tr("Legal","paper type"),QPrinter::Legal,215.9,355.6) + << Paper_s(tr("Letter","paper type"),QPrinter::Letter,215.9,279.4) + << Paper_s(tr("Tabloid","paper type"),QPrinter::Tabloid,279.4,431.8) + ; +} + + + +MLabelPrintDialog::MLabelPrintDialog(QPrinter* prn, QWidget* parent, Qt::WindowFlags f) + : QDialog(parent, f) + , mprinter(prn) +{ + setWindowTitle(tr("Chose Printer")); + QFormLayout*fl; + setLayout(fl=new QFormLayout); + fl->addRow(tr("Printer:"),mprnselect=new QComboBox); + int cur=-1,def=-1; + for(const QPrinterInfo&info:QPrinterInfo::availablePrinters()){ + if(info.printerName()==prn->printerName())cur=mprnselect->count(); + if(info.isDefault())def=mprnselect->count(); + mprnselect->addItem(info.printerName()); + } + if(cur>=0)mprnselect->setCurrentIndex(cur); + else if(def>=0)mprnselect->setCurrentIndex(def); + connect(mprnselect,SIGNAL(currentIndexChanged(int)), this,SLOT(printerChanged(int))); + fl->addRow(tr("Make & Model:"),mmakemodel=new QLabel); + fl->addRow(tr("Location:"),mlocation=new QLabel); + fl->addRow(tr("Description:"),mdescript=new QLabel); + fl->addRow(tr("Unit of Measure:"),munit=new QComboBox); + munit->addItem(tr("Millimeter"),1.); + munit->addItem(tr("Centimeter"),10.); + munit->addItem(tr("Inch"),25.4); + connect(munit,SIGNAL(currentIndexChanged(int)),this,SLOT(unitChanged(int))); + fl->addRow(tr("Paper Type:"),mpaper=new QComboBox); + initPaper(); + cur=0; + for(const auto&paper:paperTypes){ + if(mprinter->paperSize()==paper.ptype)cur=mpaper->count(); + mpaper->addItem(paper.pname,paper.ptype); + } + mpaper->setCurrentIndex(cur); + connect(mpaper,SIGNAL(currentIndexChanged(int)), this,SLOT(paperChanged(int))); + QHBoxLayout*hl; + fl->addRow(tr("Paper Size:"),hl=new QHBoxLayout); + hl->addWidget(msizex=new QDoubleSpinBox,1); + msizex->setRange(0,10000); + hl->addWidget(new QLabel(tr("wide x","paper size: width"))); + hl->addWidget(msizey=new QDoubleSpinBox); + msizey->setRange(0,10000); + hl->addWidget(new QLabel(tr("high","paper size: height"))); + QGridLayout*gl; + fl->addRow(tr("Margins:"),gl=new QGridLayout); + qreal ml,mr,mt,mb; + mprinter->getPageMargins(&ml,&mt,&mr,&mb,QPrinter::Millimeter); + gl->addWidget(mmargtop=new QDoubleSpinBox,0,1); + mmargtop->setValue(mt/moldunit); + gl->addWidget(mmargleft=new QDoubleSpinBox,1,0); + mmargleft->setValue(ml/moldunit); + gl->addWidget(mmargright=new QDoubleSpinBox,1,2); + mmargright->setValue(mr/moldunit); + gl->addWidget(mmargbottom=new QDoubleSpinBox,2,1); + mmargbottom->setValue(mb/moldunit); + fl->addRow(tr("Resolution:"),mresolution=new QComboBox); + fl->addRow(new QLabel(" ")); + fl->addRow(hl=new QHBoxLayout); + hl->addStretch(1); + hl->addWidget(mokbtn=new QPushButton(tr("Print"))); + connect(mokbtn,SIGNAL(clicked(bool)),this,SLOT(okBtnPressed())); + QPushButton *cc; + hl->addWidget(cc=new QPushButton(tr("Cancel"))); + connect(cc,SIGNAL(clicked(bool)),this,SLOT(reject())); + + //make sure it is selected + printerChanged(mprnselect->currentIndex()); + + //reset selected paper type and size from printer + paperChanged(mpaper->currentIndex()); + //reset margins from printer +} + +void MLabelPrintDialog::setButtonLabel(const QString& l) +{ + mokbtn->setText(l); +} + +// QString MLabelPrintDialog::paperSourceStr(int p) +// { +// switch(p){ +// case QPrinter::Auto:return tr("Automatic","paper source"); +// case QPrinter::Cassette:return tr("Cassette","paper source"); +// case QPrinter::Envelope:return tr("Envelope","paper source"); +// case QPrinter::EnvelopeManual:return tr("Manual Envelope","paper source"); +// case QPrinter::FormSource:return tr("Forms","paper source"); +// case QPrinter::LargeCapacity:return tr("Large Capacity","paper source"); +// case QPrinter::LargeFormat:return tr("Large Format","paper source"); +// case QPrinter::Lower:return tr("Lower Tray","paper source"); +// case QPrinter::MaxPageSource:return tr("Maximum Page Source","paper source"); +// case QPrinter::Middle:return tr("Middle Tray","paper source"); +// case QPrinter::Manual:return tr("Manual Feed","paper source"); +// case QPrinter::OnlyOne:return tr("Only One Source","paper source"); +// case QPrinter::Tractor:return tr("Tractor","paper source"); +// case QPrinter::SmallFormat:return tr("Small Format","paper source"); +// default:return tr("Unknown","paper source"); +// } +// } + +void MLabelPrintDialog::printerChanged(int idx) +{ + //get printer + const QString &pname=mprnselect->itemText(idx); + if(pname.isEmpty())return; + //set location and description + const QPrinterInfo info=QPrinterInfo::printerInfo(pname); + if(info.isNull())return; + mmakemodel->setText(info.makeAndModel()); + mlocation->setText(info.location()); + mdescript->setText(info.description()); + //set resolution + QPrinter prn(info); + mresolution->clear(); + const int cur=mresolution->itemData(mresolution->currentIndex()).toInt(); + int curidx=-1; + const int def=prn.resolution(); + int defidx=-1; + for(auto r:prn.supportedResolutions()){ + if(r==cur)curidx=mresolution->count(); + if(r==def)defidx=mresolution->count(); + mresolution->addItem(QString("%1 dpi").arg(r),r); + } + if(curidx>=0)defidx=curidx; + if(defidx>=0)mresolution->setCurrentIndex(defidx); +} + +void MLabelPrintDialog::paperChanged(int idx) +{ + const int ptype=mpaper->itemData(idx).toInt(); + for(const auto&paper:paperTypes){ + if(paper==ptype){ + //set paper size + if(ptype == QPrinter::Custom){ + msizex->setValue(mprinter->pageSizeMM().width()/moldunit); + msizey->setValue(mprinter->pageSizeMM().height()/moldunit); + }else{ + msizex->setValue(paper.psize.width()/moldunit); + msizey->setValue(paper.psize.height()/moldunit); + } + break; + } + } + msizex->setEnabled(ptype==QPrinter::Custom); + msizey->setEnabled(ptype==QPrinter::Custom); +} + +void MLabelPrintDialog::unitChanged(int idx) +{ + const double fac=munit->itemData(idx).toDouble(); + //recalculate everything + msizex->setValue(msizex->value()*moldunit/fac); + msizey->setValue(msizey->value()*moldunit/fac); + mmargbottom->setValue(mmargbottom->value()*moldunit/fac); + mmargleft->setValue(mmargleft->value()*moldunit/fac); + mmargright->setValue(mmargright->value()*moldunit/fac); + mmargtop->setValue(mmargtop->value()*moldunit/fac); + //remember + moldunit=fac; +} + +void MLabelPrintDialog::okBtnPressed() +{ + //save data to printer + mprinter->setPrinterName(mprnselect->itemText(mprnselect->currentIndex())); + mprinter->setPaperSize(QPrinter::PaperSize(mpaper->itemData(mpaper->currentIndex()).toInt())); + mprinter->setPageSizeMM(QSizeF(msizex->value()*moldunit,msizey->value()*moldunit)); + mprinter->setPageMargins(mmargleft->value()*moldunit, mmargtop->value()*moldunit, mmargright->value()*moldunit, mmargbottom->value()*moldunit, QPrinter::Millimeter); + mprinter->setResolution(mresolution->itemData(mresolution->currentIndex()).toInt()); + + //close the dialog + accept(); +} diff --git a/src/templates/labeldlg.h b/src/templates/labeldlg.h index ff9537e..001ada6 100644 --- a/src/templates/labeldlg.h +++ b/src/templates/labeldlg.h @@ -17,6 +17,7 @@ #include #include +class QDoubleSpinBox; class QPaintDevice; class QCheckBox; class QComboBox; @@ -33,15 +34,19 @@ class MLabelConfig ///the type of label whose configuration is being queried enum LabelType { + ///invalid config object + Invalid=0, ///ticket label - Ticket, + Ticket=1, ///voucher label - Voucher + Voucher=2 }; + ///instantiate an empty configuration object + MLabelConfig(){initMeta();} ///instantiates a configuration object for the given label type - MLabelConfig(LabelType lt):m_type(lt){} + MLabelConfig(LabelType lt):m_type(lt){initMeta();} ///copies the config object - MLabelConfig(const MLabelConfig&lc):m_type(lc.m_type){} + MLabelConfig(const MLabelConfig&lc):m_type(lc.m_type){initMeta();} ///copies the config object MLabelConfig& operator=(const MLabelConfig&lc){m_type=lc.m_type;return *this;} @@ -103,7 +108,17 @@ class MLabelConfig void storeAskModes(PrintAskMode,PageAskMode); /// \internal return the config group QString cgroup()const; + + /// \internal initialize meta type info + void initMeta(); + + friend QDataStream&operator<<(QDataStream&,const MLabelConfig&); + friend QDataStream&operator>>(QDataStream&,MLabelConfig&); }; +QDataStream &operator<<(QDataStream &out, const MLabelConfig &myObj); +QDataStream &operator>>(QDataStream &in, MLabelConfig &myObj); + +Q_DECLARE_METATYPE(MLabelConfig); ///dialog that allows to select which labels on a sheet of paper are used class MLabelDialog:public QDialog @@ -167,4 +182,31 @@ class MLabelDialog:public QDialog int findLabel(int n,int&row,int&col)const; }; + +class MLabelPrintDialog:public QDialog +{ + Q_OBJECT + public: + explicit MLabelPrintDialog(QPrinter*,QWidget* parent = 0, Qt::WindowFlags f = 0); + public slots: + void setButtonLabel(const QString&); + private slots: + void printerChanged(int); + void paperChanged(int); + void unitChanged(int); + + void okBtnPressed(); + private: + QPrinter*mprinter; + QComboBox*mprnselect,*mresolution,*mpaper,*munit; + QDoubleSpinBox*mmargleft,*mmargright,*mmargtop,*mmargbottom; + QDoubleSpinBox*msizex,*msizey; + QLabel*mdescript,*mlocation,*mmakemodel; + QPushButton*mokbtn; + double moldunit=1.; + + //internal: initialize paper types + void initPaper(); +}; + #endif