BatteryMonitor/upgradeicon.cpp

161 lines
3.8 KiB
C++
Raw Normal View History

2024-11-07 16:04:50 +08:00
#include "upgradeicon.h"
upgradeIcon::upgradeIcon(QWidget *parent) : QWidget(parent)
{
setContentsMargins(0,0,0,0);
borderWidth = 4;
headWidth = 20;
value = 50;
minValue = 0;
maxValue = 100;
alarmValue = 20;
borderRadio = 10;
drawInRadio = 8;
borderColor = QColor(96, 96, 96);
}
void upgradeIcon::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
drawBorder(&painter);
// drawRightHead(&painter);
drawBackground(&painter);
drawText(&painter);
}
void upgradeIcon::resizeEvent(QResizeEvent *)
{
}
// 画边框
void upgradeIcon::drawBorder(QPainter *painter)
{
painter->save();
painter->setPen(QPen(borderColor,borderWidth));
borderRect = QRect(borderWidth,borderWidth,width()-2*borderWidth-headWidth,height()-2*borderWidth);
painter->drawRoundedRect(borderRect,borderRadio,borderRadio);
painter->restore();
}
// 画右边电池头部
void upgradeIcon::drawRightHead(QPainter *painter)
{
painter->save();
double headHeight = height()/2;
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(96, 96, 96));
QRectF headRect = QRectF(width()-headWidth-borderWidth/2,height()/2-headHeight/2,headWidth / 1.75,headHeight);
painter->drawRect(headRect);
painter->restore();
}
// 画数值对应颜色
void upgradeIcon::drawBackground(QPainter *painter)
{
painter->save();
double batteryWidth = width()-headWidth-3*borderWidth; // 电池宽度
double bgWidth= value * batteryWidth / (maxValue-minValue); // 背景绘制宽度
QLinearGradient batteryGradient(QPointF(0, 0), QPointF(borderWidth+bgWidth,height()));
batteryGradient.setColorAt(0.0, QColor(135, 206, 250));
// batteryGradient.setColorAt(1.0, QColor(102, 255, 0));
painter->setBrush(QBrush(batteryGradient));
painter->setPen(Qt::NoPen);
QRectF rectBg = QRectF(borderWidth+borderWidth/2,borderWidth+borderWidth/2,bgWidth,height()-3*borderWidth);
painter->drawRoundedRect(rectBg,drawInRadio,drawInRadio);
painter->restore();
}
// 画百分比数值
void upgradeIcon::drawText(QPainter *painter)
{
painter->save();
QString value_str = QString("%1%").arg(value);
painter->setPen(QPen(QColor(Qt::black)));
double w = qMin(borderRect.width(),borderRect.height());
// 文字大小自适应 90为控件初始高度30位字体默认大小
int fontSize = 30 * w / 90;
painter->setFont(QFont("Microsoft YaHei UI",fontSize));
painter->drawText(borderRect,value_str,QTextOption(Qt::AlignHCenter | Qt::AlignVCenter));
painter->restore();
}
// 设置当前值
void upgradeIcon::setValue(int value)
{
this->value = value;
update();
}
void upgradeIcon::setMinValue(int min)
{
minValue = min;
update();
}
void upgradeIcon::setMaxValue(int max)
{
maxValue = max;
update();
}
void upgradeIcon::setRange(int min, int max)
{
minValue = min;
maxValue = max;
update();
}
void upgradeIcon::setBorderWidth(int width)
{
borderWidth = width;
update();
}
void upgradeIcon::setBorderRadio(int radio)
{
borderRadio = radio;
update();
}
void upgradeIcon::setInRadio(int radio)
{
drawInRadio = radio;
update();
}
void upgradeIcon::setAlarmValue(int alarm)
{
alarmValue = alarm;
update();
}
void upgradeIcon::setBorderColor(QColor color)
{
borderColor = color;
update();
}
int upgradeIcon::getValue()
{
return value;
}
int upgradeIcon::getMinValue()
{
return minValue;
}
int upgradeIcon::getMaxValue()
{
return maxValue;
}
int upgradeIcon::getAlarmValue()
{
return alarmValue;
}