//cccpushbutton.h #ifndef CCCPUSHBUTTON_H #define CCCPUSHBUTTON_H #include#include #include #include #include #include #include #include #define ANIMATION_TIME 300 class CccPushButton : public QPushButton { Q_OBJECT Q_PROPERTY(qreal o1 READ getO1 WRITE setO1) Q_PROPERTY(qreal o2 READ getO2 WRITE setO2) Q_PROPERTY(qreal o3 READ getO3 WRITE setO3) public: explicit CccPushButton(QWidget *parent=0); explicit CccPushButton(const QString &resName, const QSize &size, QWidget *parent = 0 ); qreal getO1() { return o1;} qreal getO2() { return o2;} qreal getO3() { return o3;} void setO1(qreal v){o1 = v;} void setO2(qreal v){o2 = v;} void setO3(qreal v){o3 = v;} void setResName(const QString &resName, const QSize &size,const QString& strTooltip=""); void setResName(const QString &resName, int statusCount = 4); void setResName(const QString& resName, const QString& toolTip, int statusCount = 4); void updateResName(const QString& resName, const QString& toolTip); void CreateStateMachine(); QSize GetSize(){return size;} ~CccPushButton(); public: protected: void paintEvent(QPaintEvent *); private: qreal o1,o2,o3; QString resName; QSize size; signals: public slots: void onAnimationChanged(const QVariant &); }; #endif // CCCPUSHBUTTON_H
cccpushbutton.cpp
#include "cccpushbutton.h"
CccPushButton::CccPushButton(QWidget *parent):
QPushButton(parent)
{
setFlat(true);
setStyleSheet("background-color: rgb(255, 255, 255, 0);");
CreateStateMachine();
setCursor(Qt::PointingHandCursor);
}
CccPushButton::CccPushButton(const QString &resName, const QSize &size, QWidget *parent) :
QPushButton(parent)
{
setFixedSize(size);
this->size = size;
this->resName = resName;
setFlat(true);
setStyleSheet("background-color: rgb(255, 255, 255, 0);");
CreateStateMachine();
setCursor(Qt::PointingHandCursor);
}
void CccPushButton::setResName(const QString &resName, const QSize &size,const QString& strTooltip)
{
setFixedSize(size);
this->size = size;
this->resName = resName;
if(!strTooltip.isEmpty())
this->setToolTip(strTooltip);
update();
}
void CccPushButton::setResName(const QString &resName, int statusCount)
{
QPixmap pixmap(resName);
QSize size(pixmap.size().width()/statusCount,pixmap.size().height());
setFixedSize(size);
this->size = size;
this->resName = resName;
update();
}
void CccPushButton::setResName(const QString& resName, const QString& toolTip, int statusCount)
{
QPixmap pixmap(resName);
QSize size(pixmap.size().width()/statusCount,pixmap.size().height());
setFixedSize(size);
this->size = size;
this->resName = resName;
this->setToolTip(toolTip);
update();
}
void CccPushButton::updateResName(const QString& resName, const QString& toolTip)
{
if (this->resName.isEmpty())
return;
this->resName = resName;
this->setToolTip(toolTip);
update();
}
void CccPushButton::CreateStateMachine()
{
setO1(1.0);
setProperty("o2", 0.0);
setProperty("o3", 0.0);
QStateMachine *machine = new QStateMachine(this);
QState *state1 = new QState(machine);
state1->assignProperty(this,"o1",1.0);
state1->assignProperty(this,"o2",0.0);
state1->assignProperty(this,"o3",0.0);
QState *state2 = new QState(machine);
state2->assignProperty(this,"o1",0.0);
state2->assignProperty(this,"o2",1.0);
state2->assignProperty(this,"o3",0.0);
QState *state3 = new QState(machine);
state3->assignProperty(this,"o1",0.0);
state3->assignProperty(this,"o2",0.0);
state3->assignProperty(this,"o3",1.0);
QEventTransition *t1_2 = new QEventTransition(this, QEvent::Enter);
t1_2->setTargetState(state2); QEventTransition *t2_1 = new QEventTransition(this, QEvent::Leave); t2_1->setTargetState(state1); QEventTransition *t2_3 = new QEventTransition(this, QEvent::MouseButtonPress); t2_3->setTargetState(state3); QEventTransition *t3_2 = new QEventTransition(this, QEvent::MouseButtonRelease); t3_2->setTargetState(state2);
state1->addTransition(t1_2);
state2->addTransition(t2_1);
state2->addTransition(t2_3);
state3->addTransition(t3_2);
machine->setInitialState(state1);
machine->start();
}
CccPushButton::~CccPushButton() { } void CccPushButton::paintEvent(QPaintEvent *e) {
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.setOpacity(getO1());
p.drawPixmap(0, 0, this->width(), this->height(), QPixmap(resName), 0, 0, size.width(), size.height());
p.setOpacity(getO2());
p.drawPixmap(0, 0, this->width(), this->height(), QPixmap(resName), size.width(), 0, size.width(), size.height());
p.setOpacity(getO3());
p.drawPixmap(0, 0, this->width(), this->height(), QPixmap(resName), (size.width())*2, 0, size.width(), size.height());
QPushButton::paintEvent(e);
}
void CccPushButton::onAnimationChanged(const QVariant &)
{
update();
}