src/hbcore/gestures/hbpointrecorder_p.cpp
changeset 1 f7ac710697a9
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbpointrecorder_p.h"
       
    27 
       
    28 #include <QDebug>
       
    29 //#define VELOCITY_DEBUG
       
    30 #ifndef VELOCITY_DEBUG
       
    31 # define DEBUG if (0) qDebug
       
    32 #else
       
    33 # define DEBUG qDebug
       
    34 #endif
       
    35 
       
    36 /*!
       
    37    @hbcore
       
    38    \internal
       
    39    \class HbPointRecorder
       
    40 
       
    41    \brief Class to store and hold list of points and timestamps.
       
    42 
       
    43 */
       
    44 
       
    45 /*!
       
    46     \internal
       
    47     \brief Constructor for HbPointRecorder
       
    48     \return
       
    49 
       
    50 */
       
    51 HbPointRecorder::HbPointRecorder()
       
    52 {
       
    53 }
       
    54 
       
    55 /*!
       
    56     \internal
       
    57     \brief Destructor for HbPointRecorder
       
    58     \return
       
    59 
       
    60 */
       
    61 HbPointRecorder::~HbPointRecorder()
       
    62 {
       
    63 }
       
    64 
       
    65 /*!
       
    66     \internal
       
    67     \brief Records point to list with timestamp.
       
    68     \param point Point to be recorded.
       
    69     \param time Time to be recorded.
       
    70     \return Nothing.
       
    71 
       
    72 */
       
    73 void HbPointRecorder::record(qreal point, QTime time)
       
    74 {
       
    75     // No point to record a point, if timestamp is less or equal with previous.
       
    76     if ( !isEmpty() && lastTime().msecsTo(time) == 0 )
       
    77     {
       
    78         DEBUG() << "Ignoring point, because no difference in time stamps.";
       
    79         return;
       
    80     }
       
    81 
       
    82     // In case the list contains two or more points, direction can be
       
    83     // determined. Each new point added needs to be checked for direction
       
    84     // change.
       
    85     if ( mPoints.length() > 1 )
       
    86     {
       
    87         // Clear list, on direction change. Leave the last recorded point
       
    88         // to the list, as it can be considered as first point for new direction.
       
    89         if ( dirChanged( point ) )
       
    90         {
       
    91             qreal tempPoint = lastPoint();
       
    92             QTime tempTime = lastTime();
       
    93 
       
    94             clear();
       
    95 
       
    96             mPoints.append( tempPoint );
       
    97             mTimes.append( tempTime );
       
    98         }
       
    99     }
       
   100 
       
   101     // Finally check, if the position has changed. Don't record point, when no position
       
   102     // change.
       
   103     if ( isEmpty() || point != lastPoint() )
       
   104     {
       
   105         // Add point and time to list.
       
   106         mPoints.append( point );
       
   107         mTimes.append( time );
       
   108     }
       
   109     else
       
   110     {
       
   111         DEBUG() << "Ignoring point, because it equals previous.";
       
   112     }
       
   113 }
       
   114 
       
   115 /*!
       
   116     \internal
       
   117     \brief
       
   118     \return True, when no recorded items.
       
   119 
       
   120 */
       
   121 bool HbPointRecorder::isEmpty() const
       
   122 {
       
   123     // This situation should be impossible to even happen, but in case
       
   124     // the lists are out of sync. The result of empty list dictates the
       
   125     // result of this test.
       
   126     return !mTimes.length() && !mPoints.length();
       
   127 }
       
   128 
       
   129 /*!
       
   130     \internal
       
   131     \brief
       
   132     \return Last recorded point.
       
   133 
       
   134 */
       
   135 qreal HbPointRecorder::lastPoint() const
       
   136 {
       
   137     Q_ASSERT(!isEmpty());
       
   138     return mPoints.last();
       
   139 }
       
   140 
       
   141 /*!
       
   142     \internal
       
   143     \brief
       
   144     \return Last recorded timestamp.
       
   145 
       
   146 */
       
   147 const QTime& HbPointRecorder::lastTime() const
       
   148 {
       
   149     Q_ASSERT(!isEmpty());
       
   150     return mTimes.last();
       
   151 }
       
   152 
       
   153 /*!
       
   154     \internal
       
   155     \brief Checks if new point causes direction change.
       
   156     \param point The point suspected cause direction change.
       
   157     \return True, when direction changes.
       
   158 */
       
   159 bool HbPointRecorder::dirChanged( qreal point ) const
       
   160 {
       
   161     qreal x0 = mPoints.at(mPoints.length()-2);
       
   162     qreal x1 = mPoints.at(mPoints.length()-1);
       
   163     qreal dir0 = x1 - x0;
       
   164     qreal dir1 = point - x1;
       
   165 
       
   166     // Check for '+' and '-' -signs in directions. Opposite signs means
       
   167     // direction change.
       
   168     return ( dir0 < 0 && dir1 >= 0 ) || ( dir0 > 0 && dir1 <= 0 );
       
   169 }
       
   170 
       
   171 /*!
       
   172     \internal
       
   173     \param T type of items in the list.
       
   174     \brief Returns given number of items from the end.
       
   175     \return List of items.
       
   176 
       
   177     This function gets items from the given list from the end by the
       
   178     amount of items specified by 'number' parameter. In case, when the
       
   179     length of the list is less than required number of items, complete
       
   180     list is provided instead.
       
   181 */
       
   182 template <class T>
       
   183 QList<T> HbPointRecorder::getLastItems( QList<T> list, int number ) const
       
   184 {
       
   185     if ( list.length() <= number )
       
   186     {
       
   187         return list;
       
   188     }
       
   189     else
       
   190     {
       
   191         QList<T> tempList;
       
   192         for ( int i = list.length(); --i >= list.length()-number; )
       
   193         {
       
   194             tempList.insert(0, list.at(i));
       
   195         }
       
   196         return tempList;
       
   197     }
       
   198 }
       
   199 
       
   200 /*!
       
   201     \internal
       
   202     \copydoc HbPointRecorder::getLastItems
       
   203 */
       
   204 QList<qreal> HbPointRecorder::getLastPoints( int number ) const
       
   205 {
       
   206     return getLastItems(mPoints, number);
       
   207 }
       
   208 
       
   209 /*!
       
   210     \internal
       
   211     \copydoc HbPointRecorder::getLastItems
       
   212 */
       
   213 QList<QTime> HbPointRecorder::getLastTimes( int number ) const
       
   214 {
       
   215     return getLastItems(mTimes, number);
       
   216 }
       
   217 
       
   218 /*!
       
   219     \internal
       
   220     \brief
       
   221     \return Last recorded timestamp.
       
   222 
       
   223 */
       
   224 void HbPointRecorder::clear()
       
   225 {
       
   226     mPoints.clear();
       
   227     mTimes.clear();
       
   228 }