1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

add long click helper

This commit is contained in:
tianduanrui 2018-05-19 15:43:07 +08:00
parent 02b9303369
commit 99b99cb2a4
15 changed files with 365 additions and 212 deletions

View File

@ -1,18 +1,11 @@
#include "qqtclickhelper.h"
QQtClickHelper::QQtClickHelper ( QObject* parent )
QQtClickHelper::QQtClickHelper ( QObject* parent ) : QObject ( parent )
{
mLongClickInterval = longClickInterval;
now_press = QTime::currentTime().addMSecs ( 1 );
now_release = QTime::currentTime().addMSecs ( 2 );
nClickNumWithCancel = 0;
nLongClickNumWithCancel = 0;
nTotalClickNumWithCancel = 0;
nClickNum = 0;
nLongClickNum = 0;
nTotalClickNum = 0;
mClickType = QQtNullClick;
@ -28,108 +21,44 @@ void QQtClickHelper::mousePressEvent ( QMouseEvent* event, QWidget* userWidget )
p2debug() << "press" << event->pos() << userWidget;
mPoint = event->pos();
mClickType = QQtClick;
now_press = QTime::currentTime();
}
void QQtClickHelper::mouseReleaseEvent ( QMouseEvent* event, QWidget* userWidget )
{
p2debug() << "release" << event->pos() << userWidget;
//这一次 current click
QTime t2_release_preview = now_release;
now_release = QTime::currentTime();
//这里加了个判断,其实肯定>=0
if ( now_press.msecsTo ( now_release ) >= 0 && now_press.msecsTo ( now_release ) <= mLongClickInterval )
{
//单击发生
mClickType = QQtClick;
}
else if ( now_press.msecsTo ( now_release ) >= 0 && now_press.msecsTo ( now_release ) > mLongClickInterval )
{
//长击发生
mClickType = QQtLongClick;
//这次点击,
//上次的release竟然比press晚,说明press没发生,置位click
//第一次click, 上一次release=press也在这里被置位click
if ( t2_release_preview.msecsTo ( now_release ) >= 0
&& t2_release_preview.msecsTo ( now_press ) <= 0 )
mClickType = QQtClick;
//计算点击数目
checkClickNumWithCancel();
//修改状态
mClickType = QQtNullClick;
//如果手移开了,那么不发送.
//条件是用户传入了这个Widget为真.
if ( userWidget )
{
p2debug() << userWidget << userWidget->rect() << event->pos() << userWidget->rect().contains ( event->pos() );
if ( !userWidget->rect().contains ( event->pos() ) )
{
mClickType = QQtClick;
p2debug() << "send click canceled." ;
return;
}
}
if ( mClickType == QQtLongClick )
{
//计算点击数目
checkClickNumWithCancel();
checkClickNum ( QQtClick );
//修改状态
mClickType = QQtNullClick;
//如果手移开了,那么不发送.
//条件是用户传入了这个Widget为真.
if ( userWidget )
{
p2debug() << userWidget << userWidget->rect() << event->pos() << userWidget->rect().contains ( event->pos() );
if ( !userWidget->rect().contains ( event->pos() ) )
{
p2debug() << "send long click canceled." ;
return;
}
}
checkClickNum ( QQtLongClick );
//发送长信号
p2debug() << "send long click " ;
emit longClick();
emit longClickWithPoint ( event->pos() );
emit longClickWithPointF ( event->localPos() );
//发送单击信号
p2debug() << "send click." ;
emit click();
emit clickWithPoint ( event->pos() );
emit clickWithPointF ( event->localPos() );
#if 0
emit longClick ( userWidget );
emit longClickWithPoint ( event->pos(), userWidget );
emit longClickWithPointF ( event->localPos(), userWidget );
emit click ( userWidget );
emit clickWithPoint ( event->pos(), userWidget );
emit clickWithPointF ( event->localPos(), userWidget );
#endif
return;
}
else if ( mClickType == QQtClick )
{
//计算点击数目
checkClickNumWithCancel();
//修改状态
mClickType = QQtNullClick;
//如果手移开了,那么不发送.
//条件是用户传入了这个Widget为真.
if ( userWidget )
{
p2debug() << userWidget << userWidget->rect() << event->pos() << userWidget->rect().contains ( event->pos() );
if ( !userWidget->rect().contains ( event->pos() ) )
{
p2debug() << "send click canceled." ;
return;
}
}
checkClickNum ( QQtClick );
//发送单击信号
p2debug() << "send click." ;
emit click();
emit clickWithPoint ( event->pos() );
emit clickWithPointF ( event->localPos() );
#if 0
emit click ( userWidget );
emit clickWithPoint ( event->pos(), userWidget );
emit clickWithPointF ( event->localPos(), userWidget );
#endif
return;
}
//修改状态
mClickType = QQtNullClick;
@ -146,17 +75,6 @@ void QQtClickHelper::mouseDoubleClickEvent ( QMouseEvent* event, QWidget* userWi
mPoint = event->pos();
}
void QQtClickHelper::setLongClickInterval ( int millSecond )
{
mLongClickInterval = millSecond;
}
int QQtClickHelper::getLongClickInterval() const
{
return mLongClickInterval;
}
void QQtClickHelper::checkClickNumWithCancel()
{
switch ( mClickType )
@ -171,21 +89,11 @@ void QQtClickHelper::checkClickNumWithCancel()
}
}
break;
case QQtLongClick:
{
nLongClickNumWithCancel++;
if ( nLongClickNumWithCancel >= 0xFFFFFFFF )
{
p2debug() << "out......";
nLongClickNumWithCancel = 0;
}
}
break;
default:
break;
}
nTotalClickNumWithCancel = nClickNumWithCancel + nLongClickNumWithCancel;
nTotalClickNumWithCancel = nClickNumWithCancel;
if ( nTotalClickNumWithCancel >= 0xFFFFFFFFFFFFFFFF )
{
p2debug() << "out......";
@ -207,21 +115,11 @@ void QQtClickHelper::checkClickNum ( QQtClickHelper::QQtClickType type )
}
}
break;
case QQtLongClick:
{
nLongClickNum++;
if ( nLongClickNum >= 0xFFFFFFFF )
{
p2debug() << "out......";
nLongClickNum = 0;
}
}
break;
default:
break;
}
nTotalClickNum = nClickNum + nLongClickNum;
nTotalClickNum = nClickNum;
if ( nTotalClickNum >= 0xFFFFFFFFFFFFFFFF )
{
p2debug() << "out......";

View File

@ -4,7 +4,7 @@
#include <qqt-local.h>
#include <qqtcore.h>
#if 1
#if 0
#define p2debug() p2line()
#else
#define p2debug() QNoDebug()
@ -35,15 +35,12 @@ public:
//这些信号不区分Widget,谁发过来都发出去,不论Widget
signals:
void click();
void longClick();
signals:
void clickWithPoint ( QPoint point );
void longClickWithPoint ( QPoint point );
signals:
void clickWithPointF ( QPointF point );
void longClickWithPointF ( QPointF point );
#if 0
//发给业务层
@ -53,10 +50,6 @@ signals:
void click ( QWidget* userWidget );
void clickWithPoint ( QPoint point, QWidget* userWidget );
void clickWithPointF ( QPointF point, QWidget* userWidget );
void longClick ( QWidget* userWidget );
void longClickWithPoint ( QPoint point, QWidget* userWidget );
void longClickWithPointF ( QPointF point, QWidget* userWidget );
#endif
//调用于userWidget相对应的Event里
@ -68,25 +61,16 @@ public:
//optional
public:
//设置longClick超时 default:1200ms
void setLongClickInterval ( int millSecond = longClickInterval );
int getLongClickInterval() const;
inline const quint32 clickNum() const { return nClickNum; }
inline const quint32 longClickNum() const { return nLongClickNum; }
inline const quint64 totalClickNum() const { return nTotalClickNum; }
inline const quint32 clickNumWithCancel() const { return nClickNumWithCancel; }
inline const quint32 longClickNumWithCancel() const { return nLongClickNumWithCancel; }
inline const quint64 totalClickNumWithCancel() const { return nTotalClickNumWithCancel; }
/**
*
*/
public:
Q_ENUMS ( QQtClickType )
Q_PROPERTY ( int mLongClickInterval READ getLongClickInterval WRITE setLongClickInterval )
typedef enum
{
QQtNullClick = 0,
@ -98,33 +82,23 @@ public:
QQtMaxClick
} QQtClickType;
Q_ENUMS ( QQtClickType )
protected:
//这个语法比较难,只有整型有特权.
static const int longClickInterval = 1200;
//用于记录点击到了什么状态.
QQtClickType mClickType;
//用于记录按钮的位置
QPoint mPoint;
int mLongClickInterval;
//click 检测使用
QTime now_press;//press relase
QTime now_release;//release
//click num
virtual void checkClickNumWithCancel();
virtual void checkClickNum ( QQtClickType type );
quint32 nClickNum;
quint32 nLongClickNum;
quint64 nTotalClickNum;
quint32 nClickNumWithCancel;
quint32 nLongClickNumWithCancel;
quint64 nTotalClickNumWithCancel;
};

