Exemplo n.º 1
0
bool QCupsPrintEnginePrivate::openPrintDevice()
{
    if (outDevice)
        return false;

    if (!outputFileName.isEmpty()) {
        QFile *file = new QFile(outputFileName);
        if (! file->open(QFile::WriteOnly|QFile::Truncate)) {
            delete file;
            return false;
        }
        outDevice = file;
    } else if (QCUPSSupport::isAvailable()) {
        QCUPSSupport cups;
        QPair<int, QString> ret = cups.tempFd();
        if (ret.first < 0) {
            qWarning("QPdfPrinter: Could not open temporary file to print");
            return false;
        }
        cupsTempFile = ret.second;
        outDevice = new QFile();
        static_cast<QFile *>(outDevice)->open(ret.first, QIODevice::WriteOnly);
        fd = ret.first;
    }

    return true;
}
Exemplo n.º 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;
                }
            }
        }
    }
}
Exemplo n.º 3
0
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
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
0
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;
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
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;
                }
            }
        }
    }
}
Exemplo n.º 9
0
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);
    }
}