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 <QTime>
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);
29 
30  void setWebView(QWebView *view);
31 
32  public slots:
33  void Play(void);
34  void Stop(void);
35  void Pause(void);
36 
37  void SetVolume(int volumn);
38  int GetVolume(void);
39 
40  void PlayFile(const QString& filename);
41  void PlayTrack(int trackID);
42  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;
53 
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();
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);
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  bool isMusicFile(const QString &extension, const QString &mimetype);
112  bool isVideoFile(const QString &extension, const QString &mimetype);
113 
114  QString getReplyMimetype(void);
115  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();
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) { return m_active; }
157 
159  bool IsInputToggled(void) { return m_inputToggled; }
160  void SetInputToggled(bool inputToggled) { m_inputToggled = inputToggled; }
161 
162  void SetZoom(float zoom);
163  float GetZoom(void);
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 
221 
225 
227 
228  bool m_active;
233 
234  float m_zoom;
235  QColor m_bgColor;
237  QString m_userCssFile;
240 
245 
250 };
251 
252 #endif
MythWebView(QWidget *parent, MythUIWebBrowser *parentBrowser)
MythUIWebBrowser * m_parentBrowser
unsigned int slots[4]
Definition: element.c:38
virtual bool keyPressEvent(QKeyEvent *event)
Key event handler.
Definition: mythuitype.cpp:971
void keyPressEvent(QKeyEvent *event) override
void handleDownloadRequested(const QNetworkRequest &request)
MythUIAnimation m_scrollAnimation
bool isVideoFile(const QString &extension, const QString &mimetype)
Adds a JavaScript object.
MythUIScrollBar * m_verticalScrollbar
void customEvent(QEvent *e) override
void Stop(void)
MythNetworkAccessManager()=default
MythRect m_actualBrowserArea
QString getExtensionForMimetype(const QString &mimetype)
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
bool IsInputToggled(void)
returns true if all keypresses are to be passed to the web page
virtual void CreateCopy(MythUIType *parent)
Copy the state of this widget to the one given, it must be of the same type.
QString GetDefaultSaveDirectory(void)
void Play(void)
QNetworkReply * createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override
void closeBusyPopup(void)
BrowserApi * m_api
MythUIScrollBar * m_horizontalScrollbar
QString GetMetadata(void)
void handleUnsupportedContent(QNetworkReply *reply)
MythWebView * m_browser
void openBusyPopup(const QString &message)
Subclass of QWebView.
void customEvent(QEvent *e) override
void showDownloadMenu(void)
virtual void Pulse(void)
Pulse is called 70 times a second to trigger a single frame of an animation.
Definition: mythuitype.cpp:442
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:17
void doDownload(const QString &saveFilename)
QWebView * createWindow(QWebPage::WebWindowType type) override
virtual void DrawSelf(MythPainter *p, int xoffset, int yoffset, int alphaMod, QRect clipRect)
Definition: mythuitype.cpp:465
const char * name
Definition: ParseText.cpp:328
int GetVolume(void)
void PlayURL(const QString &url)
MythWebPage * m_webpage
void PlayTrack(int trackID)
bool isMusicFile(const QString &extension, const QString &mimetype)
QNetworkRequest m_downloadRequest
void setWebView(QWebView *view)
MythUIBusyDialog * m_busyPopup
MythWebPage(QObject *parent=nullptr)
void PlayFile(const QString &filename)
Scroll bar widget.
QNetworkReply * m_downloadReply
#define MUI_PUBLIC
Definition: mythuiexp.h:9
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
void SetVolume(int volumn)
bool extension(Extension extension, const ExtensionOption *option=nullptr, ExtensionReturn *output=nullptr) override
bool supportsExtension(Extension extension) const override
void Pause(void)
QString userAgentForUrl(const QUrl &url) const override
Screen in which all other widgets are contained and rendered.
Web browsing widget.
QString m_answer
void doDownloadRequested(const QNetworkRequest &request)
QString getReplyMimetype(void)
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
BrowserApi(QObject *parent)
QString m_defaultSaveFilename
void SetInputToggled(bool inputToggled)
MythScreenType * m_parentScreen
void SetZoom(float zoom)
Definition: mythuitype.cpp:943
virtual void Finalize(void)
Perform any post-xml parsing initialisation tasks.
QString GetDefaultSaveFilename(void)
QWebFrame * m_frame
#define output