summaryrefslogtreecommitdiffstats
path: root/src/pdfquick/qquickpdfpageimage.cpp
blob: f2da067f1b589773a7e28cf20b2331f4bdf00cca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquickpdfpageimage_p.h"
#include "qquickpdfdocument_p.h"
#include <private/qpdffile_p.h>
#include <QtQuick/private/qquickimage_p_p.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(qLcImg, "qt.pdf.image")

/*!
    \qmltype PdfPageImage
//!    \instantiates QQuickPdfPageImage
    \inqmlmodule QtQuick.Pdf
    \ingroup pdf
    \inherits Image
    \brief Displays one page from a PDF document.
    \since 6.4

    The PdfPageImage type is an Image specialized to render a page from a PDF document.
*/

class QQuickPdfPageImagePrivate: public QQuickImagePrivate
{
public:
    QQuickPdfPageImagePrivate() : QQuickImagePrivate() {}

    QQuickPdfDocument *doc = nullptr;
};

QQuickPdfPageImage::QQuickPdfPageImage(QQuickItem *parent)
    : QQuickImage(*(new QQuickPdfPageImagePrivate), parent)
{
}

/*!
    \internal
*/
QQuickPdfPageImage::~QQuickPdfPageImage()
{
    Q_D(QQuickPdfPageImage);
    // cancel any async rendering job that is running on my behalf
    d->pix.clear();
}

/*!
    \qmlproperty PdfDocument PdfPageImage::document

    This property holds the PDF document from which to render an image.
*/
void QQuickPdfPageImage::setDocument(QQuickPdfDocument *document)
{
    Q_D(QQuickPdfPageImage);
    if (d->doc == document)
        return;

    if (d->doc)
        disconnect(d->doc->document(), &QPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
    d->doc = document;
    if (document) {
        connect(document->document(), &QPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
        if (document->document()->status() == QPdfDocument::Status::Ready)
            setSource(document->resolvedSource()); // calls load()
    }
    emit documentChanged();
}

QQuickPdfDocument *QQuickPdfPageImage::document() const
{
    Q_D(const QQuickPdfPageImage);
    return d->doc;
}

void QQuickPdfPageImage::load()
{
    Q_D(QQuickPdfPageImage);
    QUrl url = source();
    if (!d->doc || !d->doc->carrierFile()) {
        if (!url.isEmpty()) {
            qmlWarning(this) << "document property not set: falling back to inefficient loading of " << url;
            QQuickImageBase::load();
        }
        return;
    }
    if (url != d->doc->resolvedSource()) {
        url = d->doc->resolvedSource();
        qmlWarning(this) << "document and source properties in conflict: preferring document source " << url;
    }
    auto carrierFile = d->doc->carrierFile();
    static int thisRequestProgress = -1;
    static int thisRequestFinished = -1;
    if (thisRequestProgress == -1) {
        thisRequestProgress =
            QQuickImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
        thisRequestFinished =
            QQuickImageBase::staticMetaObject.indexOfSlot("requestFinished()");
    }

    d->pix.loadImageFromDevice(qmlEngine(this), carrierFile, url,
                               d->sourceClipRect.toRect(), d->sourcesize * d->devicePixelRatio,
                               QQuickImageProviderOptions(), d->currentFrame, d->frameCount);

    qCDebug(qLcImg) << "loading page" << d->currentFrame << "of" << d->frameCount
                    << "from" << carrierFile->fileName() << "status" << d->pix.status();

    switch (d->pix.status()) {
    case QQuickPixmap::Ready:
        pixmapChange();
        break;
    case QQuickPixmap::Loading:
        d->pix.connectFinished(this, thisRequestFinished);
        d->pix.connectDownloadProgress(this, thisRequestProgress);
        if (d->progress != 0.0) {
            d->progress = 0.0;
            emit progressChanged(d->progress);
        }
        if (d->status != Loading) {
            d->status = Loading;
            emit statusChanged(d->status);
        }
        break;
    default:
        qCDebug(qLcImg) << "unexpected status" << d->pix.status();
        break;
    }
}

void QQuickPdfPageImage::documentStatusChanged()
{
    Q_D(QQuickPdfPageImage);
    const auto status = d->doc->document()->status();
    qCDebug(qLcImg) << "document status" << status;
    if (status == QPdfDocument::Status::Ready)
        setSource(d->doc->resolvedSource()); // calls load()
}

QT_END_NAMESPACE

#include "moc_qquickpdfpageimage_p.cpp"