functionbutton.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef BUTTON_COMPONENTS_H
  2. #define BUTTON_COMPONENTS_H
  3. #include <QFontMetrics>
  4. #include <QHBoxLayout>
  5. #include <QIcon>
  6. #include <QLayout>
  7. #include <QList>
  8. #include <QMap>
  9. #include <QPaintEvent>
  10. #include <QPainter>
  11. #include <QPixmap>
  12. #include <QPolygon>
  13. #include <QPushButton>
  14. #include <QVBoxLayout>
  15. #include <QWidget>
  16. class Popover : public QWidget
  17. {
  18. Q_OBJECT
  19. Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
  20. public:
  21. explicit Popover(QWidget* parent = nullptr);
  22. ~Popover();
  23. void setContentWidget(QWidget* widget);
  24. void showAnimated(const QPoint& position);
  25. void hideAnimated();
  26. qreal opacity() const { return m_opacity; }
  27. void setOpacity(qreal opacity);
  28. // 计算阴影边距
  29. QMargins shadowMargins() const;
  30. signals:
  31. void popoverClosed();
  32. protected:
  33. void closeEvent(QCloseEvent* event) override;
  34. void paintEvent(QPaintEvent* event) override;
  35. QSize sizeHint() const override;
  36. bool eventFilter(QObject* obj, QEvent* event) override;
  37. private:
  38. QVBoxLayout* mainLayout;
  39. qreal m_opacity = 1.0;
  40. };
  41. // 自定义Popover触发按钮,确保固定大小不被全局QSS覆盖
  42. class PopoverTriggerButton : public QPushButton
  43. {
  44. Q_OBJECT
  45. public:
  46. explicit PopoverTriggerButton(QWidget* parent = nullptr);
  47. protected:
  48. QSize sizeHint() const override;
  49. QSize minimumSizeHint() const override;
  50. void resizeEvent(QResizeEvent* event) override;
  51. };
  52. // 自定义功能按钮(图标在上,文本在下)
  53. class FunctionButton : public QPushButton
  54. {
  55. Q_OBJECT
  56. public:
  57. FunctionButton(const QIcon& icon, const QString& text, QWidget* parent = nullptr);
  58. protected:
  59. void paintEvent(QPaintEvent* event) override;
  60. void enterEvent(QEvent* event) override;
  61. void leaveEvent(QEvent* event) override;
  62. private:
  63. QIcon m_icon;
  64. QString m_text;
  65. };
  66. // 按钮组容器(管理功能按钮和Popover按钮)
  67. class PopoverButtonGroup : public QWidget
  68. {
  69. Q_OBJECT
  70. public:
  71. explicit PopoverButtonGroup(Qt::Orientation orientation = Qt::Horizontal,
  72. QWidget* parent = nullptr);
  73. // 添加一个按钮(可选择是否关联Popover)
  74. void addButton(FunctionButton* button, Popover* popover = nullptr);
  75. // 为已有按钮添加Popover
  76. void addPopoverToButton(FunctionButton* button, Popover* popover);
  77. protected:
  78. QSize sizeHint() const override;
  79. private:
  80. // 创建Popover触发按钮
  81. QPushButton* createPopoverTriggerButton();
  82. // 切换Popover显示状态
  83. void togglePopover(QPushButton* popoverBtn);
  84. private:
  85. QList<FunctionButton*> buttons;
  86. QMap<QPushButton*, Popover*> popoverMap; // Popover按钮到Popover的映射
  87. QMap<FunctionButton*, QPushButton*> buttonPopoverMap; // 功能按钮到Popover按钮的映射
  88. QLayout* layout;
  89. };
  90. #endif // FUNCTIONBUTTON_H