| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2010 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 demos 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 "BrowserView.h" |
| 43 |
|
| 44 |
#include <QtGui> |
| 45 |
#include <QtNetwork> |
| 46 |
#include <QtWebKit> |
| 47 |
|
| 48 |
#include "ControlStrip.h" |
| 49 |
#include "TitleBar.h" |
| 50 |
#include "flickcharm.h" |
| 51 |
#include "webview.h" |
| 52 |
#include "ZoomStrip.h" |
| 53 |
|
| 54 |
BrowserView::BrowserView(QWidget *parent) |
| 55 |
: QWidget(parent) |
| 56 |
, m_titleBar(0) |
| 57 |
, m_webView(0) |
| 58 |
, m_progress(0) |
| 59 |
, m_currentZoom(100) |
| 60 |
{ |
| 61 |
m_titleBar = new TitleBar(this); |
| 62 |
m_webView = new WebView(this); |
| 63 |
m_zoomStrip = new ZoomStrip(this); |
| 64 |
m_controlStrip = new ControlStrip(this); |
| 65 |
|
| 66 |
m_zoomLevels << 30 << 50 << 67 << 80 << 90; |
| 67 |
m_zoomLevels << 100; |
| 68 |
m_zoomLevels << 110 << 120 << 133 << 150 << 170 << 200 << 240 << 300; |
| 69 |
|
| 70 |
QNetworkConfigurationManager manager; |
| 71 |
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { |
| 72 |
// Get saved network configuration |
| 73 |
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); |
| 74 |
settings.beginGroup(QLatin1String("QtNetwork")); |
| 75 |
const QString id = |
| 76 |
settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); |
| 77 |
settings.endGroup(); |
| 78 |
|
| 79 |
// If the saved network configuration is not currently discovered use the system |
| 80 |
// default |
| 81 |
QNetworkConfiguration config = manager.configurationFromIdentifier(id); |
| 82 |
if ((config.state() & QNetworkConfiguration::Discovered) != |
| 83 |
QNetworkConfiguration::Discovered) { |
| 84 |
config = manager.defaultConfiguration(); |
| 85 |
} |
| 86 |
|
| 87 |
m_webView->page()->networkAccessManager()->setConfiguration(config); |
| 88 |
} |
| 89 |
|
| 90 |
QTimer::singleShot(0, this, SLOT(initialize())); |
| 91 |
} |
| 92 |
|
| 93 |
void BrowserView::initialize() |
| 94 |
{ |
| 95 |
connect(m_zoomStrip, SIGNAL(zoomInClicked()), SLOT(zoomIn())); |
| 96 |
connect(m_zoomStrip, SIGNAL(zoomOutClicked()), SLOT(zoomOut())); |
| 97 |
|
| 98 |
connect(m_controlStrip, SIGNAL(menuClicked()), SIGNAL(menuButtonClicked())); |
| 99 |
connect(m_controlStrip, SIGNAL(backClicked()), m_webView, SLOT(back())); |
| 100 |
connect(m_controlStrip, SIGNAL(forwardClicked()), m_webView, SLOT(forward())); |
| 101 |
connect(m_controlStrip, SIGNAL(closeClicked()), qApp, SLOT(quit())); |
| 102 |
|
| 103 |
QPalette pal = m_webView->palette(); |
| 104 |
pal.setBrush(QPalette::Base, Qt::white); |
| 105 |
m_webView->setPalette(pal); |
| 106 |
|
| 107 |
FlickCharm *flickCharm = new FlickCharm(this); |
| 108 |
flickCharm->activateOn(m_webView); |
| 109 |
|
| 110 |
m_webView->setZoomFactor(static_cast<qreal>(m_currentZoom)/100.0); |
| 111 |
connect(m_webView, SIGNAL(loadStarted()), SLOT(start())); |
| 112 |
connect(m_webView, SIGNAL(loadProgress(int)), SLOT(setProgress(int))); |
| 113 |
connect(m_webView, SIGNAL(loadFinished(bool)), SLOT(finish(bool))); |
| 114 |
connect(m_webView, SIGNAL(urlChanged(QUrl)), SLOT(updateTitleBar())); |
| 115 |
|
| 116 |
m_webView->setHtml("about:blank"); |
| 117 |
m_webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 118 |
m_webView->setFocus(); |
| 119 |
} |
| 120 |
|
| 121 |
void BrowserView::start() |
| 122 |
{ |
| 123 |
m_progress = 0; |
| 124 |
updateTitleBar(); |
| 125 |
//m_titleBar->setText(m_webView->url().toString()); |
| 126 |
} |
| 127 |
|
| 128 |
void BrowserView::setProgress(int percent) |
| 129 |
{ |
| 130 |
m_progress = percent; |
| 131 |
updateTitleBar(); |
| 132 |
//m_titleBar->setText(QString("Loading %1%").arg(percent)); |
| 133 |
} |
| 134 |
|
| 135 |
void BrowserView::updateTitleBar() |
| 136 |
{ |
| 137 |
QUrl url = m_webView->url(); |
| 138 |
m_titleBar->setHost(url.host()); |
| 139 |
m_titleBar->setTitle(m_webView->title()); |
| 140 |
m_titleBar->setProgress(m_progress); |
| 141 |
} |
| 142 |
|
| 143 |
void BrowserView::finish(bool ok) |
| 144 |
{ |
| 145 |
m_progress = 0; |
| 146 |
updateTitleBar(); |
| 147 |
|
| 148 |
// TODO: handle error |
| 149 |
if (!ok) { |
| 150 |
//m_titleBar->setText("Loading failed."); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void BrowserView::zoomIn() |
| 155 |
{ |
| 156 |
int i = m_zoomLevels.indexOf(m_currentZoom); |
| 157 |
Q_ASSERT(i >= 0); |
| 158 |
if (i < m_zoomLevels.count() - 1) |
| 159 |
m_currentZoom = m_zoomLevels[i + 1]; |
| 160 |
|
| 161 |
m_webView->setZoomFactor(static_cast<qreal>(m_currentZoom)/100.0); |
| 162 |
} |
| 163 |
|
| 164 |
void BrowserView::zoomOut() |
| 165 |
{ |
| 166 |
int i = m_zoomLevels.indexOf(m_currentZoom); |
| 167 |
Q_ASSERT(i >= 0); |
| 168 |
if (i > 0) |
| 169 |
m_currentZoom = m_zoomLevels[i - 1]; |
| 170 |
|
| 171 |
m_webView->setZoomFactor(static_cast<qreal>(m_currentZoom)/100.0); |
| 172 |
} |
| 173 |
|
| 174 |
void BrowserView::resizeEvent(QResizeEvent *event) |
| 175 |
{ |
| 176 |
QWidget::resizeEvent(event); |
| 177 |
|
| 178 |
int h1 = m_titleBar->sizeHint().height(); |
| 179 |
int h2 = m_controlStrip->sizeHint().height(); |
| 180 |
|
| 181 |
m_titleBar->setGeometry(0, 0, width(), h1); |
| 182 |
m_controlStrip->setGeometry(0, height() - h2, width(), h2); |
| 183 |
m_webView->setGeometry(0, h1, width(), height() - h1); |
| 184 |
|
| 185 |
int zw = m_zoomStrip->sizeHint().width(); |
| 186 |
int zh = m_zoomStrip->sizeHint().height(); |
| 187 |
m_zoomStrip->move(width() - zw, (height() - zh) / 2); |
| 188 |
} |
| 189 |
|
| 190 |
void BrowserView::navigate(const QUrl &url) |
| 191 |
{ |
| 192 |
m_webView->load(url); |
| 193 |
} |