QString RSettings::getDefaultPrinterName() { #if QT_VERSION >= 0x050300 return QPrinterInfo::defaultPrinterName(); #else QPrinterInfo printer = QPrinterInfo::defaultPrinter(); if (printer.isNull()) { return QString(); } return printer.printerName(); #endif }
QDebug operator<<(QDebug debug, const QPrinterInfo &p) { QDebugStateSaver saver(debug); debug.nospace(); debug << "QPrinterInfo("; if (p.isNull()) debug << "null"; else p.d_ptr->m_printDevice.format(debug); debug << ')'; return debug; }
bool PrinterUtil::getPrinterMarginValues(const QString& printerName, const QString& pageSize, double& ptsTopMargin, double& ptsBottomMargin, double& ptsLeftMargin, double& ptsRightMargin) { bool retVal = false; QPrinterInfo pInfo = QPrinterInfo::printerInfo(printerName); if (!pInfo.isNull()) { QPrinter printer(pInfo, QPrinter::HighResolution); QMarginsF margs = printer.pageLayout().margins(QPageLayout::Point); ptsTopMargin = margs.top(); ptsBottomMargin = margs.bottom(); ptsLeftMargin = margs.left(); ptsRightMargin = margs.right(); retVal = true; } return retVal; }
void QCupsPrintEnginePrivate::closePrintDevice() { QPdfPrintEnginePrivate::closePrintDevice(); if (!cupsTempFile.isEmpty()) { QString tempFile = cupsTempFile; cupsTempFile.clear(); QCUPSSupport cups; // Set up print options. QByteArray prnName; QList<QPair<QByteArray, QByteArray> > options; QVector<cups_option_t> cupsOptStruct; if (!printerName.isEmpty()) { prnName = printerName.toLocal8Bit(); } else { QPrinterInfo def = QPrinterInfo::defaultPrinter(); if (def.isNull()) { qWarning("Could not determine printer to print to"); QFile::remove(tempFile); return; } prnName = def.printerName().toLocal8Bit(); } if (!cupsStringPageSize.isEmpty()) options.append(QPair<QByteArray, QByteArray>("media", cupsStringPageSize.toLocal8Bit())); if (copies > 1) options.append(QPair<QByteArray, QByteArray>("copies", QString::number(copies).toLocal8Bit())); if (collate) options.append(QPair<QByteArray, QByteArray>("Collate", "True")); switch (duplex) { case QPrinter::DuplexNone: options.append(QPair<QByteArray, QByteArray>("sides", "one-sided")); break; case QPrinter::DuplexAuto: if (!landscape) options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-long-edge")); else options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-short-edge")); break; case QPrinter::DuplexLongSide: options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-long-edge")); break; case QPrinter::DuplexShortSide: options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-short-edge")); break; } if (QCUPSSupport::cupsVersion() >= 10300 && landscape) options.append(QPair<QByteArray, QByteArray>("landscape", "")); QStringList::const_iterator it = cupsOptions.constBegin(); while (it != cupsOptions.constEnd()) { options.append(QPair<QByteArray, QByteArray>((*it).toLocal8Bit(), (*(it+1)).toLocal8Bit())); it += 2; } for (int c = 0; c < options.size(); ++c) { cups_option_t opt; opt.name = options[c].first.data(); opt.value = options[c].second.data(); cupsOptStruct.append(opt); } // Print the file. cups_option_t* optPtr = cupsOptStruct.size() ? &cupsOptStruct.first() : 0; cups.printFile(prnName.constData(), tempFile.toLocal8Bit().constData(), title.toLocal8Bit().constData(), cupsOptStruct.size(), optPtr); QFile::remove(tempFile); } }
/* static Receipts::ReceiptsPlugin *receiptsPlugin() { qff::MainWindow *fwk = qff::MainWindow::frameWork(); auto *ret = qobject_cast<Receipts::ReceiptsPlugin *>(fwk->plugin("Receipts")); QF_ASSERT(ret != nullptr, "Bad plugin", return 0); return ret; } */ void ReceiptsPrinter::printReceipt(const QString &report_file_name, const QVariantMap &report_data) { qfLogFuncFrame(); QF_TIME_SCOPE("ReceiptsPrinter::printReceipt()"); const ReceiptsPrinterOptions &printer_opts = m_printerOptions; QPrinter *printer = nullptr; QPaintDevice *paint_device = nullptr; if(printer_opts.printerType() == (int)ReceiptsPrinterOptions::PrinterType::GraphicPrinter) { QF_TIME_SCOPE("init graphics printer"); QPrinterInfo pi = QPrinterInfo::printerInfo(printer_opts.graphicsPrinterName()); if(pi.isNull()) { for(auto s : QPrinterInfo::availablePrinterNames()) { qfInfo() << "available printer:" << s; } pi = QPrinterInfo::defaultPrinter(); } if(pi.isNull()) { qfWarning() << "Default printer not set"; return; } qfInfo() << "printing on:" << pi.printerName(); printer = new QPrinter(pi); paint_device = printer; } else { qfInfo() << "printing on:" << printer_opts.characterPrinterModel() << "at:" << printer_opts.characterPrinterDevice(); qff::MainWindow *fwk = qff::MainWindow::frameWork(); paint_device = fwk; } qf::qmlwidgets::reports::ReportProcessor rp(paint_device); { QF_TIME_SCOPE("setting report and data"); rp.setReport(report_file_name); for(auto key : report_data.keys()) { rp.setTableData(key, report_data.value(key)); } } if(printer_opts.printerType() == (int)ReceiptsPrinterOptions::PrinterType::GraphicPrinter) { QF_TIME_SCOPE("process graphics"); { QF_TIME_SCOPE("process report"); rp.process(); } qf::qmlwidgets::reports::ReportItemMetaPaintReport *doc; { QF_TIME_SCOPE("getting processor output"); doc = rp.processorOutput(); } qf::qmlwidgets::reports::ReportItemMetaPaint *it = doc->child(0); if(it) { QF_TIME_SCOPE("draw meta-paint"); qf::qmlwidgets::reports::ReportPainter painter(paint_device); painter.drawMetaPaint(it); } QF_SAFE_DELETE(printer); } else if(printer_opts.printerType() == (int)ReceiptsPrinterOptions::PrinterType::CharacterPrinter) { QDomDocument doc; doc.setContent(QLatin1String("<?xml version=\"1.0\"?><report><body/></report>")); QDomElement el_body = doc.documentElement().firstChildElement("body"); qf::qmlwidgets::reports::ReportProcessor::HtmlExportOptions opts; opts.setConvertBandsToTables(false); rp.processHtml(el_body, opts); //qfInfo() << doc.toString(); QList<QByteArray> data_lines = createPrinterData(el_body, printer_opts); auto save_file = [data_lines](const QString &fn) { QFile f(fn); if(f.open(QFile::WriteOnly)) { for(QByteArray ba : data_lines) { f.write(ba); f.write("\n"); } } else { qfError() << "Cannot open file" << f.fileName() << "for writing!"; } }; if(!printer_opts.characterPrinterDirectory().isEmpty()) { QString fn = printer_opts.characterPrinterDirectory(); qf::core::utils::FileUtils::ensurePath(fn); QCryptographicHash ch(QCryptographicHash::Sha1); for(QByteArray ba : data_lines) ch.addData(ba); fn += '/' + QString::fromLatin1(ch.result().toHex().mid(0, 8)) + ".txt"; save_file(fn); } else if (!printer_opts.characterPrinterDevice().isEmpty()) { save_file(printer_opts.characterPrinterDevice()); } } }