StateToolTip.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "StateToolTip.h"
  2. #include "Common/Icon.h"
  3. #include "Common/StyleSheet.h"
  4. #include "QFluentWidgets.h"
  5. #include <QPainter>
  6. #include <QTimer>
  7. StateCloseButton::StateCloseButton(QWidget *parent) : QToolButton(parent), m_isEnter(false), m_isPressed(false)
  8. {
  9. setFixedSize(12, 12);
  10. }
  11. void StateCloseButton::mousePressEvent(QMouseEvent *event)
  12. {
  13. m_isPressed = true;
  14. update();
  15. QToolButton::mousePressEvent(event);
  16. }
  17. void StateCloseButton::mouseReleaseEvent(QMouseEvent *event)
  18. {
  19. m_isPressed = false;
  20. update();
  21. QToolButton::mouseReleaseEvent(event);
  22. }
  23. void StateCloseButton::enterEvent(QEvent * /*event*/)
  24. {
  25. m_isPressed = true;
  26. update();
  27. }
  28. void StateCloseButton::leaveEvent(QEvent * /*event*/)
  29. {
  30. m_isPressed = false;
  31. m_isEnter = false;
  32. update();
  33. }
  34. void StateCloseButton::paintEvent(QPaintEvent * /*event*/)
  35. {
  36. QPainter painter(this);
  37. painter.setRenderHints(QPainter::Antialiasing);
  38. if (m_isPressed) {
  39. painter.setOpacity(0.6);
  40. } else {
  41. painter.setOpacity(0.8);
  42. }
  43. QScopedPointer<FluentIcon> ficon(NEWFLICON(FluentIcon, CLOSE));
  44. ficon->render(&painter, rect());
  45. }
  46. StateToolTip::StateToolTip(const QString &title, const QString &content, QWidget *parent)
  47. : QWidget(parent),
  48. m_title(title),
  49. m_content(content),
  50. m_titleLabel(new QLabel(title, this)),
  51. m_contentLabel(new QLabel(content, this)),
  52. m_rotateTimer(new QTimer(this)),
  53. m_opacityEffect(new QGraphicsOpacityEffect(this)),
  54. m_animation(new QPropertyAnimation(this)),
  55. m_closeButton(new StateCloseButton(this)),
  56. m_isDone(false),
  57. m_rotateAngle(0),
  58. m_deltaAngle(20)
  59. {
  60. initWidget();
  61. }
  62. void StateToolTip::setTitle(const QString &title)
  63. {
  64. m_title = title;
  65. m_titleLabel->setText(title);
  66. m_titleLabel->adjustSize();
  67. }
  68. void StateToolTip::setContent(const QString &content)
  69. {
  70. m_content = content;
  71. m_contentLabel->setText(content);
  72. // adjustSize() will mask spinner get stuck
  73. m_contentLabel->adjustSize();
  74. }
  75. void StateToolTip::setState(bool isDone)
  76. {
  77. m_isDone = isDone;
  78. update();
  79. if (isDone) {
  80. QTimer::singleShot(1000, this, &StateToolTip::fadeOut);
  81. }
  82. }
  83. /// get suitable position in main window
  84. QPoint StateToolTip::getSuitablePos()
  85. {
  86. QPoint pos;
  87. QWidget *p = qobject_cast<QWidget *>(this->parent());
  88. for (int i = 0; i < 10; ++i) {
  89. int dy = i * (this->height() + 16);
  90. pos = QPoint(p->width() - this->width() - 24, 50 + dy);
  91. QWidget *w = p->childAt(pos + QPoint(2, 2));
  92. if (qobject_cast<StateToolTip *>(w) != nullptr) {
  93. pos += QPoint(0, this->height() + 10);
  94. } else {
  95. break;
  96. }
  97. }
  98. return pos;
  99. }
  100. void StateToolTip::paintEvent(QPaintEvent *event)
  101. {
  102. QWidget::paintEvent(event);
  103. QPainter painter(this);
  104. painter.setRenderHints(QPainter::Antialiasing);
  105. painter.setPen(Qt::NoPen);
  106. // NOTE: 未完成
  107. if (!m_isDone) {
  108. painter.translate(19, 18);
  109. painter.rotate(m_rotateAngle);
  110. if (QFWIns.isDarkTheme()) {
  111. FluentIcon ficon(FluentIcon::SYNC, Qfw::DARK);
  112. ficon.render(&painter, QRect(-8, -8, 16, 16));
  113. } else {
  114. FluentIcon ficon(FluentIcon::SYNC, Qfw::LIGHT);
  115. ficon.render(&painter, QRect(-8, -8, 16, 16));
  116. }
  117. } else {
  118. if (QFWIns.isDarkTheme()) {
  119. FluentIcon ficon(FluentIcon::COMPLETED, Qfw::DARK);
  120. ficon.render(&painter, QRect(-8, -8, 16, 16));
  121. } else {
  122. FluentIcon ficon(FluentIcon::COMPLETED, Qfw::LIGHT);
  123. ficon.render(&painter, QRect(11, 10, 16, 16));
  124. }
  125. }
  126. }
  127. void StateToolTip::onCloseButtonClicked()
  128. {
  129. emit closedSignal();
  130. hide();
  131. }
  132. void StateToolTip::rotateTimerFlowSlot()
  133. {
  134. m_rotateAngle = (m_rotateAngle + m_deltaAngle) % 360;
  135. update();
  136. }
  137. void StateToolTip::fadeOut()
  138. {
  139. m_rotateTimer->stop();
  140. m_animation->setDuration(200);
  141. m_animation->setStartValue(1);
  142. m_animation->setEndValue(0);
  143. connect(m_animation, &QPropertyAnimation::finished, this, &StateToolTip::deleteLater);
  144. m_animation->start();
  145. }
  146. void StateToolTip::initWidget()
  147. {
  148. setAttribute(Qt::WA_StyledBackground);
  149. setGraphicsEffect(m_opacityEffect);
  150. m_opacityEffect->setOpacity(1);
  151. m_rotateTimer->setInterval(50);
  152. m_contentLabel->setMinimumWidth(200);
  153. // connect signal to slot
  154. connect(m_closeButton, &StateCloseButton::clicked, this, &StateToolTip::onCloseButtonClicked);
  155. connect(m_rotateTimer, &QTimer::timeout, this, &StateToolTip::rotateTimerFlowSlot);
  156. setQss();
  157. initLayout();
  158. m_rotateTimer->start();
  159. }
  160. void StateToolTip::setQss()
  161. {
  162. m_titleLabel->setObjectName("titleLabel");
  163. m_contentLabel->setObjectName("contentLabel");
  164. FluentStyleSheet::apply("STATE_TOOL_TIP", this);
  165. m_titleLabel->adjustSize();
  166. m_contentLabel->adjustSize();
  167. }
  168. void StateToolTip::initLayout()
  169. {
  170. setFixedSize(qMax(m_titleLabel->width(), m_contentLabel->width()) + 56, 51);
  171. m_titleLabel->move(32, 9);
  172. m_contentLabel->move(12, 27);
  173. m_closeButton->move(this->width() - 24, 19);
  174. }