Rename file battery.h & battery.cpp to BatteryWidget.h & BatteryWidget.cpp, rename class Battery to BatteryWidget, and refactor BatteryWidget.

This commit is contained in:
Krad
2022-06-07 15:15:24 +08:00
parent 365386d50b
commit 9c5838eaf8
4 changed files with 446 additions and 469 deletions

View File

@@ -0,0 +1,421 @@
#include "BatteryWidget.h"
#include <QPainter>
#include <QTimer>
#include <QDebug>
namespace {
const int DEFAULT_WIDTH = 90;
const int DEFAULT_HEIGHT = 120;
const int DEFAULT_MIN_WIDTH = 30;
const int DEFAULT_MIN_HEIGHT = 40;
}
BatteryWidget::BatteryWidget(QWidget* parent)
: QWidget(parent)
{
mMinValue = 0;
mMaxValue = 100;
mValue = 0;
mAlarmValue = 90;
mStep = 0.5;
mBorderWidth = 5;
mBorderRadius = 8;
mBgRadius = 5;
mHeadRadius = 3;
mBorderColorStart = QColor(100, 100, 100);
mBorderColorEnd = QColor(80, 80, 80);
mAlarmColorStart = QColor(250, 118, 113);
mAlarmColorEnd = QColor(204, 38, 38);
mNormalColorStart = QColor(50, 205, 51);
mNormalColorEnd = QColor(60, 179, 133);
mIsForward = false;
mCurrentValue = 0;
mTimer = new QTimer(this);
mTimer->setInterval(10);
connect(mTimer, SIGNAL(timeout()), this, SLOT(updateValue()));
this->setFixedSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
this->setMinimumSize(DEFAULT_MIN_WIDTH,DEFAULT_MIN_HEIGHT);
}
BatteryWidget::~BatteryWidget()
{
if (mTimer->isActive()) {
mTimer->stop();
}
}
void BatteryWidget::paintEvent(QPaintEvent*)
{
//绘制准备工作,启用反锯齿
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//绘制边框
drawBorder(&painter);
//绘制背景
drawBg(&painter);
//绘制头部
//drawHead(&painter);
}
void BatteryWidget::drawBorder(QPainter* painter)
{
painter->save();
//double headWidth = width() / 15;
double batteryWidth = width() - mBorderWidth;
//绘制电池边框
QPointF topLeft(mBorderWidth, mBorderWidth);
QPointF bottomRight(batteryWidth, height() - mBorderWidth);
mBatteryRect = QRectF(topLeft, bottomRight);
painter->setPen(QPen(mBorderColorStart, mBorderWidth));
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(mBatteryRect, mBorderRadius, mBorderRadius);
painter->restore();
}
void BatteryWidget::drawBg(QPainter* painter)
{
if (mValue == mMinValue) {
return;
}
painter->save();
QLinearGradient batteryGradient(QPointF(0, 0), QPointF(0, height()));
if (mCurrentValue >= mAlarmValue) {
batteryGradient.setColorAt(0.0, mAlarmColorStart);
batteryGradient.setColorAt(1.0, mAlarmColorEnd);
}
else {
batteryGradient.setColorAt(0.0, mNormalColorStart);
batteryGradient.setColorAt(1.0, mNormalColorEnd);
}
int margin = qMin(width(), height()) / 20;
double unit = (mBatteryRect.height() - (margin * 2)) / mMaxValue;
double height = (mMaxValue - mCurrentValue) * unit;
QPointF topLeft(mBatteryRect.topLeft().x() + margin, margin + height);
QPointF bottomRight(mBatteryRect.bottomRight().x() - margin, mBatteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, mBgRadius, mBgRadius);
painter->restore();
}
void BatteryWidget::drawHead(QPainter* painter)
{
painter->save();
QPointF headRectTopLeft(mBatteryRect.topRight().x(), height() / 3);
QPointF headRectBottomRight(width(), height() - height() / 3);
QRectF headRect(headRectTopLeft, headRectBottomRight);
QLinearGradient headRectGradient(headRect.topLeft(), headRect.bottomLeft());
headRectGradient.setColorAt(0.0, mBorderColorStart);
headRectGradient.setColorAt(1.0, mBorderColorEnd);
painter->setPen(Qt::NoPen);
painter->setBrush(headRectGradient);
painter->drawRoundedRect(headRect, mHeadRadius, mHeadRadius);
painter->restore();
}
void BatteryWidget::updateValue()
{
if (mIsForward) {
mCurrentValue -= mStep;
if (mCurrentValue <= mValue) {
mCurrentValue = mValue;
mTimer->stop();
}
}
else {
mCurrentValue += mStep;
if (mCurrentValue >= mValue) {
mCurrentValue = mValue;
mTimer->stop();
}
}
this->update();
}
double BatteryWidget::getMinValue() const
{
return this->mMinValue;
}
double BatteryWidget::getMaxValue() const
{
return this->mMaxValue;
}
double BatteryWidget::getValue() const
{
return this->mValue;
}
double BatteryWidget::getAlarmValue() const
{
return this->mAlarmValue;
}
double BatteryWidget::getStep() const
{
return this->mStep;
}
int BatteryWidget::getBorderWidth() const
{
return this->mBorderWidth;
}
int BatteryWidget::getBorderRadius() const
{
return this->mBorderRadius;
}
int BatteryWidget::getBgRadius() const
{
return this->mBgRadius;
}
int BatteryWidget::getHeadRadius() const
{
return this->mHeadRadius;
}
QColor BatteryWidget::getBorderColorStart() const
{
return this->mBorderColorStart;
}
QColor BatteryWidget::getBorderColorEnd() const
{
return this->mBorderColorEnd;
}
QColor BatteryWidget::getAlarmColorStart() const
{
return this->mAlarmColorStart;
}
QColor BatteryWidget::getAlarmColorEnd() const
{
return this->mAlarmColorEnd;
}
QColor BatteryWidget::getNormalColorStart() const
{
return this->mNormalColorStart;
}
QColor BatteryWidget::getNormalColorEnd() const
{
return this->mNormalColorEnd;
}
void BatteryWidget::setRange(double minVal, double maxVal)
{
//如果最小值大于或者等于最大值则不设置
if (minVal >= maxVal) {
return;
}
this->mMinValue = minVal;
this->mMaxValue = maxVal;
//如果目标值不在范围值内,则重新设置目标值
//值小于最小值则取最小值,大于最大值则取最大值
if (mValue < mMinValue) {
setValue(mMinValue);
}
else if (mValue > mMaxValue) {
setValue(mMaxValue);
}
this->update();
}
void BatteryWidget::setRange(int minVal, int maxVal)
{
setRange((double)minVal, (double)maxVal);
}
void BatteryWidget::setMinValue(double minVal)
{
setRange(minVal, mMaxValue);
}
void BatteryWidget::setMaxValue(double maxVal)
{
setRange(mMinValue, maxVal);
}
void BatteryWidget::setValue(double value)
{
//值和当前值一致则无需处理
if (value == this->mValue) {
return;
}
//值小于最小值则取最小值,大于最大值则取最大值
if (value < mMinValue) {
value = mMinValue;
}
else if (value > mMaxValue) {
value = mMaxValue;
}
if (value > mCurrentValue) {
mIsForward = false;
}
else if (value < mCurrentValue) {
mIsForward = true;
}
else {
this->mValue = value;
this->update();
return;
}
this->mValue = value;
this->update();
emit valueChanged(value);
//timer->stop();
//timer->start();
}
void BatteryWidget::startAnimation()
{
mTimer->stop();
mTimer->start();
}
void BatteryWidget::setValue(int value)
{
setValue((double)value);
}
void BatteryWidget::setAlarmValue(double alarmValue)
{
if (this->mAlarmValue != alarmValue) {
this->mAlarmValue = alarmValue;
this->update();
}
}
void BatteryWidget::setAlarmValue(int alarmValue)
{
setAlarmValue((double)alarmValue);
}
void BatteryWidget::setStep(double step)
{
if (this->mStep != step) {
this->mStep = step;
this->update();
}
}
void BatteryWidget::setStep(int step)
{
setStep((double)step);
}
void BatteryWidget::setBorderWidth(int borderWidth)
{
if (this->mBorderWidth != borderWidth) {
this->mBorderWidth = borderWidth;
this->update();
}
}
void BatteryWidget::setBorderRadius(int borderRadius)
{
if (this->mBorderRadius != borderRadius) {
this->mBorderRadius = borderRadius;
this->update();
}
}
void BatteryWidget::setBgRadius(int bgRadius)
{
if (this->mBgRadius != bgRadius) {
this->mBgRadius = bgRadius;
this->update();
}
}
void BatteryWidget::setHeadRadius(int headRadius)
{
if (this->mHeadRadius != headRadius) {
this->mHeadRadius = headRadius;
this->update();
}
}
void BatteryWidget::setBorderColorStart(const QColor& borderColorStart)
{
if (this->mBorderColorStart != borderColorStart) {
this->mBorderColorStart = borderColorStart;
this->update();
}
}
void BatteryWidget::setBorderColorEnd(const QColor& borderColorEnd)
{
if (this->mBorderColorEnd != borderColorEnd) {
this->mBorderColorEnd = borderColorEnd;
this->update();
}
}
void BatteryWidget::setAlarmColorStart(const QColor& alarmColorStart)
{
if (this->mAlarmColorStart != alarmColorStart) {
this->mAlarmColorStart = alarmColorStart;
this->update();
}
}
void BatteryWidget::setAlarmColorEnd(const QColor& alarmColorEnd)
{
if (this->mAlarmColorEnd != alarmColorEnd) {
this->mAlarmColorEnd = alarmColorEnd;
this->update();
}
}
void BatteryWidget::setNormalColorStart(const QColor& normalColorStart)
{
if (this->mNormalColorStart != normalColorStart) {
this->mNormalColorStart = normalColorStart;
this->update();
}
}
void BatteryWidget::setNormalColorEnd(const QColor& normalColorEnd)
{
if (this->mNormalColorEnd != normalColorEnd) {
this->mNormalColorEnd = normalColorEnd;
this->update();
}
}

