QList<QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const
{
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
    const Q_D(QPrinterInfo);

    if (isNull())
        return d->paperSizes;

    if (!d->hasPaperSizes) {
        d->hasPaperSizes = true;

        if (QCUPSSupport::isAvailable()) {
            // Find paper sizes from CUPS.
            QCUPSSupport cups;
            cups.setCurrentPrinter(d->cupsPrinterIndex);
            const ppd_option_t* sizes = cups.pageSizes();
            if (sizes) {
                for (int j = 0; j < sizes->num_choices; ++j)
                    d->paperSizes.append(string2PaperSize(sizes->choices[j].choice));
            }
        }
    }

    return d->paperSizes;
#else
    return QList<QPrinter::PaperSize>();
#endif
}
Example #2
0
void QCupsPrintEnginePrivate::setPaperSize()
{
    if (QCUPSSupport::isAvailable()) {
        QCUPSSupport cups;
        QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(printerPaperSize));

        if (cups.currentPPD()) {
            const ppd_option_t* pageSizes = cups.pageSizes();
            for (int i = 0; i < pageSizes->num_choices; ++i) {
                QByteArray cupsPageSize = pageSizes->choices[i].choice;
                QRect tmpCupsPaperRect = cups.paperRect(cupsPageSize);
                QRect tmpCupsPageRect = cups.pageRect(cupsPageSize);

                if (qAbs(size.width - tmpCupsPaperRect.width()) < 5  && qAbs(size.height - tmpCupsPaperRect.height()) < 5) {
                    cupsPaperRect = tmpCupsPaperRect;
                    cupsPageRect = tmpCupsPageRect;

                    leftMargin = cupsPageRect.x() - cupsPaperRect.x();
                    topMargin = cupsPageRect.y() - cupsPaperRect.y();
                    rightMargin = cupsPaperRect.right() - cupsPageRect.right();
                    bottomMargin = cupsPaperRect.bottom() - cupsPageRect.bottom();

                    updatePaperSize();
                    break;
                }
            }
        }
    }
}
Example #3
0
QList<QPrinter::PaperSize> QCUPSSupport::getCupsPrinterPaperSizes(int cupsPrinterIndex)
{
    QList<QPrinter::PaperSize> result;
    if (!QCUPSSupport::isAvailable() || cupsPrinterIndex < 0)
        return result;
    // Find paper sizes from CUPS.
    QCUPSSupport cups;
    cups.setCurrentPrinter(cupsPrinterIndex);
    if (const ppd_option_t* size = cups.pageSizes()) {
        for (int j = 0; j < size->num_choices; ++j)
            result.append(string2PaperSize(size->choices[j].choice));
    }
    return result;
}
Example #4
0
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;
                }
            }
        }
    }
}