PDF4QT/Pdf4QtViewerPlugins/OutputPreviewPlugin/outputpreviewdialog.cpp

166 lines
4.8 KiB
C++
Raw Normal View History

2021-01-30 18:54:38 +01:00
// Copyright (C) 2021 Jakub Melka
//
// This file is part of Pdf4Qt.
//
// Pdf4Qt is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pdf4Qt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
#include "outputpreviewdialog.h"
#include "ui_outputpreviewdialog.h"
2021-01-31 19:15:11 +01:00
#include "pdfcms.h"
#include "pdfrenderer.h"
#include "pdfdrawspacecontroller.h"
2021-02-13 19:20:05 +01:00
#include <QCloseEvent>
#include <QtConcurrent/QtConcurrent>
2021-01-30 18:54:38 +01:00
namespace pdfplugin
{
2021-01-31 19:15:11 +01:00
OutputPreviewDialog::OutputPreviewDialog(const pdf::PDFDocument* document, pdf::PDFWidget* widget, QWidget* parent) :
2021-02-11 19:42:55 +01:00
QDialog(parent, Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint),
2021-01-31 19:15:11 +01:00
ui(new Ui::OutputPreviewDialog),
m_inkMapper(document),
m_document(document),
2021-02-13 19:20:05 +01:00
m_widget(widget),
m_needUpdateImage(false),
m_futureWatcher(nullptr)
2021-01-30 18:54:38 +01:00
{
ui->setupUi(this);
2021-01-31 19:15:11 +01:00
ui->pageIndexScrollBar->setMinimum(1);
ui->pageIndexScrollBar->setValue(1);
ui->pageIndexScrollBar->setMaximum(int(document->getCatalog()->getPageCount()));
m_inkMapper.createSpotColors(true);
2021-02-13 19:20:05 +01:00
updatePageImage();
2021-01-30 18:54:38 +01:00
}
OutputPreviewDialog::~OutputPreviewDialog()
{
delete ui;
}
2021-02-07 18:59:39 +01:00
void OutputPreviewDialog::resizeEvent(QResizeEvent* event)
{
QDialog::resizeEvent(event);
2021-02-13 19:20:05 +01:00
updatePageImage();
2021-02-07 18:59:39 +01:00
}
2021-02-13 19:20:05 +01:00
void OutputPreviewDialog::closeEvent(QCloseEvent* event)
2021-01-31 19:15:11 +01:00
{
2021-02-13 19:20:05 +01:00
if (!isRenderingDone())
{
event->ignore();
}
}
void OutputPreviewDialog::showEvent(QShowEvent* event)
{
Q_UNUSED(event);
updatePageImage();
}
void OutputPreviewDialog::updatePageImage()
{
if (!isRenderingDone())
{
m_needUpdateImage = true;
return;
}
m_needUpdateImage = false;
2021-01-31 19:15:11 +01:00
const pdf::PDFPage* page = m_document->getCatalog()->getPage(ui->pageIndexScrollBar->value() - 1);
if (!page)
{
ui->imageLabel->setPixmap(QPixmap());
2021-02-13 19:20:05 +01:00
return;
2021-01-31 19:15:11 +01:00
}
2021-02-13 19:20:05 +01:00
QApplication::setOverrideCursor(Qt::WaitCursor);
QSize renderSize = ui->imageLabel->size();
auto renderImage = [this, page, renderSize]() -> RenderedImage
{
return renderPage(page, renderSize);
};
m_future = QtConcurrent::run(renderImage);
m_futureWatcher = new QFutureWatcher<RenderedImage>();
connect(m_futureWatcher, &QFutureWatcher<RenderedImage>::finished, this, &OutputPreviewDialog::onPageImageRendered);
m_futureWatcher->setFuture(m_future);
}
OutputPreviewDialog::RenderedImage OutputPreviewDialog::renderPage(const pdf::PDFPage* page, QSize renderSize)
{
RenderedImage result;
2021-01-31 19:15:11 +01:00
QRectF pageRect = page->getRotatedMediaBox();
QSizeF pageSize = pageRect.size();
2021-02-13 19:20:05 +01:00
pageSize.scale(renderSize.width(), renderSize.height(), Qt::KeepAspectRatio);
2021-01-31 19:15:11 +01:00
QSize imageSize = pageSize.toSize();
if (!imageSize.isValid())
{
2021-02-13 19:20:05 +01:00
return result;
2021-01-31 19:15:11 +01:00
}
2021-02-13 17:09:57 +01:00
pdf::PDFTransparencyRendererSettings settings;
// Jakub Melka: debug is very slow, use multithreading
#ifdef QT_DEBUG
settings.flags.setFlag(pdf::PDFTransparencyRendererSettings::MultithreadedPathSampler, true);
#endif
2021-01-31 19:15:11 +01:00
QMatrix pagePointToDevicePoint = pdf::PDFRenderer::createPagePointToDevicePointMatrix(page, QRect(QPoint(0, 0), imageSize));
pdf::PDFDrawWidgetProxy* proxy = m_widget->getDrawWidgetProxy();
pdf::PDFCMSPointer cms = proxy->getCMSManager()->getCurrentCMS();
2021-02-13 17:09:57 +01:00
pdf::PDFTransparencyRenderer renderer(page, m_document, proxy->getFontCache(), cms.data(), proxy->getOptionalContentActivity(), &m_inkMapper, settings, pagePointToDevicePoint);
2021-01-31 19:15:11 +01:00
renderer.beginPaint(imageSize);
renderer.processContents();
renderer.endPaint();
2021-02-06 18:49:37 +01:00
2021-02-11 19:42:55 +01:00
QImage image = renderer.toImage(false, true, pdf::PDFRGB{ 1.0f, 1.0f, 1.0f });
2021-02-13 19:20:05 +01:00
result.image = qMove(image);
return result;
}
void OutputPreviewDialog::onPageImageRendered()
{
QApplication::restoreOverrideCursor();
RenderedImage result = m_future.result();
m_future = QFuture<RenderedImage>();
m_futureWatcher->deleteLater();
m_futureWatcher = nullptr;
ui->imageLabel->setPixmap(QPixmap::fromImage(result.image));
if (m_needUpdateImage)
{
updatePageImage();
}
}
bool OutputPreviewDialog::isRenderingDone() const
{
return !(m_futureWatcher && m_futureWatcher->isRunning());
2021-01-31 19:15:11 +01:00
}
2021-01-30 18:54:38 +01:00
} // namespace pdfplugin