| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #include "xtoolbutton.h"
- XToolButton::XToolButton(QWidget *parent) :
- QWidget(parent)
- {
- initVariable();
- initSetupUi();
- }
- XToolButton::~XToolButton()
- {
- delete m_pLabelIcon;
- delete m_pLabelText;
- delete m_pLayout;
- }
- void XToolButton::setPixmap(const QPixmap &pixmap)
- {
- m_pLabelIcon->setPixmap(pixmap.scaled(QSize(48, 48), Qt::KeepAspectRatio, Qt::SmoothTransformation));
- }
- void XToolButton::setText(const QString &text)
- {
- m_pLabelText->setText(text);
- }
- void XToolButton::setMouseEnterFlag(bool flag)
- {
- m_mouseEnterFlag = flag;
- this->update();
- }
- void XToolButton::setMousePressFlag(bool flag)
- {
- m_mousePressFlag = flag;
- this->update();
- }
- void XToolButton::enterEvent(QEvent *e)
- {
- if (!getMousePressFlag())
- {
- setMouseEnterFlag(true);
- }
- this->setCursor(Qt::PointingHandCursor);
- e->ignore();
- }
- void XToolButton::leaveEvent(QEvent *e)
- {
- setMouseEnterFlag(false);
- e->ignore();
- }
- void XToolButton::mousePressEvent(QMouseEvent *e)
- {
- if (e->button() == Qt::LeftButton)
- {
- setMousePressFlag(true);
- emit signalLabelPress(this);
- }
- e->ignore();
- }
- void XToolButton::paintEvent(QPaintEvent *e)
- {
- QPainter painter(this);
- if (getMouseEnterFlag())
- {
- paintWidget(50, &painter);
- }
- else if (getMousePressFlag())
- {
- paintWidget(80, &painter);
- }
- QWidget::paintEvent(e);
- }
- void XToolButton::initVariable()
- {
- setMouseEnterFlag(false);
- setMousePressFlag(false);
- }
- void XToolButton::initSetupUi()
- {
- createFrame();
- createWidget();
- createLayout();
- }
- void XToolButton::createFrame()
- {
- this->setStyleSheet("QWidget {background:transparent;border:0px;color:white;font-size:12px;width:40px;height:48px}");
- }
- void XToolButton::createWidget()
- {
- m_pLabelIcon = new QLabel(this);
- m_pLabelIcon->setAlignment(Qt::AlignCenter);
- m_pLabelText = new QLabel(this);
- m_pLabelText->setAlignment(Qt::AlignCenter);
- // setMinimumSize(70,70);
- // setMaximumSize(70,70);
- setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
- }
- void XToolButton::createLayout()
- {
- m_pLayout = new QVBoxLayout(this);
- m_pLayout->addWidget(m_pLabelIcon);
- m_pLayout->addWidget(m_pLabelText);
- m_pLayout->setContentsMargins(20,10,20,10);
- setLayout(m_pLayout);
- }
- void XToolButton::paintWidget(int transparency, QPainter *device)
- {
- // QPen pen(Qt::NoBrush, 1);
- // device->setPen(pen);
- // QLinearGradient linear(this->rect().topLeft(), this->rect().bottomLeft());
- // linear.setColorAt(0,QColor(255,255,255,transparency));
- // linear.setColorAt(0.5,QColor(200,200,200,transparency));
- // linear.setColorAt(1,QColor(0,0,0,transparency));
- // QBrush brush(linear);
- // device->setBrush(brush);
- // device->drawRoundedRect(this->rect(), 2, 2);/**/
- QPainter painter(this);
- QPen pen(Qt::NoBrush, 1);
- painter.setPen(pen);
- QLinearGradient linear(rect().topLeft(), rect().bottomLeft());
- linear.setColorAt(0, QColor(230, 230, 230, 0));
- linear.setColorAt(0.5, QColor(230, 230, 230, 100));
- linear.setColorAt(1, QColor(230, 230, 230, 150));
- painter.setBrush(linear);
- painter.drawRect(rect());
- }
- inline bool XToolButton::getMouseEnterFlag()
- {
- return m_mouseEnterFlag;
- }
- inline bool XToolButton::getMousePressFlag()
- {
- return m_mousePressFlag;
- }
|