#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>
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
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());
};
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...
case PrintAskNever:/*do not override the force*/ break;
}
if(force){
- QPrintDialog pd(&printer);
+ MLabelPrintDialog pd(&printer);
return pd.exec()==QDialog::Accepted;
}else
return gotit;
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;
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;
}
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);
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();
}
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();
+}