2025-12-01 20:29:36 +08:00
|
|
|
|
#include "monitorItemPreviewDlg.h"
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
|
|
|
|
MonitorItemPreviewDlg::MonitorItemPreviewDlg(QWidget* parent)
|
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
initial();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MonitorItemPreviewDlg::~MonitorItemPreviewDlg()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MonitorItemPreviewDlg::initial()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MonitorItemPreviewDlg::setSvgFile(const QByteArray &bytSvg)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_renderer.load(bytSvg);
|
|
|
|
|
|
_curSvg = bytSvg;
|
|
|
|
|
|
update(); // 触发重绘
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MonitorItemPreviewDlg::setColors(const QColor &color)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Color = color;
|
|
|
|
|
|
update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-03 18:27:49 +08:00
|
|
|
|
void MonitorItemPreviewDlg::clearSvg()
|
|
|
|
|
|
{
|
|
|
|
|
|
QByteArray emptySvg = "<svg xmlns='http://www.w3.org/2000/svg'/>";
|
|
|
|
|
|
_curSvg.clear();
|
|
|
|
|
|
m_renderer.load(emptySvg);
|
|
|
|
|
|
m_curDeviceType.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-01 20:29:36 +08:00
|
|
|
|
void MonitorItemPreviewDlg::paintEvent(QPaintEvent *) {
|
2025-12-03 18:27:49 +08:00
|
|
|
|
if(m_curDeviceType == "cable"){
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置线宽
|
|
|
|
|
|
int lineWidth = 3;
|
|
|
|
|
|
painter.setPen(QPen(m_Color, lineWidth));
|
|
|
|
|
|
|
|
|
|
|
|
// 定义折线的各个顶点
|
|
|
|
|
|
QPolygon polyline;
|
|
|
|
|
|
polyline << QPoint(20, 20) // 起点:左上角
|
|
|
|
|
|
<< QPoint(width()/2, height()/2) // 中间点:中心
|
|
|
|
|
|
<< QPoint(width()-20, height()-20); // 终点:右下角
|
|
|
|
|
|
painter.drawPolyline(polyline);
|
|
|
|
|
|
}
|
|
|
|
|
|
else{
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 先将SVG渲染到一个透明图像上作为遮罩
|
|
|
|
|
|
QImage maskImage(size(), QImage::Format_ARGB32);
|
|
|
|
|
|
maskImage.fill(Qt::transparent);
|
|
|
|
|
|
|
|
|
|
|
|
QPainter maskPainter(&maskImage);
|
|
|
|
|
|
m_renderer.render(&maskPainter);
|
|
|
|
|
|
maskPainter.end();
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 使用目标颜色填充,但只在SVG的非透明区域显示
|
|
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < maskImage.height(); ++y) {
|
|
|
|
|
|
for (int x = 0; x < maskImage.width(); ++x) {
|
|
|
|
|
|
QRgb pixel = maskImage.pixel(x, y);
|
|
|
|
|
|
if (qAlpha(pixel) > 0) { // 只处理非透明像素
|
|
|
|
|
|
// 保持原始透明度,只改变颜色
|
|
|
|
|
|
QColor newColor = m_Color;
|
|
|
|
|
|
newColor.setAlpha(qAlpha(pixel)); // 保持原始alpha值
|
|
|
|
|
|
painter.fillRect(x, y, 1, 1, newColor);
|
|
|
|
|
|
}
|
2025-12-01 20:29:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|