| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #ifndef BUTTON_COMPONENTS_H
- #define BUTTON_COMPONENTS_H
- #include <QFontMetrics>
- #include <QHBoxLayout>
- #include <QIcon>
- #include <QLayout>
- #include <QList>
- #include <QMap>
- #include <QPaintEvent>
- #include <QPainter>
- #include <QPixmap>
- #include <QPolygon>
- #include <QPushButton>
- #include <QVBoxLayout>
- #include <QWidget>
- class Popover : public QWidget
- {
- Q_OBJECT
- Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
- public:
- explicit Popover(QWidget* parent = nullptr);
- ~Popover();
- void setContentWidget(QWidget* widget);
- void showAnimated(const QPoint& position);
- void hideAnimated();
- qreal opacity() const { return m_opacity; }
- void setOpacity(qreal opacity);
- // 计算阴影边距
- QMargins shadowMargins() const;
- signals:
- void popoverClosed();
- protected:
- void closeEvent(QCloseEvent* event) override;
- void paintEvent(QPaintEvent* event) override;
- QSize sizeHint() const override;
- bool eventFilter(QObject* obj, QEvent* event) override;
- private:
- QVBoxLayout* mainLayout;
- qreal m_opacity = 1.0;
- };
- // 自定义Popover触发按钮,确保固定大小不被全局QSS覆盖
- class PopoverTriggerButton : public QPushButton
- {
- Q_OBJECT
- public:
- explicit PopoverTriggerButton(QWidget* parent = nullptr);
-
- protected:
- QSize sizeHint() const override;
- QSize minimumSizeHint() const override;
- void resizeEvent(QResizeEvent* event) override;
- };
- // 自定义功能按钮(图标在上,文本在下)
- class FunctionButton : public QPushButton
- {
- Q_OBJECT
- public:
- FunctionButton(const QIcon& icon, const QString& text, QWidget* parent = nullptr);
- protected:
- void paintEvent(QPaintEvent* event) override;
- void enterEvent(QEvent* event) override;
- void leaveEvent(QEvent* event) override;
- private:
- QIcon m_icon;
- QString m_text;
- };
- // 按钮组容器(管理功能按钮和Popover按钮)
- class PopoverButtonGroup : public QWidget
- {
- Q_OBJECT
- public:
- explicit PopoverButtonGroup(Qt::Orientation orientation = Qt::Horizontal,
- QWidget* parent = nullptr);
- // 添加一个按钮(可选择是否关联Popover)
- void addButton(FunctionButton* button, Popover* popover = nullptr);
- // 为已有按钮添加Popover
- void addPopoverToButton(FunctionButton* button, Popover* popover);
- protected:
- QSize sizeHint() const override;
- private:
- // 创建Popover触发按钮
- QPushButton* createPopoverTriggerButton();
- // 切换Popover显示状态
- void togglePopover(QPushButton* popoverBtn);
- private:
- QList<FunctionButton*> buttons;
- QMap<QPushButton*, Popover*> popoverMap; // Popover按钮到Popover的映射
- QMap<FunctionButton*, QPushButton*> buttonPopoverMap; // 功能按钮到Popover按钮的映射
- QLayout* layout;
- };
- #endif // FUNCTIONBUTTON_H
|