File indexing completed on 2024-05-12 16:06:53

0001 /*
0002     SPDX-FileCopyrightText: 2012 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef QDOCUMENTITEM_H
0008 #define QDOCUMENTITEM_H
0009 
0010 #include <QObject>
0011 
0012 #include "settings.h"
0013 
0014 #include <core/document.h>
0015 #include <core/observer.h>
0016 
0017 #include "gui/signaturemodel.h"
0018 #include "gui/tocmodel.h"
0019 
0020 namespace Okular
0021 {
0022 class Document;
0023 }
0024 
0025 class Observer;
0026 class SignatureModel;
0027 class TOCModel;
0028 
0029 class DocumentItem : public QObject
0030 {
0031     Q_OBJECT
0032 
0033     /**
0034      * Absolute URI to document file to open
0035      */
0036     Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
0037 
0038     /**
0039      * Suggested window title if a window represents this document. may be pathname or document title, depending on Okular settings.
0040      */
0041     Q_PROPERTY(QString windowTitleForDocument READ windowTitleForDocument NOTIFY windowTitleForDocumentChanged)
0042 
0043     /**
0044      * Current displaying page for the document
0045      */
0046     Q_PROPERTY(int currentPage READ currentPage WRITE setCurrentPage NOTIFY currentPageChanged)
0047 
0048     /**
0049      * True if this DocumentItem instance has a document file opened
0050      */
0051     Q_PROPERTY(bool opened READ isOpened NOTIFY openedChanged)
0052 
0053     /**
0054      * True if this DocumentItem instance needs a password to open the document
0055      */
0056     Q_PROPERTY(bool needsPassword READ needsPassword NOTIFY needsPasswordChanged)
0057 
0058     /**
0059      * How many pages there are in the document
0060      */
0061     Q_PROPERTY(int pageCount READ pageCount NOTIFY pageCountChanged)
0062 
0063     /**
0064      * True if the document is able to perform full text searches in its contents
0065      */
0066     Q_PROPERTY(bool supportsSearching READ supportsSearching NOTIFY supportsSearchingChanged)
0067 
0068     /**
0069      * True if a search is currently in progress and results didn't arrive yet
0070      */
0071     Q_PROPERTY(bool searchInProgress READ isSearchInProgress NOTIFY searchInProgressChanged)
0072 
0073     /**
0074      * A list of all pages that contain a match for the search terms. If no text has been searched, all pages are returned.
0075      */
0076     Q_PROPERTY(QVariantList matchingPages READ matchingPages NOTIFY matchingPagesChanged)
0077 
0078     /**
0079      * Table of contents for the document, if available
0080      */
0081     Q_PROPERTY(TOCModel *tableOfContents READ tableOfContents CONSTANT)
0082 
0083     /**
0084      * Signatures model, if available
0085      */
0086     Q_PROPERTY(SignatureModel *signaturesModel READ signaturesModel CONSTANT)
0087 
0088     /**
0089      * List of pages that contain a bookmark
0090      */
0091     Q_PROPERTY(QVariantList bookmarkedPages READ bookmarkedPages NOTIFY bookmarkedPagesChanged)
0092 
0093     /**
0094      * list of bookmarks urls valid on this page
0095      */
0096     Q_PROPERTY(QStringList bookmarks READ bookmarks NOTIFY bookmarksChanged)
0097 
0098 public:
0099     explicit DocumentItem(QObject *parent = nullptr);
0100     ~DocumentItem() override;
0101 
0102     void setUrl(const QUrl &url);
0103     QUrl url() const;
0104 
0105     QString windowTitleForDocument() const;
0106 
0107     void setCurrentPage(int page);
0108     int currentPage() const;
0109 
0110     bool isOpened() const;
0111 
0112     bool needsPassword() const
0113     {
0114         return m_needsPassword;
0115     }
0116 
0117     int pageCount() const;
0118 
0119     bool supportsSearching() const;
0120 
0121     bool isSearchInProgress() const;
0122 
0123     QVariantList matchingPages() const;
0124 
0125     TOCModel *tableOfContents() const;
0126 
0127     SignatureModel *signaturesModel() const;
0128 
0129     QVariantList bookmarkedPages() const;
0130 
0131     QStringList bookmarks() const;
0132 
0133     // This could be a property, but maybe we want to have parameter for searchText
0134     /**
0135      * Performs a search in the document
0136      *
0137      * @param text the string to search in the document
0138      */
0139     Q_INVOKABLE void searchText(const QString &text);
0140 
0141     /**
0142      * Reset the search over the document.
0143      */
0144     Q_INVOKABLE void resetSearch();
0145 
0146     /**
0147      * Tries to reopen the document with the given password.
0148      */
0149     Q_INVOKABLE void setPassword(const QString &password);
0150 
0151     // Internal, not binded to qml
0152     Okular::Document *document();
0153     Observer *pageviewObserver();
0154     Observer *thumbnailObserver();
0155 
0156 Q_SIGNALS:
0157     void urlChanged();
0158     void pageCountChanged();
0159     void openedChanged();
0160     void needsPasswordChanged();
0161     void searchInProgressChanged();
0162     void matchingPagesChanged();
0163     void currentPageChanged();
0164     void supportsSearchingChanged();
0165     void bookmarkedPagesChanged();
0166     void bookmarksChanged();
0167     void windowTitleForDocumentChanged();
0168 
0169     /**
0170      * This signal is emitted whenever an error occurred.
0171      *
0172      * @param text The description of the error.
0173      * @param duration The time in milliseconds the message should be shown to the user.
0174      */
0175     void error(const QString &text, int duration);
0176 
0177     /**
0178      * This signal is emitted to signal a warning.
0179      *
0180      * @param text The description of the warning.
0181      * @param duration The time in milliseconds the message should be shown to the user.
0182      */
0183     void warning(const QString &text, int duration);
0184 
0185     /**
0186      * This signal is emitted to signal a notice.
0187      *
0188      * @param text The description of the notice.
0189      * @param duration The time in milliseconds the message should be shown to the user.
0190      */
0191     void notice(const QString &text, int duration);
0192 
0193 private Q_SLOTS:
0194     void searchFinished(int id, Okular::Document::SearchStatus endStatus);
0195 
0196 private:
0197     void openUrl(const QUrl &url, const QString &password);
0198 
0199     Okular::Document *m_document;
0200     TOCModel *m_tocModel;
0201     SignatureModel *m_signaturesModel;
0202     Observer *m_thumbnailObserver;
0203     Observer *m_pageviewObserver;
0204     QVariantList m_matchingPages;
0205     bool m_searchInProgress;
0206     bool m_needsPassword = false;
0207 };
0208 
0209 class Observer : public QObject, public Okular::DocumentObserver
0210 {
0211     Q_OBJECT
0212 
0213 public:
0214     explicit Observer(DocumentItem *parent);
0215     ~Observer() override;
0216 
0217     // inherited from DocumentObserver
0218     void notifyPageChanged(int page, int flags) override;
0219 
0220 Q_SIGNALS:
0221     void pageChanged(int page, int flags);
0222 
0223 private:
0224     DocumentItem *m_document;
0225 };
0226 
0227 #endif