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) {
|
|
83 if (BrowserMainWindow mw = qobject_cast<BrowserMainWindow>(w))
|
|
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();
|
|
120 emit loadingUrl(m_loadingUrl);
|
|
121 }
|
|
122 return QWebPage.acceptNavigationRequest(frame, request, type);
|
|
123 }
|
|
124
|
|
125 QWebPage createWindow(QWebPage.WebWindowType type)
|
|
126 {
|
|
127 //Q_UNUSED(type);
|
|
128 if (m_keyboardModifiers & Qt.ControlModifier || m_pressedButtons == Qt.MidButton)
|
|
129 m_openInNewTab = true;
|
|
130 if (m_openInNewTab) {
|
|
131 m_openInNewTab = false;
|
|
132 return mainWindow().tabWidget().newTab().page();
|
|
133 }
|
|
134 BrowserApplication.instance().newMainWindow();
|
|
135 BrowserMainWindow mainWindow = BrowserApplication.instance().mainWindow();
|
|
136 return mainWindow.currentTab().page();
|
|
137 }
|
45
|
138
|
73
|
139 version(QT_NO_UITOOLS) {} else
|
|
140 {
|
74
|
141 QObject createPlugin(QString classId, QUrl url, QStringList paramNames, QStringList paramValues);
|
73
|
142 {
|
|
143 //Q_UNUSED(url);
|
|
144 //Q_UNUSED(paramNames);
|
|
145 //Q_UNUSED(paramValues);
|
|
146 QUiLoader loader;
|
|
147 return loader.createWidget(classId, view());
|
|
148 }
|
|
149 }
|
|
150
|
|
151 private:
|
|
152
|
|
153 void handleUnsupportedContent(QNetworkReply reply)
|
|
154 {
|
|
155 if (reply.error() == QNetworkReply.NoError) {
|
|
156 BrowserApplication.downloadManager().handleUnsupportedContent(reply);
|
|
157 return;
|
|
158 }
|
|
159
|
|
160 QFile file(QLatin1String(":/notfound.html"));
|
|
161 bool isOpened = file.open(QIODevice.ReadOnly);
|
74
|
162 assert(isOpened);
|
73
|
163 QString title = tr("Error loading page: %1").arg(reply.url().toString());
|
|
164 QString html = QString(QLatin1String(file.readAll()))
|
|
165 .arg(title)
|
|
166 .arg(reply.errorString())
|
|
167 .arg(reply.url().toString());
|
45
|
168
|
73
|
169 QBuffer imageBuffer;
|
|
170 imageBuffer.open(QBuffer.ReadWrite);
|
|
171 QIcon icon = view().style().standardIcon(QStyle.SP_MessageBoxWarning, 0, view());
|
|
172 QPixmap pixmap = icon.pixmap(QSize(32,32));
|
|
173 if (pixmap.save(&imageBuffer, "PNG")) {
|
|
174 html.replace(QLatin1String("IMAGE_BINARY_DATA_HERE"),
|
|
175 QString(QLatin1String(imageBuffer.buffer().toBase64())));
|
|
176 }
|
|
177
|
74
|
178 QWebFrame[] frames;
|
73
|
179 frames.append(mainFrame());
|
|
180 while (!frames.isEmpty()) {
|
|
181 QWebFrame frame = frames.takeFirst();
|
|
182 if (frame.url() == reply.url()) {
|
|
183 frame.setHtml(html, reply.url());
|
|
184 return;
|
|
185 }
|
74
|
186 QWebFrame[] children = frame.childFrames();
|
|
187 foreach(QWebFrame frame; children)
|
73
|
188 frames.append(frame);
|
|
189 }
|
|
190 if (m_loadingUrl == reply.url()) {
|
|
191 mainFrame().setHtml(html, reply.url());
|
|
192 }
|
|
193 }
|
|
194
|
|
195 private:
|
|
196
|
|
197 // set the webview mousepressedevent
|
|
198 Qt.KeyboardModifiers m_keyboardModifiers;
|
|
199 Qt.MouseButtons m_pressedButtons;
|
|
200 bool m_openInNewTab;
|
|
201 QUrl m_loadingUrl;
|
|
202 };
|
|
203
|
|
204 class WebView : public QWebView
|
45
|
205 {
|
73
|
206 public:
|
|
207
|
|
208 WebView(QWidget parent = null)
|
|
209 {
|
|
210 super(parent);
|
|
211 m_progress = 0;
|
|
212 m_page = new WebPage(this);
|
|
213 setPage(m_page);
|
|
214 page().statusBarMessage.connect(&setStatusBarText);
|
|
215 this.loadProgress.connect(&this.setProgress);
|
|
216 this.loadFinished.connect(&this.loadFinished);
|
|
217 page().loadingUrl.connect(&this.urlChanged);
|
|
218 page().downloadRequested.connect(&this.downloadRequested);
|
|
219 page().setForwardUnsupportedContent(true);
|
|
220 }
|
|
221
|
|
222 WebPage webPage() { return m_page; }
|
|
223
|
|
224 void loadUrl(QUrl url)
|
|
225 {
|
|
226 m_initialUrl = url;
|
|
227 load(url);
|
|
228 }
|
|
229
|
74
|
230 QUrl url()
|
73
|
231 {
|
|
232 QUrl url = QWebView.url();
|
|
233 if (!url.isEmpty())
|
|
234 return url;
|
|
235 return m_initialUrl;
|
|
236 }
|
|
237
|
|
238 QString lastStatusBarText()
|
|
239 {
|
|
240 return m_statusBarText;
|
|
241 }
|
|
242
|
|
243 int progress() const { return m_progress; }
|
|
244
|
|
245 protected:
|
45
|
246
|
74
|
247 void mousePressEvent(QMouseEvent event)
|
73
|
248 {
|
|
249 m_page.m_pressedButtons = event.buttons();
|
|
250 m_page.m_keyboardModifiers = event.modifiers();
|
|
251 QWebView.mousePressEvent(event);
|
|
252 }
|
|
253
|
|
254 void mouseReleaseEvent(QMouseEvent event)
|
|
255 {
|
|
256 QWebView.mouseReleaseEvent(event);
|
|
257 if (!event.isAccepted() && (m_page.m_pressedButtons & Qt.MidButton)) {
|
|
258 QUrl url(QApplication.clipboard().text(QClipboard.Selection));
|
|
259 if (!url.isEmpty() && url.isValid() && !url.scheme().isEmpty()) {
|
|
260 setUrl(url);
|
|
261 }
|
|
262 }
|
|
263 }
|
|
264
|
|
265 void contextMenuEvent(QContextMenuEvent event)
|
|
266 {
|
|
267 QWebHitTestResult r = page().mainFrame().hitTestContent(event.pos());
|
|
268 if (!r.linkUrl().isEmpty()) {
|
|
269 QMenu menu(this);
|
|
270 menu.addAction(pageAction(QWebPage.OpenLinkInNewWindow));
|
|
271 menu.addAction(tr("Open in New Tab"), this, SLOT(openLinkInNewTab()));
|
|
272 menu.addSeparator();
|
|
273 menu.addAction(pageAction(QWebPage.DownloadLinkToDisk));
|
|
274 // Add link to bookmarks...
|
|
275 menu.addSeparator();
|
|
276 menu.addAction(pageAction(QWebPage.CopyLinkToClipboard));
|
|
277 if (page().settings().testAttribute(QWebSettings.DeveloperExtrasEnabled))
|
|
278 menu.addAction(pageAction(QWebPage.InspectElement));
|
|
279 menu.exec(mapToGlobal(event.pos()));
|
|
280 return;
|
|
281 }
|
|
282 QWebView.contextMenuEvent(event);
|
|
283 }
|
|
284
|
|
285 void wheelEvent(QWheelEvent event)
|
|
286 {
|
|
287 if (QApplication.keyboardModifiers() & Qt.ControlModifier) {
|
|
288 int numDegrees = event.delta() / 8;
|
|
289 int numSteps = numDegrees / 15;
|
|
290 setTextSizeMultiplier(textSizeMultiplier() + numSteps * 0.1);
|
|
291 event.accept();
|
|
292 return;
|
|
293 }
|
|
294 QWebView.wheelEvent(event);
|
|
295 }
|
45
|
296
|
|
297
|
73
|
298 private:
|
|
299
|
|
300 void setProgress(int progress)
|
|
301 {
|
|
302 m_progress = progress;
|
|
303 }
|
45
|
304
|
73
|
305 void loadFinished()
|
|
306 {
|
|
307 if (100 != m_progress) {
|
|
308 qWarning() << "Received finished signal while progress is still:" << progress() << "Url:" << url();
|
|
309 }
|
|
310 m_progress = 0;
|
|
311 }
|
45
|
312
|
73
|
313 void setStatusBarText(QString string)
|
|
314 {
|
|
315 m_statusBarText = string;
|
|
316 }
|
45
|
317
|
73
|
318 void downloadRequested(QNetworkRequest request)
|
|
319 {
|
|
320 BrowserApplication.downloadManager().download(request);
|
|
321 }
|
45
|
322
|
73
|
323 void openLinkInNewTab()
|
|
324 {
|
|
325 m_page.m_openInNewTab = true;
|
|
326 pageAction(QWebPage.OpenLinkInNewWindow).trigger();
|
|
327 }
|
|
328
|
45
|
329 private:
|
73
|
330
|
|
331 QString m_statusBarText;
|
|
332 QUrl m_initialUrl;
|
|
333 int m_progress;
|
|
334 WebPage m_page;
|
45
|
335 }
|