vbuttonwithwidget.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "vbuttonwithwidget.h"
  2. #include <QMenu>
  3. #include <QDragEnterEvent>
  4. #include <QDropEvent>
  5. #include <QMimeData>
  6. #include <QRect>
  7. #include <QPainter>
  8. #include <QPainterPath>
  9. #include <QBrush>
  10. VButtonWithWidget::VButtonWithWidget(QWidget *p_widget,
  11. QWidget *p_parent)
  12. : QPushButton(p_parent), m_popupWidget(p_widget)
  13. {
  14. init();
  15. }
  16. VButtonWithWidget::VButtonWithWidget(const QString &p_text,
  17. QWidget *p_widget,
  18. QWidget *p_parent)
  19. : QPushButton(p_text, p_parent), m_popupWidget(p_widget)
  20. {
  21. init();
  22. }
  23. VButtonWithWidget::VButtonWithWidget(const QIcon &p_icon,
  24. const QString &p_text,
  25. QWidget *p_widget,
  26. QWidget *p_parent)
  27. : QPushButton(p_icon, p_text, p_parent), m_popupWidget(p_widget)
  28. {
  29. init();
  30. }
  31. void VButtonWithWidget::init()
  32. {
  33. m_popupWidget->setParent(this);
  34. m_bubbleFg = QColor(Qt::white);
  35. m_bubbleBg = QColor("#15AE67");
  36. QMenu *menu = new QMenu(this);
  37. VButtonWidgetAction *act = new VButtonWidgetAction(m_popupWidget, menu);
  38. menu->addAction(act);
  39. connect(menu, &QMenu::aboutToShow,
  40. this, [this]() {
  41. emit popupWidgetAboutToShow(m_popupWidget);
  42. });
  43. setMenu(menu);
  44. VButtonPopupWidget *popup = getButtonPopupWidget();
  45. if (popup) {
  46. popup->setButton(this);
  47. setAcceptDrops(popup->isAcceptDrops());
  48. connect(this, &VButtonWithWidget::popupWidgetAboutToShow,
  49. this, [this]() {
  50. getButtonPopupWidget()->handleAboutToShow();
  51. });
  52. }
  53. }
  54. QWidget *VButtonWithWidget::getPopupWidget() const
  55. {
  56. return m_popupWidget;
  57. }
  58. void VButtonWithWidget::showPopupWidget()
  59. {
  60. showMenu();
  61. }
  62. void VButtonWithWidget::dragEnterEvent(QDragEnterEvent *p_event)
  63. {
  64. VButtonPopupWidget *popup = getButtonPopupWidget();
  65. Q_ASSERT(popup);
  66. if (popup->handleDragEnterEvent(p_event)) {
  67. return;
  68. }
  69. QPushButton::dragEnterEvent(p_event);
  70. }
  71. void VButtonWithWidget::dropEvent(QDropEvent *p_event)
  72. {
  73. VButtonPopupWidget *popup = getButtonPopupWidget();
  74. Q_ASSERT(popup);
  75. if (popup->handleDropEvent(p_event)) {
  76. return;
  77. }
  78. QPushButton::dropEvent(p_event);
  79. }
  80. void VButtonWithWidget::paintEvent(QPaintEvent *p_event)
  81. {
  82. QPushButton::paintEvent(p_event);
  83. if (!isEnabled() || m_bubbleStr.isEmpty()) {
  84. return;
  85. }
  86. QRect re = rect();
  87. int bubbleWidth = re.width() * 3.0 / 7;
  88. int x = re.width() - bubbleWidth;
  89. int y = 0;
  90. QRect bubbleRect(x, y, bubbleWidth, bubbleWidth);
  91. QPainter painter(this);
  92. QPainterPath bgPath;
  93. bgPath.addEllipse(bubbleRect);
  94. painter.fillPath(bgPath, m_bubbleBg);
  95. QFont font = painter.font();
  96. font.setPixelSize(bubbleWidth / 1.3);
  97. painter.setFont(font);
  98. painter.setPen(m_bubbleFg);
  99. painter.drawText(bubbleRect, Qt::AlignCenter, m_bubbleStr);
  100. }
  101. void VButtonWithWidget::setBubbleNumber(int p_num)
  102. {
  103. if (p_num < 0) {
  104. m_bubbleStr.clear();
  105. } else {
  106. m_bubbleStr = QString::number(p_num);
  107. }
  108. update();
  109. }