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 {
|
77
|
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;
|
|
198 };
|
|
199
|
|
200 class WebView : public QWebView
|
45
|
201 {
|
73
|
202 public:
|
|
203
|
|
204 WebView(QWidget parent = null)
|
|
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
|
|
239 int progress() const { return m_progress; }
|
|
240
|
|
241 protected:
|
45
|
242
|
74
|
243 void mousePressEvent(QMouseEvent event)
|
73
|
244 {
|
|
245 m_page.m_pressedButtons = event.buttons();
|
|
246 m_page.m_keyboardModifiers = event.modifiers();
|
|
247 QWebView.mousePressEvent(event);
|
|
248 }
|
|
249
|
|
250 void mouseReleaseEvent(QMouseEvent event)
|
|
251 {
|
|
252 QWebView.mouseReleaseEvent(event);
|
|
253 if (!event.isAccepted() && (m_page.m_pressedButtons & Qt.MidButton)) {
|
|
254 QUrl url(QApplication.clipboard().text(QClipboard.Selection));
|
|
255 if (!url.isEmpty() && url.isValid() && !url.scheme().isEmpty()) {
|
|
256 setUrl(url);
|
|
257 }
|
|
258 }
|
|
259 }
|
|
260
|
|
261 void contextMenuEvent(QContextMenuEvent event)
|
|
262 {
|
|
263 QWebHitTestResult r = page().mainFrame().hitTestContent(event.pos());
|
|
264 if (!r.linkUrl().isEmpty()) {
|
|
265 QMenu menu(this);
|
|
266 menu.addAction(pageAction(QWebPage.OpenLinkInNewWindow));
|
|
267 menu.addAction(tr("Open in New Tab"), this, SLOT(openLinkInNewTab()));
|
|
268 menu.addSeparator();
|
|
269 menu.addAction(pageAction(QWebPage.DownloadLinkToDisk));
|
|
270 // Add link to bookmarks...
|
|
271 menu.addSeparator();
|
|
272 menu.addAction(pageAction(QWebPage.CopyLinkToClipboard));
|
|
273 if (page().settings().testAttribute(QWebSettings.DeveloperExtrasEnabled))
|
|
274 menu.addAction(pageAction(QWebPage.InspectElement));
|
|
275 menu.exec(mapToGlobal(event.pos()));
|
|
276 return;
|
|
277 }
|
|
278 QWebView.contextMenuEvent(event);
|
|
279 }
|
|
280
|
|
281 void wheelEvent(QWheelEvent event)
|
|
282 {
|
|
283 if (QApplication.keyboardModifiers() & Qt.ControlModifier) {
|
|
284 int numDegrees = event.delta() / 8;
|
|
285 int numSteps = numDegrees / 15;
|
|
286 setTextSizeMultiplier(textSizeMultiplier() + numSteps * 0.1);
|
|
287 event.accept();
|
|
288 return;
|
|
289 }
|
|
290 QWebView.wheelEvent(event);
|
|
291 }
|
45
|
292
|
|
293
|
73
|
294 private:
|
|
295
|
|
296 void setProgress(int progress)
|
|
297 {
|
|
298 m_progress = progress;
|
|
299 }
|
45
|
300
|
73
|
301 void loadFinished()
|
|
302 {
|
|
303 if (100 != m_progress) {
|
|
304 qWarning() << "Received finished signal while progress is still:" << progress() << "Url:" << url();
|
|
305 }
|
|
306 m_progress = 0;
|
|
307 }
|
45
|
308
|
77
|
309 void setStatusBarText(string str)
|
73
|
310 {
|
77
|
311 m_statusBarText = str;
|
73
|
312 }
|
45
|
313
|
73
|
314 void downloadRequested(QNetworkRequest request)
|
|
315 {
|
|
316 BrowserApplication.downloadManager().download(request);
|
|
317 }
|
45
|
318
|
73
|
319 void openLinkInNewTab()
|
|
320 {
|
|
321 m_page.m_openInNewTab = true;
|
|
322 pageAction(QWebPage.OpenLinkInNewWindow).trigger();
|
|
323 }
|
|
324
|
45
|
325 private:
|
73
|
326
|
77
|
327 string m_statusBarText;
|
73
|
328 QUrl m_initialUrl;
|
|
329 int m_progress;
|
|
330 WebPage m_page;
|
45
|
331 }
|