qt_demoe/control/battery/battery.cpp

422 lines
8.9 KiB
C++
Raw Normal View History

2019-10-23 06:32:50 +00:00
#pragma execution_character_set("utf-8")
#include "battery.h"
#include "qpainter.h"
#include "qtimer.h"
#include "qdebug.h"
Battery::Battery(QWidget *parent) : QWidget(parent)
{
minValue = 0;
maxValue = 100;
value = 0;
alarmValue = 30;
2023-03-20 07:04:23 +00:00
animation = true;
animationStep = 0.5;
2019-10-23 06:32:50 +00:00
2020-12-24 10:00:09 +00:00
borderWidth = 5;
2019-10-23 06:32:50 +00:00
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()));
}
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();
2020-12-24 10:00:09 +00:00
double headWidth = width() / 15;
double batteryWidth = width() - headWidth;
2019-10-23 06:32:50 +00:00
//绘制电池边框
2020-12-24 10:00:09 +00:00
QPointF topLeft(borderWidth, borderWidth);
QPointF bottomRight(batteryWidth, height() - borderWidth);
2019-10-23 06:32:50 +00:00
batteryRect = QRectF(topLeft, bottomRight);
2020-12-24 10:00:09 +00:00
painter->setPen(QPen(borderColorStart, borderWidth));
2019-10-23 06:32:50 +00:00
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);
painter->restore();
}
void Battery::drawBg(QPainter *painter)
{
2020-12-24 10:00:09 +00:00
if (value == minValue) {
return;
}
2019-10-23 06:32:50 +00:00
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;
2023-03-20 07:04:23 +00:00
double unit = (batteryRect.width() - (margin * 2)) / (maxValue - minValue);
2020-12-24 10:00:09 +00:00
double width = currentValue * unit;
2019-10-23 06:32:50 +00:00
QPointF topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y() + margin);
2020-12-24 10:00:09 +00:00
QPointF bottomRight(width + margin + borderWidth, batteryRect.bottomRight().y() - margin);
2019-10-23 06:32:50 +00:00
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) {
2023-03-20 07:04:23 +00:00
currentValue -= animationStep;
2019-10-23 06:32:50 +00:00
if (currentValue <= value) {
2021-05-30 07:59:42 +00:00
currentValue = value;
2019-10-23 06:32:50 +00:00
timer->stop();
}
} else {
2023-03-20 07:04:23 +00:00
currentValue += animationStep;
2019-10-23 06:32:50 +00:00
if (currentValue >= value) {
2021-05-30 07:59:42 +00:00
currentValue = value;
2019-10-23 06:32:50 +00:00
timer->stop();
}
}
this->update();
}
2020-12-24 10:00:09 +00:00
double Battery::getMinValue() const
2019-10-23 06:32:50 +00:00
{
return this->minValue;
}
2020-12-24 10:00:09 +00:00
double Battery::getMaxValue() const
2019-10-23 06:32:50 +00:00
{
return this->maxValue;
}
2020-12-24 10:00:09 +00:00
double Battery::getValue() const
2019-10-23 06:32:50 +00:00
{
return this->value;
}
2020-12-24 10:00:09 +00:00
double Battery::getAlarmValue() const
2019-10-23 06:32:50 +00:00
{
return this->alarmValue;
}
2023-03-20 07:04:23 +00:00
bool Battery::getAnimation() const
{
return this->animation;
}
double Battery::getAnimationStep() const
2019-10-23 06:32:50 +00:00
{
2023-03-20 07:04:23 +00:00
return this->animationStep;
2019-10-23 06:32:50 +00:00
}
2020-12-24 10:00:09 +00:00
int Battery::getBorderWidth() const
{
return this->borderWidth;
}
2019-10-23 06:32:50 +00:00
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 QSize(150, 80);
}
QSize Battery::minimumSizeHint() const
{
return QSize(30, 10);
}
2020-12-24 10:00:09 +00:00
void Battery::setRange(double minValue, double maxValue)
2019-10-23 06:32:50 +00:00
{
//如果最小值大于或者等于最大值则不设置
if (minValue >= maxValue) {
return;
}
this->minValue = minValue;
this->maxValue = maxValue;
//如果目标值不在范围值内,则重新设置目标值
//值小于最小值则取最小值,大于最大值则取最大值
if (value < minValue) {
setValue(minValue);
} else if (value > maxValue) {
setValue(maxValue);
}
this->update();
}
void Battery::setRange(int minValue, int maxValue)
{
2020-12-24 10:00:09 +00:00
setRange((double)minValue, (double)maxValue);
2019-10-23 06:32:50 +00:00
}
2020-12-24 10:00:09 +00:00
void Battery::setMinValue(double minValue)
2019-10-23 06:32:50 +00:00
{
setRange(minValue, maxValue);
}
2020-12-24 10:00:09 +00:00
void Battery::setMaxValue(double maxValue)
2019-10-23 06:32:50 +00:00
{
setRange(minValue, maxValue);
}
2020-12-24 10:00:09 +00:00
void Battery::setValue(double value)
2019-10-23 06:32:50 +00:00
{
//值和当前值一致则无需处理
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 {
2020-12-24 10:00:09 +00:00
this->value = value;
this->update();
2019-10-23 06:32:50 +00:00
return;
}
this->value = value;
emit valueChanged(value);
2023-03-20 07:04:23 +00:00
if (animation) {
timer->stop();
timer->start();
} else {
this->currentValue = value;
this->update();
}
2019-10-23 06:32:50 +00:00
}
void Battery::setValue(int value)
{
2020-12-24 10:00:09 +00:00
setValue((double)value);
2019-10-23 06:32:50 +00:00
}
2020-12-24 10:00:09 +00:00
void Battery::setAlarmValue(double alarmValue)
2019-10-23 06:32:50 +00:00
{
if (this->alarmValue != alarmValue) {
this->alarmValue = alarmValue;
this->update();
}
}
void Battery::setAlarmValue(int alarmValue)
{
2020-12-24 10:00:09 +00:00
setAlarmValue((double)alarmValue);
2019-10-23 06:32:50 +00:00
}
2023-03-20 07:04:23 +00:00
void Battery::setAnimation(bool animation)
2019-10-23 06:32:50 +00:00
{
2023-03-20 07:04:23 +00:00
if (this->animation != animation) {
this->animation = animation;
2019-10-23 06:32:50 +00:00
this->update();
}
}
2023-03-20 07:04:23 +00:00
void Battery::setAnimationStep(double animationStep)
2019-10-23 06:32:50 +00:00
{
2023-03-20 07:04:23 +00:00
if (this->animationStep != animationStep) {
this->animationStep = animationStep;
this->update();
}
2020-12-24 10:00:09 +00:00
}
void Battery::setBorderWidth(int borderWidth)
{
if (this->borderWidth != borderWidth) {
this->borderWidth = borderWidth;
this->update();
}
2019-10-23 06:32:50 +00:00
}
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();
}
}