demos/embedded/anomaly/src/BrowserView.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
child 18 2f34d5167611
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 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 "ZoomStrip.h"
       
    52 
       
    53 #if defined (Q_OS_SYMBIAN)
       
    54 #include "sym_iap_util.h"
       
    55 #endif
       
    56 
       
    57 BrowserView::BrowserView(QWidget *parent)
       
    58     : QWidget(parent)
       
    59     , m_titleBar(0)
       
    60     , m_webView(0)
       
    61     , m_progress(0)
       
    62     , m_currentZoom(100)
       
    63 {
       
    64     m_titleBar = new TitleBar(this);
       
    65     m_webView = new QWebView(this);
       
    66     m_zoomStrip = new ZoomStrip(this);
       
    67     m_controlStrip = new ControlStrip(this);
       
    68 
       
    69     m_zoomLevels << 30 << 50 << 67 << 80 << 90;
       
    70     m_zoomLevels << 100;
       
    71     m_zoomLevels << 110 << 120 << 133 << 150 << 170 << 200 << 240 << 300;
       
    72 
       
    73     QTimer::singleShot(0, this, SLOT(initialize()));
       
    74 }
       
    75 
       
    76 void BrowserView::initialize()
       
    77 {
       
    78     connect(m_zoomStrip, SIGNAL(zoomInClicked()), SLOT(zoomIn()));
       
    79     connect(m_zoomStrip, SIGNAL(zoomOutClicked()), SLOT(zoomOut()));
       
    80 
       
    81     connect(m_controlStrip, SIGNAL(menuClicked()), SIGNAL(menuButtonClicked()));
       
    82     connect(m_controlStrip, SIGNAL(backClicked()), m_webView, SLOT(back()));
       
    83     connect(m_controlStrip, SIGNAL(forwardClicked()), m_webView, SLOT(forward()));
       
    84 
       
    85     QPalette pal = m_webView->palette();
       
    86     pal.setBrush(QPalette::Base, Qt::white);
       
    87     m_webView->setPalette(pal);
       
    88 
       
    89     FlickCharm *flickCharm = new FlickCharm(this);
       
    90     flickCharm->activateOn(m_webView);
       
    91 
       
    92     m_webView->setZoomFactor(static_cast<qreal>(m_currentZoom)/100.0);
       
    93     connect(m_webView, SIGNAL(loadStarted()), SLOT(start()));
       
    94     connect(m_webView, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
       
    95     connect(m_webView, SIGNAL(loadFinished(bool)), SLOT(finish(bool)));
       
    96     connect(m_webView, SIGNAL(urlChanged(QUrl)), SLOT(updateTitleBar()));
       
    97 
       
    98     m_webView->setHtml("Will try to load page soon!");
       
    99     m_webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   100     m_webView->setFocus();
       
   101 #ifdef Q_OS_SYMBIAN
       
   102     QTimer::singleShot(0, this, SLOT(setDefaultIap()));
       
   103 #endif
       
   104 }
       
   105 
       
   106 void BrowserView::start()
       
   107 {
       
   108     m_progress = 0;
       
   109     updateTitleBar();
       
   110     //m_titleBar->setText(m_webView->url().toString());
       
   111 }
       
   112 
       
   113 void BrowserView::setProgress(int percent)
       
   114 {
       
   115     m_progress = percent;
       
   116     updateTitleBar();
       
   117     //m_titleBar->setText(QString("Loading %1%").arg(percent));
       
   118 }
       
   119 
       
   120 void BrowserView::updateTitleBar()
       
   121 {
       
   122     QUrl url = m_webView->url();
       
   123     m_titleBar->setHost(url.host());
       
   124     m_titleBar->setTitle(m_webView->title());
       
   125     m_titleBar->setProgress(m_progress);
       
   126 }
       
   127 
       
   128 void BrowserView::finish(bool ok)
       
   129 {
       
   130     m_progress = 0;
       
   131     updateTitleBar();
       
   132 
       
   133     // TODO: handle error
       
   134     if (!ok) {
       
   135         //m_titleBar->setText("Loading failed.");
       
   136     }
       
   137 }
       
   138 
       
   139 void BrowserView::zoomIn()
       
   140 {
       
   141     int i = m_zoomLevels.indexOf(m_currentZoom);
       
   142     Q_ASSERT(i >= 0);
       
   143     if (i < m_zoomLevels.count() - 1)
       
   144         m_currentZoom = m_zoomLevels[i + 1];
       
   145 
       
   146     m_webView->setZoomFactor(static_cast<qreal>(m_currentZoom)/100.0);
       
   147 }
       
   148 
       
   149 void BrowserView::zoomOut()
       
   150 {
       
   151     int i = m_zoomLevels.indexOf(m_currentZoom);
       
   152     Q_ASSERT(i >= 0);
       
   153     if (i > 0)
       
   154         m_currentZoom = m_zoomLevels[i - 1];
       
   155 
       
   156     m_webView->setZoomFactor(static_cast<qreal>(m_currentZoom)/100.0);
       
   157 }
       
   158 
       
   159 void BrowserView::resizeEvent(QResizeEvent *event)
       
   160 {
       
   161     QWidget::resizeEvent(event);
       
   162 
       
   163     int h1 = m_titleBar->sizeHint().height();
       
   164     int h2 = m_controlStrip->sizeHint().height();
       
   165 
       
   166     m_titleBar->setGeometry(0, 0, width(), h1);
       
   167     m_controlStrip->setGeometry(0, height() - h2, width(), h2);
       
   168     m_webView->setGeometry(0, h1, width(), height() - h1);
       
   169 
       
   170     int zw = m_zoomStrip->sizeHint().width();
       
   171     int zh = m_zoomStrip->sizeHint().height();
       
   172     m_zoomStrip->move(width() - zw, (height() - zh) / 2);
       
   173 }
       
   174 #ifdef Q_OS_SYMBIAN
       
   175 void BrowserView::setDefaultIap()
       
   176 {
       
   177     qt_SetDefaultIap();
       
   178     m_webView->load(QUrl("http://news.bbc.co.uk/text_only.stm"));
       
   179 }
       
   180 #endif
       
   181 
       
   182 void BrowserView::navigate(const QUrl &url)
       
   183 {
       
   184     m_webView->load(url);
       
   185 }