View File

@@ -1,24 +1,9 @@
#ifndef BATTERY_H
#define BATTERY_H
/**
* :feiyangqingyun(QQ:517216493) 2016-10-23
* 1.
* 2.
* 3.
* 4.
* 5.
* 6.
*/
#include <QWidget>
#ifdef quc
class Q_DECL_EXPORT Battery : public QWidget
#else
class Battery : public QWidget
#endif
class BatteryWidget : public QWidget
{
Q_OBJECT
@@ -43,11 +28,11 @@ class Battery : public QWidget
Q_PROPERTY(QColor normalColorEnd READ getNormalColorEnd WRITE setNormalColorEnd)
public:
explicit Battery(QWidget* parent = 0);
~Battery();
explicit BatteryWidget(QWidget* parent = nullptr);
~BatteryWidget() override;
protected:
void paintEvent(QPaintEvent*);
void paintEvent(QPaintEvent*) override ;
void drawBorder(QPainter* painter);
void drawBg(QPainter* painter);
void drawHead(QPainter* painter);
@@ -57,30 +42,30 @@ private slots:
void updateValue();
private:
double minValue; //最小值
double maxValue; //最大值
double value; //目标电量
double alarmValue; //电池电量警戒值
double mMinValue; //最小值
double mMaxValue; //最大值
double mValue; //目标电量
double mAlarmValue; //电池电量警戒值
double step; //每次移动的步长
int borderWidth; //边框粗细
int borderRadius; //边框圆角角度
int bgRadius; //背景进度圆角角度
int headRadius; //头部圆角角度
double mStep; //每次移动的步长
int mBorderWidth; //边框粗细
int mBorderRadius; //边框圆角角度
int mBgRadius; //背景进度圆角角度
int mHeadRadius; //头部圆角角度
QColor borderColorStart; //边框渐变开始颜色
QColor borderColorEnd; //边框渐变结束颜色
QColor mBorderColorStart; //边框渐变开始颜色
QColor mBorderColorEnd; //边框渐变结束颜色
QColor alarmColorStart; //电池低电量时的渐变开始颜色
QColor alarmColorEnd; //电池低电量时的渐变结束颜色
QColor mAlarmColorStart; //电池低电量时的渐变开始颜色
QColor mAlarmColorEnd; //电池低电量时的渐变结束颜色
QColor normalColorStart; //电池正常电量时的渐变开始颜色
QColor normalColorEnd; //电池正常电量时的渐变结束颜色
QColor mNormalColorStart; //电池正常电量时的渐变开始颜色
QColor mNormalColorEnd; //电池正常电量时的渐变结束颜色
bool isForward; //是否往前移
double currentValue; //当前电量
QRectF batteryRect; //电池主体区域
QTimer* timer; //绘制定时器
bool mIsForward; //是否往前移
double mCurrentValue; //当前电量
QRectF mBatteryRect; //电池主体区域
QTimer* mTimer; //绘制定时器
public:
double getMinValue() const;
@@ -103,8 +88,6 @@ public:
QColor getNormalColorStart() const;
QColor getNormalColorEnd() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置范围值

View File

@@ -1,427 +0,0 @@
#pragma execution_character_set("utf-8")
#include "battery.h"
#include <QPainter>
#include <QTimer>
#include <QDebug>
Battery::Battery(QWidget* parent) : QWidget(parent)
{
minValue = 0;
maxValue = 100;
value = 0;
alarmValue = 90;
step = 0.5;
borderWidth = 5;
borderRadius = 8;
bgRadius = 5;
headRadius = 3;
borderColorStart = QColor(100, 100, 100);
borderColorEnd = QColor(80, 80, 80);
alarmColorStart = QColor(250, 118, 113);
alarmColorEnd = QColor(204, 38, 38);
normalColorStart = QColor(50, 205, 51);
normalColorEnd = QColor(60, 179, 133);
isForward = false;
currentValue = 0;
timer = new QTimer(this);
timer->setInterval(10);
connect(timer, SIGNAL(timeout()), this, SLOT(updateValue()));
this->setFixedSize(sizeHint());
}
Battery::~Battery()
{
if (timer->isActive()) {
timer->stop();
}
}
void Battery::paintEvent(QPaintEvent*)
{
//绘制准备工作,启用反锯齿
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//绘制边框
drawBorder(&painter);
//绘制背景
drawBg(&painter);
//绘制头部
//drawHead(&painter);
}
void Battery::drawBorder(QPainter* painter)
{
painter->save();
//double headWidth = width() / 15;
double batteryWidth = width() - borderWidth;
//绘制电池边框
QPointF topLeft(borderWidth, borderWidth);
QPointF bottomRight(batteryWidth, height() - borderWidth);
batteryRect = QRectF(topLeft, bottomRight);
painter->setPen(QPen(borderColorStart, borderWidth));
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);
painter->restore();
}
void Battery::drawBg(QPainter* painter)
{
if (value == minValue) {
return;
}
painter->save();
QLinearGradient batteryGradient(QPointF(0, 0), QPointF(0, height()));
if (currentValue >= alarmValue) {
batteryGradient.setColorAt(0.0, alarmColorStart);
batteryGradient.setColorAt(1.0, alarmColorEnd);
}
else {
batteryGradient.setColorAt(0.0, normalColorStart);
batteryGradient.setColorAt(1.0, normalColorEnd);
}
int margin = qMin(width(), height()) / 20;
//double unit = (batteryRect.width() - (margin * 2)) / 100;
//double width = currentValue * unit;
//QPointF topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y() + margin);
//QPointF bottomRight(width + margin + borderWidth, batteryRect.bottomRight().y() - margin);
double unit = (batteryRect.height() - (margin * 2)) / maxValue;
double height = (maxValue - currentValue) * unit;
QPointF topLeft(batteryRect.topLeft().x() + margin, margin + height);
QPointF bottomRight(batteryRect.bottomRight().x() - margin, batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
painter->restore();
}
void Battery::drawHead(QPainter* painter)
{
painter->save();
QPointF headRectTopLeft(batteryRect.topRight().x(), height() / 3);
QPointF headRectBottomRight(width(), height() - height() / 3);
QRectF headRect(headRectTopLeft, headRectBottomRight);
QLinearGradient headRectGradient(headRect.topLeft(), headRect.bottomLeft());
headRectGradient.setColorAt(0.0, borderColorStart);
headRectGradient.setColorAt(1.0, borderColorEnd);
painter->setPen(Qt::NoPen);
painter->setBrush(headRectGradient);
painter->drawRoundedRect(headRect, headRadius, headRadius);
painter->restore();
}
void Battery::updateValue()
{
if (isForward) {
currentValue -= step;
if (currentValue <= value) {
currentValue = value;
timer->stop();
}
}
else {
currentValue += step;
if (currentValue >= value) {
currentValue = value;
timer->stop();
}
}
this->update();
}
double Battery::getMinValue() const
{
return this->minValue;
}
double Battery::getMaxValue() const
{
return this->maxValue;
}
double Battery::getValue() const
{
return this->value;
}
double Battery::getAlarmValue() const
{
return this->alarmValue;
}
double Battery::getStep() const
{
return this->step;
}
int Battery::getBorderWidth() const
{
return this->borderWidth;
}
int Battery::getBorderRadius() const
{
return this->borderRadius;
}
int Battery::getBgRadius() const
{
return this->bgRadius;
}
int Battery::getHeadRadius() const
{
return this->headRadius;
}
QColor Battery::getBorderColorStart() const
{
return this->borderColorStart;
}
QColor Battery::getBorderColorEnd() const
{
return this->borderColorEnd;
}
QColor Battery::getAlarmColorStart() const
{
return this->alarmColorStart;
}
QColor Battery::getAlarmColorEnd() const
{
return this->alarmColorEnd;
}
QColor Battery::getNormalColorStart() const
{
return this->normalColorStart;
}
QColor Battery::getNormalColorEnd() const
{
return this->normalColorEnd;
}
QSize Battery::sizeHint() const
{
return {90, 120};
}
QSize Battery::minimumSizeHint() const
{
return {10, 30};
}
void Battery::setRange(double minVal, double maxVal)
{
//如果最小值大于或者等于最大值则不设置
if (minVal >= maxVal) {
return;
}
this->minValue = minVal;
this->maxValue = maxVal;
//如果目标值不在范围值内,则重新设置目标值
//值小于最小值则取最小值,大于最大值则取最大值
if (value < minValue) {
setValue(minValue);
}
else if (value > maxValue) {
setValue(maxValue);
}
this->update();
}
void Battery::setRange(int minVal, int maxVal)
{
setRange((double)minVal, (double)maxVal);
}
void Battery::setMinValue(double minVal)
{
setRange(minVal, maxValue);
}
void Battery::setMaxValue(double maxVal)
{
setRange(minValue, maxVal);
}
void Battery::setValue(double value)
{
//值和当前值一致则无需处理
if (value == this->value) {
return;
}
//值小于最小值则取最小值,大于最大值则取最大值
if (value < minValue) {
value = minValue;
}
else if (value > maxValue) {
value = maxValue;
}
if (value > currentValue) {
isForward = false;
}
else if (value < currentValue) {
isForward = true;
}
else {
this->value = value;
this->update();
return;
}
this->value = value;
this->update();
emit valueChanged(value);
//timer->stop();
//timer->start();
}
void Battery::startAnimation()
{
timer->stop();
timer->start();
}
void Battery::setValue(int value)
{
setValue((double)value);
}
void Battery::setAlarmValue(double alarmValue)
{
if (this->alarmValue != alarmValue) {
this->alarmValue = alarmValue;
this->update();
}
}
void Battery::setAlarmValue(int alarmValue)
{
setAlarmValue((double)alarmValue);
}
void Battery::setStep(double step)
{
if (this->step != step) {
this->step = step;
this->update();
}
}
void Battery::setStep(int step)
{
setStep((double)step);
}
void Battery::setBorderWidth(int borderWidth)
{
if (this->borderWidth != borderWidth) {
this->borderWidth = borderWidth;
this->update();
}
}
void Battery::setBorderRadius(int borderRadius)
{
if (this->borderRadius != borderRadius) {
this->borderRadius = borderRadius;
this->update();
}
}
void Battery::setBgRadius(int bgRadius)
{
if (this->bgRadius != bgRadius) {
this->bgRadius = bgRadius;
this->update();
}
}
void Battery::setHeadRadius(int headRadius)
{
if (this->headRadius != headRadius) {
this->headRadius = headRadius;
this->update();
}
}
void Battery::setBorderColorStart(const QColor& borderColorStart)
{
if (this->borderColorStart != borderColorStart) {
this->borderColorStart = borderColorStart;
this->update();
}
}
void Battery::setBorderColorEnd(const QColor& borderColorEnd)
{
if (this->borderColorEnd != borderColorEnd) {
this->borderColorEnd = borderColorEnd;
this->update();
}
}
void Battery::setAlarmColorStart(const QColor& alarmColorStart)
{
if (this->alarmColorStart != alarmColorStart) {
this->alarmColorStart = alarmColorStart;
this->update();
}
}
void Battery::setAlarmColorEnd(const QColor& alarmColorEnd)
{
if (this->alarmColorEnd != alarmColorEnd) {
this->alarmColorEnd = alarmColorEnd;
this->update();
}
}
void Battery::setNormalColorStart(const QColor& normalColorStart)
{
if (this->normalColorStart != normalColorStart) {
this->normalColorStart = normalColorStart;
this->update();
}
}
void Battery::setNormalColorEnd(const QColor& normalColorEnd)
{
if (this->normalColorEnd != normalColorEnd) {
this->normalColorEnd = normalColorEnd;
this->update();
}
}

View File

@@ -275,7 +275,7 @@
<widget class="QWidget" name="widget_5" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="Battery" name="batIcon" native="true"/>
<widget class="BatteryWidget" name="batIcon" native="true"/>
</item>
<item>
<widget class="QWidget" name="widget_6" native="true">
@@ -336,7 +336,7 @@
<container>1</container>
</customwidget>
<customwidget>
<class>Battery</class>
<class>BatteryWidget</class>
<extends>QWidget</extends>
<header location="global">components/battery.h</header>
<container>1</container>