demos/embedded/anomaly/src/BrowserWindow.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    41 
    41 
    42 #include "BrowserWindow.h"
    42 #include "BrowserWindow.h"
    43 
    43 
    44 #include <QtCore>
    44 #include <QtCore>
    45 #include <QtGui>
    45 #include <QtGui>
       
    46 #include <QPropertyAnimation>
       
    47 #include <QResizeEvent>
    46 
    48 
    47 #include "BrowserView.h"
    49 #include "BrowserView.h"
    48 #include "HomeView.h"
    50 #include "HomeView.h"
    49 
    51 
    50 BrowserWindow::BrowserWindow()
    52 BrowserWindow::BrowserWindow()
    51     : QWidget()
    53     : m_slidingSurface(new QWidget(this))
    52     , m_homeView(0)
    54     , m_homeView(new HomeView(m_slidingSurface))
    53     , m_browserView(0)
    55     , m_browserView(new BrowserView(m_slidingSurface))
       
    56     , m_animation(new QPropertyAnimation(this, "slideValue"))
    54 {
    57 {
    55     m_timeLine = new QTimeLine(300, this);
    58     m_slidingSurface->setAutoFillBackground(true);
    56     m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
       
    57     QTimer::singleShot(0, this, SLOT(initialize()));
       
    58 }
       
    59 
    59 
    60 void BrowserWindow::initialize()
    60     m_homeView->resize(size());
    61 {
       
    62     m_homeView = new HomeView(this);
       
    63     m_browserView = new BrowserView(this);
       
    64 
    61 
    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());
    62     m_browserView->resize(size());
    71     m_browserView->move(0, 0);
       
    72 
    63 
    73     connect(m_homeView, SIGNAL(addressEntered(QString)), SLOT(gotoAddress(QString)));
    64     connect(m_homeView, SIGNAL(addressEntered(QString)), SLOT(gotoAddress(QString)));
    74     connect(m_homeView, SIGNAL(urlActivated(QUrl)), SLOT(navigate(QUrl)));
    65     connect(m_homeView, SIGNAL(urlActivated(QUrl)), SLOT(navigate(QUrl)));
    75 
    66 
    76     connect(m_browserView, SIGNAL(menuButtonClicked()), SLOT(showHomeView()));
    67     connect(m_browserView, SIGNAL(menuButtonClicked()), SLOT(showHomeView()));
    77 
    68 
    78     m_homeView->setVisible(false);
    69     m_animation->setDuration(200);
    79     m_browserView->setVisible(false);
    70     connect(m_animation, SIGNAL(finished()), SLOT(animationFinished()));
    80     slide(0);
       
    81 
    71 
    82     connect(m_timeLine, SIGNAL(frameChanged(int)), SLOT(slide(int)));
    72     setSlideValue(0.0f);
    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 }
    73 }
   127 
    74 
   128 void BrowserWindow::gotoAddress(const QString &address)
    75 void BrowserWindow::gotoAddress(const QString &address)
   129 {
    76 {
   130     m_browserView->navigate(guessUrlFromString(address));
    77     m_browserView->navigate(QUrl::fromUserInput(address));
   131     showBrowserView();
    78     showBrowserView();
       
    79 }
       
    80 
       
    81 void BrowserWindow::animationFinished()
       
    82 {
       
    83     m_animation->setDirection(QAbstractAnimation::Forward);
   132 }
    84 }
   133 
    85 
   134 void BrowserWindow::navigate(const QUrl &url)
    86 void BrowserWindow::navigate(const QUrl &url)
   135 {
    87 {
   136     m_browserView->navigate(url);
    88     m_browserView->navigate(url);
   137     showBrowserView();
    89     showBrowserView();
   138 }
    90 }
   139 
    91 
   140 void BrowserWindow::slide(int pos)
    92 void BrowserWindow::setSlideValue(qreal slideRatio)
   141 {
    93 {
   142     m_browserView->move(pos, 0);
    94     // we use a ratio to handle resize corectly
   143     m_homeView->move(pos - width(), 0);
    95     const int pos = -qRound(slideRatio * width());
   144     m_browserView->show();
    96     m_slidingSurface->scroll(pos - m_homeView->x(), 0);
   145     m_homeView->show();
    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();
   146 }
   114 }
   147 
   115 
   148 void BrowserWindow::showHomeView()
   116 void BrowserWindow::showHomeView()
   149 {
   117 {
   150     if (m_timeLine->state() != QTimeLine::NotRunning)
   118     m_animation->setStartValue(slideValue());
   151         return;
   119     m_animation->setEndValue(0.0f);
   152 
   120     m_animation->start();
   153     m_timeLine->setFrameRange(0, width());
       
   154     m_timeLine->start();
       
   155     m_homeView->setFocus();
   121     m_homeView->setFocus();
   156 }
   122 }
   157 
   123 
   158 void BrowserWindow::showBrowserView()
   124 void BrowserWindow::showBrowserView()
   159 {
   125 {
   160     if (m_timeLine->state() != QTimeLine::NotRunning)
   126     m_animation->setStartValue(slideValue());
   161         return;
   127     m_animation->setEndValue(1.0f);
       
   128     m_animation->start();
   162 
   129 
   163     m_timeLine->setFrameRange(width(), 0);
       
   164     m_timeLine->start();
       
   165     m_browserView->setFocus();
   130     m_browserView->setFocus();
   166 }
   131 }
   167 
   132 
   168 void BrowserWindow::keyReleaseEvent(QKeyEvent *event)
   133 void BrowserWindow::keyReleaseEvent(QKeyEvent *event)
   169 {
   134 {
   170     QWidget::keyReleaseEvent(event);
   135     QWidget::keyReleaseEvent(event);
   171 
   136 
   172     if (event->key() == Qt::Key_F3) {
   137     if (event->key() == Qt::Key_F3) {
   173         if (m_homeView->isVisible())
   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
   174             showBrowserView();
   146             showBrowserView();
   175         else
   147         event->accept();
   176             showHomeView();
       
   177     }
   148     }
   178 }
   149 }
   179 
   150 
   180 void BrowserWindow::resizeEvent(QResizeEvent *event)
   151 void BrowserWindow::resizeEvent(QResizeEvent *event)
   181 {
   152 {
   182     if (m_homeView)
   153     const QSize oldSize = event->oldSize();
   183         m_homeView->resize(size());
   154     const qreal oldSlidingRatio = static_cast<qreal>(qAbs(m_homeView->x())) / oldSize.width();
   184 
   155 
   185     if (m_browserView)
   156     const QSize newSize = event->size();
   186         m_browserView->resize(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);
   187 }
   166 }