1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Assistant of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef HELPVIEWER_H
30 #define HELPVIEWER_H
31 
32 #include <QtCore/qglobal.h>
33 #include <QtCore/QString>
34 #include <QtCore/QUrl>
35 #include <QtCore/QVariant>
36 
37 #include <QtWidgets/QAction>
38 #include <QtGui/QFont>
39 
40 #if defined(BROWSER_QTWEBKIT)
41 #  include <QWebView>
42 #elif defined(BROWSER_QTEXTBROWSER)
43 #  include <QtWidgets/QTextBrowser>
44 #endif
45 
46 QT_BEGIN_NAMESPACE
47 
48 class HelpEngineWrapper;
49 
50 #if defined(BROWSER_QTWEBKIT)
51 #define TEXTBROWSER_OVERRIDE
52 class HelpViewer : public QWebView
53 #elif defined(BROWSER_QTEXTBROWSER)
54 #define TEXTBROWSER_OVERRIDE override
55 class HelpViewer : public QTextBrowser
56 #endif
57 {
58     Q_OBJECT
59     class HelpViewerPrivate;
60     Q_DISABLE_COPY(HelpViewer)
61 
62 public:
63     enum FindFlag {
64         FindBackward = 0x01,
65         FindCaseSensitively = 0x02
66     };
67     Q_DECLARE_FLAGS(FindFlags, FindFlag)
68 
69     HelpViewer(qreal zoom, QWidget *parent = nullptr);
70     ~HelpViewer() override;
71 
72     QFont viewerFont() const;
73     void setViewerFont(const QFont &font);
74 
75     void scaleUp();
76     void scaleDown();
77 
78     void resetScale();
79     qreal scale() const;
80 
81     QString title() const;
82     void setTitle(const QString &title);
83 
84     QUrl source() const;
85     void setSource(const QUrl &url) TEXTBROWSER_OVERRIDE;
86 
87     QString selectedText() const;
88     bool isForwardAvailable() const;
89     bool isBackwardAvailable() const;
90 
91     bool findText(const QString &text, FindFlags flags, bool incremental,
92         bool fromSearch);
93 
94     static const QString AboutBlank;
95     static const QString LocalHelpFile;
96     static const QString PageNotFoundMessage;
97 
98     static bool isLocalUrl(const QUrl &url);
99     static bool canOpenPage(const QString &url);
100     static QString mimeFromUrl(const QUrl &url);
101     static bool launchWithExternalApp(const QUrl &url);
102 
103 public slots:
104 #if QT_CONFIG(clipboard)
105     void copy();
106 #endif
107     void home() TEXTBROWSER_OVERRIDE;
108 
109     void forward() TEXTBROWSER_OVERRIDE;
110     void backward() TEXTBROWSER_OVERRIDE;
111 
112 signals:
113     void titleChanged();
114 #if !defined(BROWSER_QTEXTBROWSER)
115     // Provide signals present in QTextBrowser, QTextEdit for browsers that do not inherit QTextBrowser
116     void copyAvailable(bool yes);
117     void sourceChanged(const QUrl &url);
118     void forwardAvailable(bool enabled);
119     void backwardAvailable(bool enabled);
120     void highlighted(const QUrl &link);
121     void printRequested();
122 #elif !defined(BROWSER_QTWEBKIT)
123     // Provide signals present in QWebView for browsers that do not inherit QWebView
124     void loadStarted();
125     void loadFinished(bool finished);
126 #endif
127 
128 protected:
129     void keyPressEvent(QKeyEvent *e) override;
130     void wheelEvent(QWheelEvent *event) override;
131     void mousePressEvent(QMouseEvent *event) override;
132     void mouseReleaseEvent(QMouseEvent *event) override;
133 #if defined(BROWSER_QTEXTBROWSER)
134     void resizeEvent(QResizeEvent *e) override;
135 #endif
136 
137 private slots:
138     void actionChanged();
139     void setLoadStarted();
140     void setLoadFinished(bool ok);
141 
142 private:
143     bool eventFilter(QObject *obj, QEvent *event) override;
144     void contextMenuEvent(QContextMenuEvent *event) override;
145     QVariant loadResource(int type, const QUrl &name) TEXTBROWSER_OVERRIDE;
146     bool handleForwardBackwardMouseButtons(QMouseEvent *e);
147     void scrollToTextPosition(int position);
148 
149 private:
150     HelpViewerPrivate *d;
151 };
152 
153 QT_END_NAMESPACE
154 Q_DECLARE_METATYPE(HelpViewer*)
155 
156 #endif  // HELPVIEWER_H
157