QList<QPrinterInfo> QPrinterInfo::availablePrinters() { QList<QPrinterInfo> printers; #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) if (QCUPSSupport::isAvailable()) { QCUPSSupport cups; int cupsPrinterCount = cups.availablePrintersCount(); const cups_dest_t* cupsPrinters = cups.availablePrinters(); for (int i = 0; i < cupsPrinterCount; ++i) { QString printerName(QString::fromLocal8Bit(cupsPrinters[i].name)); if (cupsPrinters[i].instance) printerName += QLatin1Char('/') + QString::fromLocal8Bit(cupsPrinters[i].instance); QPrinterInfo printerInfo(printerName); if (cupsPrinters[i].is_default) printerInfo.d_ptr->isDefault = true; printerInfo.d_ptr->cupsPrinterIndex = i; printers.append(printerInfo); } } else #endif { QList<QPrinterDescription> lprPrinters; int defprn = qt_getLprPrinters(lprPrinters); // populating printer combo foreach (const QPrinterDescription &description, lprPrinters) printers.append(QPrinterInfo(description.name)); if (defprn >= 0 && defprn < printers.size()) printers[defprn].d_ptr->isDefault = true; } return printers; }
QList<QCUPSSupport::Printer> QCUPSSupport::availableUnixPrinters() { QList<Printer> printers; if (QCUPSSupport::isAvailable()) { QCUPSSupport cups; int cupsPrinterCount = cups.availablePrintersCount(); const cups_dest_t* cupsPrinters = cups.availablePrinters(); for (int i = 0; i < cupsPrinterCount; ++i) { QString printerName(QString::fromLocal8Bit(cupsPrinters[i].name)); if (cupsPrinters[i].instance) printerName += QLatin1Char('/') + QString::fromLocal8Bit(cupsPrinters[i].instance); Printer p(printerName); if (cupsPrinters[i].is_default) p.isDefault = true; p.cupsPrinterIndex = i; printers.append(p); } } return printers; }
QT_BEGIN_NAMESPACE QCupsPrintEngine::QCupsPrintEngine(QPrinter::PrinterMode m) : QPdfPrintEngine(*new QCupsPrintEnginePrivate(m)) { Q_D(QCupsPrintEngine); if (QCUPSSupport::isAvailable()) { QCUPSSupport cups; const cups_dest_t* printers = cups.availablePrinters(); int prnCount = cups.availablePrintersCount(); for (int i = 0; i < prnCount; ++i) { if (printers[i].is_default) { d->printerName = QString::fromLocal8Bit(printers[i].name); d->setCupsDefaults(); break; } } } state = QPrinter::Idle; }
void QCupsPrintEnginePrivate::setCupsDefaults() { if (QCUPSSupport::isAvailable()) { int cupsPrinterIndex = -1; QCUPSSupport cups; const cups_dest_t* printers = cups.availablePrinters(); int prnCount = cups.availablePrintersCount(); for (int i = 0; i < prnCount; ++i) { QString name = QString::fromLocal8Bit(printers[i].name); if (name == printerName) { cupsPrinterIndex = i; break; } } if (cupsPrinterIndex < 0) return; cups.setCurrentPrinter(cupsPrinterIndex); if (cups.currentPPD()) { const ppd_option_t *ppdDuplex = cups.ppdOption("Duplex"); if (ppdDuplex) { if (qstrcmp(ppdDuplex->defchoice, "DuplexTumble") == 0) duplex = QPrinter::DuplexShortSide; else if (qstrcmp(ppdDuplex->defchoice, "DuplexNoTumble") == 0) duplex = QPrinter::DuplexLongSide; else duplex = QPrinter::DuplexNone; } grayscale = !cups.currentPPD()->color_device; const ppd_option_t *ppdCollate = cups.ppdOption("Collate"); if (ppdCollate) collate = qstrcmp(ppdCollate->defchoice, "True") == 0; const ppd_option_t* pageSizes = cups.pageSizes(); QByteArray cupsPageSize; for (int i = 0; i < pageSizes->num_choices; ++i) { if (static_cast<int>(pageSizes->choices[i].marked) == 1) cupsPageSize = pageSizes->choices[i].choice; } cupsOptions = cups.options(); cupsPaperRect = cups.paperRect(cupsPageSize); cupsPageRect = cups.pageRect(cupsPageSize); for (int ps = 0; ps < QPrinter::NPageSize; ++ps) { QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(ps)); if (qAbs(size.width - cupsPaperRect.width()) < 5 && qAbs(size.height - cupsPaperRect.height()) < 5) { printerPaperSize = static_cast<QPrinter::PaperSize>(ps); leftMargin = cupsPageRect.x() - cupsPaperRect.x(); topMargin = cupsPageRect.y() - cupsPaperRect.y(); rightMargin = cupsPaperRect.right() - cupsPageRect.right(); bottomMargin = cupsPaperRect.bottom() - cupsPageRect.bottom(); updatePaperSize(); break; } } } } }