Button.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "Button.h"
  2. #include "Common/StyleSheet.h"
  3. #include "Common/Icon.h"
  4. #include "QFluentWidgets.h"
  5. #include <QDesktopServices>
  6. #include <QPainter>
  7. PushButton::PushButton(const QString &text, QWidget *parent) : PushButton(text, nullptr, parent) { }
  8. PushButton::PushButton(const QString &text, FluentIconBase *ficon, QWidget *parent)
  9. : QPushButton(text, parent), m_isPressed(false), m_ficon(ficon)
  10. {
  11. FluentStyleSheet::apply("BUTTON", this);
  12. if (m_ficon.isNull()) {
  13. setProperty("hasIcon", false);
  14. } else {
  15. if (ficon->icon().isNull()) {
  16. setProperty("hasIcon", false);
  17. } else {
  18. setProperty("hasIcon", true);
  19. }
  20. }
  21. setIconSize(QSize(16, 16));
  22. }
  23. void PushButton::setIcon(FluentIconBase *ficon)
  24. {
  25. m_ficon.reset(ficon);
  26. update();
  27. }
  28. FluentIconBase *PushButton::ficon() const
  29. {
  30. return m_ficon.data();
  31. }
  32. QIcon PushButton::icon() const
  33. {
  34. if (!m_ficon) {
  35. return QIcon();
  36. }
  37. return m_ficon->icon();
  38. }
  39. void PushButton::drawIcon(QPainter *painter, const QRect &rect)
  40. {
  41. if (m_ficon) {
  42. m_ficon->render(painter, rect);
  43. }
  44. }
  45. void PushButton::mousePressEvent(QMouseEvent *event)
  46. {
  47. m_isPressed = true;
  48. QPushButton::mousePressEvent(event);
  49. }
  50. void PushButton::mouseReleaseEvent(QMouseEvent *event)
  51. {
  52. m_isPressed = false;
  53. QPushButton::mouseReleaseEvent(event);
  54. }
  55. void PushButton::paintEvent(QPaintEvent *event)
  56. {
  57. QPushButton::paintEvent(event);
  58. if (!m_ficon) {
  59. return;
  60. }
  61. QPainter painter(this);
  62. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  63. if (!isEnabled()) {
  64. painter.setOpacity(0.43);
  65. } else if (m_isPressed) {
  66. painter.setOpacity(0.63);
  67. }
  68. int w = iconSize().width();
  69. int h = iconSize().height();
  70. int y = (height() - h) / 2;
  71. int mw = this->minimumSizeHint().width();
  72. if (mw > 0) {
  73. drawIcon(&painter, QRectF(12 + (width() - mw) / 2, y, w, h).toRect());
  74. } else {
  75. drawIcon(&painter, QRect(12, y, w, h));
  76. }
  77. }
  78. PrimaryPushButton::PrimaryPushButton(const QString &text, QWidget *parent) : PushButton(text, nullptr, parent) { }
  79. PrimaryPushButton::PrimaryPushButton(const QString &text, FluentIconBase *ficon, QWidget *parent)
  80. : PushButton(text, ficon, parent)
  81. {
  82. }
  83. void PrimaryPushButton::drawIcon(QPainter *painter, const QRect &rect)
  84. {
  85. FluentIconBase *ic = ficon();
  86. if (ic && this->isEnabled()) {
  87. // reverse icon color
  88. Qfw::Theme theme;
  89. if (QFWIns.isDarkTheme()) {
  90. theme = Qfw::Theme::DARK;
  91. } else {
  92. theme = Qfw::Theme::LIGHT;
  93. }
  94. ic->setTheme(theme);
  95. return PushButton::drawIcon(painter, rect);
  96. } else if (!this->isEnabled()) {
  97. painter->setOpacity((QFWIns.isDarkTheme() ? 0.786 : 0.9));
  98. return PushButton::drawIcon(painter, rect);
  99. }
  100. return PushButton::drawIcon(painter, rect);
  101. }
  102. HyperlinkButton::HyperlinkButton(const QString &url, const QString &text, QWidget *parent)
  103. : QPushButton(text, parent), m_url(QUrl(url))
  104. {
  105. connect(this, &QPushButton::clicked, [this]() { QDesktopServices::openUrl(m_url); });
  106. FluentStyleSheet::apply("BUTTON", this);
  107. setCursor(Qt::PointingHandCursor);
  108. }
  109. RadioButton::RadioButton(const QString &text, QWidget *parent) : QRadioButton(text, parent)
  110. {
  111. FluentStyleSheet::apply("BUTTON", this);
  112. }
  113. ToolButton::ToolButton(FluentIconBase *ficon, QWidget *parent) : QToolButton(parent), m_isPressed(false), m_ficon(ficon)
  114. {
  115. FluentStyleSheet::apply("BUTTON", this);
  116. }
  117. void ToolButton::setIcon(FluentIconBase *icon)
  118. {
  119. m_ficon.reset(icon);
  120. update();
  121. }
  122. FluentIconBase *ToolButton::ficon() const
  123. {
  124. return m_ficon.data();
  125. }
  126. void ToolButton::mousePressEvent(QMouseEvent *event)
  127. {
  128. m_isPressed = true;
  129. QToolButton::mousePressEvent(event);
  130. }
  131. void ToolButton::mouseReleaseEvent(QMouseEvent *event)
  132. {
  133. m_isPressed = false;
  134. QToolButton::mouseReleaseEvent(event);
  135. }
  136. void ToolButton::paintEvent(QPaintEvent *event)
  137. {
  138. QToolButton::paintEvent(event);
  139. if (!m_ficon) {
  140. return;
  141. }
  142. QPainter painter(this);
  143. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  144. if (!this->isEnabled()) {
  145. painter.setOpacity(0.43);
  146. } else {
  147. painter.setOpacity(0.63);
  148. }
  149. int w = this->iconSize().width();
  150. int h = this->iconSize().height();
  151. int x = (this->width() - w) / 2;
  152. int y = (this->height() - h) / 2;
  153. m_ficon->render(&painter, QRect(x, y, w, h));
  154. }
  155. TransparentToolButton::TransparentToolButton(FluentIconBase *ficon, QWidget *parent)
  156. : QToolButton(parent), m_ficon(ficon)
  157. {
  158. setCursor(Qt::PointingHandCursor);
  159. FluentStyleSheet::apply("BUTTON", this);
  160. }
  161. void TransparentToolButton::paintEvent(QPaintEvent *event)
  162. {
  163. QToolButton::paintEvent(event);
  164. if (!m_ficon) {
  165. return;
  166. }
  167. QPainter painter(this);
  168. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  169. int iw = iconSize().width();
  170. int ih = iconSize().height();
  171. int w = width();
  172. int h = height();
  173. QRect rect((w - iw) / 2, (h - ih) / 2, iw, ih);
  174. m_ficon->render(&painter, rect);
  175. }