00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qtimer.h>
00022
00023 #include "stylusnormalizer.h"
00024
00025 static const int FLUSHTIME = 100;
00026
00027 _StylusEvent::_StylusEvent( const QPoint& newPt )
00028 : _pt( newPt ),
00029 _t( QTime::currentTime() )
00030 {
00031 }
00032
00033 _StylusEvent::~_StylusEvent()
00034 {
00035 }
00036
00037 StylusNormalizer::StylusNormalizer( QWidget *parent, const char* name )
00038 : QWidget( parent, name ),
00039 _next( 0 ),
00040 bFirst( true )
00041 {
00042
00043 int i;
00044 for (i = 0; i < SAMPLES; i++ ) {
00045 _ptList[i].setPoint( -1, -1 );
00046 }
00047 _tExpire = new QTimer( this );
00048 QObject::connect( _tExpire, SIGNAL( timeout() ),
00049 this, SLOT( slotAveragePoint() ) );
00050 }
00051
00052 StylusNormalizer::~StylusNormalizer()
00053 {
00054 }
00055
00056 void StylusNormalizer::addEvent( const QPoint& pt )
00057 {
00058 _ptList[_next].setPoint( pt );
00059 _ptList[_next++].setTime( QTime::currentTime() );
00060 if ( _next >= SAMPLES ) {
00061 _next = 0;
00062 }
00063
00064 if ( bFirst ) {
00065 slotAveragePoint();
00066 bFirst = false;
00067 }
00068 }
00069
00070 void StylusNormalizer::slotAveragePoint( void )
00071 {
00072 QPoint pt( 0, 0 );
00073 QTime tCurr = QTime::currentTime();
00074 int i,
00075 size;
00076 size = 0;
00077 for ( i = 0; i < SAMPLES; i++ ) {
00078 if ( ( (_ptList[i]).time().msecsTo( tCurr ) < FLUSHTIME ) &&
00079 ( _ptList[i].point() != QPoint( -1, -1 ) ) ) {
00080 pt += _ptList[i].point();
00081 size++;
00082 }
00083 }
00084 if ( size > 0 )
00085 emit signalNewPoint( pt /= size );
00086 }
00087
00088 void StylusNormalizer::start( void )
00089 {
00090 _tExpire->start( FLUSHTIME );
00091 }
00092
00093 void StylusNormalizer::stop( void )
00094 {
00095 _tExpire->stop();
00096 bFirst = true;
00097 }
00098