MythTV  master
mythuiwebbrowser.h
Go to the documentation of this file.
1 #ifndef MYTHUI_WEBBROWSER_H_
2 #define MYTHUI_WEBBROWSER_H_
3 
4 #include <QtGlobal>
5 #include <QUrl>
6 #include "mythuitype.h"
7 #include "mythuiexp.h"
8 
9 #include <QString>
10 #include <QElapsedTimer>
11 #include <QColor>
12 #include <QIcon>
13 
14 #include <QWebView>
15 #include <QWebPage>
16 #include <QNetworkReply>
17 
18 class MythUIScrollBar;
19 class MythUIWebBrowser;
20 class MythUIBusyDialog;
21 class MythScreenType;
22 
23 class BrowserApi : public QObject
24 {
25  Q_OBJECT
26  public:
27  explicit BrowserApi(QObject *parent);
28  ~BrowserApi(void) override;
29 
30  void setWebView(QWebView *view);
31 
32  public slots:
33  static void Play(void);
34  static void Stop(void);
35  static void Pause(void);
36 
37  static void SetVolume(int volumn);
38  int GetVolume(void);
39 
40  static void PlayFile(const QString& filename);
41  static void PlayTrack(int trackID);
42  static void PlayURL(const QString& url);
43 
44  QString GetMetadata(void);
45 
46  private slots:
47  void attachObject();
48 
49  private:
50  void customEvent(QEvent *e) override; // QObject
51 
52  QWebFrame *m_frame { nullptr };
53 
54  bool m_gotAnswer { false };
55  QString m_answer;
56 };
57 
58 class MythNetworkAccessManager : public QNetworkAccessManager
59 {
60  Q_OBJECT
61  public:
62  MythNetworkAccessManager() = default;
63 
64  protected:
65  QNetworkReply* createRequest(Operation op, const QNetworkRequest & req,
66  QIODevice * outgoingData = nullptr) override; // QNetworkAccessManager
67 };
68 
69 class MythWebPage : public QWebPage
70 {
71  Q_OBJECT
72 
73  public:
74  explicit MythWebPage(QObject *parent = nullptr);
75  ~MythWebPage() override;
76 
77  bool extension (Extension extension, const ExtensionOption *option = nullptr,
78  ExtensionReturn *output = nullptr) override; // QWebPage
79  bool supportsExtension (Extension extension) const override; // QWebPage
80 
81  protected:
82  QString userAgentForUrl(const QUrl &url) const override; // QWebPage
83 
84  private:
85  friend class MythWebView;
86 };
87 
88 class MythWebView : public QWebView
89 {
90  Q_OBJECT
91 
92  public:
93  MythWebView(QWidget *parent, MythUIWebBrowser *parentBrowser);
94  ~MythWebView(void) override;
95 
96  void keyPressEvent(QKeyEvent *event) override; // QWidget
97  void customEvent(QEvent *e) override; // QWidget
98 
99  protected slots:
100  void handleUnsupportedContent(QNetworkReply *reply);
101  void handleDownloadRequested(const QNetworkRequest &request);
102  QWebView *createWindow(QWebPage::WebWindowType type) override; // QWebView
103 
104  private:
105  void showDownloadMenu(void);
106  void doDownloadRequested(const QNetworkRequest &request);
107  void doDownload(const QString &saveFilename);
108  void openBusyPopup(const QString &message);
109  void closeBusyPopup(void);
110 
111  static bool isMusicFile(const QString &extension, const QString &mimetype);
112  static bool isVideoFile(const QString &extension, const QString &mimetype);
113 
114  QString getReplyMimetype(void);
115  static QString getExtensionForMimetype(const QString &mimetype);
116 
117  MythWebPage *m_webpage {nullptr};
119  BrowserApi *m_api {nullptr};
120  QNetworkRequest m_downloadRequest;
121  QNetworkReply *m_downloadReply {nullptr};
123  bool m_downloadAndPlay {false};
124 };
125 
133 {
134  Q_OBJECT
135 
136  public:
137  MythUIWebBrowser(MythUIType *parent, const QString &name);
138  ~MythUIWebBrowser() override;
139 
140  void Init(void);
141 
142  void LoadPage(const QUrl& url);
143  void SetHtml(const QString &html, const QUrl &baseUrl = QUrl());
144 
145  void LoadUserStyleSheet(const QUrl& url);
146 
147  bool keyPressEvent(QKeyEvent *event) override; // MythUIType
148  void Pulse(void) override; // MythUIType
149  void Scroll(int dx, int dy);
150 
151  QIcon GetIcon(void);
152  QUrl GetUrl(void);
153  QString GetTitle(void);
154 
155  void SetActive(bool active);
156  bool IsActive(void) const { return m_active; }
157 
159  bool IsInputToggled(void) const { return m_inputToggled; }
160  void SetInputToggled(bool inputToggled) { m_inputToggled = inputToggled; }
161 
162  void SetZoom(float zoom);
163  float GetZoom(void) const;
164 
165  bool CanGoForward(void);
166  bool CanGoBack(void);
167 
168  QVariant evaluateJavaScript(const QString& scriptSource);
169 
170  void SetDefaultSaveDirectory(const QString &saveDir);
171  QString GetDefaultSaveDirectory(void) { return m_defaultSaveDir; }
172 
173  void SetDefaultSaveFilename(const QString &filename);
174  QString GetDefaultSaveFilename(void) { return m_defaultSaveFilename; }
175 
176  public slots:
177  void Back(void);
178  void Forward(void);
179  void ZoomIn(void);
180  void ZoomOut(void);
181 
182  signals:
183  void loadStarted(void);
184  void loadFinished(bool ok);
185  void loadProgress(int progress);
186  void titleChanged(const QString &title);
187  void statusBarMessage(const QString &text);
188  void iconChanged(void);
189  void fileDownloaded(QString filename);
190 
191  protected slots:
192  void slotLoadStarted(void);
193  void slotLoadFinished(bool Ok);
194  void slotLoadProgress(int progress);
195  void slotTitleChanged(const QString &title);
196  void slotStatusBarMessage(const QString &text);
197  void slotIconChanged(void);
198  void slotLinkClicked(const QUrl &url);
199  void slotTopScreenChanged(MythScreenType *screen);
200  void slotScrollBarShowing(void);
201  void slotScrollBarHiding(void);
202 
203  protected:
204  void Finalize(void) override; // MythUIType
205  void UpdateBuffer(void);
206  void HandleMouseAction(const QString &action);
207  void SetBackgroundColor(QColor color);
208  void ResetScrollBars(void);
209  void UpdateScrollBars(void);
210  bool IsOnTopScreen(void);
211 
212  void DrawSelf(MythPainter *p, int xoffset, int yoffset,
213  int alphaMod, QRect clipRect) override; // MythUIType
214 
215  bool ParseElement(const QString &filename, QDomElement &element,
216  bool showWarnings) override; // MythUIType
217  void CopyFrom(MythUIType *base) override; // MythUIType
218  void CreateCopy(MythUIType *parent) override; // MythUIType
219 
220  MythScreenType *m_parentScreen { nullptr };
221 
222  MythWebView *m_browser { nullptr };
225 
226  MythImage *m_image { nullptr };
227 
228  bool m_active { false };
229  bool m_wasActive { false };
230  bool m_initialized { false };
231  QElapsedTimer m_lastUpdateTime;
232  int m_updateInterval { 0 };
233 
234  float m_zoom { 1.0 };
235  QColor m_bgColor;
237  QString m_userCssFile;
240 
241  bool m_inputToggled { false };
243  int m_mouseKeyCount { 0 };
244  QElapsedTimer m_lastMouseActionTime;
245 
246  MythUIScrollBar *m_horizontalScrollbar { nullptr };
247  MythUIScrollBar *m_verticalScrollbar { nullptr };
250 };
251 
252 #endif
MythUIType::keyPressEvent
virtual bool keyPressEvent(QKeyEvent *event)
Key event handler.
Definition: mythuitype.cpp:989
MythWebPage
Definition: mythuiwebbrowser.h:69
MUI_PUBLIC
#define MUI_PUBLIC
Definition: mythuiexp.h:9
MythWebPage::extension
bool extension(Extension extension, const ExtensionOption *option=nullptr, ExtensionReturn *output=nullptr) override
Definition: mythuiwebbrowser.cpp:289
MythWebView::getReplyMimetype
QString getReplyMimetype(void)
Definition: mythuiwebbrowser.cpp:770
MythUIWebBrowser::GetDefaultSaveDirectory
QString GetDefaultSaveDirectory(void)
Definition: mythuiwebbrowser.h:171
MythWebPage::userAgentForUrl
QString userAgentForUrl(const QUrl &url) const override
Definition: mythuiwebbrowser.cpp:346
MythWebView::closeBusyPopup
void closeBusyPopup(void)
Definition: mythuiwebbrowser.cpp:595
MythUIWebBrowser::IsInputToggled
bool IsInputToggled(void) const
returns true if all keypresses are to be passed to the web page
Definition: mythuiwebbrowser.h:159
progress
bool progress
Definition: mythcommflag.cpp:69
BrowserApi::GetVolume
int GetVolume(void)
Definition: mythuiwebbrowser.cpp:176
MythUIWebBrowser::m_browserArea
MythRect m_browserArea
Definition: mythuiwebbrowser.h:223
MythNetworkAccessManager::MythNetworkAccessManager
MythNetworkAccessManager()=default
MythWebView
Subclass of QWebView.
Definition: mythuiwebbrowser.h:88
MythUIWebBrowser::m_userCssFile
QString m_userCssFile
Definition: mythuiwebbrowser.h:237
MythWebView::handleUnsupportedContent
void handleUnsupportedContent(QNetworkReply *reply)
Definition: mythuiwebbrowser.cpp:459
MythWebView::m_parentBrowser
MythUIWebBrowser * m_parentBrowser
Definition: mythuiwebbrowser.h:118
BrowserApi::m_gotAnswer
bool m_gotAnswer
Definition: mythuiwebbrowser.h:54
MythWebView::openBusyPopup
void openBusyPopup(const QString &message)
Definition: mythuiwebbrowser.cpp:578
MythUIWebBrowser::SetInputToggled
void SetInputToggled(bool inputToggled)
Definition: mythuiwebbrowser.h:160
MythWebView::showDownloadMenu
void showDownloadMenu(void)
Definition: mythuiwebbrowser.cpp:695
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
MythWebView::m_api
BrowserApi * m_api
Definition: mythuiwebbrowser.h:119
BrowserApi::m_answer
QString m_answer
Definition: mythuiwebbrowser.h:55
MythWebView::doDownload
void doDownload(const QString &saveFilename)
Definition: mythuiwebbrowser.cpp:565
MythRect
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:17
MythUIType::Pulse
virtual void Pulse(void)
Pulse is called 70 times a second to trigger a single frame of an animation.
Definition: mythuitype.cpp:456
MythUIType::CreateCopy
virtual void CreateCopy(MythUIType *parent)
Copy the state of this widget to the one given, it must be of the same type.
Definition: mythuitype.cpp:1228
MythUIType::DrawSelf
virtual void DrawSelf(MythPainter *p, int xoffset, int yoffset, int alphaMod, QRect clipRect)
Definition: mythuitype.cpp:479
MythUIWebBrowser::m_defaultSaveDir
QString m_defaultSaveDir
Definition: mythuiwebbrowser.h:238
MythWebPage::~MythWebPage
~MythWebPage() override
Definition: mythuiwebbrowser.cpp:277
BrowserApi::SetVolume
static void SetVolume(int volumn)
Definition: mythuiwebbrowser.cpp:169
BrowserApi::setWebView
void setWebView(QWebView *view)
Definition: mythuiwebbrowser.cpp:136
BrowserApi
Adds a JavaScript object.
Definition: mythuiwebbrowser.h:23
MythNetworkAccessManager
Definition: mythuiwebbrowser.h:58
MythUIWebBrowser::m_lastUpdateTime
QElapsedTimer m_lastUpdateTime
Definition: mythuiwebbrowser.h:231
MythWebView::createWindow
QWebView * createWindow(QWebPage::WebWindowType type) override
Definition: mythuiwebbrowser.cpp:784
MythUIType::SetZoom
void SetZoom(float zoom)
Definition: mythuitype.cpp:961
MythUIWebBrowser::IsActive
bool IsActive(void) const
Definition: mythuiwebbrowser.h:156
hardwareprofile.config.p
p
Definition: config.py:33
MythWebView::isMusicFile
static bool isMusicFile(const QString &extension, const QString &mimetype)
Definition: mythuiwebbrowser.cpp:740
MythWebView::isVideoFile
static bool isVideoFile(const QString &extension, const QString &mimetype)
Definition: mythuiwebbrowser.cpp:755
MythUIWebBrowser::m_destinationScrollPos
QPoint m_destinationScrollPos
Definition: mythuiwebbrowser.h:249
MythWebView::m_webpage
MythWebPage * m_webpage
Definition: mythuiwebbrowser.h:117
MythWebView::getExtensionForMimetype
static QString getExtensionForMimetype(const QString &mimetype)
Definition: mythuiwebbrowser.cpp:727
BrowserApi::Pause
static void Pause(void)
Definition: mythuiwebbrowser.cpp:163
BrowserApi::PlayTrack
static void PlayTrack(int trackID)
Definition: mythuiwebbrowser.cpp:206
BrowserApi::BrowserApi
BrowserApi(QObject *parent)
Definition: mythuiwebbrowser.cpp:125
BrowserApi::Stop
static void Stop(void)
Definition: mythuiwebbrowser.cpp:157
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1171
MythUIBusyDialog
Definition: mythprogressdialog.h:36
MythUIWebBrowser::m_bgColor
QColor m_bgColor
Definition: mythuiwebbrowser.h:235
BrowserApi::GetMetadata
QString GetMetadata(void)
Definition: mythuiwebbrowser.cpp:220
MythWebPage::MythWebPage
MythWebPage(QObject *parent=nullptr)
Definition: mythuiwebbrowser.cpp:271
MythWebView::m_downloadAndPlay
bool m_downloadAndPlay
Definition: mythuiwebbrowser.h:123
MythWebPage::supportsExtension
bool supportsExtension(Extension extension) const override
Definition: mythuiwebbrowser.cpp:284
MythWebView::m_downloadReply
QNetworkReply * m_downloadReply
Definition: mythuiwebbrowser.h:121
MythUIScrollBar
Scroll bar widget.
Definition: mythuiscrollbar.h:15
BrowserApi::~BrowserApi
~BrowserApi(void) override
Definition: mythuiwebbrowser.cpp:131
MythUIWebBrowser::m_defaultSaveFilename
QString m_defaultSaveFilename
Definition: mythuiwebbrowser.h:239
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:85
MythUIWebBrowser::GetDefaultSaveFilename
QString GetDefaultSaveFilename(void)
Definition: mythuiwebbrowser.h:174
MythUIWebBrowser::m_actualBrowserArea
MythRect m_actualBrowserArea
Definition: mythuiwebbrowser.h:224
MythWebView::doDownloadRequested
void doDownloadRequested(const QNetworkRequest &request)
Definition: mythuiwebbrowser.cpp:489
MythUIAnimation
Definition: mythuianimation.h:47
MythPainter
Definition: mythpainter.h:34
MythImage
Definition: mythimage.h:36
BrowserApi::m_frame
QWebFrame * m_frame
Definition: mythuiwebbrowser.h:52
MythWebView::customEvent
void customEvent(QEvent *e) override
Definition: mythuiwebbrowser.cpp:603
BrowserApi::PlayURL
static void PlayURL(const QString &url)
Definition: mythuiwebbrowser.cpp:213
MythUIWebBrowser::m_widgetUrl
QUrl m_widgetUrl
Definition: mythuiwebbrowser.h:236
MythNetworkAccessManager::createRequest
QNetworkReply * createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override
Definition: mythuiwebbrowser.cpp:83
build_compdb.action
action
Definition: build_compdb.py:9
MythUIWebBrowser::m_scrollAnimation
MythUIAnimation m_scrollAnimation
Definition: mythuiwebbrowser.h:248
MythWebView::~MythWebView
~MythWebView(void) override
Definition: mythuiwebbrowser.cpp:375
MythWebView::m_busyPopup
MythUIBusyDialog * m_busyPopup
Definition: mythuiwebbrowser.h:122
MythUIWebBrowser
Web browsing widget.
Definition: mythuiwebbrowser.h:132
BrowserApi::customEvent
void customEvent(QEvent *e) override
Definition: mythuiwebbrowser.cpp:243
MythUIWebBrowser::m_lastMouseActionTime
QElapsedTimer m_lastMouseActionTime
Definition: mythuiwebbrowser.h:244
MythWebView::MythWebView
MythWebView(QWidget *parent, MythUIWebBrowser *parentBrowser)
Definition: mythuiwebbrowser.cpp:356
MythWebView::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Definition: mythuiwebbrowser.cpp:394
MythUIType::ParseElement
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
Definition: mythuitype.cpp:1237
BrowserApi::Play
static void Play(void)
Definition: mythuiwebbrowser.cpp:151
build_compdb.filename
filename
Definition: build_compdb.py:21
MythWebView::handleDownloadRequested
void handleDownloadRequested(const QNetworkRequest &request)
Definition: mythuiwebbrowser.cpp:483
output
#define output
Definition: synaesthesia.cpp:223
MythWebView::m_downloadRequest
QNetworkRequest m_downloadRequest
Definition: mythuiwebbrowser.h:120
mythuiexp.h
MythUIType::Finalize
virtual void Finalize(void)
Perform any post-xml parsing initialisation tasks.
Definition: mythuitype.cpp:1312
MythUIWebBrowser::m_lastMouseAction
QString m_lastMouseAction
Definition: mythuiwebbrowser.h:242
BrowserApi::attachObject
void attachObject()
Definition: mythuiwebbrowser.cpp:146
mythuitype.h
BrowserApi::PlayFile
static void PlayFile(const QString &filename)
Definition: mythuiwebbrowser.cpp:199