Example #1
0
void PrintHelper::print(Document::Ptr doc)
{
    doc->startLoadingFullImage();
    doc->waitUntilLoaded();
    QPrinter printer;

    PrintOptionsPage* optionsPage = new PrintOptionsPage(doc->size());
    optionsPage->loadConfig();

    std::unique_ptr<QPrintDialog> dialog(
        KdePrint::createPrintDialog(&printer,
                                    QList<QWidget*>() << optionsPage,
                                    d->mParent)
    );
    dialog->setWindowTitle(i18n("Print Image"));
    bool wantToPrint = dialog->exec();

    optionsPage->saveConfig();
    if (!wantToPrint) {
        return;
    }

    QPainter painter(&printer);
    QRect rect = painter.viewport();
    QSize size = d->adjustSize(optionsPage, doc, printer.resolution(), rect.size());
    QPoint pos = d->adjustPosition(optionsPage, size, rect.size());
    painter.setViewport(pos.x(), pos.y(), size.width(), size.height());

    QImage image = doc->image();
    painter.setWindow(image.rect());
    painter.drawImage(0, 0, image);
}