| 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 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 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(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 | 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 | rotateImages(rotateAction->isChecked());
|
|---|
| 141 | }
|
|---|
| 142 | //! [6]
|
|---|
| 143 |
|
|---|
| 144 | //! [7]
|
|---|
| 145 | void MainWindow::highlightAllLinks()
|
|---|
| 146 | {
|
|---|
| 147 | QString code = "$('a').each( function () { $(this).css('background-color', 'yellow') } )";
|
|---|
| 148 | view->page()->mainFrame()->evaluateJavaScript(code);
|
|---|
| 149 | }
|
|---|
| 150 | //! [7]
|
|---|
| 151 |
|
|---|
| 152 | //! [8]
|
|---|
| 153 | void MainWindow::rotateImages(bool invert)
|
|---|
| 154 | {
|
|---|
| 155 | QString code;
|
|---|
| 156 | if (invert)
|
|---|
| 157 | code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(180deg)') } )";
|
|---|
| 158 | else
|
|---|
| 159 | code = "$('img').each( function () { $(this).css('-webkit-transition', '-webkit-transform 2s'); $(this).css('-webkit-transform', 'rotate(0deg)') } )";
|
|---|
| 160 | view->page()->mainFrame()->evaluateJavaScript(code);
|
|---|
| 161 | }
|
|---|
| 162 | //! [8]
|
|---|
| 163 |
|
|---|
| 164 | //! [9]
|
|---|
| 165 | void MainWindow::removeGifImages()
|
|---|
| 166 | {
|
|---|
| 167 | QString code = "$('[src*=gif]').remove()";
|
|---|
| 168 | view->page()->mainFrame()->evaluateJavaScript(code);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | void MainWindow::removeInlineFrames()
|
|---|
| 172 | {
|
|---|
| 173 | QString code = "$('iframe').remove()";
|
|---|
| 174 | view->page()->mainFrame()->evaluateJavaScript(code);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | void MainWindow::removeObjectElements()
|
|---|
| 178 | {
|
|---|
| 179 | QString code = "$('object').remove()";
|
|---|
| 180 | view->page()->mainFrame()->evaluateJavaScript(code);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | void MainWindow::removeEmbeddedElements()
|
|---|
| 184 | {
|
|---|
| 185 | QString code = "$('embed').remove()";
|
|---|
| 186 | view->page()->mainFrame()->evaluateJavaScript(code);
|
|---|
| 187 | }
|
|---|
| 188 | //! [9]
|
|---|
| 189 |
|
|---|