DiagramDesigner/diagramCavas/source/monitorItemPreviewDlg.cpp

85 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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();
}
void MonitorItemPreviewDlg::clearSvg()
{
QByteArray emptySvg = "<svg xmlns='http://www.w3.org/2000/svg'/>";
_curSvg.clear();
m_renderer.load(emptySvg);
m_curDeviceType.clear();
}
void MonitorItemPreviewDlg::paintEvent(QPaintEvent *) {
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);
}
}
}
}
}