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;
|
94
|
47 //import qt.core.QDebug;
|
73
|
48 import qt.core.QBuffer;
|
94
|
49 import qt.webkit.QWebView;
|
|
50 import qt.webkit.QWebHitTestResult;
|
|
51 version(QT_NO_UITOOLS) {} else
|
|
52 {
|
|
53 import qt.uitools.QUiLoader;
|
|
54 }
|
100
|
55
|
|
56 import tango.text.convert.Format;
|
|
57
|
45
|
58 import browserapplication;
|
|
59 import browsermainwindow;
|
|
60 import cookiejar;
|
|
61 import downloadmanager;
|
|
62 import networkaccessmanager;
|
|
63 import tabwidget;
|
|
64
|
|
65
|
73
|
66 class WebPage : public QWebPage
|
45
|
67 {
|
74
|
68 mixin Signal!("loadingUrl", QUrl /*url*/);
|
45
|
69
|
|
70 public:
|
|
71
|
73
|
72 this(QObject parent = null)
|
|
73 {
|
|
74 super(parent);
|
94
|
75 m_keyboardModifiers = Qt_KeyboardModifier.NoModifier;
|
|
76 m_pressedButtons = Qt_MouseButton.NoButton;
|
73
|
77 m_openInNewTab = false;
|
|
78 setNetworkAccessManager(BrowserApplication.networkAccessManager());
|
|
79 this.unsupportedContent.connect(&this.handleUnsupportedContent);
|
|
80 }
|
|
81
|
|
82 BrowserMainWindow mainWindow()
|
|
83 {
|
|
84 QObject w = this.parent();
|
|
85 while (w) {
|
77
|
86 if (BrowserMainWindow mw = cast(BrowserMainWindow) w)
|
73
|
87 return mw;
|
|
88 w = w.parent();
|
|
89 }
|
|
90 return BrowserApplication.instance().mainWindow();
|
|
91 }
|
45
|
92
|
|
93 protected:
|
73
|
94
|
74
|
95 bool acceptNavigationRequest(QWebFrame frame, QNetworkRequest request, NavigationType type)
|
73
|
96 {
|
|
97 // ctrl open in new tab
|
|
98 // ctrl-shift open in new tab and select
|
|
99 // ctrl-alt open in new window
|
|
100 if (type == QWebPage.NavigationTypeLinkClicked
|
94
|
101 && (m_keyboardModifiers & Qt_KeyboardModifier.ControlModifier
|
|
102 || m_pressedButtons == Qt_MouseButton.MidButton)) {
|
|
103 bool newWindow = (m_keyboardModifiers & Qt_KeyboardModifier.AltModifier);
|
73
|
104 WebView webView;
|
|
105 if (newWindow) {
|
|
106 BrowserApplication.instance().newMainWindow();
|
|
107 BrowserMainWindow newMainWindow = BrowserApplication.instance().mainWindow();
|
|
108 webView = newMainWindow.currentTab();
|
|
109 newMainWindow.raise();
|
|
110 newMainWindow.activateWindow();
|
|
111 webView.setFocus();
|
|
112 } else {
|
94
|
113 bool selectNewTab = (m_keyboardModifiers & Qt_KeyboardModifier.ShiftModifier);
|
73
|
114 webView = mainWindow().tabWidget().newTab(selectNewTab);
|
|
115 }
|
|
116 webView.load(request);
|
94
|
117 m_keyboardModifiers = Qt_KeyboardModifier.NoModifier;
|
|
118 m_pressedButtons = Qt_MouseButton.NoButton;
|
73
|
119 return false;
|
|
120 }
|
|
121 if (frame == mainFrame()) {
|
|
122 m_loadingUrl = request.url();
|
77
|
123 loadingUrl.emit(m_loadingUrl);
|
73
|
124 }
|
|
125 return QWebPage.acceptNavigationRequest(frame, request, type);
|
|
126 }
|
|
127
|
|
128 QWebPage createWindow(QWebPage.WebWindowType type)
|
|
129 {
|
94
|
130 if (m_keyboardModifiers & Qt_KeyboardModifier.ControlModifier || m_pressedButtons == Qt_MouseButton.MidButton)
|
73
|
131 m_openInNewTab = true;
|
|
132 if (m_openInNewTab) {
|
|
133 m_openInNewTab = false;
|
|
134 return mainWindow().tabWidget().newTab().page();
|
|
135 }
|
|
136 BrowserApplication.instance().newMainWindow();
|
|
137 BrowserMainWindow mainWindow = BrowserApplication.instance().mainWindow();
|
|
138 return mainWindow.currentTab().page();
|
|
139 }
|
45
|
140
|
73
|
141 version(QT_NO_UITOOLS) {} else
|
|
142 {
|
85
|
143 QObject createPlugin(string classId, QUrl url, string[] paramNames, string[] paramValues)
|
73
|
144 {
|
77
|
145 auto loader = new QUiLoader;
|
73
|
146 return loader.createWidget(classId, view());
|
|
147 }
|
|
148 }
|
|
149
|
|
150 private:
|
|
151
|
|
152 void handleUnsupportedContent(QNetworkReply reply)
|
|
153 {
|
|
154 if (reply.error() == QNetworkReply.NoError) {
|
|
155 BrowserApplication.downloadManager().handleUnsupportedContent(reply);
|
|
156 return;
|
|
157 }
|
|
158
|
80
|
159 auto file = new QFile(":/notfound.html");
|
73
|
160 bool isOpened = file.open(QIODevice.ReadOnly);
|
74
|
161 assert(isOpened);
|
77
|
162 string title = Format(tr("Error loading page: {}"), reply.url().toString());
|
80
|
163 string html = Format(file.readAll(),
|
77
|
164 title,
|
|
165 reply.errorString(),
|
|
166 reply.url().toString());
|
45
|
167
|
73
|
168 QBuffer imageBuffer;
|
|
169 imageBuffer.open(QBuffer.ReadWrite);
|
|
170 QIcon icon = view().style().standardIcon(QStyle.SP_MessageBoxWarning, 0, view());
|
|
171 QPixmap pixmap = icon.pixmap(QSize(32,32));
|
|
172 if (pixmap.save(&imageBuffer, "PNG")) {
|
80
|
173 html.replace("IMAGE_BINARY_DATA_HERE",
|
|
174 imageBuffer.buffer().toBase64());
|
73
|
175 }
|
|
176
|
74
|
177 QWebFrame[] frames;
|
73
|
178 frames.append(mainFrame());
|
|
179 while (!frames.isEmpty()) {
|
|
180 QWebFrame frame = frames.takeFirst();
|
|
181 if (frame.url() == reply.url()) {
|
|
182 frame.setHtml(html, reply.url());
|
|
183 return;
|
|
184 }
|
74
|
185 QWebFrame[] children = frame.childFrames();
|
|
186 foreach(QWebFrame frame; children)
|
73
|
187 frames.append(frame);
|
|
188 }
|
|
189 if (m_loadingUrl == reply.url()) {
|
|
190 mainFrame().setHtml(html, reply.url());
|
|
191 }
|
|
192 }
|
|
193
|
|
194 private:
|
|
195
|
|
196 // set the webview mousepressedevent
|
94
|
197 Qt_KeyboardModifier m_keyboardModifiers;
|
|
198 Qt_MouseButton m_pressedButtons;
|
73
|
199 bool m_openInNewTab;
|
|
200 QUrl m_loadingUrl;
|
85
|
201 }
|
73
|
202
|
|
203 class WebView : public QWebView
|
45
|
204 {
|
73
|
205 public:
|
|
206
|
85
|
207 this(QWidget parent = null)
|
73
|
208 {
|
|
209 super(parent);
|
|
210 m_progress = 0;
|
|
211 m_page = new WebPage(this);
|
|
212 setPage(m_page);
|
|
213 page().statusBarMessage.connect(&setStatusBarText);
|
|
214 this.loadProgress.connect(&this.setProgress);
|
|
215 this.loadFinished.connect(&this.loadFinished);
|
|
216 page().loadingUrl.connect(&this.urlChanged);
|
|
217 page().downloadRequested.connect(&this.downloadRequested);
|
|
218 page().setForwardUnsupportedContent(true);
|
|
219 }
|
|
220
|
|
221 WebPage webPage() { return m_page; }
|
|
222
|
|
223 void loadUrl(QUrl url)
|
|
224 {
|
|
225 m_initialUrl = url;
|
|
226 load(url);
|
|
227 }
|
|
228
|
94
|
229 QUrl getUrl()
|
73
|
230 {
|
|
231 QUrl url = QWebView.url();
|
|
232 if (!url.isEmpty())
|
|
233 return url;
|
|
234 return m_initialUrl;
|
|
235 }
|
|
236
|
77
|
237 string lastStatusBarText()
|
73
|
238 {
|
|
239 return m_statusBarText;
|
|
240 }
|
|
241
|
85
|
242 int progress()
|
|
243 {
|
|
244 return m_progress;
|
|
245 }
|
73
|
246
|
|
247 protected:
|
45
|
248
|
74
|
249 void mousePressEvent(QMouseEvent event)
|
73
|
250 {
|
|
251 m_page.m_pressedButtons = event.buttons();
|
|
252 m_page.m_keyboardModifiers = event.modifiers();
|
|
253 QWebView.mousePressEvent(event);
|
|
254 }
|
|
255
|
|
256 void mouseReleaseEvent(QMouseEvent event)
|
|
257 {
|
|
258 QWebView.mouseReleaseEvent(event);
|
94
|
259 if (!event.isAccepted() && (m_page.m_pressedButtons & Qt_MouseButton.MidButton)) {
|
85
|
260 auto url = new QUrl(QApplication.clipboard().text(QClipboard.Selection));
|
73
|
261 if (!url.isEmpty() && url.isValid() && !url.scheme().isEmpty()) {
|
|
262 setUrl(url);
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
|
267 void contextMenuEvent(QContextMenuEvent event)
|
|
268 {
|
|
269 QWebHitTestResult r = page().mainFrame().hitTestContent(event.pos());
|
|
270 if (!r.linkUrl().isEmpty()) {
|
85
|
271 auto menu = new QMenu(this);
|
73
|
272 menu.addAction(pageAction(QWebPage.OpenLinkInNewWindow));
|
|
273 menu.addAction(tr("Open in New Tab"), this, SLOT(openLinkInNewTab()));
|
|
274 menu.addSeparator();
|
|
275 menu.addAction(pageAction(QWebPage.DownloadLinkToDisk));
|
|
276 // Add link to bookmarks...
|
|
277 menu.addSeparator();
|
|
278 menu.addAction(pageAction(QWebPage.CopyLinkToClipboard));
|
|
279 if (page().settings().testAttribute(QWebSettings.DeveloperExtrasEnabled))
|
|
280 menu.addAction(pageAction(QWebPage.InspectElement));
|
|
281 menu.exec(mapToGlobal(event.pos()));
|
|
282 return;
|
|
283 }
|
|
284 QWebView.contextMenuEvent(event);
|
|
285 }
|
|
286
|
|
287 void wheelEvent(QWheelEvent event)
|
|
288 {
|
|
289 if (QApplication.keyboardModifiers() & Qt.ControlModifier) {
|
|
290 int numDegrees = event.delta() / 8;
|
|
291 int numSteps = numDegrees / 15;
|
|
292 setTextSizeMultiplier(textSizeMultiplier() + numSteps * 0.1);
|
|
293 event.accept();
|
|
294 return;
|
|
295 }
|
|
296 QWebView.wheelEvent(event);
|
|
297 }
|
45
|
298
|
|
299
|
73
|
300 private:
|
|
301
|
|
302 void setProgress(int progress)
|
|
303 {
|
|
304 m_progress = progress;
|
|
305 }
|
45
|
306
|
73
|
307 void loadFinished()
|
|
308 {
|
|
309 if (100 != m_progress) {
|
94
|
310 qWarning("Received finished signal while progress is still:" ~ progress() ~ "Url:" ~ url());
|
73
|
311 }
|
|
312 m_progress = 0;
|
|
313 }
|
45
|
314
|
77
|
315 void setStatusBarText(string str)
|
73
|
316 {
|
77
|
317 m_statusBarText = str;
|
73
|
318 }
|
45
|
319
|
73
|
320 void downloadRequested(QNetworkRequest request)
|
|
321 {
|
|
322 BrowserApplication.downloadManager().download(request);
|
|
323 }
|
45
|
324
|
73
|
325 void openLinkInNewTab()
|
|
326 {
|
|
327 m_page.m_openInNewTab = true;
|
|
328 pageAction(QWebPage.OpenLinkInNewWindow).trigger();
|
|
329 }
|
|
330
|
45
|
331 private:
|
73
|
332
|
77
|
333 string m_statusBarText;
|
73
|
334 QUrl m_initialUrl;
|
|
335 int m_progress;
|
|
336 WebPage m_page;
|
45
|
337 }
|