#ifndef SPMLPPUSHBUTTON_H
#define SPMLPPUSHBUTTON_H
#include <QPushButton>
#include <time.h>
#include <sys/time.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
class spmLPPushButton : public QPushButton
{
Q_OBJECT
public:
explicit spmLPPushButton(QObject *parent = 0, long delay=1000);
~spmLPPushButton();
struct timeval *getCurrentTm( struct timeval *ptv );
double getTimeSecDist( struct timeval * tv_beg, struct timeval * tv_end );
void setDetectDelay(long);
signals:
void longPressedDown();
void longPressedUp();
public slots:
void pbpressed();
void pbreleased();
private:
struct timeval *m_tvPressed;
struct timeval *m_tvReleased;
bool m_bInLongPressed;
long m_iDelay;
};
#endif // SPMLPPUSHBUTTON_H
#include "spmLPPushButton.h"
#include <SpmGui.h>
spmLPPushButton::spmLPPushButton(QObject *parent, long delay) :
QPushButton((QWidget *)parent),
m_tvPressed(NULL),
m_tvReleased(NULL),
m_bInLongPressed(FALSE),
m_iDelay( 1000 )
{
setAutoRepeat(true);
if(delay > 100)
{
setAutoRepeatDelay(delay);
m_iDelay = delay;
}
else
setAutoRepeatDelay(1000);
setAutoRepeatInterval(9999999); // Do not Do Auto Repeat Clicked
connect( this, SIGNAL( pressed() ), this, SLOT( pbpressed() ) );
connect( this, SIGNAL( released() ), this, SLOT( pbreleased() ) );
m_tvReleased = getCurrentTm(m_tvReleased);
m_tvPressed = getCurrentTm(m_tvPressed);
}
void spmLPPushButton::setDetectDelay(long delay)
{
if(delay > 100)
{
setAutoRepeatDelay(delay);
m_iDelay = delay;
}
}
struct timeval *spmLPPushButton::getCurrentTm( struct timeval *ptv )
{
if(ptv == NULL)
{
try
{
ptv = new (struct timeval);
}
catch(...)
{
std::cerr<<__FILE__<<"Error of New operation.\n"<<std::endl;
exit(1);
}
}
memset( ptv, 0, sizeof(struct timeval) );
gettimeofday( ptv, NULL );
//time_t t;
//t = time(&t);
return ptv;
}
double spmLPPushButton::getTimeSecDist( struct timeval * tv_beg, struct timeval * tv_end )
{
double retVal = 0.000;
if( ( tv_beg == NULL ) ||
( tv_end == NULL ) )
{
printf( "@@@ Program takes error! Null timeval\n" );
return 0;
}
long tdist = (long)(tv_end->tv_sec - tv_beg->tv_sec); //(long) difftime( tv_beg->tv_sec, tv_end->tv_sec );
long usecdst = tv_end->tv_usec - tv_beg->tv_usec;
retVal = double(tdist ) + double(usecdst)/1000000;
printf( "@@@ Program takes %ld+%ldu ret:%.3fseconds.\n", tdist, usecdst, retVal );
return retVal;
}
void spmLPPushButton::pbpressed()
{
// std::cout<<ptt<<"@ Clicked On LongP Btn Pressed"<<std::endl;
m_tvPressed = getCurrentTm(m_tvPressed);
}
void spmLPPushButton::pbreleased()
{
// std::cout<<ptt<<"@ Clicked On LongP Btn Released"<<std::endl;
if( m_bInLongPressed )
{
emit longPressedUp();
std::cout<<"@@ LongP Detected Up"<<std::endl;
m_bInLongPressed = FALSE;
}
else
{
m_tvReleased = getCurrentTm(m_tvReleased);
if( getTimeSecDist( m_tvPressed, m_tvReleased ) >= 0.999 ) // The Long Press Detected Step0 1 is the seconds Delay
{
std::cout<<"@@ LongP Detected Down"<<std::endl;
emit longPressedDown();
m_bInLongPressed = TRUE;
}
else
{
}
}
}
spmLPPushButton::~spmLPPushButton()
{
delete m_tvReleased;
delete m_tvPressed;
}