tooltipbutton.cpp 3.9 KB

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