#include "NGIPushButton.h"
#include <QStylePainter>
#include <QStyleOptionButton>
#include <FrameWork/Dltlog.h>
NGIPushButton::NGIPushButton(QWidget *parent)
: QPushButton(parent)
, m_midFlag(false)
, m_down(false)
, m_iconRect(QRect())
, m_iconPixmap(QPixmap())
, m_textRect(QRect())
, m_strText(QString())
, m_strImg(QString())
, m_strFont(QString("15px"))
, m_strNormalColor(QString("rgb(255, 255, 255)"))
, m_strPressColor(QString("rgb(255, 255, 255)"))
, m_strDisColor(QString("rgb(51, 51, 51)"))
, m_strSelectedColor(QString("rgb(0, 255, 255)"))
, m_strPath(QString())
, m_strFormat(QString())
, m_bLongPressed(false)
, m_eTextOrientation(Qt::Horizontal)
{
init();
}
NGIPushButton::~NGIPushButton()
{
}
void NGIPushButton::setIcon(const QRect &rect, const QString &iconPath)
{
m_iconRect = rect;
m_iconPixmap = QPixmap(iconPath);
update();
}
void NGIPushButton::setIcon(const QIcon &icon)
{
QPushButton::setIcon(icon);
}
void NGIPushButton::setText(const QRect &rect, const QString &text, bool midFlag, Qt::Orientation eOrientation)
{
m_textRect = rect;
m_strText = text;
m_midFlag = midFlag;
m_eTextOrientation = eOrientation;
update();
}
void NGIPushButton::setText(const QString &text)
{
m_strText = text;
QPushButton::setText(text);
}
void NGIPushButton::setFont(const QFont &font)
{
QPushButton::setFont(font);
}
void NGIPushButton::setBackground(const QString &strStyle)
{
int index = strStyle.lastIndexOf(".");
if (-1 == index)
{
return;
}
m_strImg = strStyle;
m_strPath = strStyle.mid(0, index);
m_strFormat = strStyle.mid(index, strStyle.size() - 1);
setBackgroundStyle();
}
void NGIPushButton::setFont(const QString &strFontSize,
const QString &strNormalColor,
const QString &strPressedColor,
const QString &strDisabledColor,
const QString &strSelectedColor)
{
m_strNormalColor = strNormalColor;
m_strPressColor = strPressedColor;
m_strDisColor = strDisabledColor;
m_strSelectedColor = strSelectedColor;
m_strFont = strFontSize;
setBackgroundStyle();
}
bool NGIPushButton::event(QEvent * event)
{
if (isEnabled())
{
switch (event->type())
{
case QEvent::TouchBegin:
touchBeginEvent((QTouchEvent*)event);
break;
case QEvent::TouchUpdate:
break;
case QEvent::TouchEnd:
touchEndEvent((QTouchEvent*)event);
break;
case QEvent::Gesture:
gestureEvent(static_cast<QGestureEvent*>(event));
break;
default:
break;
}
}
return QPushButton::event(event);
}
void NGIPushButton::paintEvent(QPaintEvent *event)
{
QPushButton::paintEvent(event);
QPainter painter(this);
painter.drawPixmap(m_iconRect, m_iconPixmap);
//------ set text color ------
if (isChecked())
{
painter.setPen(getColor(m_strSelectedColor));
}
else if (!isEnabled())
{
painter.setPen(getColor(m_strDisColor));
}
else
{
painter.setPen(getColor(m_strNormalColor));
}
//------ set text size ------
if (this->font().pixelSize() > 0)
{
painter.setFont(this->font());
}
else
{
QFont font;
QString qsFont = m_strFont;
font.setPixelSize(qsFont.remove("px").toInt());
painter.setFont(font);
}
//------ draw text ------
if (Qt::Horizontal == m_eTextOrientation)
{
if (!m_midFlag)
{
painter.drawText(m_textRect, m_strText);
}
else
{
painter.drawText(m_textRect, Qt::AlignCenter, m_strText);
}
}
else
{
QFontMetrics fontMet(this->font());
int fontHeight = fontMet.boundingRect(m_strText).height();
int spacing = (m_textRect.height() - m_strText.count()*fontHeight)/(m_strText.count()-1);
for (int i = 0; i < m_strText.count(); i++)
{
painter.drawText(QRect(m_textRect.left(),
m_textRect.top() + i * (fontHeight + spacing),
m_textRect.width(),
fontHeight),
m_strText.at(i));
}
}
}
bool NGIPushButton::touchBeginEvent(QTouchEvent* event)
{
QPointF currentPoint = event->touchPoints().at(0).pos();
if (hitTargetButton(currentPoint))
{
m_down = true;
emit touchActionPressed();
event->accept();
}
else
{
event->ignore();
}
return true;
}
bool NGIPushButton::touchEndEvent(QTouchEvent* event)
{
if (!m_down)
{
update();
event->ignore();
return false;
}
QPointF currentPoint = event->touchPoints().at(0).pos();
if (hitTargetButton(currentPoint))
{
m_down = false;
nextCheckState();
emit touchActionReleased();
if (m_bLongPressed)
{
m_bLongPressed = false;
emit longReleased();
}
else
{
emit touchActionClicked();
DEBUG("[NGIPushButton::touchEndEvent]: emit touchActionClicked()");
}
event->accept();
}
else
{
m_down = false;
event->ignore();
}
return true;
}
bool NGIPushButton::gestureEvent(QGestureEvent *event)
{
// if (QGesture* Tap = event->gesture((Qt::GestureType)GM::GMTap))
// {
// tapTriggered(static_cast<GMTapGesture*>(Tap));
// return true;
// }
if (QGesture *longPress = event->gesture((Qt::GestureType)GM::GMPress))
{
pressTriggered(static_cast<GMPressGesture*>(longPress));
return true;
}
return false;
}
void NGIPushButton::tapTriggered(GMTapGesture *pTap)
{
switch (pTap->state())
{
case Qt::GestureStarted:
break;
case Qt::GestureUpdated:
break;
case Qt::GestureFinished:
break;
case Qt::GestureCanceled:
break;
default:
break;
}
}
void NGIPushButton::pressTriggered(GMPressGesture *pPress)
{
switch (pPress->state())
{
case Qt::GestureStarted:
if (!m_bLongPressed)
{
emit longPressed();
m_bLongPressed = true;
DEBUG("[NGIPushButton::pressTriggered]: emit longPressed()");
}
break;
case Qt::GestureUpdated:
break;
case Qt::GestureFinished:
break;
case Qt::GestureCanceled:
break;
default:
break;
}
}
bool NGIPushButton::hitTargetButton(QPointF& point)
{
return rect().contains(point.toPoint());;
}
void NGIPushButton::init()
{
grabGesture(Qt::TapAndHoldGesture);
setAttribute(Qt::WA_AcceptTouchEvents);
grabGesture((Qt::GestureType)GM::GMTap);
grabGesture((Qt::GestureType)GM::GMPress);
}
void NGIPushButton::setBackgroundStyle()
{
QString strBorderStyle = QString();
QString strColorStyle = QString();
QString strPressedColor = QString();
QString strDisabledColor = QString();
QString strSelectedColor = QString();
QString strFontStyle = QString();
QString strNormalStyle = QString();
QString strPressedStyle = QString();
QString strSelectedStyle = QString();
QString strDisabledStyle = QString();
QString strPressImg = m_strPath + "_Pushed" + m_strFormat;
QString strSelectImg = m_strPath + "_Selected" + m_strFormat;
QString strDisabledImg = m_strPath + "_Inactive" + m_strFormat;
strColorStyle = "color: " + m_strNormalColor + ";";
strFontStyle = "font:" + m_strFont + ";";
strDisabledColor = "color: " + m_strDisColor + ";";
strPressedColor = "color: " + m_strPressColor + ";";
strSelectedColor = "color: " + m_strSelectedColor + ";";
if (
hgp.zip_qt 手势
版权申诉
146 浏览量
2022-09-14
22:45:10
上传
评论
收藏 3KB ZIP 举报
林当时
- 粉丝: 103
- 资源: 1万+
最新资源
- 简单小游戏制作-飞行棋
- Python数学建模例子.rar
- Java项目-基于SSM+JSP的校园外卖配送系统的设计与实现(源码+万字LW+部署视频+代码讲解视频+全套软件)
- Color-Transformer introduction
- FastStone Capture屏幕长截图软件包
- Table IoT物联网工具,简单快速的搭建物联网服务平台
- zheng2020 ecg new dataset-12 lead-add-label
- """YOLOv5-specific modules Usage: $ python path/to/models/y
- onnx-while-test.cpython-37
- 基于MapReduce的招聘数据清洗项目(免费提供源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