NavigationWidget.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "NavigationWidget.h"
  2. #include "QFluentWidgets.h"
  3. #include <QPainter>
  4. int NavigationWidget::EXPAND_WIDTH = 312;
  5. NavigationWidget::NavigationWidget(bool selectable, QWidget *parent)
  6. : QWidget(parent), isCompacted(true), isSelected(false), isPressed(false), isEnter(false), isSelectable(selectable)
  7. {
  8. setFixedSize(40, 36);
  9. }
  10. /// set whether the widget is compacted
  11. void NavigationWidget::setCompacted(bool compacted)
  12. {
  13. if (isCompacted == compacted) {
  14. return;
  15. }
  16. isCompacted = compacted;
  17. if (isCompacted) {
  18. setFixedSize(40, 36);
  19. } else {
  20. setFixedSize(EXPAND_WIDTH, 36);
  21. }
  22. update();
  23. }
  24. /**
  25. * @brief set whether the button is selected
  26. * @param isSelected: whether the button is selected
  27. */
  28. void NavigationWidget::setSelected(bool selected)
  29. {
  30. if (!isSelectable || (isSelected == selected)) {
  31. return;
  32. }
  33. isSelected = selected;
  34. update();
  35. }
  36. void NavigationWidget::mousePressEvent(QMouseEvent * /*event*/)
  37. {
  38. isPressed = true;
  39. update();
  40. }
  41. void NavigationWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
  42. {
  43. isPressed = false;
  44. update();
  45. emit clicked(true);
  46. }
  47. void NavigationWidget::enterEvent(QEvent * /*event*/)
  48. {
  49. isEnter = true;
  50. update();
  51. }
  52. void NavigationWidget::leaveEvent(QEvent * /*event*/)
  53. {
  54. isEnter = false;
  55. isPressed = false;
  56. update();
  57. }
  58. NavigationPushButton::NavigationPushButton(FluentIconBase *ficon, const QString &text, bool selectable, QWidget *parent)
  59. : NavigationWidget(selectable, parent), m_ficon(ficon), m_text(text)
  60. {
  61. setStyleSheet("NavigationPushButton{font: bold 18px }");
  62. }
  63. QString NavigationPushButton::text() const
  64. {
  65. return m_text;
  66. }
  67. void NavigationPushButton::paintEvent(QPaintEvent * /*event*/)
  68. {
  69. QPainter painter(this);
  70. painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
  71. painter.setPen(Qt::NoPen);
  72. if (isPressed) {
  73. painter.setOpacity(0.7);
  74. } else {
  75. painter.setOpacity(0.4);
  76. }
  77. // draw background
  78. int c = QFWIns.isDarkTheme() ? 255 : 0;
  79. if (isSelected) {
  80. if (isEnter) {
  81. painter.setBrush(QColor(c, c, c, 36));
  82. } else {
  83. painter.setBrush(QColor(c, c, c, 60));
  84. }
  85. painter.drawRoundedRect(rect(), 5, 5);
  86. // draw indicator
  87. painter.setBrush(themeColor());
  88. painter.drawRoundedRect(0, 10, 3, 16, 1.5, 1.5);
  89. } else {
  90. painter.setBrush(QColor(c, c, c, 10));
  91. painter.drawRoundedRect(rect(), 5, 5);
  92. }
  93. m_ficon->render(&painter, QRectF(11.5, 10, 16, 16).toRect());
  94. // draw text
  95. if (!isCompacted) {
  96. painter.setFont(font());
  97. painter.setPen(QColor(c, c, c));
  98. painter.drawText(QRect(44, 0, width() - 57, height()), Qt::AlignVCenter, m_text);
  99. }
  100. }
  101. NavigationToolButton::NavigationToolButton(FluentIconBase *ficon, QWidget *parent)
  102. : NavigationPushButton(ficon, "", false, parent)
  103. {
  104. }
  105. void NavigationToolButton::setCompacted(bool /*compacted*/)
  106. {
  107. setFixedSize(40, 36);
  108. }
  109. NavigationSeparator::NavigationSeparator(QWidget *parent) : NavigationWidget(false, parent)
  110. {
  111. setCompacted(true);
  112. }
  113. void NavigationSeparator::setCompacted(bool compacted)
  114. {
  115. if (compacted) {
  116. setFixedSize(48, 3);
  117. } else {
  118. setFixedSize(EXPAND_WIDTH + 10, 3);
  119. }
  120. update();
  121. }
  122. void NavigationSeparator::paintEvent(QPaintEvent * /*event*/)
  123. {
  124. QPainter painter(this);
  125. int c = QFWIns.isDarkTheme() ? 255 : 0;
  126. QPen pen(QColor(c, c, c, 15));
  127. pen.setCosmetic(true);
  128. painter.setPen(pen);
  129. painter.drawLine(0, 1, width(), 1);
  130. }