tooltipbutton.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. void TooltipButton::enterEvent(QEvent*) {
  24. QPropertyAnimation *animation = new QPropertyAnimation(_tooltip_opacity_effect, "opacity");
  25. animation->setDuration(300);
  26. animation->setStartValue(0);
  27. animation->setEndValue(1);
  28. animation->setEasingCurve(QEasingCurve::InBack);
  29. animation->start(QPropertyAnimation::DeleteWhenStopped);
  30. _tooltip_label->show();
  31. QPropertyAnimation *arrow_animation = new QPropertyAnimation(_tooltip_arrow_opacity_effect, "opacity");
  32. arrow_animation->setDuration(300);
  33. arrow_animation->setStartValue(0);
  34. arrow_animation->setEndValue(1);
  35. arrow_animation->setEasingCurve(QEasingCurve::InBack);
  36. arrow_animation->start(QPropertyAnimation::DeleteWhenStopped);
  37. _tooltip_label_arrow->show();
  38. }
  39. void TooltipButton::leaveEvent(QEvent*) {
  40. QPropertyAnimation *animation = new QPropertyAnimation(_tooltip_opacity_effect, "opacity");
  41. animation->setDuration(300);
  42. animation->setStartValue(1);
  43. animation->setEndValue(0);
  44. animation->setEasingCurve(QEasingCurve::InBack);
  45. animation->start(QPropertyAnimation::DeleteWhenStopped);
  46. connect(animation, &QPropertyAnimation::finished, _tooltip_label, &QLabel::hide);
  47. QPropertyAnimation *arrow_animation = new QPropertyAnimation(_tooltip_arrow_opacity_effect, "opacity");
  48. arrow_animation->setDuration(300);
  49. arrow_animation->setStartValue(1);
  50. arrow_animation->setEndValue(0);
  51. arrow_animation->setEasingCurve(QEasingCurve::InBack);
  52. arrow_animation->start(QPropertyAnimation::DeleteWhenStopped);
  53. connect(arrow_animation, &QPropertyAnimation::finished, _tooltip_label_arrow, &QLabel::hide);
  54. }
  55. void TooltipButton::moveEvent(QMoveEvent*) {
  56. updateTooltipPosition();
  57. }
  58. void TooltipButton::resizeEvent(QResizeEvent*) {
  59. updateTooltipPosition();
  60. }
  61. void TooltipButton::setTooltipParentWidget(QWidget *widget) {
  62. _tooltip_parent_widget = widget;
  63. _tooltip_label->setParent(widget);
  64. _tooltip_label_arrow->setParent(widget);
  65. updateTooltipPosition();
  66. }
  67. void TooltipButton::setTooltipText(QString text) {
  68. _tooltip_label->setText(text);
  69. updateTooltipPosition();
  70. }
  71. void TooltipButton::updateTooltipPosition() {
  72. QPoint pos_global = mapToGlobal((rect().topLeft() + rect().topRight()) / 2);
  73. QPoint pos_relative_to_tooltip_parent = _tooltip_parent_widget->mapFromGlobal(pos_global);
  74. pos_relative_to_tooltip_parent.rx() -= _tooltip_label->sizeHint().width() / 2;
  75. pos_relative_to_tooltip_parent.ry() -= _tooltip_label->sizeHint().height() + 6; // 10 for tooltip arrow
  76. _tooltip_label->move(pos_relative_to_tooltip_parent);
  77. QPoint tooltip_arrow_pos = _tooltip_parent_widget->mapFromGlobal(pos_global) - QPoint(7, 7);
  78. _tooltip_label_arrow->move(tooltip_arrow_pos);
  79. }