summaryrefslogtreecommitdiffstats log msg author committer range
path: root/src/pdf/quick/qquickpdfdocument.cpp
blob: 3d5f0fa107ab663e5c5df9527494a646ed98500e (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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 ``` ``````/**************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtPDF module of the Qt Toolkit. ** ** \$QT_BEGIN_LICENSE:LGPL3\$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPLv3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or later as published by the Free ** Software Foundation and appearing in the file LICENSE.GPL included in ** the packaging of this file. Please review the following information to ** ensure the GNU General Public License version 2.0 requirements will be ** met: http://www.gnu.org/licenses/gpl-2.0.html. ** ** \$QT_END_LICENSE\$ ** ****************************************************************************/ #include "qquickpdfdocument_p.h" #include #include #include #include QT_BEGIN_NAMESPACE /*! \qmltype PdfDocument \instantiates QQuickPdfDocument \inqmlmodule QtQuick.Pdf \ingroup pdf \brief A representation of a PDF document. \since 5.15 PdfDocument provides access to PDF document meta-information. It is not necessary for rendering, as it is enough to use an \l Image with source set to the URL of the PDF. */ /*! Constructs a PDF document. */ QQuickPdfDocument::QQuickPdfDocument(QObject *parent) : QObject(parent) { connect(&m_doc, &QPdfDocument::passwordChanged, this, &QQuickPdfDocument::passwordChanged); connect(&m_doc, &QPdfDocument::passwordRequired, this, &QQuickPdfDocument::passwordRequired); connect(&m_doc, &QPdfDocument::statusChanged, [=] (QPdfDocument::Status status) { emit statusChanged(); if (status == QPdfDocument::Ready) emit metaDataChanged(); }); connect(&m_doc, &QPdfDocument::pageCountChanged, this, &QQuickPdfDocument::pageCountChanged); } void QQuickPdfDocument::componentComplete() { if (m_doc.error() == QPdfDocument::IncorrectPasswordError) emit passwordRequired(); } /*! \qmlproperty url PdfDocument::source This property holds a URL pointing to the PDF file to be loaded. \note At this time, only local filesystem URLs are supported. */ void QQuickPdfDocument::setSource(QUrl source) { if (m_source == source) return; m_source = source; m_maxPageWidthHeight = QSizeF(); emit sourceChanged(); if (source.scheme() == QLatin1String("qrc")) m_doc.load(QLatin1Char(':') + source.path()); else m_doc.load(source.path()); } /*! \qmlproperty string PdfDocument::error This property holds a translated string representation of the current error, if any. \sa status */ QString QQuickPdfDocument::error() const { switch (m_doc.error()) { case QPdfDocument::NoError: return tr("no error"); break; case QPdfDocument::UnknownError: break; case QPdfDocument::DataNotYetAvailableError: return tr("data not yet available"); break; case QPdfDocument::FileNotFoundError: return tr("file not found"); break; case QPdfDocument::InvalidFileFormatError: return tr("invalid file format"); break; case QPdfDocument::IncorrectPasswordError: return tr("incorrect password"); break; case QPdfDocument::UnsupportedSecuritySchemeError: return tr("unsupported security scheme"); break; } return tr("unknown error"); } /*! \qmlproperty bool PdfDocument::password This property holds the document password. If the passwordRequired() signal is emitted, the UI should prompt the user and then set this property so that document opening can continue. */ void QQuickPdfDocument::setPassword(const QString &password) { if (m_doc.password() == password) return; m_doc.setPassword(password); if (source().isValid() && source().isLocalFile()) m_doc.load(source().path()); } /*! \qmlproperty int PdfDocument::pageCount This property holds the number of pages the PDF contains. */ /*! \qmlsignal PdfDocument::passwordRequired() This signal is emitted when the PDF requires a password in order to open. The UI in a typical PDF viewer should prompt the user for the password and then set the password property when the user has provided it. */ /*! \qmlmethod size PdfDocument::pagePointSize(int page) Returns the size of the given \a page in points. */ QSizeF QQuickPdfDocument::pagePointSize(int page) const { return m_doc.pageSize(page); } qreal QQuickPdfDocument::maxPageWidth() const { const_cast(this)->updateMaxPageSize(); return m_maxPageWidthHeight.width(); } qreal QQuickPdfDocument::maxPageHeight() const { const_cast(this)->updateMaxPageSize(); return m_maxPageWidthHeight.height(); } /*! \internal \qmlmethod size PdfDocument::heightSumBeforePage(int page) Returns the sum of the heights, in points, of all sets of \a facingPages pages from 0 to the given \a page, exclusive. That is, if the pages were laid out end-to-end in adjacent sets of \a facingPages, what would be the distance in points from the top of the first page to the top of the given page. */ // Workaround for lack of something analogous to ListView.positionViewAtIndex() in TableView qreal QQuickPdfDocument::heightSumBeforePage(int page, qreal spacing, int facingPages) const { qreal ret = 0; for (int i = 0; i < page; i+= facingPages) { if (i + facingPages > page) break; qreal facingPagesHeight = 0; for (int j = i; j < i + facingPages; ++j) facingPagesHeight = qMax(facingPagesHeight, pagePointSize(j).height()); ret += facingPagesHeight + spacing; } return ret; } void QQuickPdfDocument::updateMaxPageSize() { if (m_maxPageWidthHeight.isValid()) return; qreal w = 0; qreal h = 0; const int count = pageCount(); for (int i = 0; i < count; ++i) { auto size = pagePointSize(i); w = qMax(w, size.width()); h = qMax(w, size.height()); } m_maxPageWidthHeight = QSizeF(w, h); } /*! \qmlproperty real PdfDocument::maxPageWidth This property holds the width of the widest page in the document, in points. */ /*! \qmlproperty real PdfDocument::maxPageHeight This property holds the height of the tallest page in the document, in points. */ /*! \qmlproperty string PdfDocument::title This property holds the document's title. A typical viewer UI can bind this to the \c Window.title property. */ /*! \qmlproperty string PdfDocument::author This property holds the name of the person who created the document. */ /*! \qmlproperty string PdfDocument::subject This property holds the subject of the document. */ /*! \qmlproperty string PdfDocument::keywords This property holds the keywords associated with the document. */ /*! \qmlproperty string PdfDocument::creator If the document was converted to PDF from another format, this property holds the name of the software that created the original document. */ /*! \qmlproperty string PdfDocument::producer If the document was converted to PDF from another format, this property holds the name of the software that converted it to PDF. */ /*! \qmlproperty string PdfDocument::creationDate This property holds the date and time the document was created. */ /*! \qmlproperty string PdfDocument::modificationDate This property holds the date and time the document was most recently modified. */ /*! \qmlproperty enum PdfDocument::status This property tells the current status of the document. The possible values are: \value PdfDocument.Null The initial status after the document has been created or after it has been closed. \value PdfDocument.Loading The status after load() has been called and before the document is fully loaded. \value PdfDocument.Ready The status when the document is fully loaded and its data can be accessed. \value PdfDocument.Unloading The status after close() has been called on an open document. At this point the document is still valid and all its data can be accessed. \value PdfDocument.Error The status after Loading, if loading has failed. */ QT_END_NAMESPACE ``````