demos/embedded/anomaly/src/BrowserWindow.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 "BrowserWindow.h"
       
    43 
       
    44 #include <QtCore>
       
    45 #include <QtGui>
       
    46 
       
    47 #include "BrowserView.h"
       
    48 #include "HomeView.h"
       
    49 
       
    50 BrowserWindow::BrowserWindow()
       
    51     : QWidget()
       
    52     , m_homeView(0)
       
    53     , m_browserView(0)
       
    54 {
       
    55     m_timeLine = new QTimeLine(300, this);
       
    56     m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
       
    57     QTimer::singleShot(0, this, SLOT(initialize()));
       
    58 }
       
    59 
       
    60 void BrowserWindow::initialize()
       
    61 {
       
    62     m_homeView = new HomeView(this);
       
    63     m_browserView = new BrowserView(this);
       
    64 
       
    65     m_homeView->hide();
       
    66     m_homeView->resize(size());
       
    67     m_homeView->move(0, 0);
       
    68 
       
    69     m_browserView->hide();
       
    70     m_browserView->resize(size());
       
    71     m_browserView->move(0, 0);
       
    72 
       
    73     connect(m_homeView, SIGNAL(addressEntered(QString)), SLOT(gotoAddress(QString)));
       
    74     connect(m_homeView, SIGNAL(urlActivated(QUrl)), SLOT(navigate(QUrl)));
       
    75 
       
    76     connect(m_browserView, SIGNAL(menuButtonClicked()), SLOT(showHomeView()));
       
    77 
       
    78     m_homeView->setVisible(false);
       
    79     m_browserView->setVisible(false);
       
    80     slide(0);
       
    81 
       
    82     connect(m_timeLine, SIGNAL(frameChanged(int)), SLOT(slide(int)));
       
    83 }
       
    84 
       
    85 
       
    86 // from Demo Browser
       
    87 QUrl guessUrlFromString(const QString &string)
       
    88 {
       
    89     QString urlStr = string.trimmed();
       
    90     QRegExp test(QLatin1String("^[a-zA-Z]+\\:.*"));
       
    91 
       
    92     // Check if it looks like a qualified URL. Try parsing it and see.
       
    93     bool hasSchema = test.exactMatch(urlStr);
       
    94     if (hasSchema) {
       
    95         QUrl url = QUrl::fromEncoded(urlStr.toUtf8(), QUrl::TolerantMode);
       
    96         if (url.isValid())
       
    97             return url;
       
    98     }
       
    99 
       
   100     // Might be a file.
       
   101     if (QFile::exists(urlStr)) {
       
   102         QFileInfo info(urlStr);
       
   103         return QUrl::fromLocalFile(info.absoluteFilePath());
       
   104     }
       
   105 
       
   106     // Might be a shorturl - try to detect the schema.
       
   107     if (!hasSchema) {
       
   108         int dotIndex = urlStr.indexOf(QLatin1Char('.'));
       
   109         if (dotIndex != -1) {
       
   110             QString prefix = urlStr.left(dotIndex).toLower();
       
   111             QString schema = (prefix == QString("ftp")) ? prefix.toLatin1() : QString("http");
       
   112             QString location = schema + "://" + urlStr;
       
   113             QUrl url = QUrl::fromEncoded(location.toUtf8(), QUrl::TolerantMode);
       
   114             if (url.isValid())
       
   115                 return url;
       
   116         }
       
   117     }
       
   118 
       
   119     // Fall back to QUrl's own tolerant parser.
       
   120     QUrl url = QUrl::fromEncoded(string.toUtf8(), QUrl::TolerantMode);
       
   121 
       
   122     // finally for cases where the user just types in a hostname add http
       
   123     if (url.scheme().isEmpty())
       
   124         url = QUrl::fromEncoded("http://" + string.toUtf8(), QUrl::TolerantMode);
       
   125     return url;
       
   126 }
       
   127 
       
   128 void BrowserWindow::gotoAddress(const QString &address)
       
   129 {
       
   130     m_browserView->navigate(guessUrlFromString(address));
       
   131     showBrowserView();
       
   132 }
       
   133 
       
   134 void BrowserWindow::navigate(const QUrl &url)
       
   135 {
       
   136     m_browserView->navigate(url);
       
   137     showBrowserView();
       
   138 }
       
   139 
       
   140 void BrowserWindow::slide(int pos)
       
   141 {
       
   142     m_browserView->move(pos, 0);
       
   143     m_homeView->move(pos - width(), 0);
       
   144     m_browserView->show();
       
   145     m_homeView->show();
       
   146 }
       
   147 
       
   148 void BrowserWindow::showHomeView()
       
   149 {
       
   150     if (m_timeLine->state() != QTimeLine::NotRunning)
       
   151         return;
       
   152 
       
   153     m_timeLine->setFrameRange(0, width());
       
   154     m_timeLine->start();
       
   155     m_homeView->setFocus();
       
   156 }
       
   157 
       
   158 void BrowserWindow::showBrowserView()
       
   159 {
       
   160     if (m_timeLine->state() != QTimeLine::NotRunning)
       
   161         return;
       
   162 
       
   163     m_timeLine->setFrameRange(width(), 0);
       
   164     m_timeLine->start();
       
   165     m_browserView->setFocus();
       
   166 }
       
   167 
       
   168 void BrowserWindow::keyReleaseEvent(QKeyEvent *event)
       
   169 {
       
   170     QWidget::keyReleaseEvent(event);
       
   171 
       
   172     if (event->key() == Qt::Key_F3) {
       
   173         if (m_homeView->isVisible())
       
   174             showBrowserView();
       
   175         else
       
   176             showHomeView();
       
   177     }
       
   178 }
       
   179 
       
   180 void BrowserWindow::resizeEvent(QResizeEvent *event)
       
   181 {
       
   182     if (m_homeView)
       
   183         m_homeView->resize(size());
       
   184 
       
   185     if (m_browserView)
       
   186         m_browserView->resize(size());
       
   187 }