Rename file battery.h & battery.cpp to BatteryWidget.h & BatteryWidget.cpp, rename class Battery to BatteryWidget, and refactor BatteryWidget.
This commit is contained in:
138
src/components/BatteryWidget.h
Normal file
138
src/components/BatteryWidget.h
Normal file
@@ -0,0 +1,138 @@
|
||||
#ifndef BATTERY_H
|
||||
#define BATTERY_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class BatteryWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
|
||||
Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
|
||||
Q_PROPERTY(double value READ getValue WRITE setValue)
|
||||
Q_PROPERTY(double alarmValue READ getAlarmValue WRITE setAlarmValue)
|
||||
|
||||
Q_PROPERTY(double step READ getStep WRITE setStep)
|
||||
Q_PROPERTY(int borderWidth READ getBorderWidth WRITE setBorderWidth)
|
||||
Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
|
||||
Q_PROPERTY(int bgRadius READ getBgRadius WRITE setBgRadius)
|
||||
Q_PROPERTY(int headRadius READ getHeadRadius WRITE setHeadRadius)
|
||||
|
||||
Q_PROPERTY(QColor borderColorStart READ getBorderColorStart WRITE setBorderColorStart)
|
||||
Q_PROPERTY(QColor borderColorEnd READ getBorderColorEnd WRITE setBorderColorEnd)
|
||||
|
||||
Q_PROPERTY(QColor alarmColorStart READ getAlarmColorStart WRITE setAlarmColorStart)
|
||||
Q_PROPERTY(QColor alarmColorEnd READ getAlarmColorEnd WRITE setAlarmColorEnd)
|
||||
|
||||
Q_PROPERTY(QColor normalColorStart READ getNormalColorStart WRITE setNormalColorStart)
|
||||
Q_PROPERTY(QColor normalColorEnd READ getNormalColorEnd WRITE setNormalColorEnd)
|
||||
|
||||
public:
|
||||
explicit BatteryWidget(QWidget* parent = nullptr);
|
||||
~BatteryWidget() override;
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent*) override ;
|
||||
void drawBorder(QPainter* painter);
|
||||
void drawBg(QPainter* painter);
|
||||
void drawHead(QPainter* painter);
|
||||
|
||||
private slots:
|
||||
void startAnimation();
|
||||
void updateValue();
|
||||
|
||||
private:
|
||||
double mMinValue; //最小值
|
||||
double mMaxValue; //最大值
|
||||
double mValue; //目标电量
|
||||
double mAlarmValue; //电池电量警戒值
|
||||
|
||||
double mStep; //每次移动的步长
|
||||
int mBorderWidth; //边框粗细
|
||||
int mBorderRadius; //边框圆角角度
|
||||
int mBgRadius; //背景进度圆角角度
|
||||
int mHeadRadius; //头部圆角角度
|
||||
|
||||
QColor mBorderColorStart; //边框渐变开始颜色
|
||||
QColor mBorderColorEnd; //边框渐变结束颜色
|
||||
|
||||
QColor mAlarmColorStart; //电池低电量时的渐变开始颜色
|
||||
QColor mAlarmColorEnd; //电池低电量时的渐变结束颜色
|
||||
|
||||
QColor mNormalColorStart; //电池正常电量时的渐变开始颜色
|
||||
QColor mNormalColorEnd; //电池正常电量时的渐变结束颜色
|
||||
|
||||
bool mIsForward; //是否往前移
|
||||
double mCurrentValue; //当前电量
|
||||
QRectF mBatteryRect; //电池主体区域
|
||||
QTimer* mTimer; //绘制定时器
|
||||
|
||||
public:
|
||||
double getMinValue() const;
|
||||
double getMaxValue() const;
|
||||
double getValue() const;
|
||||
double getAlarmValue() const;
|
||||
|
||||
double getStep() const;
|
||||
int getBorderWidth() const;
|
||||
int getBorderRadius() const;
|
||||
int getBgRadius() const;
|
||||
int getHeadRadius() const;
|
||||
|
||||
QColor getBorderColorStart() const;
|
||||
QColor getBorderColorEnd() const;
|
||||
|
||||
QColor getAlarmColorStart() const;
|
||||
QColor getAlarmColorEnd() const;
|
||||
|
||||
QColor getNormalColorStart() const;
|
||||
QColor getNormalColorEnd() const;
|
||||
|
||||
|
||||
public Q_SLOTS:
|
||||
//设置范围值
|
||||
void setRange(double minValue, double maxValue);
|
||||
void setRange(int minValue, int maxValue);
|
||||
|
||||
//设置最大最小值
|
||||
void setMinValue(double minValue);
|
||||
void setMaxValue(double maxValue);
|
||||
|
||||
//设置电池电量值
|
||||
void setValue(double value);
|
||||
void setValue(int value);
|
||||
|
||||
//设置电池电量警戒值
|
||||
void setAlarmValue(double alarmValue);
|
||||
void setAlarmValue(int alarmValue);
|
||||
|
||||
//设置步长
|
||||
void setStep(double step);
|
||||
void setStep(int step);
|
||||
|
||||
//设置边框粗细
|
||||
void setBorderWidth(int borderWidth);
|
||||
//设置边框圆角角度
|
||||
void setBorderRadius(int borderRadius);
|
||||
//设置背景圆角角度
|
||||
void setBgRadius(int bgRadius);
|
||||
//设置头部圆角角度
|
||||
void setHeadRadius(int headRadius);
|
||||
|
||||
//设置边框渐变颜色
|
||||
void setBorderColorStart(const QColor& borderColorStart);
|
||||
void setBorderColorEnd(const QColor& borderColorEnd);
|
||||
|
||||
//设置电池电量报警时的渐变颜色
|
||||
void setAlarmColorStart(const QColor& alarmColorStart);
|
||||
void setAlarmColorEnd(const QColor& alarmColorEnd);
|
||||
|
||||
//设置电池电量正常时的渐变颜色
|
||||
void setNormalColorStart(const QColor& normalColorStart);
|
||||
void setNormalColorEnd(const QColor& normalColorEnd);
|
||||
|
||||
Q_SIGNALS:
|
||||
void valueChanged(double value);
|
||||
};
|
||||
|
||||
#endif // BATTERY_H
|
||||
Reference in New Issue
Block a user