| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef WEBVIEWER_H |
| 4 | #define WEBVIEWER_H |
| 5 | |
| 6 | #include "core/message.h" |
| 7 | #include "definitions/definitions.h" |
| 8 | |
| 9 | #include <QAction> |
| 10 | #include <QUrl> |
| 11 | |
| 12 | class QMenu; |
| 13 | class QContextMenuEvent; |
| 14 | |
| 15 | class WebBrowser; |
| 16 | class RootItem; |
| 17 | |
| 18 | struct PreparedHtml { |
| 19 | QString m_html; |
| 20 | QUrl m_baseUrl; |
| 21 | }; |
| 22 | |
| 23 | struct ContextMenuData { |
| 24 | QUrl m_linkUrl; |
| 25 | QUrl m_mediaUrl; |
| 26 | }; |
| 27 | |
| 28 | #define ACCEPTABLE_IMAGE_PERCENTUAL_WIDTH 0.97 |
| 29 | |
| 30 | // Interface for web/article viewers. |
| 31 | class WebViewer { |
| 32 | public: |
| 33 | WebViewer(); |
| 34 | virtual ~WebViewer(); |
| 35 | |
| 36 | // Performs necessary steps to make viewer work with browser. |
| 37 | // NOTE: Each implementor must do this in this method: |
| 38 | // 1. Initialize all WebBrowser QActions and maintain their "enabled" state all the time. |
| 39 | // 2. Connect to all slots of WebBrowser to ensure updating of title/icon, notifications |
| 40 | // of loading start/progress/finish, link highlight etc. |
| 41 | // 3. Viewer must set WebBrowser to be event filter at some point. |
| 42 | virtual void bindToBrowser(WebBrowser* browser) = 0; |
| 43 | |
| 44 | // Perform inline search. |
| 45 | // NOTE: When text is empty, cancel search. |
| 46 | virtual void findText(const QString& text, bool backwards) = 0; |
| 47 | |
| 48 | // Loads URL into the viewer. |
| 49 | virtual void setUrl(const QUrl& url) = 0; |
| 50 | |
| 51 | // Set static HTML into the viewer. |
| 52 | virtual void setHtml(const QString& html, const QUrl& base_url = {}) = 0; |
| 53 | |
| 54 | // Set HTML generated by "readability" into the viewer. |
| 55 | virtual void setReadabledHtml(const QString& html, const QUrl& base_url = {}) = 0; |
| 56 | |
| 57 | // Returns current static HTML loaded in the viewer. |
| 58 | virtual QString html() const = 0; |
| 59 | |
| 60 | // Returns current URL. |
| 61 | virtual QUrl url() const = 0; |
| 62 | |
| 63 | // Clears displayed URL. |
| 64 | virtual void clear() = 0; |
| 65 | |
| 66 | // Displays all messages and ensures that vertical scrollbar is set to 0 (scrolled to top). |
| 67 | virtual void loadMessages(const QList<Message>& messages, RootItem* root) = 0; |
| 68 | |
| 69 | // Returns final HTML generated for the articles. |
| 70 | virtual PreparedHtml htmlForMessages(const QList<Message>& messages, RootItem* root) const = 0; |
| 71 | |
| 72 | // Vertical scrollbar changer. |
| 73 | virtual double verticalScrollBarPosition() const = 0; |
| 74 | virtual void setVerticalScrollBarPosition(double pos) = 0; |
| 75 | |
| 76 | // Apply font. |
| 77 | virtual void applyFont(const QFont& fon) = 0; |
| 78 | |
| 79 | // Zooming. |
| 80 | virtual bool canZoomIn() const; |
| 81 | virtual bool canZoomOut() const; |
| 82 | virtual void zoomIn(); |
| 83 | virtual void zoomOut(); |
| 84 | virtual qreal zoomFactor() const = 0; |
| 85 | virtual void setZoomFactor(qreal zoom_factor) = 0; |
| 86 | |
| 87 | protected: |
| 88 | void processContextMenu(QMenu* specific_menu, QContextMenuEvent* event); |
| 89 | |
| 90 | virtual ContextMenuData provideContextMenuData(QContextMenuEvent* event) const = 0; |
| 91 | |
| 92 | signals: |
| 93 | virtual void pageTitleChanged(const QString& new_title) = 0; |
| 94 | virtual void pageUrlChanged(const QUrl& url) = 0; |
| 95 | virtual void pageIconChanged(const QIcon&) = 0; |
| 96 | virtual void linkMouseHighlighted(const QUrl& url) = 0; |
| 97 | virtual void loadingStarted() = 0; |
| 98 | virtual void loadingProgress(int progress) = 0; |
| 99 | virtual void loadingFinished(bool success) = 0; |
| 100 | virtual void newWindowRequested(WebViewer* viewer) = 0; |
| 101 | virtual void closeWindowRequested() = 0; |
| 102 | |
| 103 | private: |
| 104 | void playClickedLinkAsMedia(); |
| 105 | void openClickedLinkInExternalBrowser(); |
| 106 | void initializeCommonMenuItems(); |
| 107 | |
| 108 | private: |
| 109 | QScopedPointer<QAction> m_actionOpenExternalBrowser; |
| 110 | QScopedPointer<QAction> m_actionPlayLink; |
| 111 | ContextMenuData m_contextMenuData; |
| 112 | }; |
| 113 | |
| 114 | Q_DECLARE_INTERFACE(WebViewer, "WebViewer") |
| 115 | |
| 116 | inline void WebViewer::zoomIn() { |
| 117 | setZoomFactor(zoomFactor() + double(ZOOM_FACTOR_STEP)); |
| 118 | } |
| 119 | |
| 120 | inline void WebViewer::zoomOut() { |
| 121 | setZoomFactor(zoomFactor() - double(ZOOM_FACTOR_STEP)); |
| 122 | } |
| 123 | |
| 124 | inline bool WebViewer::canZoomIn() const { |
| 125 | return zoomFactor() <= double(MAX_ZOOM_FACTOR) - double(ZOOM_FACTOR_STEP); |
| 126 | } |
| 127 | |
| 128 | inline bool WebViewer::canZoomOut() const { |
| 129 | return zoomFactor() >= double(MIN_ZOOM_FACTOR) + double(ZOOM_FACTOR_STEP); |
| 130 | } |
| 131 | |
| 132 | #endif // WEBVIEWER_H |