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); }
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; }
void ImageOpsContextManagerItem::resizeImage() { if (!d->ensureEditable()) { return; } Document::Ptr doc = DocumentFactory::instance()->load(contextManager()->currentUrl()); doc->startLoadingFullImage(); DialogGuard<ResizeImageDialog> dialog(d->mMainWindow); dialog->setOriginalSize(doc->size()); if (!dialog->exec()) { return; } ResizeImageOperation* op = new ResizeImageOperation(dialog->size()); applyImageOperation(op); }