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, Albert Astals Cid <aacid@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "pageview.h"
22 
23 #include <poppler-qt5.h>
24 
25 #include <QtWidgets/QApplication>
26 #include <QtWidgets/QDesktopWidget>
27 #include <QtGui/QImage>
28 #include <QtWidgets/QLabel>
29 #include <QtGui/QPixmap>
30 #include <QDebug>
31 
PageView(QWidget * parent)32 PageView::PageView(QWidget *parent) : QScrollArea(parent), m_zoom(1.0), m_rotation(0), m_dpiX(QApplication::desktop()->physicalDpiX()), m_dpiY(QApplication::desktop()->physicalDpiY())
33 {
34     m_imageLabel = new QLabel(this);
35     m_imageLabel->resize(0, 0);
36     setWidget(m_imageLabel);
37 }
38 
~PageView()39 PageView::~PageView() { }
40 
documentLoaded()41 void PageView::documentLoaded() { }
42 
documentClosed()43 void PageView::documentClosed()
44 {
45     m_imageLabel->clear();
46     m_imageLabel->resize(0, 0);
47 }
48 
pageChanged(int page)49 void PageView::pageChanged(int page)
50 {
51     Poppler::Page *popplerPage = document()->page(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     QImage image = popplerPage->renderToImage(resX, resY, -1, -1, -1, -1, rot);
71     if (!image.isNull()) {
72         m_imageLabel->resize(image.size());
73         m_imageLabel->setPixmap(QPixmap::fromImage(image));
74     } else {
75         m_imageLabel->resize(0, 0);
76         m_imageLabel->setPixmap(QPixmap());
77     }
78     delete popplerPage;
79 }
80 
slotZoomChanged(qreal value)81 void PageView::slotZoomChanged(qreal value)
82 {
83     m_zoom = value;
84     if (!document()) {
85         return;
86     }
87     reloadPage();
88 }
89 
slotRotationChanged(int value)90 void PageView::slotRotationChanged(int value)
91 {
92     m_rotation = value;
93     if (!document()) {
94         return;
95     }
96     reloadPage();
97 }
98