InfoBar.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifndef INFOBAR_H
  2. #define INFOBAR_H
  3. #include <QBoxLayout>
  4. #include <QFrame>
  5. #include <QLabel>
  6. #include <QPropertyAnimation>
  7. #include <QGraphicsOpacityEffect>
  8. #include <QToolButton>
  9. #include <QParallelAnimationGroup>
  10. #include "Common/Icon.h"
  11. class InfoBarCloseButton : public QToolButton
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit InfoBarCloseButton(QWidget *parent = nullptr);
  16. // QWidget interface
  17. protected:
  18. void paintEvent(QPaintEvent *event) override;
  19. private:
  20. QScopedPointer<FluentIcon> m_ficon;
  21. };
  22. class InfoBarIcon : public FluentIconBase
  23. {
  24. Q_OBJECT
  25. public:
  26. enum IconType
  27. {
  28. INFORMATION = 0,
  29. SUCCESS,
  30. WARNING,
  31. ERROR
  32. };
  33. Q_ENUM(IconType)
  34. // const QString INFORMATION = "Info";
  35. // const QString SUCCESS = "Success";
  36. // const QString WARNING = "Warning";
  37. // const QString ERROR = "Error";
  38. static QString iconName(IconType type);
  39. InfoBarIcon(IconType type, Qfw::Theme t = Qfw::AUTO);
  40. ~InfoBarIcon();
  41. QString iconPath();
  42. // FluentIconBase interface
  43. QIcon icon() override;
  44. QString typeName() const override;
  45. QString enumName() const override;
  46. FluentIconBase *clone() override;
  47. Qfw::Theme theme() const;
  48. void setTheme(const Qfw::Theme &theme) override;
  49. private:
  50. Qfw::Theme m_theme;
  51. IconType m_type;
  52. };
  53. enum InfoBarPosition
  54. {
  55. TOP = 0,
  56. BOTTOM = 1,
  57. TOP_LEFT = 2,
  58. TOP_RIGHT = 3,
  59. BOTTOM_LEFT = 4,
  60. BOTTOM_RIGHT = 5,
  61. NONE = 6
  62. };
  63. class InfoIconWidget : public QWidget
  64. {
  65. Q_OBJECT
  66. public:
  67. InfoIconWidget(FluentIconBase *icon, QWidget *parent = nullptr);
  68. // QWidget interface
  69. protected:
  70. void paintEvent(QPaintEvent *event) override;
  71. private:
  72. QScopedPointer<FluentIconBase> m_icon;
  73. };
  74. class InfoBar : public QFrame
  75. {
  76. Q_OBJECT
  77. public:
  78. explicit InfoBar(FluentIconBase *ficon, const QString &title, const QString &content,
  79. Qt::Orientation orient = Qt::Horizontal, bool isClosable = true, int duration = 1000,
  80. InfoBarPosition position = InfoBarPosition::TOP_RIGHT, QWidget *parent = nullptr);
  81. void adjustSize();
  82. void addWidget(QWidget *widget, int stretch = 0);
  83. void setCustomBackgroundColor(const QColor &light, const QColor &dark);
  84. static InfoBar *creator(FluentIconBase *ficon, const QString &title, const QString &content,
  85. Qt::Orientation orient = Qt::Horizontal, bool isClosable = true, int duration = 1000,
  86. InfoBarPosition position = InfoBarPosition::TOP_RIGHT, QWidget *parent = nullptr);
  87. static InfoBar *info(const QString &title, const QString &content, Qt::Orientation orient = Qt::Horizontal,
  88. bool isClosable = true, int duration = 1000,
  89. InfoBarPosition position = InfoBarPosition::TOP_RIGHT, QWidget *parent = nullptr);
  90. static InfoBar *success(const QString &title, const QString &content, Qt::Orientation orient = Qt::Horizontal,
  91. bool isClosable = true, int duration = 1000,
  92. InfoBarPosition position = InfoBarPosition::TOP_RIGHT, QWidget *parent = nullptr);
  93. static InfoBar *warning(const QString &title, const QString &content, Qt::Orientation orient = Qt::Horizontal,
  94. bool isClosable = true, int duration = 1000,
  95. InfoBarPosition position = InfoBarPosition::TOP_RIGHT, QWidget *parent = nullptr);
  96. static InfoBar *error(const QString &title, const QString &content, Qt::Orientation orient = Qt::Horizontal,
  97. bool isClosable = true, int duration = 1000,
  98. InfoBarPosition position = InfoBarPosition::TOP_RIGHT, QWidget *parent = nullptr);
  99. protected:
  100. void adjustText();
  101. bool eventFilter(QObject *watched, QEvent *event) override;
  102. void paintEvent(QPaintEvent *event) override;
  103. void closeEvent(QCloseEvent *event) override;
  104. void showEvent(QShowEvent *event) override;
  105. signals:
  106. void closedSignal();
  107. private:
  108. void initWidget();
  109. void setQss();
  110. void initLayout();
  111. private slots:
  112. void fadeOut();
  113. private:
  114. QString m_title;
  115. QString m_content;
  116. Qt::Orientation m_orient;
  117. QSharedPointer<FluentIconBase> m_ficon;
  118. // FluentIconBase *m_ficon;
  119. int m_duration;
  120. bool m_isClosable;
  121. InfoBarPosition m_position;
  122. QLabel *m_titleLabel;
  123. QLabel *m_contentLabel;
  124. InfoBarCloseButton *m_closeButton;
  125. InfoIconWidget *m_iconWidget;
  126. QVBoxLayout *m_vBoxLayout;
  127. QHBoxLayout *m_hBoxLayout;
  128. QBoxLayout *m_contentLayout;
  129. QGraphicsOpacityEffect *m_opacityEffect;
  130. QPropertyAnimation *m_opacityAni;
  131. QColor m_lightBackgroundColor;
  132. QColor m_darkBackgroundColor;
  133. };
  134. Q_DECLARE_METATYPE(QPropertyAnimation *)
  135. class InfoBarManagerBase : public QObject
  136. {
  137. Q_OBJECT
  138. public:
  139. InfoBarManagerBase(QObject *parent = nullptr);
  140. void add(InfoBar *infoBar);
  141. void remove(InfoBar *infoBar);
  142. int spacing;
  143. int margin;
  144. QHash<QWidget *, QList<InfoBar *>> infoBars;
  145. QHash<QWidget *, QParallelAnimationGroup *> aniGroups;
  146. QList<QPropertyAnimation *> slideAnis;
  147. QList<QPropertyAnimation *> dropAnis;
  148. protected:
  149. QPropertyAnimation *createSlideAni(InfoBar *infoBar);
  150. void updateDropAni(QWidget *parent);
  151. virtual QPoint pos(InfoBar *infoBar, const QSize &parentSize = QSize()) = 0;
  152. virtual QPoint slideStartPos(InfoBar *infoBar) = 0;
  153. bool eventFilter(QObject *watched, QEvent *event) override;
  154. };
  155. class TopInfoBarManager : public InfoBarManagerBase
  156. {
  157. Q_OBJECT
  158. public:
  159. explicit TopInfoBarManager(QObject *parent = nullptr);
  160. // InfoBarManagerBase interface
  161. protected:
  162. QPoint pos(InfoBar *infoBar, const QSize &parentSize = QSize()) override;
  163. QPoint slideStartPos(InfoBar *infoBar) override;
  164. };
  165. class TopRightInfoBarManager : public InfoBarManagerBase
  166. {
  167. Q_OBJECT
  168. public:
  169. explicit TopRightInfoBarManager(QObject *parent = nullptr);
  170. // InfoBarManagerBase interface
  171. protected:
  172. QPoint pos(InfoBar *infoBar, const QSize &parentSize = QSize()) override;
  173. QPoint slideStartPos(InfoBar *infoBar) override;
  174. };
  175. class BottomRightInfoBarManager : public InfoBarManagerBase
  176. {
  177. Q_OBJECT
  178. public:
  179. explicit BottomRightInfoBarManager(QObject *parent = nullptr);
  180. // InfoBarManagerBase interface
  181. protected:
  182. QPoint pos(InfoBar *infoBar, const QSize &parentSize = QSize()) override;
  183. QPoint slideStartPos(InfoBar *infoBar) override;
  184. };
  185. class TopLeftInfoBarManager : public InfoBarManagerBase
  186. {
  187. Q_OBJECT
  188. public:
  189. explicit TopLeftInfoBarManager(QObject *parent = nullptr);
  190. // InfoBarManagerBase interface
  191. protected:
  192. QPoint pos(InfoBar *infoBar, const QSize &parentSize = QSize()) override;
  193. QPoint slideStartPos(InfoBar *infoBar) override;
  194. };
  195. class BottomLeftInfoBarManager : public InfoBarManagerBase
  196. {
  197. Q_OBJECT
  198. public:
  199. explicit BottomLeftInfoBarManager(QObject *parent = nullptr);
  200. // InfoBarManagerBase interface
  201. protected:
  202. QPoint pos(InfoBar *infoBar, const QSize &parentSize = QSize()) override;
  203. QPoint slideStartPos(InfoBar *infoBar) override;
  204. };
  205. class BottomInfoBarManager : public InfoBarManagerBase
  206. {
  207. Q_OBJECT
  208. public:
  209. explicit BottomInfoBarManager(QObject *parent = nullptr);
  210. // InfoBarManagerBase interface
  211. protected:
  212. QPoint pos(InfoBar *infoBar, const QSize &parentSize = QSize()) override;
  213. QPoint slideStartPos(InfoBar *infoBar) override;
  214. };
  215. class InfoBarManagerFactory
  216. {
  217. public:
  218. static InfoBarManagerBase &factory(InfoBarPosition postion);
  219. };
  220. #endif // INFOBAR_H