1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "helpviewer.h"
29 #include "openpagesmanager.h"
30 
31 #include <QTextBrowser>
32 
33 namespace Help {
34 namespace Internal {
35 
36 class TextBrowserHelpWidget;
37 
38 class TextBrowserHelpViewer : public HelpViewer
39 {
40     Q_OBJECT
41 
42 public:
43     explicit TextBrowserHelpViewer(QWidget *parent = nullptr);
44     ~TextBrowserHelpViewer() override;
45 
46     void setViewerFont(const QFont &font) override;
47 
48     void setScale(qreal scale) override;
49 
50     QString title() const override;
51 
52     QUrl source() const override;
53     void setSource(const QUrl &url) override;
54 
55     void setHtml(const QString &html) override;
56 
57     QString selectedText() const override;
58     bool isForwardAvailable() const override;
59     bool isBackwardAvailable() const override;
60     void addBackHistoryItems(QMenu *backMenu) override;
61     void addForwardHistoryItems(QMenu *forwardMenu) override;
62 
63     bool findText(const QString &text, Core::FindFlags flags,
64                   bool incremental, bool fromSearch, bool *wrapped = nullptr) override;
65 
66     void copy() override;
67     void stop() override;
68     void forward() override;
69     void backward() override;
70     void print(QPrinter *printer) override;
71 
72 private:
73     void goToHistoryItem();
74     void setFontAndScale(const QFont &font, qreal scale);
75 
76     TextBrowserHelpWidget *m_textBrowser;
77 };
78 
79 class TextBrowserHelpWidget : public QTextBrowser
80 {
81     Q_OBJECT
82 
83 public:
84     TextBrowserHelpWidget(TextBrowserHelpViewer *parent);
85 
86     QVariant loadResource(int type, const QUrl &name) override;
87 
88     void withFixedTopPosition(const std::function<void()> &action);
89 
90 protected:
91     void contextMenuEvent(QContextMenuEvent *event) override;
92     bool eventFilter(QObject *obj, QEvent *event) override;
93     void wheelEvent(QWheelEvent *e) override;
94     void mousePressEvent(QMouseEvent *e) override;
95     void mouseReleaseEvent(QMouseEvent *e) override;
96     void resizeEvent(QResizeEvent *e) override;
97 
98 private:
99     QString linkAt(const QPoint &pos);
100     void scrollToTextPosition(int position);
101 
102     TextBrowserHelpViewer *m_parent;
103     friend class Help::Internal::TextBrowserHelpViewer;
104 };
105 
106 }   // namespace Internal
107 }   // namespace Help
108