View File

@ -1,11 +1 @@
#include "qqtclicksoundhelper.h"
QQtClickSoundHelper::QQtClickSoundHelper ( QObject* parent )
{
mClickHelper = 0;
}
QQtClickSoundHelper::~QQtClickSoundHelper()
{
}

View File

@ -4,53 +4,41 @@
#include <qqt-local.h>
#include <qqtcore.h>
#include <qqtclickhelper.h>
#include <qqtsoundeffect.h>
/**
* QQtClickSoundHelper
*
* Widget提供按键声音支持.
*
* ClickSoundHelper和Widget共用ClickHelper,Widget按键同时能播放声音的能力
* QQtClickHelper实现
*
* click
*
*/
class QQTSHARED_EXPORT QQtClickSoundHelper : public QObject
class QQTSHARED_EXPORT QQtClickSoundHelper : public QQtClickHelper
{
Q_OBJECT
public:
explicit QQtClickSoundHelper ( QObject* parent = 0 );
explicit QQtClickSoundHelper ( QObject* parent = 0 ) : QQtClickHelper ( parent ) {
connect ( this, SIGNAL ( click() ), this, SLOT ( slotClick() ) );
}
virtual ~QQtClickSoundHelper();
void setClickSound();
void setLongClickSound();
public:
//一般,调用widget的clickHelper,设置到这里.
inline void setClickHelper ( QQtClickHelper* helper ) {
uninstallClickHelper();
mClickHelper = helper;
installClickHelper();
void setClickSound ( const QString clickSoundFile ) {
this->clickSoundFile = clickSoundFile;
}
inline const QQtClickHelper* clickHelper() const {
return mClickHelper;
}
protected:
//允许重写
virtual void installClickHelper() {
}
virtual void uninstallClickHelper() {
}
QQtClickHelper* mClickHelper;
/**
*
*/
public slots:
void slotClick() {}
void slotLongClick() {}
void slotClick() {
}
private:
QString clickSoundFile;
};
#endif // QQTCLICKSOUNDHELPER_H

View File

@ -1,7 +1,7 @@
#include "qqtdoubleclickhelper.h"
QQtDoubleClickHelper::QQtDoubleClickHelper ( QObject* parent ) :
QQtClickHelper ( parent )
QQtLongClickHelper ( parent )
{
mDoubleClickInterval = doubleClickInterval;
@ -275,7 +275,7 @@ void QQtDoubleClickHelper::slotDoubleClickTimeout()
void QQtDoubleClickHelper::checkClickNumWithCancel()
{
QQtClickHelper::checkClickNumWithCancel();
QQtLongClickHelper::checkClickNumWithCancel();
switch ( mClickType )
{
case QQtDoubleClick:
@ -303,7 +303,7 @@ void QQtDoubleClickHelper::checkClickNumWithCancel()
void QQtDoubleClickHelper::checkClickNum ( QQtClickType type )
{
QQtClickHelper::checkClickNum ( type );
QQtLongClickHelper::checkClickNum ( type );
switch ( type )
{
case QQtDoubleClick:

View File

@ -4,7 +4,7 @@
#include <QTimer>
#include <qqtevent.h>
#include <qqtclickhelper.h>
#include <qqtlongclickhelper.h>
#include <qqt-local.h>
#include <qqtcore.h>
@ -12,6 +12,11 @@
/**
* @brief The QQtDoubleClickHelper class
*
*
* click
* longClick
* doubleClick
*
*
* release开始检测.
* longClick只发送longClick.
@ -28,7 +33,7 @@
*
* DoubleClick检测是独立于系统的,,widget一个时延系统也没问题.
*/
class QQTSHARED_EXPORT QQtDoubleClickHelper : public QQtClickHelper
class QQTSHARED_EXPORT QQtDoubleClickHelper : public QQtLongClickHelper
{
Q_OBJECT
@ -54,9 +59,6 @@ signals:
//optional
public:
//这个语法比较难,只有整型有特权.
static const int doubleClickInterval = 200;
//设置双击检测时延 default: doubleClickInterval ms
//不会影响系统默认时延
void setDoubleClickInterval ( int millSecond = doubleClickInterval ) {
@ -80,6 +82,9 @@ public slots:
void slotDoubleClickTimeout();
protected:
//这个语法比较难,只有整型有特权.
static const int doubleClickInterval = 260;
//clickTimer
QTimer* m_click_timer;
//longClickTimer
@ -87,8 +92,6 @@ protected:
//doubleClickTimer
QTimer* m_double_click_timer;
//用于记录按钮的位置
QPoint mPoint;
//双击200ms. 这个比较符合双击舒适度.Qt内部的Timer慢一些,可能是计算了比较完整的时间.
//在这个范围内的都 才 是双击
@ -110,8 +113,9 @@ protected:
QQtMouseEvent* mMouseEvent;
//click num
virtual void checkClickNumWithCancel();
virtual void checkClickNum ( QQtClickType type ) override;
virtual void checkClickNumWithCancel() override;
quint32 nDoubleClickNum;
quint32 nDoubleClickNumWithCancel;
};

View File

@ -0,0 +1,205 @@
#include "qqtlongclickhelper.h"
QQtLongClickHelper::QQtLongClickHelper ( QObject* parent ) : QQtClickHelper ( parent )
{
mLongClickInterval = longClickInterval;
now_press = QTime::currentTime().addMSecs ( 1 );
now_release = QTime::currentTime().addMSecs ( 2 );
nLongClickNum = 0;
nLongClickNumWithCancel = 0;
}
QQtLongClickHelper::~QQtLongClickHelper()
{
}
void QQtLongClickHelper::mousePressEvent ( QMouseEvent* event, QWidget* userWidget )
{
p2debug() << "press" << event->pos() << userWidget;
mPoint = event->pos();
mClickType = QQtClick;
now_press = QTime::currentTime();
}
void QQtLongClickHelper::mouseReleaseEvent ( QMouseEvent* event, QWidget* userWidget )
{
p2debug() << "release" << event->pos() << userWidget;
//这一次 current click
QTime t2_release_preview = now_release;
now_release = QTime::currentTime();
//这里加了个判断,其实肯定>=0
if ( now_press.msecsTo ( now_release ) >= 0 && now_press.msecsTo ( now_release ) <= mLongClickInterval )
{
//单击发生
mClickType = QQtClick;
}
else if ( now_press.msecsTo ( now_release ) >= 0 && now_press.msecsTo ( now_release ) > mLongClickInterval )
{
//长击发生
mClickType = QQtLongClick;
//这次点击,
//上次的release竟然比press晚,说明press没发生,置位click
//第一次click, 上一次release=press也在这里被置位click
if ( t2_release_preview.msecsTo ( now_release ) >= 0
&& t2_release_preview.msecsTo ( now_press ) <= 0 )
{
mClickType = QQtClick;
}
}
if ( mClickType == QQtLongClick )
{
//计算点击数目
checkClickNumWithCancel();
//修改状态
mClickType = QQtNullClick;
//如果手移开了,那么不发送.
//条件是用户传入了这个Widget为真.
if ( userWidget )
{
p2debug() << userWidget << userWidget->rect() << event->pos() << userWidget->rect().contains ( event->pos() );
if ( !userWidget->rect().contains ( event->pos() ) )
{
p2debug() << "send long click canceled." ;
return;
}
}
checkClickNum ( QQtLongClick );
//发送长信号
p2debug() << "send long click " ;
emit longClick();
emit longClickWithPoint ( event->pos() );
emit longClickWithPointF ( event->localPos() );
#if 0
emit longClick ( userWidget );
emit longClickWithPoint ( event->pos(), userWidget );
emit longClickWithPointF ( event->localPos(), userWidget );
#endif
return;
}
else if ( mClickType == QQtClick )
{
//计算点击数目
checkClickNumWithCancel();
//修改状态
mClickType = QQtNullClick;
//如果手移开了,那么不发送.
//条件是用户传入了这个Widget为真.
if ( userWidget )
{
p2debug() << userWidget << userWidget->rect() << event->pos() << userWidget->rect().contains ( event->pos() );
if ( !userWidget->rect().contains ( event->pos() ) )
{
p2debug() << "send click canceled." ;
return;
}
}
checkClickNum ( QQtClick );
//发送单击信号
p2debug() << "send click." ;
emit click();
emit clickWithPoint ( event->pos() );
emit clickWithPointF ( event->localPos() );
#if 0
emit click ( userWidget );
emit clickWithPoint ( event->pos(), userWidget );
emit clickWithPointF ( event->localPos(), userWidget );
#endif
return;
}
//修改状态
mClickType = QQtNullClick;
//保存位置
mPoint = event->pos();
return;
}
void QQtLongClickHelper::mouseDoubleClickEvent ( QMouseEvent* event, QWidget* userWidget )
{
p2debug() << "double click" << event->pos() << userWidget;
mPoint = event->pos();
}
void QQtLongClickHelper::setLongClickInterval ( int millSecond )
{
mLongClickInterval = millSecond;
}
int QQtLongClickHelper::getLongClickInterval() const
{
return mLongClickInterval;
}
void QQtLongClickHelper::checkClickNumWithCancel()
{
QQtClickHelper::checkClickNumWithCancel();
switch ( mClickType )
{
case QQtLongClick:
{
nLongClickNumWithCancel++;
if ( nLongClickNumWithCancel >= 0xFFFFFFFF )
{
p2debug() << "out......";
nLongClickNumWithCancel = 0;
}
}
break;
default:
break;
}
nTotalClickNumWithCancel = nClickNumWithCancel + nLongClickNumWithCancel;
if ( nTotalClickNumWithCancel >= 0xFFFFFFFFFFFFFFFF )
{
p2debug() << "out......";
nTotalClickNumWithCancel = 0;
}
}
void QQtLongClickHelper::checkClickNum ( QQtLongClickHelper::QQtClickType type )
{
QQtClickHelper::checkClickNum ( type );
switch ( type )
{
case QQtLongClick:
{
nLongClickNum++;
if ( nLongClickNum >= 0xFFFFFFFF )
{
p2debug() << "out......";
nLongClickNum = 0;
}
}
break;
default:
break;
}
nTotalClickNum = nClickNum + nLongClickNum;
if ( nTotalClickNum >= 0xFFFFFFFFFFFFFFFF )
{
p2debug() << "out......";
nTotalClickNum = 0;
}
}

View File

@ -0,0 +1,92 @@
#ifndef QQTLONGCLICKHELPER_H
#define QQTLONGCLICKHELPER_H
#include <qqtclickhelper.h>
#include <qqt-local.h>
#include <qqtcore.h>
/**
* QQtLongClickHelper
* Helper是给Widget响应按钮事件用的
* QQtWidget系列添加按钮信号识别帮助
*
*
* click
* longClick
* DoubleClick
*
*
*/
class QQTSHARED_EXPORT QQtLongClickHelper : public QQtClickHelper
{
Q_OBJECT
public:
explicit QQtLongClickHelper ( QObject* parent = 0 );
virtual ~QQtLongClickHelper();
//发给业务层
//发回界面层
//这些信号不区分Widget,谁发过来都发出去,不论Widget
signals:
void click();
void longClick();
signals:
void clickWithPoint ( QPoint point );
void longClickWithPoint ( QPoint point );
signals:
void clickWithPointF ( QPointF point );
void longClickWithPointF ( QPointF point );
#if 0
//发给业务层
//发回界面层
//这里的信号都带着Widget指针,可以区分Widget
signals:
void longClick ( QWidget* userWidget );
void longClickWithPoint ( QPoint point, QWidget* userWidget );
void longClickWithPointF ( QPointF point, QWidget* userWidget );
#endif
//调用于userWidget相对应的Event里
//允许重写
public:
virtual void mousePressEvent ( QMouseEvent* event, QWidget* userWidget = 0 ) override;
virtual void mouseReleaseEvent ( QMouseEvent* event, QWidget* userWidget = 0 ) override;
virtual void mouseDoubleClickEvent ( QMouseEvent* event, QWidget* userWidget = 0 ) override;
//optional
public:
//设置longClick超时 default:1200ms
void setLongClickInterval ( int millSecond = longClickInterval );
int getLongClickInterval() const;
inline const quint32 longClickNum() const { return nLongClickNum; }
inline const quint32 longClickNumWithCancel() const { return nLongClickNumWithCancel; }
/**
*
*/
protected:
//这个语法比较难,只有整型有特权.
static const int longClickInterval = 1200;
int mLongClickInterval;
//click 检测使用
QTime now_press;//press relase
QTime now_release;//release
//click num
virtual void checkClickNum ( QQtClickType type ) override;
virtual void checkClickNumWithCancel() override;
quint32 nLongClickNum;
quint32 nLongClickNumWithCancel;
};
#endif // QQTLONGCLICKHELPER_H

View File

@ -2,7 +2,7 @@
#define QQTLONGCLICKLABEL_H
#include <qqtlabel.h>
#include <qqtclickhelper.h>
#include <qqtlongclickhelper.h>
#include <qqt-local.h>
#include <qqtcore.h>
@ -19,7 +19,7 @@ public:
explicit QQtLongClickLabel ( QWidget* parent = 0 ) :
QQtLabel ( parent ) {
mClickHelper = 0;
mClickHelper = new QQtClickHelper ( this );
mClickHelper = new QQtLongClickHelper ( this );
installClickHelper ( mClickHelper );
}
virtual ~QQtLongClickLabel() {}

View File

@ -2,7 +2,7 @@
#define QQTLONGCLICKWIDGET_H
#include <qqtwidget.h>
#include <qqtclickhelper.h>
#include <qqtlongclickhelper.h>
#include <qqt-local.h>
#include <qqtcore.h>
@ -19,7 +19,7 @@ public:
explicit QQtLongClickWidget ( QWidget* parent = 0 ) :
QQtWidget ( parent ) {
mClickHelper = 0;
mClickHelper = new QQtClickHelper ( this );
mClickHelper = new QQtLongClickHelper ( this );
installClickHelper ( mClickHelper );
}
virtual ~QQtLongClickWidget() {}

0
src/linux_cd_path.sh Executable file → Normal file
View File

0
src/linux_cp_files.sh Executable file → Normal file
View File

0
src/linux_cur_path.sh Executable file → Normal file
View File

View File

@ -334,9 +334,11 @@ contains (DEFINES, __EXQUISITE__) {
SOURCES += \
$$PWD/exquisite/clickwidgets/qqtclickhelper.cpp \
$$PWD/exquisite/clickwidgets/qqtlongclickhelper.cpp \
$$PWD/exquisite/clickwidgets/qqtdoubleclickhelper.cpp
HEADERS += \
$$PWD/exquisite/clickwidgets/qqtclickhelper.h \
$$PWD/exquisite/clickwidgets/qqtlongclickhelper.h \
$$PWD/exquisite/clickwidgets/qqtdoubleclickhelper.h
SOURCES += \

View File

@ -18,7 +18,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>2</number>
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
@ -90,7 +90,7 @@
<x>0</x>
<y>0</y>
<width>816</width>
<height>39</height>
<height>17</height>
</rect>
</property>
</widget>