[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 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 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[556] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[556] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[556] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 | #include <QtWebKit>
|
---|
| 43 | #include "mainwindow.h"
|
---|
| 44 |
|
---|
| 45 | //! [1]
|
---|
| 46 |
|
---|
[846] | 47 | MainWindow::MainWindow(const QUrl& url)
|
---|
[556] | 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);
|
---|
[846] | 62 | view->load(url);
|
---|
[556] | 63 | connect(view, SIGNAL(loadFinished(bool)), SLOT(adjustLocation()));
|
---|
| 64 | connect(view, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
|
---|
| 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 |
|
---|
[846] | 80 | QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
|
---|
| 81 | QAction* viewSourceAction = new QAction("Page Source", this);
|
---|
| 82 | connect(viewSourceAction, SIGNAL(triggered()), SLOT(viewSource()));
|
---|
| 83 | viewMenu->addAction(viewSourceAction);
|
---|
| 84 |
|
---|
[556] | 85 | //! [3]
|
---|
| 86 | QMenu *effectMenu = menuBar()->addMenu(tr("&Effect"));
|
---|
| 87 | effectMenu->addAction("Highlight all links", this, SLOT(highlightAllLinks()));
|
---|
| 88 |
|
---|
| 89 | rotateAction = new QAction(this);
|
---|
| 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 |
|
---|
[846] | 107 | void MainWindow::viewSource()
|
---|
| 108 | {
|
---|
| 109 | QNetworkAccessManager* accessManager = view->page()->networkAccessManager();
|
---|
| 110 | QNetworkRequest request(view->url());
|
---|
| 111 | QNetworkReply* reply = accessManager->get(request);
|
---|
| 112 | connect(reply, SIGNAL(finished()), this, SLOT(slotSourceDownloaded()));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void MainWindow::slotSourceDownloaded()
|
---|
| 116 | {
|
---|
| 117 | QNetworkReply* reply = qobject_cast<QNetworkReply*>(const_cast<QObject*>(sender()));
|
---|
| 118 | QTextEdit* textEdit = new QTextEdit(NULL);
|
---|
| 119 | textEdit->setAttribute(Qt::WA_DeleteOnClose);
|
---|
| 120 | textEdit->show();
|
---|
| 121 | textEdit->setPlainText(reply->readAll());
|
---|
| 122 | reply->deleteLater();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[556] | 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);
|
---|
| 161 |
|
---|
| 162 | rotateImages(rotateAction->isChecked());
|
---|
| 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]
|
---|
| 175 | void MainWindow::rotateImages(bool invert)
|
---|
| 176 | {
|
---|
| 177 | QString code;
|
---|
| 178 | if (invert)
|
---|
| 179 | code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(180deg)') } )";
|
---|
| 180 | else
|
---|
| 181 | code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(0deg)') } )";
|
---|
| 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 |
|
---|