util/demos/embedded/anomaly/src/BrowserWindow.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     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 "BrowserWindow.h"
       
    43 
       
    44 #include <QtCore>
       
    45 #include <QtGui>
       
    46 #include <QPropertyAnimation>
       
    47 #include <QResizeEvent>
       
    48 
       
    49 #include "BrowserView.h"
       
    50 #include "HomeView.h"
       
    51 
       
    52 BrowserWindow::BrowserWindow()
       
    53     : m_slidingSurface(new QWidget(this))
       
    54     , m_homeView(new HomeView(m_slidingSurface))
       
    55     , m_browserView(new BrowserView(m_slidingSurface))
       
    56     , m_animation(new QPropertyAnimation(this, "slideValue"))
       
    57 {
       
    58     m_slidingSurface->setAutoFillBackground(true);
       
    59 
       
    60     m_homeView->resize(size());
       
    61 
       
    62     m_browserView->resize(size());
       
    63 
       
    64     connect(m_homeView, SIGNAL(addressEntered(QString)), SLOT(gotoAddress(QString)));
       
    65     connect(m_homeView, SIGNAL(urlActivated(QUrl)), SLOT(navigate(QUrl)));
       
    66 
       
    67     connect(m_browserView, SIGNAL(menuButtonClicked()), SLOT(showHomeView()));
       
    68 
       
    69     m_animation->setDuration(200);
       
    70     connect(m_animation, SIGNAL(finished()), SLOT(animationFinished()));
       
    71 
       
    72     setSlideValue(0.0f);
       
    73 }
       
    74 
       
    75 void BrowserWindow::gotoAddress(const QString &address)
       
    76 {
       
    77     m_browserView->navigate(QUrl::fromUserInput(address));
       
    78     showBrowserView();
       
    79 }
       
    80 
       
    81 void BrowserWindow::animationFinished()
       
    82 {
       
    83     m_animation->setDirection(QAbstractAnimation::Forward);
       
    84 }
       
    85 
       
    86 void BrowserWindow::navigate(const QUrl &url)
       
    87 {
       
    88     m_browserView->navigate(url);
       
    89     showBrowserView();
       
    90 }
       
    91 
       
    92 void BrowserWindow::setSlideValue(qreal slideRatio)
       
    93 {
       
    94     // we use a ratio to handle resize corectly
       
    95     const int pos = -qRound(slideRatio * width());
       
    96     m_slidingSurface->scroll(pos - m_homeView->x(), 0);
       
    97 
       
    98     if (qFuzzyCompare(slideRatio, static_cast<qreal>(1.0f))) {
       
    99         m_browserView->show();
       
   100         m_homeView->hide();
       
   101     } else if (qFuzzyCompare(slideRatio, static_cast<qreal>(0.0f))) {
       
   102         m_homeView->show();
       
   103         m_browserView->hide();
       
   104     } else {
       
   105         m_browserView->show();
       
   106         m_homeView->show();
       
   107     }
       
   108 }
       
   109 
       
   110 qreal BrowserWindow::slideValue() const
       
   111 {
       
   112     Q_ASSERT(m_slidingSurface->x() < width());
       
   113     return static_cast<qreal>(qAbs(m_homeView->x())) / width();
       
   114 }
       
   115 
       
   116 void BrowserWindow::showHomeView()
       
   117 {
       
   118     m_animation->setStartValue(slideValue());
       
   119     m_animation->setEndValue(0.0f);
       
   120     m_animation->start();
       
   121     m_homeView->setFocus();
       
   122 }
       
   123 
       
   124 void BrowserWindow::showBrowserView()
       
   125 {
       
   126     m_animation->setStartValue(slideValue());
       
   127     m_animation->setEndValue(1.0f);
       
   128     m_animation->start();
       
   129 
       
   130     m_browserView->setFocus();
       
   131 }
       
   132 
       
   133 void BrowserWindow::keyReleaseEvent(QKeyEvent *event)
       
   134 {
       
   135     QWidget::keyReleaseEvent(event);
       
   136 
       
   137     if (event->key() == Qt::Key_F3) {
       
   138         if (m_animation->state() == QAbstractAnimation::Running) {
       
   139             const QAbstractAnimation::Direction direction =  m_animation->direction() == QAbstractAnimation::Forward
       
   140                                                              ? QAbstractAnimation::Forward
       
   141                                                                  : QAbstractAnimation::Backward;
       
   142             m_animation->setDirection(direction);
       
   143         } else if (qFuzzyCompare(slideValue(), static_cast<qreal>(1.0f)))
       
   144             showHomeView();
       
   145         else
       
   146             showBrowserView();
       
   147         event->accept();
       
   148     }
       
   149 }
       
   150 
       
   151 void BrowserWindow::resizeEvent(QResizeEvent *event)
       
   152 {
       
   153     const QSize oldSize = event->oldSize();
       
   154     const qreal oldSlidingRatio = static_cast<qreal>(qAbs(m_homeView->x())) / oldSize.width();
       
   155 
       
   156     const QSize newSize = event->size();
       
   157     m_slidingSurface->resize(newSize.width() * 2, newSize.height());
       
   158 
       
   159     m_homeView->resize(newSize);
       
   160     m_homeView->move(0, 0);
       
   161 
       
   162     m_browserView->resize(newSize);
       
   163     m_browserView->move(newSize.width(), 0);
       
   164 
       
   165     setSlideValue(oldSlidingRatio);
       
   166 }