123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #ifndef LOADING_H
- #define LOADING_H
- #include <QWidget>
- #include <QEasingCurve>
- #include <QTimer>
- class BaseLoading : public QWidget
- {
- Q_OBJECT
- public:
- explicit BaseLoading(QWidget *parent = nullptr);
- void setEasingCurve(const QEasingCurve& curve); // 设置插值器
- void setFrameRate(const int& frameRate); // 设置帧率
- void setDuration(const int& duration); // 设置动画时长
- void setExtendDuration(const int& duration); // 设置追击动画时长
- void updateFrameData(); // 更新绘制帧数据
- bool isActive() { return m_animationTimer->isActive(); } // 动画是否已经被激活
- public slots:
- void start(); // 开始动画
- void stop(); // 结束动画
- void updateFrameRate(); // 更新动画刷新率,动画刷新率与屏幕刷新率一致
- protected:
- inline QEasingCurve easingCurve() { return m_curve; }
- inline int frameCount() { return m_frameRects.count(); }
- inline qreal refreshTime() { return m_animationTimer->interval(); }
- inline int duration() { return m_duration; }
- inline int extendFrameCount() { return m_extendFrameCount; }
- // 获取某帧需要的绘制信息
- inline QRect frameRect(const int& index) {
- if (0 <= index && index <= m_frameRects.count())
- return m_frameRects.at(index);
- return QRect();
- }
- inline bool stopLoading() { return m_animationStop; }
- // 设置某帧的绘制信息
- inline void setFrameRect(const int& index,const QRect& rect) {
- if (0 <= index && index < m_frameRects.count())
- m_frameRects[index] = rect;
- }
- // 停止计时器
- inline void stopAnimationTimer() {
- m_animationTimer->stop();
- emit loaddingStoped();
- }
- // 获取某一帧时的值
- inline qreal frameValue(const int& index) {
- if (0 <= index && index < m_frameValues.count()) {
- return m_frameValues.at(index);
- }
- return 0;
- }
- // 计算每帧需要的绘制信息,在调用updateFrameData后会触发此函数
- virtual void updateFrameRects() = 0;
- // 刷新帧,即每次刷新帧都会调用此函数
- virtual void updateFrame() = 0;
- signals:
- void loaddingStoped(); // 加载动画已经停止
- private:
- void updateFrameValues();
- private:
- QEasingCurve m_curve; // 插值器
- QList<QRect> m_frameRects; // 每一帧需要的rect数据
- QTimer* m_animationTimer; // 用于控制动画的计时器
- int m_frameRate; // 帧率
- int m_duration; // 完成一次动画需要的时间
- int m_extendDuration; // 追击延迟时间
- int m_extendFrameCount; // 追击帧数
- bool m_animationStop; // 是否停止动画
- QList<qreal> m_frameValues; // 每一帧对应的数值
- };
- class LoadingHorizontal : public BaseLoading
- {
- Q_OBJECT
- public:
- explicit LoadingHorizontal(QWidget *parent = nullptr);
- void setItemCount(const int& count); // 设置数量
- // 设置长度
- void setItemLength(const int& dotRadius) { m_itemLength = dotRadius; }
- void setItemColor(const int& index, const QColor& color); // 设置某个item的颜色
- void setItemColors(const QList<QColor>& colors); // 设置item的颜色
- void setBackgroundColor(const QColor& color) { m_bgColor = color; } // 设置背景颜色
- private slots:
- void animationStoped();
- protected:
- inline int currentFrame() { return m_currentFrame; }
- inline void setCurrentFrame(const int& frame) { m_currentFrame = frame; }
- inline QColor backgroundColor() { return m_bgColor; }
- inline QList<QColor> itemColors() { return m_itemColors; }
- inline int totalCount() { return m_totalCount; }
- inline void setTotalCount(const int& count) { m_totalCount = count; }
- inline int itemLength() { return m_itemLength; }
- inline int itemCount() { return m_itemCount; }
- protected:
- virtual void updateFrame() override;
- virtual void updateFrameRects() override;
- virtual void paintEvent(QPaintEvent*) override;
- virtual void resizeEvent(QResizeEvent*) override;
- private:
- int m_itemCount; // 运动对象数量
- int m_itemLength; // 运动对象
- int m_currentFrame; // 当前帧下标
- QColor m_bgColor; // 独立显示时的背景颜色
- QList<QColor> m_itemColors; // item的颜色
- int m_totalCount; // 总共的帧数
- };
- class LoadingCircle : public LoadingHorizontal
- {
- Q_OBJECT
- public:
- explicit LoadingCircle(QWidget *parent = nullptr);
- protected:
- virtual void updateFrameRects() override;
- virtual void paintEvent(QPaintEvent *) override;
- signals:
- };
- class Loading : public LoadingHorizontal
- {
- Q_OBJECT
- public:
- explicit Loading(QWidget* parent = nullptr);
-
- void Show();
- void Hide();
-
- private:
- };
- #endif // LOADING_H
|