ginebra2/KineticHelper.cpp
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
     1 /*
     1 /*
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
     4 *
     9 * Initial Contributors:
     5 * This program is free software: you can redistribute it and/or modify
    10 * Nokia Corporation - initial contribution.
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
    11 *
     8 *
    12 * Contributors:
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
    13 *
    13 *
    14 * Description: 
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
    15 *
    19 *
    16 */
    20 */
    17 
       
    18 #include <QTimer>
    21 #include <QTimer>
    19 #include <QTime>
    22 #include <QTime>
    20 #include <QPoint>
    23 #include <QPoint>
    21 #include <QDebug> 
    24 #include <QDebug>
    22 
    25 
    23 #include "KineticHelper.h"
    26 #include "KineticHelper.h"
    24 
    27 
    25 
    28 
    26 
    29 
    27 KineticHelper::KineticHelper(KineticScrollable* scrollable, qreal decel, int kineticTimeout) :
    30 KineticHelper::KineticHelper(KineticScrollable* scrollable, qreal decel, int kineticTimeout) :
    28      m_scrollable(scrollable),
    31      m_scrollable(scrollable),
    29      m_decel(decel), 
    32      m_decel(decel),
    30      m_kineticTimeout(kineticTimeout)
    33      m_kineticTimeout(kineticTimeout)
    31 {
    34 {
    32     m_kineticTimer = new QTimer(this);
    35     m_kineticTimer = new QTimer(this);
    33     QObject::connect(m_kineticTimer, SIGNAL(timeout()), this, SLOT(kineticScroll()));
    36     QObject::connect(m_kineticTimer, SIGNAL(timeout()), this, SLOT(kineticScroll()));
    34     m_kineticTimer->setSingleShot(false);
    37     m_kineticTimer->setSingleShot(false);
    45 }
    48 }
    46 
    49 
    47 
    50 
    48 void KineticHelper::startScrolling()
    51 void KineticHelper::startScrolling()
    49 {
    52 {
    50 	m_initialSpeed = m_scrollable->getInitialSpeed() * 1000;	
    53     m_initialSpeed = m_scrollable->getInitialSpeed() * 1000;
    51 	m_initialPos = m_scrollable->getInitialPosition();
    54     m_initialPos = m_scrollable->getInitialPosition();
    52 	if (m_kineticTimer->isActive()) {
    55     if (m_kineticTimer->isActive()) {
    53 	    m_kineticTimer->stop();
    56         m_kineticTimer->stop();
    54 	}
    57     }
    55 	m_kineticSteps = 0;
    58     m_kineticSteps = 0;
    56 	m_kineticScrollTime = 0.0;
    59     m_kineticScrollTime = 0.0;
    57 	m_kineticTimer->start(m_kineticTimeout);
    60     m_kineticTimer->start(m_kineticTimeout);
    58 	m_actualTime = QTime::currentTime();
    61     m_actualTime = QTime::currentTime();
    59 	m_actualTime.start();
    62     m_actualTime.start();
    60 	qDebug() << "starting kinetic timer at " << m_initialPos << ", at " << m_actualTime;  
       
    61 }
    63 }
    62 
    64 
    63 
    65 
    64 void KineticHelper::kineticScroll()
    66 void KineticHelper::kineticScroll()
    65 {
    67 {
    71     qreal vx = 0;
    73     qreal vx = 0;
    72     qreal vy = 0;
    74     qreal vy = 0;
    73 
    75 
    74     int t = m_actualTime.elapsed();
    76     int t = m_actualTime.elapsed();
    75     m_kineticScrollTime += (((qreal)t) / 1000);
    77     m_kineticScrollTime += (((qreal)t) / 1000);
    76     
    78 
    77     qDebug() << "kineticScroll(): elapsed: " << t << ", m_kineticSteps: " << m_kineticSteps << 
       
    78                 ", m_kineticScrollTime: " << m_kineticScrollTime <<
       
    79                 ", decelX: "<< decelX << ", decelY: " << decelY << ", m_initialSpeed: " << m_initialSpeed;
       
    80     if (m_initialSpeed.x()) {
    79     if (m_initialSpeed.x()) {
    81         vx = m_initialSpeed.x() + decelX * m_kineticScrollTime;
    80         vx = m_initialSpeed.x() + decelX * m_kineticScrollTime;
    82         qDebug() << "vx: " << vx;
       
    83         if (vx * m_initialSpeed.x() < 0) {
    81         if (vx * m_initialSpeed.x() < 0) {
    84             dx = 0;
    82             dx = 0;
    85             vx = 0;
    83             vx = 0;
    86         }
    84         }
    87         else {
    85         else {
    88             dx = m_kineticScrollTime * m_initialSpeed.x() + 
    86             dx = m_kineticScrollTime * m_initialSpeed.x() +
    89                 0.5 * decelX * m_kineticScrollTime * m_kineticScrollTime;
    87                 0.5 * decelX * m_kineticScrollTime * m_kineticScrollTime;
    90         }
    88         }
    91     }
    89     }
    92     
    90 
    93     if (m_initialSpeed.y()) {
    91     if (m_initialSpeed.y()) {
    94         vy = m_initialSpeed.y() + decelY * m_kineticScrollTime;
    92         vy = m_initialSpeed.y() + decelY * m_kineticScrollTime;
    95         qDebug() << "vy: " << vy;
       
    96         if (vy * m_initialSpeed.y() < 0) {
    93         if (vy * m_initialSpeed.y() < 0) {
    97             dy = 0;
    94             dy = 0;
    98             vy = 0;
    95             vy = 0;
    99         }
    96         }
   100         else {
    97         else {
   101             dy = m_kineticScrollTime * m_initialSpeed.y() + 
    98             dy = m_kineticScrollTime * m_initialSpeed.y() +
   102                  0.5 * decelY * m_kineticScrollTime * m_kineticScrollTime;
    99                  0.5 * decelY * m_kineticScrollTime * m_kineticScrollTime;
   103         }
   100         }
   104     }
   101     }
   105         
   102 
   106     QPoint scrollPos = m_scrollable->getScrollPosition();
   103     QPoint scrollPos = m_scrollable->getScrollPosition();
   107     QPoint distPos = m_initialPos + QPointF(dx, dy).toPoint();
   104     QPoint distPos = m_initialPos + QPointF(dx, dy).toPoint();
   108         
   105 
   109     if (vx != 0 || vy != 0) {
   106     if (vx != 0 || vy != 0) {
   110         qDebug() << "kineticScroll(): scroll from " << scrollPos << " to " << distPos;
       
   111         m_scrollable->scrollTo(distPos);
   107         m_scrollable->scrollTo(distPos);
   112     }
   108     }
   113     
   109 
   114     if ((vx == 0 && vy == 0) || scrollPos == m_scrollable->getScrollPosition()) {
   110     if ((vx == 0 && vy == 0) || scrollPos == m_scrollable->getScrollPosition()) {
   115         qDebug() << "kineticScroll(): stopping timer";
       
   116         stopScrolling();
   111         stopScrolling();
   117     }
   112     }
   118 }
   113 }
   119 
   114 
   120 void KineticHelper::stopScrolling()
   115 void KineticHelper::stopScrolling()