|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 #include <QtWebKit> |
|
44 #include "mainwindow.h" |
|
45 |
|
46 //! [1] |
|
47 |
|
48 MainWindow::MainWindow() |
|
49 { |
|
50 progress = 0; |
|
51 |
|
52 QFile file; |
|
53 file.setFileName(":/jquery.min.js"); |
|
54 file.open(QIODevice::ReadOnly); |
|
55 jQuery = file.readAll(); |
|
56 file.close(); |
|
57 //! [1] |
|
58 |
|
59 QNetworkProxyFactory::setUseSystemConfiguration(true); |
|
60 |
|
61 //! [2] |
|
62 view = new QWebView(this); |
|
63 view->load(QUrl("http://www.google.com/ncr")); |
|
64 connect(view, SIGNAL(loadFinished(bool)), SLOT(adjustLocation())); |
|
65 connect(view, SIGNAL(titleChanged(const QString&)), SLOT(adjustTitle())); |
|
66 connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int))); |
|
67 connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool))); |
|
68 |
|
69 locationEdit = new QLineEdit(this); |
|
70 locationEdit->setSizePolicy(QSizePolicy::Expanding, locationEdit->sizePolicy().verticalPolicy()); |
|
71 connect(locationEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); |
|
72 |
|
73 QToolBar *toolBar = addToolBar(tr("Navigation")); |
|
74 toolBar->addAction(view->pageAction(QWebPage::Back)); |
|
75 toolBar->addAction(view->pageAction(QWebPage::Forward)); |
|
76 toolBar->addAction(view->pageAction(QWebPage::Reload)); |
|
77 toolBar->addAction(view->pageAction(QWebPage::Stop)); |
|
78 toolBar->addWidget(locationEdit); |
|
79 //! [2] |
|
80 |
|
81 //! [3] |
|
82 QMenu *effectMenu = menuBar()->addMenu(tr("&Effect")); |
|
83 effectMenu->addAction("Highlight all links", this, SLOT(highlightAllLinks())); |
|
84 |
|
85 QAction *rotateAction = new QAction(this); |
|
86 rotateAction->setIcon(style()->standardIcon(QStyle::SP_FileDialogDetailedView)); |
|
87 rotateAction->setCheckable(true); |
|
88 rotateAction->setText(tr("Turn images upside down")); |
|
89 connect(rotateAction, SIGNAL(toggled(bool)), this, SLOT(rotateImages(bool))); |
|
90 effectMenu->addAction(rotateAction); |
|
91 |
|
92 QMenu *toolsMenu = menuBar()->addMenu(tr("&Tools")); |
|
93 toolsMenu->addAction(tr("Remove GIF images"), this, SLOT(removeGifImages())); |
|
94 toolsMenu->addAction(tr("Remove all inline frames"), this, SLOT(removeInlineFrames())); |
|
95 toolsMenu->addAction(tr("Remove all object elements"), this, SLOT(removeObjectElements())); |
|
96 toolsMenu->addAction(tr("Remove all embedded elements"), this, SLOT(removeEmbeddedElements())); |
|
97 |
|
98 setCentralWidget(view); |
|
99 setUnifiedTitleAndToolBarOnMac(true); |
|
100 } |
|
101 //! [3] |
|
102 |
|
103 //! [4] |
|
104 void MainWindow::adjustLocation() |
|
105 { |
|
106 locationEdit->setText(view->url().toString()); |
|
107 } |
|
108 |
|
109 void MainWindow::changeLocation() |
|
110 { |
|
111 QUrl url = QUrl(locationEdit->text()); |
|
112 view->load(url); |
|
113 view->setFocus(); |
|
114 } |
|
115 //! [4] |
|
116 |
|
117 //! [5] |
|
118 void MainWindow::adjustTitle() |
|
119 { |
|
120 if (progress <= 0 || progress >= 100) |
|
121 setWindowTitle(view->title()); |
|
122 else |
|
123 setWindowTitle(QString("%1 (%2%)").arg(view->title()).arg(progress)); |
|
124 } |
|
125 |
|
126 void MainWindow::setProgress(int p) |
|
127 { |
|
128 progress = p; |
|
129 adjustTitle(); |
|
130 } |
|
131 //! [5] |
|
132 |
|
133 //! [6] |
|
134 void MainWindow::finishLoading(bool) |
|
135 { |
|
136 progress = 100; |
|
137 adjustTitle(); |
|
138 view->page()->mainFrame()->evaluateJavaScript(jQuery); |
|
139 } |
|
140 //! [6] |
|
141 |
|
142 //! [7] |
|
143 void MainWindow::highlightAllLinks() |
|
144 { |
|
145 QString code = "$('a').each( function () { $(this).css('background-color', 'yellow') } )"; |
|
146 view->page()->mainFrame()->evaluateJavaScript(code); |
|
147 } |
|
148 //! [7] |
|
149 |
|
150 //! [8] |
|
151 void MainWindow::rotateImages(bool toggle) |
|
152 { |
|
153 QString code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s') } )"; |
|
154 view->page()->mainFrame()->evaluateJavaScript(code); |
|
155 if (toggle) |
|
156 code = "$('img').each( function () { $(this).css('-webkit-transform', 'rotate(180deg)') } )"; |
|
157 else |
|
158 code = "$('img').each( function () { $(this).css('-webkit-transform', 'rotate(0deg)') } )"; |
|
159 view->page()->mainFrame()->evaluateJavaScript(code); |
|
160 } |
|
161 //! [8] |
|
162 |
|
163 //! [9] |
|
164 void MainWindow::removeGifImages() |
|
165 { |
|
166 QString code = "$('[src*=gif]').remove()"; |
|
167 view->page()->mainFrame()->evaluateJavaScript(code); |
|
168 } |
|
169 |
|
170 void MainWindow::removeInlineFrames() |
|
171 { |
|
172 QString code = "$('iframe').remove()"; |
|
173 view->page()->mainFrame()->evaluateJavaScript(code); |
|
174 } |
|
175 |
|
176 void MainWindow::removeObjectElements() |
|
177 { |
|
178 QString code = "$('object').remove()"; |
|
179 view->page()->mainFrame()->evaluateJavaScript(code); |
|
180 } |
|
181 |
|
182 void MainWindow::removeEmbeddedElements() |
|
183 { |
|
184 QString code = "$('embed').remove()"; |
|
185 view->page()->mainFrame()->evaluateJavaScript(code); |
|
186 } |
|
187 //! [9] |
|
188 |