Loading.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef LOADING_H
  2. #define LOADING_H
  3. #include <QWidget>
  4. #include <QEasingCurve>
  5. #include <QTimer>
  6. class BaseLoading : public QWidget
  7. {
  8. Q_OBJECT
  9. public:
  10. explicit BaseLoading(QWidget *parent = nullptr);
  11. void setEasingCurve(const QEasingCurve& curve); // 设置插值器
  12. void setFrameRate(const int& frameRate); // 设置帧率
  13. void setDuration(const int& duration); // 设置动画时长
  14. void setExtendDuration(const int& duration); // 设置追击动画时长
  15. void updateFrameData(); // 更新绘制帧数据
  16. bool isActive() { return m_animationTimer->isActive(); } // 动画是否已经被激活
  17. public slots:
  18. void start(); // 开始动画
  19. void stop(); // 结束动画
  20. void updateFrameRate(); // 更新动画刷新率,动画刷新率与屏幕刷新率一致
  21. protected:
  22. inline QEasingCurve easingCurve() { return m_curve; }
  23. inline int frameCount() { return m_frameRects.count(); }
  24. inline qreal refreshTime() { return m_animationTimer->interval(); }
  25. inline int duration() { return m_duration; }
  26. inline int extendFrameCount() { return m_extendFrameCount; }
  27. // 获取某帧需要的绘制信息
  28. inline QRect frameRect(const int& index) {
  29. if (0 <= index && index <= m_frameRects.count())
  30. return m_frameRects.at(index);
  31. return QRect();
  32. }
  33. inline bool stopLoading() { return m_animationStop; }
  34. // 设置某帧的绘制信息
  35. inline void setFrameRect(const int& index,const QRect& rect) {
  36. if (0 <= index && index < m_frameRects.count())
  37. m_frameRects[index] = rect;
  38. }
  39. // 停止计时器
  40. inline void stopAnimationTimer() {
  41. m_animationTimer->stop();
  42. emit loaddingStoped();
  43. }
  44. // 获取某一帧时的值
  45. inline qreal frameValue(const int& index) {
  46. if (0 <= index && index < m_frameValues.count()) {
  47. return m_frameValues.at(index);
  48. }
  49. return 0;
  50. }
  51. // 计算每帧需要的绘制信息,在调用updateFrameData后会触发此函数
  52. virtual void updateFrameRects() = 0;
  53. // 刷新帧,即每次刷新帧都会调用此函数
  54. virtual void updateFrame() = 0;
  55. signals:
  56. void loaddingStoped(); // 加载动画已经停止
  57. private:
  58. void updateFrameValues();
  59. private:
  60. QEasingCurve m_curve; // 插值器
  61. QList<QRect> m_frameRects; // 每一帧需要的rect数据
  62. QTimer* m_animationTimer; // 用于控制动画的计时器
  63. int m_frameRate; // 帧率
  64. int m_duration; // 完成一次动画需要的时间
  65. int m_extendDuration; // 追击延迟时间
  66. int m_extendFrameCount; // 追击帧数
  67. bool m_animationStop; // 是否停止动画
  68. QList<qreal> m_frameValues; // 每一帧对应的数值
  69. };
  70. class LoadingHorizontal : public BaseLoading
  71. {
  72. Q_OBJECT
  73. public:
  74. explicit LoadingHorizontal(QWidget *parent = nullptr);
  75. void setItemCount(const int& count); // 设置数量
  76. // 设置长度
  77. void setItemLength(const int& dotRadius) { m_itemLength = dotRadius; }
  78. void setItemColor(const int& index, const QColor& color); // 设置某个item的颜色
  79. void setItemColors(const QList<QColor>& colors); // 设置item的颜色
  80. void setBackgroundColor(const QColor& color) { m_bgColor = color; } // 设置背景颜色
  81. private slots:
  82. void animationStoped();
  83. protected:
  84. inline int currentFrame() { return m_currentFrame; }
  85. inline void setCurrentFrame(const int& frame) { m_currentFrame = frame; }
  86. inline QColor backgroundColor() { return m_bgColor; }
  87. inline QList<QColor> itemColors() { return m_itemColors; }
  88. inline int totalCount() { return m_totalCount; }
  89. inline void setTotalCount(const int& count) { m_totalCount = count; }
  90. inline int itemLength() { return m_itemLength; }
  91. inline int itemCount() { return m_itemCount; }
  92. protected:
  93. virtual void updateFrame() override;
  94. virtual void updateFrameRects() override;
  95. virtual void paintEvent(QPaintEvent*) override;
  96. virtual void resizeEvent(QResizeEvent*) override;
  97. private:
  98. int m_itemCount; // 运动对象数量
  99. int m_itemLength; // 运动对象
  100. int m_currentFrame; // 当前帧下标
  101. QColor m_bgColor; // 独立显示时的背景颜色
  102. QList<QColor> m_itemColors; // item的颜色
  103. int m_totalCount; // 总共的帧数
  104. };
  105. class LoadingCircle : public LoadingHorizontal
  106. {
  107. Q_OBJECT
  108. public:
  109. explicit LoadingCircle(QWidget *parent = nullptr);
  110. protected:
  111. virtual void updateFrameRects() override;
  112. virtual void paintEvent(QPaintEvent *) override;
  113. signals:
  114. };
  115. class Loading : public LoadingHorizontal
  116. {
  117. Q_OBJECT
  118. public:
  119. explicit Loading(QWidget* parent = nullptr);
  120. void Show();
  121. void Hide();
  122. private:
  123. };
  124. #endif // LOADING_H