move some more label printing code to label dialog
authorKonrad Rosenbaum <konrad@silmor.de>
Sun, 15 Jan 2012 20:25:29 +0000 (21:25 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Sun, 15 Jan 2012 20:25:29 +0000 (21:25 +0100)
src/dialogs/orderwin.cpp
src/dialogs/orderwin.h
src/templates/labeldlg.cpp
src/templates/labeldlg.h

index 5c57eb8..e1d3170 100644 (file)
@@ -339,10 +339,11 @@ void MOrderWindow::printTickets(QList<MOTicket> ticketsin)
        }
        //get printer settings
        QPrinter printer;
-       restorePrinter(printer,"ticket");
+       MLabelConfig lconf(MLabelConfig::Ticket);
+       lconf.selectPrinter(printer);
        QPrintDialog pd(&printer,this);
        if(pd.exec()!=QDialog::Accepted)return;
-       storePrinter(printer,"ticket");
+//     storePrinter(printer,"ticket");
        //label arrangement
        MTicketRenderer render(tf);
        MLabelDialog ld(this,&printer,tickets.size(),render.labelSize(printer));
@@ -380,10 +381,11 @@ void MOrderWindow::printVouchers(QList<MOVoucher> vouchersin)
        }
        //get printer settings
        QPrinter printer;
-       restorePrinter(printer,"voucher");
+       MLabelConfig lconf(MLabelConfig::Voucher);
+       lconf.selectPrinter(printer);
        QPrintDialog pd(&printer,this);
        if(pd.exec()!=QDialog::Accepted)return;
-       storePrinter(printer,"voucher");
+//     storePrinter(printer,"voucher");
        //label arrangement
        MVoucherRenderer render(tf);
        MLabelDialog ld(this,&printer,vouchers.size(),render.labelSize(printer));
@@ -400,6 +402,7 @@ void MOrderWindow::printVouchers(QList<MOVoucher> vouchersin)
        }
 }
 
+/*
 void MOrderWindow::restorePrinter(QPrinter&prn,QString key)
 {
 #if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)
@@ -442,6 +445,7 @@ void MOrderWindow::storePrinter(QPrinter&prn,QString key)
        set.setValue("papersize",prn.paperSize(QPrinter::Point));
        set.setValue("resolution",prn.resolution());
 }
+*/
 
 void MOrderWindow::printBill()
 {
index a6f65eb..446a801 100644 (file)
@@ -130,9 +130,9 @@ class MOrderWindow:public QMainWindow
                void donePrintBuffer();
                
                //helper: fetches printer settings from registry
-               void restorePrinter(QPrinter&,QString);
+//             void restorePrinter(QPrinter&,QString);
                //helper: stores printer settings in registry
-               void storePrinter(QPrinter&,QString);
+//             void storePrinter(QPrinter&,QString);
 };
 
 class MTicketRenderer;
index 80f10f2..0811de4 100644 (file)
@@ -13,6 +13,8 @@
 #include "labeldlg.h"
 #include "lambda.h"
 
+#include "msinterface.h"
+
 #include <QBoxLayout>
 #include <QCheckBox>
 #include <QComboBox>
@@ -24,6 +26,7 @@
 #include <QLineEdit>
 #include <QPrintDialog>
 #include <QPrinter>
+#include <QPrinterInfo>
 #include <QPushButton>
 #include <QScrollArea>
 #include <QSettings>
@@ -383,7 +386,7 @@ void MLabelConfig::configDialog(QWidget* parent)
        v_pagmode->addItem(tr("Never ask for page layout"),PageAskNever);
        QLabel*v_prn;QMap<QString,QVariant>v_prnset;
        fl->addRow(tr("Printer:"),hl=new QHBoxLayout);
