author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
#include <QtWebKit> |
|
43 |
#include "mainwindow.h" |
|
44 |
||
45 |
//! [1] |
|
46 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
47 |
MainWindow::MainWindow(const QUrl& url) |
0 | 48 |
{ |
49 |
progress = 0; |
|
50 |
||
51 |
QFile file; |
|
52 |
file.setFileName(":/jquery.min.js"); |
|
53 |
file.open(QIODevice::ReadOnly); |
|
54 |
jQuery = file.readAll(); |
|
55 |
file.close(); |
|
56 |
//! [1] |
|
57 |
||
58 |
QNetworkProxyFactory::setUseSystemConfiguration(true); |
|
59 |
||
60 |
//! [2] |
|
61 |
view = new QWebView(this); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
62 |
view->load(url); |
0 | 63 |
connect(view, SIGNAL(loadFinished(bool)), SLOT(adjustLocation())); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
connect(view, SIGNAL(titleChanged(QString)), SLOT(adjustTitle())); |
0 | 65 |
connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int))); |
66 |
connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool))); |
|
67 |
||
68 |
locationEdit = new QLineEdit(this); |
|
69 |
locationEdit->setSizePolicy(QSizePolicy::Expanding, locationEdit->sizePolicy().verticalPolicy()); |
|
70 |
connect(locationEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); |
|
71 |
||
72 |
QToolBar *toolBar = addToolBar(tr("Navigation")); |
|
73 |
toolBar->addAction(view->pageAction(QWebPage::Back)); |
|
74 |
toolBar->addAction(view->pageAction(QWebPage::Forward)); |
|
75 |
toolBar->addAction(view->pageAction(QWebPage::Reload)); |
|
76 |
toolBar->addAction(view->pageAction(QWebPage::Stop)); |
|
77 |
toolBar->addWidget(locationEdit); |
|
78 |
//! [2] |
|
79 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
80 |
QMenu *viewMenu = menuBar()->addMenu(tr("&View")); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
81 |
QAction* viewSourceAction = new QAction("Page Source", this); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
82 |
connect(viewSourceAction, SIGNAL(triggered()), SLOT(viewSource())); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
83 |
viewMenu->addAction(viewSourceAction); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
84 |
|
0 | 85 |
//! [3] |
86 |
QMenu *effectMenu = menuBar()->addMenu(tr("&Effect")); |
|
87 |
effectMenu->addAction("Highlight all links", this, SLOT(highlightAllLinks())); |
|
88 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
rotateAction = new QAction(this); |
0 | 90 |
rotateAction->setIcon(style()->standardIcon(QStyle::SP_FileDialogDetailedView)); |
91 |
rotateAction->setCheckable(true); |
|
92 |
rotateAction->setText(tr("Turn images upside down")); |
|
93 |
connect(rotateAction, SIGNAL(toggled(bool)), this, SLOT(rotateImages(bool))); |
|
94 |
effectMenu->addAction(rotateAction); |
|
95 |
||
96 |
QMenu *toolsMenu = menuBar()->addMenu(tr("&Tools")); |
|
97 |
toolsMenu->addAction(tr("Remove GIF images"), this, SLOT(removeGifImages())); |
|
98 |
toolsMenu->addAction(tr("Remove all inline frames"), this, SLOT(removeInlineFrames())); |
|
99 |
toolsMenu->addAction(tr("Remove all object elements"), this, SLOT(removeObjectElements())); |
|
100 |
toolsMenu->addAction(tr("Remove all embedded elements"), this, SLOT(removeEmbeddedElements())); |
|
101 |
||
102 |
setCentralWidget(view); |
|
103 |
setUnifiedTitleAndToolBarOnMac(true); |
|
104 |
} |
|
105 |
//! [3] |
|
106 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
107 |
void MainWindow::viewSource() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
108 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
109 |
QNetworkAccessManager* accessManager = view->page()->networkAccessManager(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
110 |
QNetworkRequest request(view->url()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
111 |
QNetworkReply* reply = accessManager->get(request); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
112 |
connect(reply, SIGNAL(finished()), this, SLOT(slotSourceDownloaded())); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
113 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
114 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
115 |
void MainWindow::slotSourceDownloaded() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
116 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
117 |
QNetworkReply* reply = qobject_cast<QNetworkReply*>(const_cast<QObject*>(sender())); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
118 |
QTextEdit* textEdit = new QTextEdit(NULL); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
119 |
textEdit->setAttribute(Qt::WA_DeleteOnClose); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
120 |
textEdit->show(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
121 |
textEdit->setPlainText(reply->readAll()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
122 |
reply->deleteLater(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
123 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
124 |
|
0 | 125 |
//! [4] |
126 |
void MainWindow::adjustLocation() |
|
127 |
{ |
|
128 |
locationEdit->setText(view->url().toString()); |
|
129 |
} |
|
130 |
||
131 |
void MainWindow::changeLocation() |
|
132 |
{ |
|
133 |
QUrl url = QUrl(locationEdit->text()); |
|
134 |
view->load(url); |
|
135 |
view->setFocus(); |
|
136 |
} |
|
137 |
//! [4] |
|
138 |
||
139 |
//! [5] |
|
140 |
void MainWindow::adjustTitle() |
|
141 |
{ |
|
142 |
if (progress <= 0 || progress >= 100) |
|
143 |
setWindowTitle(view->title()); |
|
144 |
else |
|
145 |
setWindowTitle(QString("%1 (%2%)").arg(view->title()).arg(progress)); |
|
146 |
} |
|
147 |
||
148 |
void MainWindow::setProgress(int p) |
|
149 |
{ |
|
150 |
progress = p; |
|
151 |
adjustTitle(); |
|
152 |
} |
|
153 |
//! [5] |
|
154 |
||
155 |
//! [6] |
|
156 |
void MainWindow::finishLoading(bool) |
|
157 |
{ |
|
158 |
progress = 100; |
|
159 |
adjustTitle(); |
|
160 |
view->page()->mainFrame()->evaluateJavaScript(jQuery); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
rotateImages(rotateAction->isChecked()); |
0 | 163 |
} |
164 |
//! [6] |
|
165 |
||
166 |
//! [7] |
|
167 |
void MainWindow::highlightAllLinks() |
|
168 |
{ |
|
169 |
QString code = "$('a').each( function () { $(this).css('background-color', 'yellow') } )"; |
|
170 |
view->page()->mainFrame()->evaluateJavaScript(code); |
|
171 |
} |
|
172 |
//! [7] |
|
173 |
||
174 |
//! [8] |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
void MainWindow::rotateImages(bool invert) |
0 | 176 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
QString code; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
if (invert) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(180deg)') } )"; |
0 | 180 |
else |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(0deg)') } )"; |
0 | 182 |
view->page()->mainFrame()->evaluateJavaScript(code); |
183 |
} |
|
184 |
//! [8] |
|
185 |
||
186 |
//! [9] |
|
187 |
void MainWindow::removeGifImages() |
|
188 |
{ |
|
189 |
QString code = "$('[src*=gif]').remove()"; |
|
190 |
view->page()->mainFrame()->evaluateJavaScript(code); |
|
191 |
} |
|
192 |
||
193 |
void MainWindow::removeInlineFrames() |
|
194 |
{ |
|
195 |
QString code = "$('iframe').remove()"; |
|
196 |
view->page()->mainFrame()->evaluateJavaScript(code); |
|
197 |
} |
|
198 |
||
199 |
void MainWindow::removeObjectElements() |
|
200 |
{ |
|
201 |
QString code = "$('object').remove()"; |
|
202 |
view->page()->mainFrame()->evaluateJavaScript(code); |
|
203 |
} |
|
204 |
||
205 |
void MainWindow::removeEmbeddedElements() |
|
206 |
{ |
|
207 |
QString code = "$('embed').remove()"; |
|
208 |
view->page()->mainFrame()->evaluateJavaScript(code); |
|
209 |
} |
|
210 |
//! [9] |
|
211 |