TitleBarButton.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "TitleBarButton.h"
  2. #include <QFile>
  3. #include <QPainter>
  4. #include <QSvgRenderer>
  5. #include <QDebug>
  6. #include <QMouseEvent>
  7. #include <QPainterPath>
  8. TitleBarButton::TitleBarButton(QWidget *parent)
  9. : QAbstractButton(parent),
  10. m_state(TitleBarButtonState::NORMAL),
  11. m_normalColor(QColor(0, 0, 0)),
  12. m_hoverColor(QColor(0, 0, 0)),
  13. m_pressedColor(QColor(0, 0, 0)),
  14. m_normalBgColor(QColor(0, 0, 0, 0)),
  15. m_hoverBgColor(QColor(0, 0, 0, 26)),
  16. m_pressedBgColor(QColor(0, 0, 0, 51))
  17. {
  18. this->setCursor(Qt::ArrowCursor);
  19. this->setFixedSize(46, 32);
  20. }
  21. void TitleBarButton::setState(TitleBarButtonState state)
  22. {
  23. this->m_state = state;
  24. this->update();
  25. }
  26. bool TitleBarButton::isPressed()
  27. {
  28. return m_state == TitleBarButtonState::PRESSED;
  29. }
  30. QColor TitleBarButton::getNormalColor()
  31. {
  32. return m_normalColor;
  33. }
  34. QColor TitleBarButton::getHoverColor()
  35. {
  36. return m_hoverColor;
  37. }
  38. QColor TitleBarButton::getPressedColor()
  39. {
  40. return m_pressedColor;
  41. }
  42. QColor TitleBarButton::getNormalBackgroundColor()
  43. {
  44. return m_normalBgColor;
  45. }
  46. QColor TitleBarButton::getHoverBackgroundColor()
  47. {
  48. return m_hoverBgColor;
  49. }
  50. QColor TitleBarButton::getPressedBackgroundColor()
  51. {
  52. return m_pressedBgColor;
  53. }
  54. void TitleBarButton::setNormalColor(const QColor &color)
  55. {
  56. m_normalColor = color;
  57. this->update();
  58. }
  59. void TitleBarButton::setHoverColor(const QColor &color)
  60. {
  61. m_hoverColor = color;
  62. this->update();
  63. }
  64. void TitleBarButton::setPressedColor(const QColor &color)
  65. {
  66. m_pressedColor = color;
  67. this->update();
  68. }
  69. void TitleBarButton::setNormalBackgroundColor(const QColor &color)
  70. {
  71. m_normalBgColor = color;
  72. this->update();
  73. }
  74. void TitleBarButton::setHoverBackgroundColor(const QColor &color)
  75. {
  76. m_hoverBgColor = color;
  77. this->update();
  78. }
  79. void TitleBarButton::setPressedBackgroundColor(const QColor &color)
  80. {
  81. m_pressedBgColor = color;
  82. this->update();
  83. }
  84. /**
  85. * @brief get the icon color and background color
  86. * @return
  87. */
  88. QVector<QColor> TitleBarButton::getColors()
  89. {
  90. if (m_state == TitleBarButtonState::NORMAL) {
  91. return QVector<QColor>() << m_normalColor << m_normalBgColor;
  92. } else if (m_state == TitleBarButtonState::HOVER) {
  93. return QVector<QColor>() << m_hoverColor << m_hoverBgColor;
  94. } else {
  95. return QVector<QColor>() << m_pressedColor << m_pressedBgColor;
  96. }
  97. }
  98. void TitleBarButton::mousePressEvent(QMouseEvent *event)
  99. {
  100. if (event->button() != Qt::LeftButton) {
  101. return;
  102. }
  103. setState(TitleBarButtonState::PRESSED);
  104. QAbstractButton::mousePressEvent(event);
  105. }
  106. void TitleBarButton::enterEvent(QEvent *event)
  107. {
  108. setState(TitleBarButtonState::HOVER);
  109. QAbstractButton::enterEvent(event);
  110. }
  111. void TitleBarButton::leaveEvent(QEvent *event)
  112. {
  113. setState(TitleBarButtonState::NORMAL);
  114. QAbstractButton::leaveEvent(event);
  115. }
  116. SvgTitleBarButton::SvgTitleBarButton(const QString &iconPath, QWidget *parent)
  117. : TitleBarButton(parent), m_svgDom(new QDomDocument)
  118. {
  119. setIcon(iconPath);
  120. }
  121. SvgTitleBarButton::~SvgTitleBarButton()
  122. {
  123. delete m_svgDom;
  124. }
  125. void SvgTitleBarButton::setIcon(const QString &iconPath)
  126. {
  127. QFile f(iconPath);
  128. if (f.open(QFile::ReadOnly)) {
  129. m_svgDom->setContent(f.readAll());
  130. f.close();
  131. }
  132. }
  133. void SvgTitleBarButton::paintEvent(QPaintEvent *)
  134. {
  135. QPainter painter(this);
  136. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  137. auto c = this->getColors();
  138. QColor color = c.at(0);
  139. QColor bgColor = c.at(1);
  140. // draw background
  141. painter.setBrush(bgColor);
  142. painter.setPen(Qt::NoPen);
  143. painter.drawRect(rect());
  144. // draw icon
  145. QString colorName = color.name();
  146. QDomNodeList pathNodes = m_svgDom->elementsByTagName("path");
  147. for (int i = 0; i < pathNodes.count(); ++i) {
  148. QDomElement element = pathNodes.at(i).toElement();
  149. element.setAttribute("stroke", colorName);
  150. }
  151. QSvgRenderer renderer(m_svgDom->toByteArray());
  152. renderer.render(&painter, QRectF(rect()));
  153. }
  154. MinimizeButton::MinimizeButton(QWidget *parent) : TitleBarButton(parent) { }
  155. void MinimizeButton::paintEvent(QPaintEvent *)
  156. {
  157. QPainter painter(this);
  158. auto c = this->getColors();
  159. QColor color = c.at(0);
  160. QColor bgColor = c.at(1);
  161. // draw background
  162. painter.setBrush(bgColor);
  163. painter.setPen(Qt::NoPen);
  164. painter.drawRect(rect());
  165. // draw icon
  166. painter.setBrush(Qt::NoBrush);
  167. QPen pen = QPen(color, 1);
  168. pen.setCosmetic(true);
  169. painter.setPen(pen);
  170. painter.drawLine(18, 16, 28, 16);
  171. }
  172. MaximizeButton::MaximizeButton(QWidget *parent) : TitleBarButton(parent), m_isMax(false) { }
  173. /// update the maximized state and icon
  174. void MaximizeButton::setMaxState(bool isMax)
  175. {
  176. if (isMax == m_isMax) {
  177. return;
  178. }
  179. m_isMax = isMax;
  180. setState(TitleBarButtonState::NORMAL);
  181. }
  182. void MaximizeButton::paintEvent(QPaintEvent *)
  183. {
  184. QPainter painter(this);
  185. auto c = this->getColors();
  186. QColor color = c.at(0);
  187. QColor bgColor = c.at(1);
  188. // draw background
  189. painter.setBrush(bgColor);
  190. painter.setPen(Qt::NoPen);
  191. painter.drawRect(rect());
  192. // draw icon
  193. painter.setBrush(Qt::NoBrush);
  194. QPen pen = QPen(color, 1);
  195. pen.setCosmetic(true);
  196. painter.setPen(pen);
  197. qreal r = this->devicePixelRatioF();
  198. painter.scale(1 / r, 1 / r);
  199. if (!m_isMax) {
  200. painter.drawRect(18 * r, 11 * r, 10 * r, 10 * r);
  201. } else {
  202. painter.drawRect(18 * r, 13 * r, 8 * r, 8 * r);
  203. int x0 = 18 * r + 2 * r;
  204. int y0 = 13 * r;
  205. int dw = 2 * r;
  206. QPainterPath path(QPointF(x0, y0));
  207. path.lineTo(x0, y0 - dw);
  208. path.lineTo(x0 + 8 * r, y0 - dw);
  209. path.lineTo(x0 + 8 * r, y0 - dw + 8 * r);
  210. path.lineTo(x0 + 8 * r - dw, y0 - dw + 8 * r);
  211. painter.drawPath(path);
  212. }
  213. }
  214. CloseButton::CloseButton(QWidget *parent) : SvgTitleBarButton(":/qframelesswindow/images/close.svg", parent)
  215. {
  216. this->setHoverColor(Qt::white);
  217. this->setPressedColor(Qt::white);
  218. this->setHoverBackgroundColor(QColor(232, 17, 35));
  219. this->setPressedBackgroundColor(QColor(241, 112, 122));
  220. };