fix label printing
authorKonrad Rosenbaum <konrad@silmor.de>
Fri, 12 Jul 2013 14:04:52 +0000 (16:04 +0200)
committerKonrad Rosenbaum <konrad@silmor.de>
Fri, 12 Jul 2013 14:04:52 +0000 (16:04 +0200)
src/templates/labeldlg.cpp
src/templates/labeldlg.h

index 41181af..b8b2e9e 100644 (file)
 #include <QComboBox>
 #include <QCoreApplication>
 #include <QDebug>
+#include <QDoubleSpinBox>
 #include <QFormLayout>
 #include <QGridLayout>
 #include <QLabel>
 #include <QLineEdit>
-#include <QPrintDialog>
+// #include <QPrintDialog>
 #include <QPrinter>
 #include <QPrinterInfo>
 #include <QPushButton>
@@ -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<MLabelConfig>();
+        qRegisterMetaTypeStreamOperators<MLabelConfig>();
+}
+
 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"<<p.printerName()<<s<<"valid?"<<p.isValid();
+        qDebug()<<"  page mm"<<p.pageSizeMM();
+        qreal ml,mr,mt,mb;
+        p.getPageMargins(&ml,&mr,&mb,&mt,QPrinter::Millimeter);
+        qDebug()<<"  margin"<<ml<<mr<<mb<<mt;
+        qDebug()<<"  duplex"<<p.duplex()
+        <<"orient"<<(p.orientation()==QPrinter::Portrait?"portrait":"landscape");
+        qDebug()<<"  resolution"<<p.resolution();
+}
+
 bool MLabelConfig::configurePrinter(QPrinter&prn ) const
 {
        QSettings set;
@@ -492,21 +534,27 @@ bool MLabelConfig::configurePrinter(QPrinter&prn ) const
                 return false;
         }
        prn.setPrinterName(pname);
-        qDebug()<<"config prn"<<prn.isValid();
+        dumpPrn(prn,"before setting printer object");
 #if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)
        prn.setPrinterSelectionOption(set.value("soption","").toString());
 #endif
-       prn.setPaperSize(QPrinter::PageSize(set.value("paper",QPrinter::Custom).toInt()));
-       prn.setPaperSize(set.value("papersize").toSizeF(),QPrinter::Point);
+        QPrinter::PageSize pgsz=QPrinter::PageSize(set.value("paper",QPrinter::Custom).toInt());
+       prn.setPaperSize(pgsz);
+        if(pgsz==QPrinter::Custom)
+                prn.setPageSizeMM(set.value("papersizemm").toSizeF());
        qreal ml,mr,mt,mb;
        ml=set.value("marginLeft").toDouble();
        mr=set.value("marginRight").toDouble();
        mb=set.value("marginBottom").toDouble();
        mt=set.value("marginTop").toDouble();
-       prn.setPageMargins(ml,mr,mb,mt,QPrinter::Point);
+       prn.setPageMargins(ml,mr,mb,mt,QPrinter::Millimeter);
        prn.setDuplex(QPrinter::DuplexMode(set.value("duplex",QPrinter::DuplexAuto).toInt()));
        prn.setOrientation((set.value("orientation","portrait").toString()=="portrait")?QPrinter::Portrait:QPrinter::Landscape);
-       prn.setResolution(set.value("resolution").toInt());
+        if(set.contains("resolution"))
+                prn.setResolution(set.value("resolution").toInt());
+        if(set.contains("papersource"))
+                prn.setPaperSource(QPrinter::PaperSource(set.value("papersource").toInt()));
+        dumpPrn(prn,"after setting printer object");
        return true;
 }
 
@@ -519,7 +567,7 @@ void MLabelConfig::storePrinter(QPrinter& prn)
        set.setValue("soption",prn.printerSelectionOption());
 #endif
        qreal ml,mr,mt,mb;
-       prn.getPageMargins(&ml,&mr,&mb,&mt,QPrinter::Point);
+       prn.getPageMargins(&ml,&mr,&mb,&mt,QPrinter::Millimeter);
        set.setValue("marginLeft",ml);
        set.setValue("marginRight",mr);
        set.setValue("marginBottom",mb);
@@ -527,8 +575,11 @@ void MLabelConfig::storePrinter(QPrinter& prn)
        set.setValue("duplex",(int)prn.duplex());
        set.setValue("orientation",prn.orientation()==QPrinter::Portrait?"portrait":"landscape");
        set.setValue("paper",(int)prn.paperSize());
-       set.setValue("papersize",prn.paperSize(QPrinter::Point));
+//         set.setValue("papersize",prn.paperSize(QPrinter::Point));
+        set.setValue("papersizemm",prn.pageSizeMM());
        set.setValue("resolution",prn.resolution());
+        set.setValue("papersource",prn.paperSource());
+        set.setValue("settings",QVariant::fromValue(*this));
         qDebug()<<"storing printer settings"<<cgroup()<<prn.printerName()<<prn.paperSize();
 }
 
@@ -554,3 +605,235 @@ void MLabelConfig::storeAskModes(MLabelConfig::PrintAskMode prm, MLabelConfig::P
        set.setValue("printAskMode",(int)prm);
        set.setValue("pageAskMode",(int)pgm);
 }
+
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+struct Paper_s {
+        int ptype=0;
+        QSizeF psize;
+        QString pname;
+        
+        Paper_s()=default;
+        Paper_s(int t):ptype(t){}
+        Paper_s(const Paper_s&)=default;
+        Paper_s(const QString&n,int t,double x,double y):ptype(t),psize(x,y),pname(n){}
+        
+        Paper_s&operator=(const Paper_s&)=default;
+        
+        bool operator==(const Paper_s&t)const{return ptype==t.ptype;}
+        bool operator==(int t)const{return ptype==t;}
+};
+
+static QList<Paper_s> paperTypes;
+
+void MLabelPrintDialog::initPaper()
+{
+        if(paperTypes.size())return;
+        paperTypes=QList<Paper_s>()
+        << 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();
+}
index ff9537e..001ada6 100644 (file)
@@ -17,6 +17,7 @@
 #include <QPointF>
 #include <QList>
 
+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