-       hl->addWidget(t_prn=new QLabel("printer"),1);
+       hl->addWidget(v_prn=new QLabel("printer"),1);
        hl->addWidget(p=new QToolButton);
        p->setText("...");
        MLambda setvprn([&]{
@@ -410,3 +413,96 @@ QString MLabelConfig::tr(const char* s, const char* c)
 {
        return QCoreApplication::translate("MLabelConfig",s,c);
 }
+
+QString MLabelConfig::printerName() const
+{
+       QString key=m_type==Ticket?"ticket":"voucher";
+       QSettings set;
+       set.beginGroup(req->settingsGroup()+"/"+key);
+       if(!set.contains("name"))return QString();
+       return set.value("name").toString();
+}
+
+bool MLabelConfig::selectPrinter(QPrinter& printer, bool force)
+{
+       bool gotit=configurePrinter(printer);
+       if(!force)
+       switch(printAskMode()){
+               case PageAskAlways:force=true;break;
+               case PageAskIfNeeded:if(!gotit)force=true;break;
+               case PageAskNever:/*do not override the force*/ break;
+       }
+       if(force){
+               QPrintDialog pd(&printer);
+               return pd.exec()==QDialog::Accepted;
+       }else
+               return gotit;
+}
+
+bool MLabelConfig::configurePrinter(QPrinter&prn ) const
+{
+       QString key=m_type==Ticket?"ticket":"voucher";
+       QSettings set;
+       set.beginGroup(req->settingsGroup()+"/"+key);
+       if(!set.contains("name"))return false;
+       QString pname=set.value("name").toString();
+       bool found=false;
+       for(QPrinterInfo &info : QPrinterInfo::availablePrinters()){
+               if(info.printerName()==pname){
+                       found=true;
+                       break;
+               }
+       }
+       if(!found)return false;
+       prn.setPrinterName(pname);
+#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);
+       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.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());
+       return true;
+}
+
+void MLabelConfig::storePrinter(QPrinter& prn) const
+{
+       QString key=m_type==Ticket?"ticket":"voucher";
+       QSettings set;
+       set.beginGroup(req->settingsGroup()+"/"+key);
+       set.setValue("name",prn.printerName());
+#if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)
+       set.setValue("soption",prn.printerSelectionOption());
+#endif
+       qreal ml,mr,mt,mb;
+       prn.getPageMargins(&ml,&mr,&mb,&mt,QPrinter::Point);
+       set.setValue("marginLeft",ml);
+       set.setValue("marginRight",mr);
+       set.setValue("marginBottom",mb);
+       set.setValue("marginTop",mt);
+       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("resolution",prn.resolution());
+}
+
+
+MLabelConfig::PageAskMode MLabelConfig::pageAskMode() const
+{
+       //TODO: implement
+       return PageAskAlways;
+}
+
+MLabelConfig::PrintAskMode MLabelConfig::printAskMode() const
+{
+       //TODO: implement
+       return PrintAskAlways;
+}
index 322e64c..b746b73 100644 (file)
@@ -54,7 +54,7 @@ class MLabelConfig
                ///configures the QPrinter object to the printer set for this label type;
                ///use selectPrinter if you want to fall back to a selection dialog
                /// \returns true if the printer was successfully configured
-               bool configurePrinter(QPrinter*)const;
+               bool configurePrinter(QPrinter&)const;
                
                ///configures the printer for this particular label type;
                ///whether the dialog is shown depends on settings and printer availability;
@@ -62,7 +62,7 @@ class MLabelConfig
                /// \param printer pointer to the QPrinter object to be configured
                /// \param force if true: force showing the printer dialog
                /// \returns true if the selection was successful
-               bool selectPrinter(QPrinter*printer,bool force=false);
+               bool selectPrinter(QPrinter&printer,bool force=false);
                
                ///whether to ask for a printer when printing labels
                enum PrintAskMode{
@@ -83,9 +83,18 @@ class MLabelConfig
                        PageAskNever=2,
                };
                
+               ///returns the policy for asking for a printer
+               PrintAskMode printAskMode()const;
+               
+               ///returns the policy for asking for page layout
+               PageAskMode pageAskMode()const;
+               
        private:
                LabelType m_type;
                static QString tr(const char*,const char*c=0);
+               
+               /// \internal helper to store printer settings for next time
+               void storePrinter(QPrinter&)const;
 };
 
 ///dialog that allows to select which labels on a sheet of paper are used
@@ -111,7 +120,7 @@ class MLabelDialog:public QDialog
                /// \param printer pointer to the QPrinter object to be configured
                /// \param force if true: force showing the printer dialog
                /// \returns true if the selection was successful
-               static inline bool selectPrinter(MLabelConfig::LabelType type, QPrinter*printer, bool force=false)
+               static inline bool selectPrinter(MLabelConfig::LabelType type, QPrinter&printer, bool force=false)
                {return MLabelConfig(type).selectPrinter(printer,force);}
                
        public slots: