45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
73
|
41 module webview;
|
|
42
|
|
43 import qt.gui.QClipboard;
|
|
44 import qt.gui.QMenu;
|
|
45 import qt.gui.QMessageBox;
|
|
46 import qt.gui.QMouseEvent;
|
|
47
|
|
48 import qt.core.QDebug;
|
|
49 import qt.core.QBuffer;
|
45
|
50
|
|
51 import QtWebKit.QWebView;
|
73
|
52 import QtWebKit.QWebHitTestResult;
|
|
53 import QtUiTools.QUiLoader;
|
45
|
54
|
|
55 import browserapplication;
|
|
56 import browsermainwindow;
|
|
57 import cookiejar;
|
|
58 import downloadmanager;
|
|
59 import networkaccessmanager;
|
|
60 import tabwidget;
|
|
61
|
|
62
|
73
|
63 class WebPage : public QWebPage
|
45
|
64 {
|
74
|
65 mixin Signal!("loadingUrl", QUrl /*url*/);
|
45
|
66
|
|
67 public:
|
|
68
|
73
|
69 this(QObject parent = null)
|
|
70 {
|
|
71 super(parent);
|
|
72 m_keyboardModifiers = Qt.NoModifier;
|
|
73 m_pressedButtons = Qt.NoButton;
|
|
74 m_openInNewTab = false;
|
|
75 setNetworkAccessManager(BrowserApplication.networkAccessManager());
|
|
76 this.unsupportedContent.connect(&this.handleUnsupportedContent);
|
|
77 }
|
|
78
|
|
79 BrowserMainWindow mainWindow()
|
|
80 {
|
|
81 QObject w = this.parent();
|
|
82 while (w) {
|
77
|
83 if (BrowserMainWindow mw = cast(BrowserMainWindow) w)
|
73
|
84 return mw;
|
|
85 w = w.parent();
|
|
86 }
|
|
87 return BrowserApplication.instance().mainWindow();
|
|
88 }
|
45
|
89
|
|
90 protected:
|
73
|
91
|
74
|
92 bool acceptNavigationRequest(QWebFrame frame, QNetworkRequest request, NavigationType type)
|
73
|
93 {
|
|
94 // ctrl open in new tab
|
|
95 // ctrl-shift open in new tab and select
|
|
96 // ctrl-alt open in new window
|
|
97 if (type == QWebPage.NavigationTypeLinkClicked
|
|
98 && (m_keyboardModifiers & Qt.ControlModifier
|
|
99 || m_pressedButtons == Qt.MidButton)) {
|
|
100 bool newWindow = (m_keyboardModifiers & Qt.AltModifier);
|
|
101 WebView webView;
|
|
102 if (newWindow) {
|
|
103 BrowserApplication.instance().newMainWindow();
|
|
104 BrowserMainWindow newMainWindow = BrowserApplication.instance().mainWindow();
|
|
105 webView = newMainWindow.currentTab();
|
|
106 newMainWindow.raise();
|
|
107 newMainWindow.activateWindow();
|
|
108 webView.setFocus();
|
|
109 } else {
|
|
110 bool selectNewTab = (m_keyboardModifiers & Qt.ShiftModifier);
|
|
111 webView = mainWindow().tabWidget().newTab(selectNewTab);
|
|
112 }
|
|
113 webView.load(request);
|
|
114 m_keyboardModifiers = Qt.NoModifier;
|
|
115 m_pressedButtons = Qt.NoButton;
|
|
116 return false;
|
|
117 }
|
|
118 if (frame == mainFrame()) {
|
|
119 m_loadingUrl = request.url();
|
77
|
120 loadingUrl.emit(m_loadingUrl);
|
73
|
121 }
|
|
122 return QWebPage.acceptNavigationRequest(frame, request, type);
|
|
123 }
|
|
124
|
|
125 QWebPage createWindow(QWebPage.WebWindowType type)
|
|
126 {
|
|
127 if (m_keyboardModifiers & Qt.ControlModifier || m_pressedButtons == Qt.MidButton)
|
|
128 m_openInNewTab = true;
|
|
129 if (m_openInNewTab) {
|
|
130 m_openInNewTab = false;
|
|
131 return mainWindow().tabWidget().newTab().page();
|
|
132 }
|
|
133 BrowserApplication.instance().newMainWindow();
|
|
134 BrowserMainWindow mainWindow = BrowserApplication.instance().mainWindow();
|
|
135 return mainWindow.currentTab().page();
|
|
136 }
|
45
|
137
|
73
|
138 version(QT_NO_UITOOLS) {} else
|
|
139 {
|
85
|
140 QObject createPlugin(string classId, QUrl url, string[] paramNames, string[] paramValues)
|
73
|
141 {
|
77
|
142 auto loader = new QUiLoader;
|
73
|
143 return loader.createWidget(classId, view());
|
|
144 }
|
|
145 }
|
|
146
|
|
147 private:
|
|
148
|
|
149 void handleUnsupportedContent(QNetworkReply reply)
|
|
150 {
|
|
151 if (reply.error() == QNetworkReply.NoError) {
|
|
152 BrowserApplication.downloadManager().handleUnsupportedContent(reply);
|
|
153 return;
|
|
154 }
|
|
155
|
80
|
156 auto file = new QFile(":/notfound.html");
|
73
|
157 bool isOpened = file.open(QIODevice.ReadOnly);
|
74
|
158 assert(isOpened);
|
77
|
159 string title = Format(tr("Error loading page: {}"), reply.url().toString());
|
80
|
160 string html = Format(file.readAll(),
|
77
|
161 title,
|
|
162 reply.errorString(),
|
|
163 reply.url().toString());
|
45
|
164
|
73
|
165 QBuffer imageBuffer;
|
|
166 imageBuffer.open(QBuffer.ReadWrite);
|
|
167 QIcon icon = view().style().standardIcon(QStyle.SP_MessageBoxWarning, 0, view());
|
|
168 QPixmap pixmap = icon.pixmap(QSize(32,32));
|
|
169 if (pixmap.save(&imageBuffer, "PNG")) {
|
80
|
170 html.replace("IMAGE_BINARY_DATA_HERE",
|
|
171 imageBuffer.buffer().toBase64());
|
73
|
172 }
|
|
173
|
74
|
174 QWebFrame[] frames;
|
73
|
175 frames.append(mainFrame());
|
|
176 while (!frames.isEmpty()) {
|
|
177 QWebFrame frame = frames.takeFirst();
|
|
178 if (frame.url() == reply.url()) {
|
|
179 frame.setHtml(html, reply.url());
|
|
180 return;
|
|
181 }
|
74
|
182 QWebFrame[] children = frame.childFrames();
|
|
183 foreach(QWebFrame frame; children)
|
73
|
184 frames.append(frame);
|
|
185 }
|
|
186 if (m_loadingUrl == reply.url()) {
|
|
187 mainFrame().setHtml(html, reply.url());
|
|
188 }
|
|
189 }
|
|
190
|
|
191 private:
|
|
192
|
|
193 // set the webview mousepressedevent
|
|
194 Qt.KeyboardModifiers m_keyboardModifiers;
|
|
195 Qt.MouseButtons m_pressedButtons;
|
|
196 bool m_openInNewTab;
|
|
197 QUrl m_loadingUrl;
|
85
|
198 }
|
73
|
199
|
|
200 class WebView : public QWebView
|
45
|
201 {
|
73
|
202 public:
|
|
203
|
85
|
204 this(QWidget parent = null)
|
73
|
205 {
|
|
206 super(parent);
|
|
207 m_progress = 0;
|
|
208 m_page = new WebPage(this);
|
|
209 setPage(m_page);
|
|
210 page().statusBarMessage.connect(&setStatusBarText);
|
|
211 this.loadProgress.connect(&this.setProgress);
|
|
212 this.loadFinished.connect(&this.loadFinished);
|
|
213 page().loadingUrl.connect(&this.urlChanged);
|
|
214 page().downloadRequested.connect(&this.downloadRequested);
|
|
215 page().setForwardUnsupportedContent(true);
|
|
216 }
|
|
217
|
|
218 WebPage webPage() { return m_page; }
|
|
219
|
|
220 void loadUrl(QUrl url)
|
|
221 {
|
|
222 m_initialUrl = url;
|
|
223 load(url);
|
|
224 }
|
|
225
|
74
|
226 QUrl url()
|
73
|
227 {
|
|
228 QUrl url = QWebView.url();
|
|
229 if (!url.isEmpty())
|
|
230 return url;
|
|
231 return m_initialUrl;
|
|
232 }
|
|
233
|
77
|
234 string lastStatusBarText()
|
73
|
235 {
|
|
236 return m_statusBarText;
|
|
237 }
|
|
238
|
85
|
239 int progress()
|
|
240 {
|
|
241 return m_progress;
|
|
242 }
|
73
|
243
|
|
244 protected:
|
45
|
245
|
74
|
246 void mousePressEvent(QMouseEvent event)
|
73
|
247 {
|
|
248 m_page.m_pressedButtons = event.buttons();
|
|
249 m_page.m_keyboardModifiers = event.modifiers();
|
|
250 QWebView.mousePressEvent(event);
|
|
251 }
|
|
252
|
|
253 void mouseReleaseEvent(QMouseEvent event)
|
|
254 {
|
|
255 QWebView.mouseReleaseEvent(event);
|
|
256 if (!event.isAccepted() && (m_page.m_pressedButtons & Qt.MidButton)) {
|
85
|
257 auto url = new QUrl(QApplication.clipboard().text(QClipboard.Selection));
|
73
|
258 if (!url.isEmpty() && url.isValid() && !url.scheme().isEmpty()) {
|
|
259 setUrl(url);
|
|
260 }
|
|
261 }
|
|
262 }
|
|
263
|
|
264 void contextMenuEvent(QContextMenuEvent event)
|
|
265 {
|
|
266 QWebHitTestResult r = page().mainFrame().hitTestContent(event.pos());
|
|
267 if (!r.linkUrl().isEmpty()) {
|
85
|
268 auto menu = new QMenu(this);
|
73
|
269 menu.addAction(pageAction(QWebPage.OpenLinkInNewWindow));
|
|
270 menu.addAction(tr("Open in New Tab"), this, SLOT(openLinkInNewTab()));
|
|
271 menu.addSeparator();
|
|
272 menu.addAction(pageAction(QWebPage.DownloadLinkToDisk));
|
|
273 // Add link to bookmarks...
|
|
274 menu.addSeparator();
|
|
275 menu.addAction(pageAction(QWebPage.CopyLinkToClipboard));
|
|
276 if (page().settings().testAttribute(QWebSettings.DeveloperExtrasEnabled))
|
|
277 menu.addAction(pageAction(QWebPage.InspectElement));
|
|
278 menu.exec(mapToGlobal(event.pos()));
|
|
279 return;
|
|
280 }
|
|
281 QWebView.contextMenuEvent(event);
|
|
282 }
|
|
283
|
|
284 void wheelEvent(QWheelEvent event)
|
|
285 {
|
|
286 if (QApplication.keyboardModifiers() & Qt.ControlModifier) {
|
|
287 int numDegrees = event.delta() / 8;
|
|
288 int numSteps = numDegrees / 15;
|
|
289 setTextSizeMultiplier(textSizeMultiplier() + numSteps * 0.1);
|
|
290 event.accept();
|
|
291 return;
|
|
292 }
|
|
293 QWebView.wheelEvent(event);
|
|
294 }
|
45
|
295
|
|
296
|
73
|
297 private:
|
|
298
|
|
299 void setProgress(int progress)
|
|
300 {
|
|
301 m_progress = progress;
|
|
302 }
|
45
|
303
|
73
|
304 void loadFinished()
|
|
305 {
|
|
306 if (100 != m_progress) {
|
|
307 qWarning() << "Received finished signal while progress is still:" << progress() << "Url:" << url();
|
|
308 }
|
|
309 m_progress = 0;
|
|
310 }
|
45
|
311
|
77
|
312 void setStatusBarText(string str)
|
73
|
313 {
|
77
|
314 m_statusBarText = str;
|
73
|
315 }
|
45
|
316
|
73
|
317 void downloadRequested(QNetworkRequest request)
|
|
318 {
|
|
319 BrowserApplication.downloadManager().download(request);
|
|
320 }
|
45
|
321
|
73
|
322 void openLinkInNewTab()
|
|
323 {
|
|
324 m_page.m_openInNewTab = true;
|
|
325 pageAction(QWebPage.OpenLinkInNewWindow).trigger();
|
|
326 }
|
|
327
|
45
|
328 private:
|
73
|
329
|
77
|
330 string m_statusBarText;
|
73
|
331 QUrl m_initialUrl;
|
|
332 int m_progress;
|
|
333 WebPage m_page;
|
45
|
334 }
|