~mutse-young/ubuntu-docviewer-app/trunk

« back to all changes in this revision

Viewing changes to src/plugin/poppler-qml-plugin/pdfModel.cpp

  • Committer: David Planella
  • Date: 2014-10-28 22:01:16 UTC
  • mfrom: (31.2.8 add-poppler-plugin)
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: david.planella@ubuntu.com-20141028220116-bryaeq3if69o6r6o
Merged branch to add Poppler plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authors: Anthony Granger <grangeranthony@gmail.com>
 
17
 *          Stefano Verzegnassi <stefano92.100@gmail.com>
 
18
 */
 
19
 
 
20
#include <pdfModel.h>
 
21
#include <pdfPage.h>
 
22
#include <pageImageProvider.h>
 
23
#include <pagesWorkerThread.h>
 
24
 
 
25
#include <poppler/qt5/poppler-qt5.h>
 
26
#include <QDebug>
 
27
#include <QQmlEngine>
 
28
#include <QQmlContext>
 
29
 
 
30
PdfModel::PdfModel(QAbstractListModel *parent):
 
31
    QAbstractListModel(parent)
 
32
{
 
33
    int metatype_id = qRegisterMetaType<PdfPagesList>("PdfPagesList");
 
34
}
 
35
 
 
36
QHash<int, QByteArray> PdfModel::roleNames() const
 
37
{
 
38
    QHash<int, QByteArray> roles;
 
39
    roles[WidthRole] = "width";
 
40
    roles[HeightRole] = "height";
 
41
    return roles;
 
42
}
 
43
 
 
44
int PdfModel::rowCount(const QModelIndex & parent) const
 
45
{
 
46
    return m_pages.count();
 
47
}
 
48
 
 
49
QVariant PdfModel::data(const QModelIndex & index, int role) const
 
50
{
 
51
    if (index.row() < 0 || index.row() > m_pages.count())
 
52
        return QVariant();
 
53
 
 
54
    const PdfPage &pdfPage = m_pages.at(index.row());
 
55
 
 
56
    switch (role) {
 
57
    case WidthRole:
 
58
        return pdfPage.width();
 
59
    case HeightRole:
 
60
        return pdfPage.height();
 
61
    default:
 
62
        return 0;
 
63
    }
 
64
}
 
65
 
 
66
void PdfModel::setPath(QString &pathName)
 
67
{
 
68
    if (pathName.isEmpty())
 
69
    {
 
70
        return;
 
71
    }
 
72
 
 
73
    this->path = pathName;
 
74
 
 
75
    emit pathChanged(pathName);
 
76
 
 
77
    if ( !loadDocument(pathName) ) {
 
78
        return;
 
79
    }
 
80
 
 
81
    loadPages();
 
82
    loadProvider();
 
83
}
 
84
 
 
85
int PdfModel::loadDocument(QString &pathName)
 
86
{
 
87
    qDebug() << "Loading document...";
 
88
 
 
89
    if (pathName.isEmpty()) {
 
90
        qDebug() << "Can't load the document, path is empty.";
 
91
        return 0;
 
92
    }
 
93
 
 
94
    this->document = Poppler::Document::load(pathName);
 
95
 
 
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);
 
99
 
 
100
        delete document;
 
101
        return 0;
 
102
    }
 
103
 
 
104
    qDebug() << "Document loaded successfully !";
 
105
 
 
106
    document->setRenderHint(Poppler::Document::Antialiasing, true);
 
107
    document->setRenderHint(Poppler::Document::TextAntialiasing, true);
 
108
 
 
109
    return 1;
 
110
}
 
111
 
 
112
QDateTime PdfModel::getDocumentDate(QString data)
 
113
{
 
114
    if (!document) {
 
115
        return QDateTime();
 
116
    }
 
117
 
 
118
    if (data == "CreationDate" || data == "ModDate") {
 
119
        return document->date(data);
 
120
    } else {
 
121
        return QDateTime();
 
122
    }
 
123
}
 
124
 
 
125
QString PdfModel::getDocumentInfo(QString data)
 
126
{
 
127
    if (!document) {
 
128
        return QString("");
 
129
    }
 
130
 
 
131
    if (data == "Title" || data == "Subject" || data == "Author" || data == "Creator" || data == "Producer") {
 
132
        return document->info(data);
 
133
    } else {
 
134
        return QString("");
 
135
    }
 
136
}
 
137
 
 
138
int PdfModel::loadPages()
 
139
{
 
140
    qDebug() << "Populating model...";
 
141
 
 
142
    m_pages.clear();
 
143
 
 
144
    if (!document) {
 
145
        return 0;
 
146
    }
 
147
 
 
148
    PDFPagesWorkerThread *workerThread = new PDFPagesWorkerThread();
 
149
    workerThread->setDocument(this->document);
 
150
 
 
151
    connect(workerThread, SIGNAL(resultReady(PdfPagesList)), this, SLOT(populate(PdfPagesList)));
 
152
    connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
 
153
    workerThread->start();
 
154
 
 
155
    return 1;
 
156
}
 
157
 
 
158
void PdfModel::populate(PdfPagesList pagesList)
 
159
{
 
160
    qDebug() << "Number of pages:" << pagesList.length();
 
161
 
 
162
    foreach (Poppler::Page *page, pagesList) {
 
163
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
 
164
        m_pages << page;
 
165
        endInsertRows();
 
166
    }
 
167
 
 
168
    qDebug() << "Model has been successfully populated!";
 
169
    emit pagesLoaded();
 
170
}
 
171
 
 
172
int PdfModel::loadProvider()
 
173
{
 
174
    qDebug() << "Loading image provider...";
 
175
    QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
 
176
 
 
177
    engine->addImageProvider(QLatin1String("poppler"), new PageImageProvider(document));
 
178
 
 
179
    qDebug() << "Image provider loaded successfully !";
 
180
 
 
181
    return 1;
 
182
}
 
183
 
 
184
PdfModel::~PdfModel()
 
185
{
 
186
}