Commit 73fb83e9 authored by Alexander Stippich's avatar Alexander Stippich
Browse files

add single image PDF support

CCBUG: 299517
parent a5906e64
......@@ -11,10 +11,11 @@
#include "SkanliteImageSaver.h"
#include <QMutex>
#include <QDebug>
#include <KSaneWidget>
#include <QString>
#include <QUrl>
#include <QImage>
#include <QPdfWriter>
#include <QPainter>
struct SkanliteImageSaver::Private {
bool m_savedOk;
......@@ -23,13 +24,13 @@ struct SkanliteImageSaver::Private {
QUrl m_url;
QString m_name;
QImage m_image;
int m_dpi;
QString m_fileFormat;
int m_quality;
SkanliteImageSaver *q;
bool saveQImage();
bool savePDF();
};
// ------------------------------------------------------------------------
......@@ -62,7 +63,12 @@ bool SkanliteImageSaver::saveQImage(const QUrl &url, const QString &name, const
void SkanliteImageSaver::run()
{
d->m_savedOk = d->saveQImage();
if (d->m_fileFormat == QLatin1String("pdf")) {
d->m_savedOk = d->savePDF();
} else {
d->m_savedOk = d->saveQImage();
}
Q_EMIT imageSaved(d->m_url, d->m_name, d->m_savedOk);
d->m_runMutex.unlock();
......@@ -73,3 +79,26 @@ bool SkanliteImageSaver::Private::saveQImage()
return m_image.save(m_name, qPrintable(m_fileFormat), m_quality);
}
bool SkanliteImageSaver::Private::savePDF()
{
const QPageSize pageSize = QPageSize(QSizeF(m_image.width() * 1000.0 / m_image.dotsPerMeterX() , m_image.height() * 1000.0 / m_image.dotsPerMeterY()), QPageSize::Millimeter);
const int dpi = qRound(m_image.dotsPerMeterX() / 1000.0 * 25.4);
QPainter painter;
QPdfWriter writer(m_name);
writer.setPageOrientation(QPageLayout::Portrait);
writer.setResolution(dpi);
writer.setPageSize(pageSize);
writer.setPageMargins(QMarginsF(0, 0, 0, 0));
writer.setCreator(QStringLiteral("Skanlite"));
writer.setTitle(m_name);
QRect paintArea(0, 0, writer.width(), writer.height());
if (!painter.begin(&writer)) {
return false;
}
painter.drawImage(paintArea, m_image);
return painter.end();
}
......@@ -144,6 +144,7 @@ Skanlite::Skanlite(const QString &device, QWidget *parent)
m_filterList.insert(0, QStringLiteral("image/png"));
m_filterList.insert(1, QStringLiteral("image/jpeg"));
m_filterList.insert(2, QStringLiteral("image/tiff"));
m_filterList.insert(3, QStringLiteral("application/pdf"));
m_filter16BitList << QStringLiteral("image/png");
m_filter16BitList << QStringLiteral("image/tiff");
......@@ -511,6 +512,9 @@ void Skanlite::saveImage()
if (suffix.isEmpty()) {
fileFormat = QStringLiteral("png");
}
if (suffix == QLatin1String("pdf")) {
fileFormat = QStringLiteral("pdf");
}
if (!fileUrl.isLocalFile()) {
QTemporaryFile tmp;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment