Files
GUI/src/components/BatteryWidget.cpp

422 lines
8.5 KiB
C++

#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();
}
}