qtmaterialiconbutton.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "qtmaterialiconbutton.h"
  2. #include "qtmaterialiconbutton_p.h"
  3. #include <QPainter>
  4. #include <QEvent>
  5. #include "lib/qtmaterialstyle.h"
  6. #include "lib/qtmaterialrippleoverlay.h"
  7. /*!
  8. * \class QtMaterialIconButtonPrivate
  9. * \internal
  10. */
  11. QtMaterialIconButtonPrivate::QtMaterialIconButtonPrivate(QtMaterialIconButton *q)
  12. : q_ptr(q)
  13. {
  14. }
  15. QtMaterialIconButtonPrivate::~QtMaterialIconButtonPrivate()
  16. {
  17. }
  18. void QtMaterialIconButtonPrivate::init()
  19. {
  20. Q_Q(QtMaterialIconButton);
  21. rippleOverlay = new QtMaterialRippleOverlay(q->parentWidget());
  22. useThemeColors = true;
  23. rippleOverlay->installEventFilter(q);
  24. q->setStyle(&QtMaterialStyle::instance());
  25. QSizePolicy policy;
  26. policy.setWidthForHeight(true);
  27. q->setSizePolicy(policy);
  28. }
  29. void QtMaterialIconButtonPrivate::updateRipple()
  30. {
  31. Q_Q(QtMaterialIconButton);
  32. QRect r(q->rect());
  33. r.setSize(QSize(q->width()*2, q->height()*2));
  34. r.moveCenter(q->geometry().center());
  35. rippleOverlay->setGeometry(r);
  36. }
  37. /*!
  38. * \class QtMaterialIconButton
  39. */
  40. QtMaterialIconButton::QtMaterialIconButton(const QIcon &icon, QWidget *parent)
  41. : QAbstractButton(parent),
  42. d_ptr(new QtMaterialIconButtonPrivate(this))
  43. {
  44. d_func()->init();
  45. setIcon(icon);
  46. }
  47. QtMaterialIconButton::~QtMaterialIconButton()
  48. {
  49. }
  50. /*!
  51. * \reimp
  52. */
  53. QSize QtMaterialIconButton::sizeHint() const
  54. {
  55. return iconSize();
  56. }
  57. void QtMaterialIconButton::setUseThemeColors(bool value)
  58. {
  59. Q_D(QtMaterialIconButton);
  60. if (d->useThemeColors == value) {
  61. return;
  62. }
  63. d->useThemeColors = value;
  64. update();
  65. }
  66. bool QtMaterialIconButton::useThemeColors() const
  67. {
  68. Q_D(const QtMaterialIconButton);
  69. return d->useThemeColors;
  70. }
  71. void QtMaterialIconButton::setColor(const QColor &color)
  72. {
  73. Q_D(QtMaterialIconButton);
  74. d->color = color;
  75. MATERIAL_DISABLE_THEME_COLORS
  76. update();
  77. }
  78. QColor QtMaterialIconButton::color() const
  79. {
  80. Q_D(const QtMaterialIconButton);
  81. if (d->useThemeColors || !d->color.isValid()) {
  82. return QtMaterialStyle::instance().themeColor("text");
  83. }
  84. return d->color;
  85. }
  86. void QtMaterialIconButton::setDisabledColor(const QColor &color)
  87. {
  88. Q_D(QtMaterialIconButton);
  89. d->disabledColor = color;
  90. MATERIAL_DISABLE_THEME_COLORS
  91. update();
  92. }
  93. QColor QtMaterialIconButton::disabledColor() const
  94. {
  95. Q_D(const QtMaterialIconButton);
  96. if (d->useThemeColors || !d->disabledColor.isValid()) {
  97. return QtMaterialStyle::instance().themeColor("disabled");
  98. }
  99. return d->disabledColor;
  100. }
  101. QtMaterialIconButton::QtMaterialIconButton(QtMaterialIconButtonPrivate &d, QWidget *parent)
  102. : QAbstractButton(parent),
  103. d_ptr(&d)
  104. {
  105. d_func()->init();
  106. }
  107. /*!
  108. * \reimp
  109. */
  110. bool QtMaterialIconButton::event(QEvent *event)
  111. {
  112. Q_D(QtMaterialIconButton);
  113. switch (event->type())
  114. {
  115. case QEvent::Move:
  116. case QEvent::Resize:
  117. d->updateRipple();
  118. break;
  119. case QEvent::ParentChange: {
  120. QWidget *widget;
  121. if ((widget = parentWidget())) {
  122. d->rippleOverlay->setParent(widget);
  123. }
  124. break;
  125. }
  126. default:
  127. break;
  128. }
  129. return QAbstractButton::event(event);
  130. }
  131. /*!
  132. * \reimp
  133. */
  134. bool QtMaterialIconButton::eventFilter(QObject *obj, QEvent *event)
  135. {
  136. if (QEvent::Resize == event->type())
  137. {
  138. Q_D(QtMaterialIconButton);
  139. d->updateRipple();
  140. }
  141. return QAbstractButton::eventFilter(obj, event);
  142. }
  143. /*!
  144. * \reimp
  145. */
  146. void QtMaterialIconButton::mousePressEvent(QMouseEvent *event)
  147. {
  148. Q_D(QtMaterialIconButton);
  149. d->rippleOverlay->addRipple(QPoint(d->rippleOverlay->width(),
  150. d->rippleOverlay->height())/2,
  151. iconSize().width());
  152. emit clicked();
  153. QAbstractButton::mousePressEvent(event);
  154. }
  155. /*!
  156. * \reimp
  157. */
  158. void QtMaterialIconButton::paintEvent(QPaintEvent *event)
  159. {
  160. Q_UNUSED(event)
  161. QPainter painter(this);
  162. QPixmap pixmap = icon().pixmap(iconSize());
  163. QPainter icon(&pixmap);
  164. icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
  165. icon.fillRect(pixmap.rect(), isEnabled() ? color() : disabledColor());
  166. QRect r(rect());
  167. const qreal w = pixmap.width();
  168. const qreal h = pixmap.height();
  169. painter.drawPixmap(QRect((r.width()-w)/2, (r.height()-h)/2, w, h), pixmap);
  170. }