functionbutton.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. void setText(const QString& text);
  59. QString text() const;
  60. protected:
  61. void paintEvent(QPaintEvent* event) override;
  62. void enterEvent(QEvent* event) override;
  63. void leaveEvent(QEvent* event) override;
  64. private:
  65. QIcon m_icon;
  66. QString m_text;
  67. };
  68. // 按钮组容器(管理功能按钮和Popover按钮)
  69. class PopoverButtonGroup : public QWidget
  70. {
  71. Q_OBJECT
  72. public:
  73. explicit PopoverButtonGroup(Qt::Orientation orientation = Qt::Horizontal,
  74. QWidget* parent = nullptr);
  75. // 添加一个按钮(可选择是否关联Popover)
  76. void addButton(FunctionButton* button, Popover* popover = nullptr);
  77. // 为已有按钮添加Popover
  78. void addPopoverToButton(FunctionButton* button, Popover* popover);
  79. // 移除按钮
  80. void removeButton(FunctionButton* button);
  81. // 公开 sizeHint,供外部布局/尺寸计算使用
  82. QSize sizeHint() const override;
  83. private:
  84. // 创建Popover触发按钮
  85. QPushButton* createPopoverTriggerButton();
  86. // 切换Popover显示状态
  87. void togglePopover(QPushButton* popoverBtn);
  88. private:
  89. QList<FunctionButton*> buttons;
  90. QMap<QPushButton*, Popover*> popoverMap; // Popover按钮到Popover的映射
  91. QMap<FunctionButton*, QPushButton*> buttonPopoverMap; // 功能按钮到Popover按钮的映射
  92. QLayout* layout;
  93. };
  94. #endif // FUNCTIONBUTTON_H