src/hbcore/gestures/hbpointrecorder_p.cpp
changeset 30 80e4d18b72f5
parent 2 06ff229162e9
equal deleted inserted replaced
28:b7da29130b0e 30:80e4d18b72f5
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 
    26 #include "hbpointrecorder_p.h"
    26 #include "hbpointrecorder_p.h"
    27 
    27 
    28 #include <QPair>
    28 #include <QPair>
    29 #include <QTime>
       
    30 #include <QDebug>
    29 #include <QDebug>
    31 
    30 
    32 //#define POINT_DEBUG
    31 //#define POINT_DEBUG
    33 #ifndef POINT_DEBUG
    32 #ifndef POINT_DEBUG
    34 # define DEBUG if (0) qDebug
    33 # define DEBUG if (0) qDebug
    73     \param threshold set used threshold to detect movement and stops
    72     \param threshold set used threshold to detect movement and stops
    74 */
    73 */
    75 void HbPointRecorder::resetRecorder(qreal threshold)
    74 void HbPointRecorder::resetRecorder(qreal threshold)
    76 {
    75 {
    77     mThreshold = threshold;
    76     mThreshold = threshold;
    78     clear();
    77     mPoints.clear();
    79 }
    78 }
    80 
    79 
    81 /*!
    80 /*!
    82     \internal
    81     \internal
    83     \brief Records point to list with timestamp.
    82     \brief Records point to list with timestamp.
    84     \param point Point to be recorded.
    83     \param point Point to be recorded.
    85     \param time Time to be recorded.
    84     \param time Time to be recorded.
    86     \return Nothing.
    85     \return Nothing.
    87 
    86 
    88 */
    87 */
    89 void HbPointRecorder::record(qreal point, const QTime &time)
    88 void HbPointRecorder::record(qreal point, qint64 time)
    90 {
    89 {
       
    90 #ifdef POINT_DEBUG
       
    91     if (mThreshold == 0.0f)
       
    92         hbWarning("Threshold has not been set for HbPointRecorder");
       
    93 #endif
       
    94 
    91     // Empty list always accepts first point without tests.
    95     // Empty list always accepts first point without tests.
    92     if ( !isEmpty() ) {
    96     if ( !mPoints.isEmpty() ) {
    93         // No point to record a point, if timestamp is less or equal with previous.
    97         // No point to record a point, if timestamp is less or equal with previous.
    94         if ( lastTime().msecsTo(time) == 0 ) {
    98         if ( time - lastTime() <= 0 ) {
    95             DEBUG() << "Ignoring point, because no difference in time stamps.";
    99             DEBUG() << "Ignoring point, because no difference in time stamps.";
    96             return;
   100             return;
    97         }
   101         }
    98 
   102 
    99         // Don't tolerate points, which are too close to previously recorded point.
   103         // Don't tolerate points, which are too close to previously recorded point.
   100         if ( qAbs(lastPoint() - point) < mThreshold ) {
   104         if ( qAbs(lastPoint() - point) < mThreshold ) {            
   101             DEBUG() << "Ignoring point, because it is withing threshold of previous point";
   105             DEBUG() << "Ignoring point, because it is withing threshold of previous point";
   102             return;
   106             return;
   103         }
   107         }
   104     }
   108     }
   105 
   109 
   106     // In case the list contains two or more points, direction can be
   110     // In case the list contains two or more points, direction can be
   107     // determined. Each new point added needs to be checked for direction
   111     // determined. Each new point added needs to be checked for direction
   108     // change.
   112     // change.
   109     if ( length() > 1 ) {
   113     if ( mPoints.length() > 1 ) {
   110         // Clear list, on direction change. Leave the last recorded point
   114         // Clear list, on direction change. Leave the last recorded point
   111         // to the list, as it can be considered as first point for new direction.
   115         // to the list, as it can be considered as first point for new direction.
   112         if ( dirChanged( point ) ) {
   116         if ( dirChanged( point ) ) {
   113             HbPointTime temp = last();
   117             HbPointTime temp = mPoints.last();
   114             clear();
   118             mPoints.clear();
   115             append(temp);
   119             mPoints.append(temp);
   116         }
   120         }
   117     }
   121     }
   118 
   122 
   119     // Finally check, if the position has changed. Don't record point, when no position
   123     // Finally check, if the position has changed. Don't record point, when no position
   120     // change.
   124     // change.
   121     if ( isEmpty() || point != lastPoint() ) {
   125     if ( mPoints.isEmpty() || point != lastPoint() ) {
   122         // Add point and time to list.
   126         // Add point and time to list.
   123         append(HbPointTime(point, time));
   127         mPoints.append(HbPointTime(point, time));
   124     } else {
   128     } else {
   125         DEBUG() << "Ignoring point, because it equals previous.";
   129         DEBUG() << "Ignoring point, because it equals previous.";
   126     }
   130     }
   127 }
   131 }
   128 
   132 
   132     \return Last recorded point.
   136     \return Last recorded point.
   133 
   137 
   134 */
   138 */
   135 qreal HbPointRecorder::lastPoint() const
   139 qreal HbPointRecorder::lastPoint() const
   136 {
   140 {
   137     Q_ASSERT(!isEmpty());
   141     Q_ASSERT(!mPoints.isEmpty());
   138     return last().first;
   142     return mPoints.last().first;
   139 }
   143 }
   140 
   144 
   141 /*!
   145 /*!
   142     \internal
   146     \internal
   143     \brief
   147     \brief
   144     \return Last recorded timestamp.
   148     \return Last recorded timestamp.
   145 
   149 
   146 */
   150 */
   147 const QTime& HbPointRecorder::lastTime() const
   151 qint64 HbPointRecorder::lastTime() const
   148 {
   152 {
   149     Q_ASSERT(!isEmpty());
   153     Q_ASSERT(!mPoints.isEmpty());
   150     return last().second;
   154     return mPoints.last().second;
   151 }
   155 }
   152 
   156 
   153 /*!
   157 /*!
   154     \internal
   158     \internal
   155     \brief Checks if new point causes direction change.
   159     \brief Checks if new point causes direction change.
   156     \param point The point suspected cause direction change.
   160     \param point The point suspected cause direction change.
   157     \return True, when direction changes.
   161     \return True, when direction changes.
   158 */
   162 */
   159 bool HbPointRecorder::dirChanged( qreal point ) const
   163 bool HbPointRecorder::dirChanged( qreal point ) const
   160 {
   164 {
       
   165     int len = mPoints.length();
       
   166 
   161     // Without direction, the direction cannot really change
   167     // Without direction, the direction cannot really change
   162     if ( length() < 2 ) { return false; }
   168     if ( len < 2 ) { return false; }
   163 
   169 
   164     qreal pos0 = (*this)[-2].first;
   170     qreal pos0 = mPoints.at(len-2).first;
   165     qreal pos1 = (*this)[-1].first;
   171     qreal pos1 = mPoints.at(len-1).first;
   166     qreal dir0 = pos1 - pos0;
   172     qreal dir0 = pos1 - pos0;
   167     qreal dir1 = point - pos1;
   173     qreal dir1 = point - pos1;
   168 
   174 
   169     // Don't react on movement, when changes happen within threshold
   175     // Don't react on movement, when changes happen within threshold
   170     if ( qAbs(dir1) <= mThreshold ) { return false; }
   176     if ( qAbs(dir1) <= mThreshold ) { return false; }
   172     // Check for '+' and '-' -signs in directions. Opposite signs means
   178     // Check for '+' and '-' -signs in directions. Opposite signs means
   173     // direction change.
   179     // direction change.
   174     return ( dir0 < 0 && dir1 >= 0 ) || ( dir0 > 0 && dir1 <= 0 );
   180     return ( dir0 < 0 && dir1 >= 0 ) || ( dir0 > 0 && dir1 <= 0 );
   175 }
   181 }
   176 
   182 
   177 /*!
       
   178     \internal
       
   179     \brief Operator to access the list.
       
   180 
       
   181     Supports negative values, which will define order from the end.
       
   182 */
       
   183 HbPointTime HbPointRecorder::operator[]( int number ) const
       
   184 {
       
   185     // Let's trust, that QList::at() takes care of validity checks.
       
   186     if ( number < 0 ) {
       
   187         number = count() + number;
       
   188     }
       
   189     return at(number);
       
   190 }
       
   191 
       
   192 
   183 
   193 /*!
   184 /*!
   194     \internal
   185     \internal
   195     \copydoc HbPointRecorder::getLastRecords
   186     \copydoc HbPointRecorder::getLastRecords
   196 */
   187 */
   197 QList<HbPointTime> HbPointRecorder::getLastRecords( int number ) const
   188 QList<HbPointTime> HbPointRecorder::getLastRecords( int number ) const
   198 {
   189 {
   199     QList<HbPointTime> recordings;
   190     number = qMin(number, mPoints.count());
   200     number = number > count() ? count() : number;
   191     return mPoints.mid(mPoints.count() - number);
   201     for (int i=count()-number; i < count(); i++) {
       
   202         recordings.append((*this)[i]);
       
   203     }
       
   204 
       
   205     return recordings;
       
   206 }
   192 }