Example #1
0
    QSize adjustSize(PrintOptionsPage* optionsPage, Document::Ptr doc, int printerResolution, const QSize & viewportSize)
    {
        QSize size = doc->size();
        PrintOptionsPage::ScaleMode scaleMode = optionsPage->scaleMode();
        if (scaleMode == PrintOptionsPage::ScaleToPage) {
            bool imageBiggerThanPaper =
                size.width() > viewportSize.width()
                || size.height() > viewportSize.height();

            if (imageBiggerThanPaper || optionsPage->enlargeSmallerImages()) {
                size.scale(viewportSize, Qt::KeepAspectRatio);
            }

        } else if (scaleMode == PrintOptionsPage::ScaleToCustomSize) {
            double wImg = optionsPage->scaleWidth();
            double hImg = optionsPage->scaleHeight();
            size.setWidth(int(wImg * printerResolution));
            size.setHeight(int(hImg * printerResolution));

        } else {
            // No scale
            const double INCHES_PER_METER = 100. / 2.54;
            int dpmX = doc->image().dotsPerMeterX();
            int dpmY = doc->image().dotsPerMeterY();
            if (dpmX > 0 && dpmY > 0) {
                double wImg = double(size.width()) / double(dpmX) * INCHES_PER_METER;
                double hImg = double(size.height()) / double(dpmY) * INCHES_PER_METER;
                size.setWidth(int(wImg * printerResolution));
                size.setHeight(int(hImg * printerResolution));
            }
        }
        return size;
    }
Example #2
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);
}
void TransformImageOperationTest::testRotate90()
{
    KUrl url = urlForTestFile("test.png");
    QImage image;

    bool ok = image.load(url.toLocalFile());
    QVERIFY2(ok, "Could not load 'test.png'");
    QMatrix matrix = ImageUtils::transformMatrix(ROT_90);
    image = image.transformed(matrix);

    Document::Ptr doc = DocumentFactory::instance()->load(url);

    TransformImageOperation* op = new TransformImageOperation(ROT_90);
    QEventLoop loop;
    connect(doc.data(), SIGNAL(allTasksDone()), &loop, SLOT(quit()));
    op->applyToDocument(doc);
    loop.exec();

    QCOMPARE(image, doc->image());
}