CircleProgressLabel.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef CIRCLEPROGRESSLABEL_H
  2. #define CIRCLEPROGRESSLABEL_H
  3. #include <QLabel>
  4. #include <QObject>
  5. struct Cache {
  6. qreal maxR;
  7. QPointF center;
  8. qreal tickRadius;
  9. QVector<qreal> ringRadii;
  10. };
  11. /**
  12. * @brief CircleProgressLabel 是一个继承自 QLabel 的自定义控件,用于显示圆形进度条。
  13. *
  14. * 该类主要用于在界面上显示一个圆形进度条,并在中心显示当前进度值和提示信息。
  15. * 可以通过设置不同的颜色主题来改变进度条和中心区域的显示效果。
  16. *
  17. * 核心功能包括:
  18. * - 设置当前进度值(0.0 到 1.0)
  19. * - 设置中心显示的值和提示信息
  20. * - 设置主题颜色(主色和副色)
  21. * - 设置准备状态
  22. *
  23. * 使用示例:
  24. *
  25. * 构造函数参数:
  26. * - QWidget *parent:指定父窗口,默认为 nullptr。
  27. *
  28. * 特殊使用限制或潜在的副作用:
  29. * - 进度值必须在 0.0 到 1.0 之间,否则可能导致显示异常。
  30. * - 主题颜色的设置会影响进度条和中心区域的显示效果,请确保颜色搭配合理。
  31. */
  32. class CircleProgressLabel : public QLabel
  33. {
  34. Q_OBJECT
  35. public:
  36. explicit CircleProgressLabel(QWidget *parent = nullptr);
  37. void setProgress(qreal value); // 0.0 ~ 1.0
  38. void setCenterValue(const QString &newCenterValue);
  39. void setCenterTips(const QString &newCenterTips);
  40. void setThemeColor(const QColor& main, const QColor& sub);
  41. void setPrepareState(int newPrepareState = 0);
  42. private:
  43. void updateTheme(); // 根据主色自动调整渐变
  44. protected:
  45. void paintEvent(QPaintEvent *event) override;
  46. void resizeEvent(QResizeEvent* event) override;
  47. private:
  48. qreal m_progress = 0.75; // 进度,0~1
  49. QString m_centerValue = "%75";
  50. QString m_centerTips = "Charging";
  51. int m_prepareState;
  52. Cache m_cache;
  53. QColor m_mainColor = QColor(0xFF, 0x7A, 0x2F); // 主色(橙/蓝)
  54. QColor m_subColor = QColor(0xFF, 0xB3, 0x6B); // 副色(浅橙/浅蓝)
  55. QColor m_centerGradStart = QColor(0x6B, 0x3B, 0x1A);
  56. QColor m_centerGradEnd = QColor(0x8B, 0x52, 0x20);
  57. };
  58. #endif // CIRCLEPROGRESSLABEL_H