18 qCDebug(ColoredSvgImageProviderLog) <<
"id:" <<
id <<
"requestedSize:" << requestedSize;
20 const qsizetype q =
id.indexOf(QLatin1Char(
'?'));
22 qCWarning(ColoredSvgImageProviderLog) <<
"missing ?color= in id:" << id;
26 QString path =
id.left(q);
27 const QString query =
id.mid(q + 1);
30 if (path.startsWith(QLatin1String(
"qrc:/"))) {
32 }
else if (path.startsWith(QLatin1Char(
'/'))) {
33 path = QLatin1Char(
':') + path;
35 path = QStringLiteral(
":/") + path;
39 for (
const QString &kv : query.split(QLatin1Char(
'&'), Qt::SkipEmptyParts)) {
40 if (kv.startsWith(QLatin1String(
"color="))) {
41 tint = QColor(QLatin1Char(
'#') + kv.mid(6));
45 if (!tint.isValid()) {
46 qCWarning(ColoredSvgImageProviderLog) <<
"invalid color in id:" << id;
50 const bool isSvg = QFileInfo(path).suffix().compare(QLatin1String(
"svg"), Qt::CaseInsensitive) == 0;
52 QSize outSize = requestedSize;
58 auto fillMissingDim = [](QSize req, QSize intrinsic) -> QSize {
59 if (intrinsic.width() <= 0 || intrinsic.height() <= 0) {
62 const bool hasW = req.width() > 0;
63 const bool hasH = req.height() > 0;
65 return {req.width(), qRound(req.width() * (qreal(intrinsic.height()) / intrinsic.width()))};
68 return {qRound(req.height() * (qreal(intrinsic.width()) / intrinsic.height())), req.height()};
74 QSvgRenderer renderer(path);
75 if (!renderer.isValid()) {
76 qCWarning(ColoredSvgImageProviderLog) <<
"QSvgRenderer rejected:" << path;
80 renderer.setAspectRatioMode(Qt::KeepAspectRatio);
81 outSize = fillMissingDim(outSize, renderer.defaultSize());
82 if (outSize.width() <= 0 || outSize.height() <= 0) {
83 outSize = renderer.defaultSize();
85 outSize = outSize.expandedTo(QSize(1, 1));
86 rendered = QImage(outSize, QImage::Format_ARGB32_Premultiplied);
87 rendered.fill(Qt::transparent);
88 QPainter p(&rendered);
89 p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
94 qCWarning(ColoredSvgImageProviderLog) <<
"failed to load:" << path;
97 outSize = fillMissingDim(outSize, src.size());
98 if (outSize.width() > 0 && outSize.height() > 0 && outSize != src.size()) {
99 src = src.scaled(outSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
101 outSize = src.size().expandedTo(QSize(1, 1));
102 rendered = src.convertToFormat(QImage::Format_ARGB32_Premultiplied);
106 QPainter p(&rendered);
107 p.setCompositionMode(QPainter::CompositionMode_SourceIn);
108 p.fillRect(rendered.rect(), tint);
111 if (size !=
nullptr) {