summaryrefslogtreecommitdiffstats
path: root/src/pdf/qpdfdocument.h
blob: 8355246ae3cc34ad4caebf59a251571786b69149 (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
// Copyright (C) 2020 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

#ifndef QPDFDOCUMENT_H
#define QPDFDOCUMENT_H

#include <QtPdf/qtpdfglobal.h>

#include <QtCore/qobject.h>
#include <QtCore/QAbstractListModel>
#include <QtGui/qimage.h>
#include <QtPdf/qpdfdocumentrenderoptions.h>
#include <QtPdf/qpdfselection.h>

QT_BEGIN_NAMESPACE

class QPdfDocumentPrivate;
class QNetworkReply;

class Q_PDF_EXPORT QPdfDocument : public QObject
{
    Q_OBJECT

    Q_PROPERTY(int pageCount READ pageCount NOTIFY pageCountChanged FINAL)
    Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged FINAL)
    Q_PROPERTY(Status status READ status NOTIFY statusChanged FINAL)
    Q_PROPERTY(QAbstractListModel* pageModel READ pageModel NOTIFY pageModelChanged FINAL)

public:
    enum class Status {
        Null,
        Loading,
        Ready,
        Unloading,
        Error
    };
    Q_ENUM(Status)

    enum class Error {
        None,
        Unknown,
        DataNotYetAvailable,
        FileNotFound,
        InvalidFileFormat,
        IncorrectPassword,
        UnsupportedSecurityScheme
    };
    Q_ENUM(Error)

    enum class MetaDataField {
        Title,
        Subject,
        Author,
        Keywords,
        Producer,
        Creator,
        CreationDate,
        ModificationDate
    };
    Q_ENUM(MetaDataField)

    enum class PageModelRole {
        Label = Qt::UserRole,
        PointSize,
        NRoles
    };
    Q_ENUM(PageModelRole)

    QPdfDocument() : QPdfDocument(nullptr) {}
    explicit QPdfDocument(QObject *parent);
    ~QPdfDocument() override;

    Error load(const QString &fileName);

    Status status() const;

    void load(QIODevice *device);
    void setPassword(const QString &password);
    QString password() const;

    QVariant metaData(MetaDataField field) const;

    Error error() const;

    void close();

    int pageCount() const;

    Q_INVOKABLE QSizeF pagePointSize(int page) const;

    Q_INVOKABLE QString pageLabel(int page);
    Q_INVOKABLE int pageIndexForLabel(const QString &label);

    QAbstractListModel *pageModel();

    QImage render(int page, QSize imageSize, QPdfDocumentRenderOptions options = QPdfDocumentRenderOptions());

    Q_INVOKABLE QPdfSelection getSelection(int page, QPointF start, QPointF end);
    Q_INVOKABLE QPdfSelection getSelectionAtIndex(int page, int startIndex, int maxLength);
    Q_INVOKABLE QPdfSelection getAllText(int page);

Q_SIGNALS:
    void passwordChanged();
    void passwordRequired();
    void statusChanged(QPdfDocument::Status status);
    void pageCountChanged(int pageCount);
    void pageModelChanged();

private:
    friend struct QPdfBookmarkModelPrivate;
    friend class QPdfFile;
    friend class QPdfLinkModelPrivate;
    friend class QPdfPageModel;
    friend class QPdfSearchModel;
    friend class QPdfSearchModelPrivate;
    friend class QQuickPdfSelection;

    QString fileName() const;

    Q_PRIVATE_SLOT(d, void _q_tryLoadingWithSizeFromContentHeader())
    Q_PRIVATE_SLOT(d, void _q_copyFromSequentialSourceDevice())
    QScopedPointer<QPdfDocumentPrivate> d;
};

QT_END_NAMESPACE

#endif // QPDFDOCUMENT_H