60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
|
|
#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::paintEvent(QPaintEvent *) {
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|