1/*
2 * Copyright (C) 2008-2009, Pino Toscano <pino@kde.org>
3 * Copyright (C) 2013, Fabio D'Urso <fabiodurso@hotmail.it>
4 * Copyright (C) 2017, 2020, Albert Astals Cid <aacid@kde.org>
5 * Copyright (C) 2021, Oliver Sander <oliver.sander@tu-dresden.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include "pageview.h"
23
24#include <poppler-qt6.h>
25
26#include <QtWidgets/QApplication>
27#include <QtGui/QImage>
28#include <QtWidgets/QLabel>
29#include <QtGui/QPixmap>
30#include <QDebug>
31
32PageView::PageView(QWidget *parent) : QScrollArea(parent), m_zoom(1.0), m_rotation(0), m_dpiX(physicalDpiX()), m_dpiY(physicalDpiY())
33{
34 m_imageLabel = new QLabel(this);
35 m_imageLabel->resize(w: 0, h: 0);
36 setWidget(m_imageLabel);
37}
38
39PageView::~PageView() { }
40
41void PageView::documentLoaded() { }
42
43void PageView::documentClosed()
44{
45 m_imageLabel->clear();
46 m_imageLabel->resize(w: 0, h: 0);
47}
48
49void PageView::pageChanged(int page)
50{
51 std::unique_ptr<Poppler::Page> popplerPage = document()->page(index: page);
52
53 if (!popplerPage) {
54 qDebug() << "Page" << page << "is malformed";
55 return;
56 }
57 const double resX = m_dpiX * m_zoom;
58 const double resY = m_dpiY * m_zoom;
59
60 Poppler::Page::Rotation rot;
61 if (m_rotation == 0) {
62 rot = Poppler::Page::Rotate0;
63 } else if (m_rotation == 90) {
64 rot = Poppler::Page::Rotate90;
65 } else if (m_rotation == 180) {
66 rot = Poppler::Page::Rotate180;
67 } else { // m_rotation == 270
68 rot = Poppler::Page::Rotate270;
69 }
70
71 QImage image = popplerPage->renderToImage(xres: resX, yres: resY, x: -1, y: -1, w: -1, h: -1, rotate: rot);
72 if (!image.isNull()) {
73 m_imageLabel->resize(image.size());
74 m_imageLabel->setPixmap(QPixmap::fromImage(image));
75 } else {
76 m_imageLabel->resize(w: 0, h: 0);
77 m_imageLabel->setPixmap(QPixmap());
78 }
79}
80
81void PageView::slotZoomChanged(qreal value)
82{
83 m_zoom = value;
84 if (!document()) {
85 return;
86 }
87 reloadPage();
88}
89
90void PageView::slotRotationChanged(int value)
91{
92 m_rotation = value;
93 if (!document()) {
94 return;
95 }
96 reloadPage();
97}
98

source code of poppler/qt6/demos/pageview.cpp