| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- #include "BmpButton.h"
- #include <QDebug>
- #include <QPainter>
- #include <QFile>
- #include <QScopedPointer>
- #include <QEvent>
- #include <QTimer>
- class BmpButtonPrivate
- {
- Q_DISABLE_COPY(BmpButtonPrivate)
- public:
- explicit BmpButtonPrivate(BmpButton* parent);
- ~BmpButtonPrivate();
- void initializeTimer();
- QScopedPointer<QPixmap> m_NormalPixmap;
- QString m_NormalPath;
- QScopedPointer<QPixmap> m_PressPixmap;
- QString m_PressPath;
- QScopedPointer<QPixmap> m_CheckPixmap;
- QString m_CheckPath;
- BmpButton::ButtonStatus m_Status;
- BmpButton::Type m_LanguageType;
- QTimer* m_Timer;
- bool m_Filter;
- bool m_LongPressRestore;
- bool m_Reload;
- QList<BmpButton::CustomString> m_List;
- private:
- BmpButton* m_Parent;
- };
- BmpButton::BmpButton(QWidget *parent)
- : QAbstractButton(parent)
- , m_Private(new BmpButtonPrivate(this))
- {
- }
- BmpButton::~BmpButton()
- {
- }
- void BmpButton::setNormalBmpPath(const QString &path)
- {
- if (QFile::exists(path)) {
- m_Private->m_NormalPath = path;
- m_Private->m_NormalPixmap.reset(new QPixmap(path));
- m_Private->m_Reload = false;
- if (isVisible()) {
- update();
- }
- }
- }
- void BmpButton::setPressBmpPath(const QString &path)
- {
- if (QFile::exists(path)) {
- m_Private->m_PressPath = path;
- m_Private->m_PressPixmap.reset(new QPixmap(path));
- m_Private->m_Reload = false;
- if (isVisible()) {
- update();
- }
- }
- }
- void BmpButton::setCheckBmpPath(const QString &path)
- {
- if (QFile::exists(path)) {
- m_Private->m_CheckPath = path;
- m_Private->m_CheckPixmap.reset(new QPixmap(path));
- m_Private->m_Reload = false;
- if (isVisible()) {
- update();
- }
- }
- }
- void BmpButton::setStatus(const BmpButton::ButtonStatus &status)
- {
- if (status != m_Private->m_Status) {
- m_Private->m_Status = status;
- if (isVisible()) {
- update();
- }
- }
- }
- BmpButton::ButtonStatus BmpButton::getStatus()
- {
- return m_Private->m_Status;
- }
- void BmpButton::enableLongPress(const bool flag)
- {
- if (flag) {
- m_Private->initializeTimer();
- }
- }
- void BmpButton::enableLongPressRestore(const bool flag)
- {
- m_Private->m_LongPressRestore = flag;
- }
- void BmpButton::setLanguageType(const BmpButton::Type type)
- {
- m_Private->m_LanguageType = type;
- }
- void BmpButton::setCustomText(const QList<BmpButton::CustomString> &text)
- {
- m_Private->m_List = text;
- if (isVisible()) {
- update();
- }
- }
- bool BmpButton::event(QEvent *event)
- {
- switch (event->type()) {
- case QEvent::MouseButtonPress:
- case QEvent::MouseButtonRelease:
- case QEvent::MouseMove: {
- event->ignore();
- break;
- }
- default: {
- break;
- }
- }
- return QAbstractButton::event(event);
- }
- void BmpButton::styleChange()
- {
- m_Private->m_Reload = true;
- }
- void BmpButton::mousePressEvent(QMouseEvent *event)
- {
- Q_UNUSED(event);
- m_Private->m_Status = BmpButton::B_Press;
- if (isVisible()) {
- update();
- }
- m_Private->m_Filter = false;
- if (NULL != m_Private->m_Timer) {
- m_Private->m_Timer->start();
- }
- // emit bmpButtomPress();
- // QWidget::mousePressEvent(event);
- }
- void BmpButton::mouseReleaseEvent(QMouseEvent *event)
- {
- Q_UNUSED(event);
- if (NULL != m_Private->m_Timer) {
- m_Private->m_Timer->stop();
- }
- if (isVisible()) {
- if ((NULL == m_Private->m_Timer)
- || (!m_Private->m_Filter)) {
- m_Private->m_Status = BmpButton::B_Normal;
- update();
- emit released();
- }
- //emit bmpButtonRelease();
- }
- //QWidget::mouseReleaseEvent(event);
- }
- void BmpButton::paintEvent(QPaintEvent *event)
- {
- Q_UNUSED(event);
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing, true);
- if (m_Private->m_Reload) {
- setNormalBmpPath(m_Private->m_NormalPath);
- setPressBmpPath(m_Private->m_PressPath);
- setCheckBmpPath(m_Private->m_CheckPath);
- }
- switch (m_Private->m_Status) {
- case BmpButton::B_Check: {
- checkPaint(painter);
- break;
- }
- case BmpButton::B_Press: {
- pressPaint(painter);
- break;
- }
- case BmpButton::B_Normal:
- default : {
- normalPaint(painter);
- break;
- }
- }
- painter.setPen(Qt::white);
- if (0 != m_Private->m_List.size()) {
- QString text;
- for (int i = 0; i < m_Private->m_List.size(); ++i) {
- if ((BmpButton::T_Translate == m_Private->m_LanguageType)
- && (m_Private->m_List.at(i).translate)) {
- text += QObject::tr(m_Private->m_List.at(i).string.toLocal8Bit().constData());
- } else {
- text += m_Private->m_List.at(i).string;
- }
- }
- painter.drawText(rect(), Qt::AlignCenter, text);
- } else if (!text().isEmpty()) {
- if (BmpButton::T_Translate == m_Private->m_LanguageType) {
- painter.drawText(rect(), Qt::AlignCenter, QObject::tr(text().toLocal8Bit().constData()));
- } else {
- painter.drawText(rect(), Qt::AlignCenter, text());
- }
- }
- }
- void BmpButton::normalPaint(QPainter &painter)
- {
- if (NULL != m_Private->m_NormalPixmap) {
- int x = (width() - m_Private->m_NormalPixmap->width()) >> 1;
- int y = (height() - m_Private->m_NormalPixmap->height()) >> 1;
- painter.drawPixmap(x, y, *m_Private->m_NormalPixmap);
- }
- }
- void BmpButton::pressPaint(QPainter &painter)
- {
- if (NULL != m_Private->m_PressPixmap) {
- int x = (width() - m_Private->m_PressPixmap->width()) >> 1;
- int y = (height() - m_Private->m_PressPixmap->height()) >> 1;
- painter.drawPixmap(x, y, *m_Private->m_PressPixmap);
- }
- }
- void BmpButton::checkPaint(QPainter &painter)
- {
- if (NULL != m_Private->m_CheckPixmap) {
- int x = (width() - m_Private->m_CheckPixmap->width()) >> 1;
- int y = (height() - m_Private->m_CheckPixmap->height()) >> 1;
- painter.drawPixmap(x, y, *m_Private->m_CheckPixmap);
- }
- }
- void BmpButton::onTimeout()
- {
- if (isVisible()) {
- m_Private->m_Filter = true;
- emit longPressed();
- }
- if (m_Private->m_LongPressRestore) {
- m_Private->m_Status = BmpButton::B_Normal;
- }
- if (isVisible()) {
- update();
- }
- }
- BmpButtonPrivate::BmpButtonPrivate(BmpButton* parent)
- : m_Parent(parent)
- {
- m_Status = BmpButton::B_Normal;
- m_LanguageType = BmpButton::T_Translate;
- m_Timer = NULL;
- m_Filter = false;
- m_LongPressRestore = true;
- m_Reload = false;
- m_List.clear();
- }
- BmpButtonPrivate::~BmpButtonPrivate()
- {
- }
- void BmpButtonPrivate::initializeTimer()
- {
- if (NULL == m_Timer) {
- m_Timer = new QTimer(m_Parent);
- m_Timer->setSingleShot(true);
- m_Timer->setInterval(1500);
- QObject::connect(m_Timer, SIGNAL(timeout()),
- m_Parent, SLOT(onTimeout()));
- }
- }
|