xtoolbutton.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "xtoolbutton.h"
  2. XToolButton::XToolButton(QWidget *parent) :
  3. QWidget(parent)
  4. {
  5. initVariable();
  6. initSetupUi();
  7. }
  8. XToolButton::~XToolButton()
  9. {
  10. delete m_pLabelIcon;
  11. delete m_pLabelText;
  12. delete m_pLayout;
  13. }
  14. void XToolButton::setPixmap(const QPixmap &pixmap)
  15. {
  16. m_pLabelIcon->setPixmap(pixmap.scaled(QSize(48, 48), Qt::KeepAspectRatio, Qt::SmoothTransformation));
  17. }
  18. void XToolButton::setText(const QString &text)
  19. {
  20. m_pLabelText->setText(text);
  21. }
  22. void XToolButton::setMouseEnterFlag(bool flag)
  23. {
  24. m_mouseEnterFlag = flag;
  25. this->update();
  26. }
  27. void XToolButton::setMousePressFlag(bool flag)
  28. {
  29. m_mousePressFlag = flag;
  30. this->update();
  31. }
  32. void XToolButton::enterEvent(QEvent *e)
  33. {
  34. if (!getMousePressFlag())
  35. {
  36. setMouseEnterFlag(true);
  37. }
  38. this->setCursor(Qt::PointingHandCursor);
  39. e->ignore();
  40. }
  41. void XToolButton::leaveEvent(QEvent *e)
  42. {
  43. setMouseEnterFlag(false);
  44. e->ignore();
  45. }
  46. void XToolButton::mousePressEvent(QMouseEvent *e)
  47. {
  48. if (e->button() == Qt::LeftButton)
  49. {
  50. setMousePressFlag(true);
  51. emit signalLabelPress(this);
  52. }
  53. e->ignore();
  54. }
  55. void XToolButton::paintEvent(QPaintEvent *e)
  56. {
  57. QPainter painter(this);
  58. if (getMouseEnterFlag())
  59. {
  60. paintWidget(50, &painter);
  61. }
  62. else if (getMousePressFlag())
  63. {
  64. paintWidget(80, &painter);
  65. }
  66. QWidget::paintEvent(e);
  67. }
  68. void XToolButton::initVariable()
  69. {
  70. setMouseEnterFlag(false);
  71. setMousePressFlag(false);
  72. }
  73. void XToolButton::initSetupUi()
  74. {
  75. createFrame();
  76. createWidget();
  77. createLayout();
  78. }
  79. void XToolButton::createFrame()
  80. {
  81. this->setStyleSheet("QWidget {background:transparent;border:0px;color:white;font-size:12px;width:40px;height:48px}");
  82. }
  83. void XToolButton::createWidget()
  84. {
  85. m_pLabelIcon = new QLabel(this);
  86. m_pLabelIcon->setAlignment(Qt::AlignCenter);
  87. m_pLabelText = new QLabel(this);
  88. m_pLabelText->setAlignment(Qt::AlignCenter);
  89. // setMinimumSize(70,70);
  90. // setMaximumSize(70,70);
  91. setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  92. }
  93. void XToolButton::createLayout()
  94. {
  95. m_pLayout = new QVBoxLayout(this);
  96. m_pLayout->addWidget(m_pLabelIcon);
  97. m_pLayout->addWidget(m_pLabelText);
  98. m_pLayout->setContentsMargins(20,10,20,10);
  99. setLayout(m_pLayout);
  100. }
  101. void XToolButton::paintWidget(int transparency, QPainter *device)
  102. {
  103. // QPen pen(Qt::NoBrush, 1);
  104. // device->setPen(pen);
  105. // QLinearGradient linear(this->rect().topLeft(), this->rect().bottomLeft());
  106. // linear.setColorAt(0,QColor(255,255,255,transparency));
  107. // linear.setColorAt(0.5,QColor(200,200,200,transparency));
  108. // linear.setColorAt(1,QColor(0,0,0,transparency));
  109. // QBrush brush(linear);
  110. // device->setBrush(brush);
  111. // device->drawRoundedRect(this->rect(), 2, 2);/**/
  112. QPainter painter(this);
  113. QPen pen(Qt::NoBrush, 1);
  114. painter.setPen(pen);
  115. QLinearGradient linear(rect().topLeft(), rect().bottomLeft());
  116. linear.setColorAt(0, QColor(230, 230, 230, 0));
  117. linear.setColorAt(0.5, QColor(230, 230, 230, 100));
  118. linear.setColorAt(1, QColor(230, 230, 230, 150));
  119. painter.setBrush(linear);
  120. painter.drawRect(rect());
  121. }
  122. inline bool XToolButton::getMouseEnterFlag()
  123. {
  124. return m_mouseEnterFlag;
  125. }
  126. inline bool XToolButton::getMousePressFlag()
  127. {
  128. return m_mousePressFlag;
  129. }