tooltipbutton.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "tooltipbutton.h"
  2. #include <QDebug>
  3. #include <QPoint>
  4. #include <QPropertyAnimation>
  5. TooltipButton::TooltipButton(QWidget *parent) : QLabel(parent) {
  6. setMouseTracking(true);
  7. _tooltip_parent_widget = this;
  8. _tooltip_label = new QLabel(this);
  9. _tooltip_label->setText("Tooltip.");
  10. _tooltip_label->setMaximumWidth(360);
  11. _tooltip_label->setWordWrap(true);
  12. _tooltip_label->setStyleSheet("padding: 8px; background-color: #222; border: 1px solid #fff; border-radius: 5px; color: #fff");
  13. _tooltip_label->hide();
  14. _tooltip_label_arrow = new QWidget(this);
  15. _tooltip_label_arrow->setStyleSheet("border-image: url(:/buttons/tooltip_triangle.png);");
  16. _tooltip_label_arrow->resize(14, 7);
  17. _tooltip_label_arrow->hide();
  18. _tooltip_opacity_effect = new QGraphicsOpacityEffect(this);
  19. _tooltip_arrow_opacity_effect = new QGraphicsOpacityEffect(this);
  20. _tooltip_label->setGraphicsEffect(_tooltip_opacity_effect);
  21. _tooltip_label_arrow->setGraphicsEffect(_tooltip_arrow_opacity_effect);
  22. }
  23. TooltipButton::~TooltipButton() {
  24. _tooltip_label->deleteLater();
  25. _tooltip_label_arrow->deleteLater();
  26. _tooltip_opacity_effect->deleteLater();
  27. _tooltip_arrow_opacity_effect->deleteLater();
  28. }
  29. void TooltipButton::enterEvent(QEvent*) {
  30. QPropertyAnimation *animation = new QPropertyAnimation(_tooltip_opacity_effect, "opacity");
  31. animation->setDuration(300);
  32. animation->setStartValue(0);
  33. animation->setEndValue(1);
  34. animation->setEasingCurve(QEasingCurve::InBack);
  35. animation->start(QPropertyAnimation::DeleteWhenStopped);
  36. _tooltip_label->show();
  37. QPropertyAnimation *arrow_animation = new QPropertyAnimation(_tooltip_arrow_opacity_effect, "opacity");
  38. arrow_animation->setDuration(300);
  39. arrow_animation->setStartValue(0);
  40. arrow_animation->setEndValue(1);
  41. arrow_animation->setEasingCurve(QEasingCurve::InBack);
  42. arrow_animation->start(QPropertyAnimation::DeleteWhenStopped);
  43. _tooltip_label_arrow->show();
  44. }
  45. void TooltipButton::leaveEvent(QEvent*) {
  46. QPropertyAnimation *animation = new QPropertyAnimation(_tooltip_opacity_effect, "opacity");
  47. animation->setDuration(300);
  48. animation->setStartValue(1);
  49. animation->setEndValue(0);
  50. animation->setEasingCurve(QEasingCurve::InBack);
  51. animation->start(QPropertyAnimation::DeleteWhenStopped);
  52. connect(animation, &QPropertyAnimation::finished, _tooltip_label, &QLabel::hide);
  53. QPropertyAnimation *arrow_animation = new QPropertyAnimation(_tooltip_arrow_opacity_effect, "opacity");
  54. arrow_animation->setDuration(300);
  55. arrow_animation->setStartValue(1);
  56. arrow_animation->setEndValue(0);
  57. arrow_animation->setEasingCurve(QEasingCurve::InBack);
  58. arrow_animation->start(QPropertyAnimation::DeleteWhenStopped);
  59. connect(arrow_animation, &QPropertyAnimation::finished, _tooltip_label_arrow, &QLabel::hide);
  60. }
  61. void TooltipButton::moveEvent(QMoveEvent*) {
  62. updateTooltipPosition();
  63. }
  64. void TooltipButton::resizeEvent(QResizeEvent*) {
  65. updateTooltipPosition();
  66. }
  67. void TooltipButton::setTooltipParentWidget(QWidget *widget) {
  68. _tooltip_parent_widget = widget;
  69. _tooltip_label->setParent(widget);
  70. _tooltip_label_arrow->setParent(widget);
  71. updateTooltipPosition();
  72. }
  73. void TooltipButton::setTooltipText(QString text) {
  74. _tooltip_label->setText(text);
  75. updateTooltipPosition();
  76. }
  77. void TooltipButton::updateTooltipPosition() {
  78. QPoint pos_global = mapToGlobal((rect().topLeft() + rect().topRight()) / 2);
  79. QPoint pos_relative_to_tooltip_parent = _tooltip_parent_widget->mapFromGlobal(pos_global);
  80. pos_relative_to_tooltip_parent.rx() -= _tooltip_label->sizeHint().width() / 2;
  81. pos_relative_to_tooltip_parent.ry() -= _tooltip_label->sizeHint().height() + 6; // 10 for tooltip arrow
  82. _tooltip_label->move(pos_relative_to_tooltip_parent);
  83. QPoint tooltip_arrow_pos = _tooltip_parent_widget->mapFromGlobal(pos_global) - QPoint(7, 7);
  84. _tooltip_label_arrow->move(tooltip_arrow_pos);
  85. }