2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authors: Anthony Granger <grangeranthony@gmail.com>
17
* Stefano Verzegnassi <stefano92.100@gmail.com>
22
#include <pageImageProvider.h>
23
#include <pagesWorkerThread.h>
25
#include <poppler/qt5/poppler-qt5.h>
28
#include <QQmlContext>
30
PdfModel::PdfModel(QAbstractListModel *parent):
31
QAbstractListModel(parent)
33
int metatype_id = qRegisterMetaType<PdfPagesList>("PdfPagesList");
36
QHash<int, QByteArray> PdfModel::roleNames() const
38
QHash<int, QByteArray> roles;
39
roles[WidthRole] = "width";
40
roles[HeightRole] = "height";
44
int PdfModel::rowCount(const QModelIndex & parent) const
46
return m_pages.count();
49
QVariant PdfModel::data(const QModelIndex & index, int role) const
51
if (index.row() < 0 || index.row() > m_pages.count())
54
const PdfPage &pdfPage = m_pages.at(index.row());
58
return pdfPage.width();
60
return pdfPage.height();
66
void PdfModel::setPath(QString &pathName)
68
if (pathName.isEmpty())
73
this->path = pathName;
75
emit pathChanged(pathName);
77
if ( !loadDocument(pathName) ) {
85
int PdfModel::loadDocument(QString &pathName)
87
qDebug() << "Loading document...";
89
if (pathName.isEmpty()) {
90
qDebug() << "Can't load the document, path is empty.";
94
this->document = Poppler::Document::load(pathName);
96
if (!document || document->isLocked()) {
97
qDebug() << "ERROR : Can't open the document located at " + pathName;
98
emit error("Can't open the document located at " + pathName);
104
qDebug() << "Document loaded successfully !";
106
document->setRenderHint(Poppler::Document::Antialiasing, true);
107
document->setRenderHint(Poppler::Document::TextAntialiasing, true);
112
QDateTime PdfModel::getDocumentDate(QString data)
118
if (data == "CreationDate" || data == "ModDate") {
119
return document->date(data);
125
QString PdfModel::getDocumentInfo(QString data)
131
if (data == "Title" || data == "Subject" || data == "Author" || data == "Creator" || data == "Producer") {
132
return document->info(data);
138
int PdfModel::loadPages()
140
qDebug() << "Populating model...";
148
PDFPagesWorkerThread *workerThread = new PDFPagesWorkerThread();
149
workerThread->setDocument(this->document);
151
connect(workerThread, SIGNAL(resultReady(PdfPagesList)), this, SLOT(populate(PdfPagesList)));
152
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
153
workerThread->start();
158
void PdfModel::populate(PdfPagesList pagesList)
160
qDebug() << "Number of pages:" << pagesList.length();
162
foreach (Poppler::Page *page, pagesList) {
163
beginInsertRows(QModelIndex(), rowCount(), rowCount());
168
qDebug() << "Model has been successfully populated!";
172
int PdfModel::loadProvider()
174
qDebug() << "Loading image provider...";
175
QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
177
engine->addImageProvider(QLatin1String("poppler"), new PageImageProvider(document));
179
qDebug() << "Image provider loaded successfully !";
184
PdfModel::~PdfModel()