8#include <private/qsgcurveprocessor_p.h>
9#include <private/qquickshape_p.h>
10#include <private/qquadpath_p.h>
11#include <private/qquickitem_p.h>
12#include <private/qquickimagebase_p_p.h>
13#include <private/qquicktext_p.h>
14#include <private/qquicktranslate_p.h>
15#include <private/qquickimage_p.h>
17#include <QtCore/qloggingcategory.h>
18#include <QtCore/qdir.h>
25 s.replace(QLatin1Char(
'"'), QLatin1String(
"\\\""));
30QQuickAnimatedProperty::PropertyAnimation QQuickAnimatedProperty::PropertyAnimation::simplified()
const
32 QQuickAnimatedProperty::PropertyAnimation res = *
this;
33 int consecutiveEquals = 0;
34 int prevTimePoint = -1;
36 for (
const auto &[timePoint, value] : frames.asKeyValueRange()) {
37 if (value != prevValue) {
38 consecutiveEquals = 1;
40 }
else if (consecutiveEquals < 2) {
44 res.frames.remove(prevTimePoint);
45 res.easingPerFrame.remove(prevTimePoint);
47 prevTimePoint = timePoint;
53QQuickQmlGenerator::QQuickQmlGenerator(
const QString fileName, QQuickVectorImageGenerator::GeneratorFlags flags,
const QString &outFileName)
54 : QQuickGenerator(fileName, flags)
55 , outputFileName(outFileName)
57 m_result.open(QIODevice::ReadWrite);
60QQuickQmlGenerator::~QQuickQmlGenerator()
64bool QQuickQmlGenerator::save()
66 if (Q_UNLIKELY(errorState()))
70 if (!outputFileName.isEmpty()) {
71 QFileInfo fileInfo(outputFileName);
72 QDir dir(fileInfo.absolutePath());
73 if (!dir.exists() && !dir.mkpath(QStringLiteral(
"."))) {
74 qCWarning(lcQuickVectorImage) <<
"Failed to create path" << dir.absolutePath();
77 QFile outFile(outputFileName);
78 if (outFile.open(QIODevice::WriteOnly)) {
79 outFile.write(m_result.data());
82 qCWarning(lcQuickVectorImage) <<
"Failed to write to file" << outFile.fileName();
88 if (lcQuickVectorImage().isDebugEnabled())
89 qCDebug(lcQuickVectorImage).noquote() << m_result.data().left(300);
94void QQuickQmlGenerator::setShapeTypeName(
const QString &name)
96 m_shapeTypeName = name.toLatin1();
99QString QQuickQmlGenerator::shapeTypeName()
const
101 return QString::fromLatin1(m_shapeTypeName);
104void QQuickQmlGenerator::setCommentString(
const QString commentString)
106 m_commentString = commentString;
109QString QQuickQmlGenerator::commentString()
const
111 return m_commentString;
114QString QQuickQmlGenerator::generateNodeBase(
const NodeInfo &info,
const QString &idSuffix)
116 static qint64 maxNodes = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_NODES").value_or(10000);
117 if (Q_UNLIKELY(!checkSanityLimit(++m_nodeCounter, maxNodes,
"nodes"_L1)))
120 if (!info.nodeId.isEmpty())
121 stream() <<
"objectName: \"" << info.nodeId <<
"\"";
123 if (!info.id.isEmpty())
124 stream() <<
"id: " << info.id << idSuffix;
126 if (!info.bounds.isNull()) {
127 stream() <<
"property var originalBounds: Qt.rect("
128 << info.bounds.x() <<
", "
129 << info.bounds.y() <<
", "
130 << info.bounds.width() <<
", "
131 << info.bounds.height() <<
")";
132 stream() <<
"width: originalBounds.width";
133 stream() <<
"height: originalBounds.height";
136 stream() <<
"transformOrigin: Item.TopLeft";
138 if (info.filterId.isEmpty() && info.maskId.isEmpty()) {
139 if (!info.isDefaultOpacity)
140 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
141 generateItemAnimations(info.id, info);
147void QQuickQmlGenerator::generateNodeEnd(
const NodeInfo &info)
149 if (Q_UNLIKELY(errorState()))
153 generateShaderUse(info);
156void QQuickQmlGenerator::generateItemAnimations(
const QString &idString,
const NodeInfo &info)
158 const bool hasTransform = info.transform.isAnimated()
159 || !info.maskId.isEmpty()
160 || !info.filterId.isEmpty()
161 || !info.isDefaultTransform
162 || !info.transformReferenceId.isEmpty()
163 || info.motionPath.isAnimated();
166 stream() <<
"transform: TransformGroup {";
169 bool hasNonConstantTransform =
false;
170 int earliestOverrideGroup = -1;
172 if (!idString.isEmpty()) {
173 stream() <<
"id: " << idString <<
"_transform_base_group";
175 if (!info.maskId.isEmpty() || !info.filterId.isEmpty())
176 stream() <<
"Translate { x: " << idString <<
".sourceX; y: " << idString <<
".sourceY }";
178 if (info.transform.isAnimated()) {
179 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
180 stream() <<
"TransformGroup {";
183 if (!idString.isEmpty())
184 stream() <<
"id: " << idString <<
"_transform_group_" << groupIndex;
186 int animationStart = info.transform.animationGroup(groupIndex);
187 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
188 ? info.transform.animationGroup(groupIndex + 1)
189 : info.transform.animationCount();
191 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
192 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
193 if (replace && earliestOverrideGroup < 0)
194 earliestOverrideGroup = groupIndex;
196 for (
int i = nextAnimationStart - 1; i >= animationStart; --i) {
197 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
198 if (animation.frames.isEmpty())
201 const QVariantList ¶meters = animation.frames.first().value<QVariantList>();
202 switch (animation.subtype) {
203 case QTransform::TxTranslate:
204 if (animation.isConstant()) {
205 const QPointF translation = parameters.value(0).value<QPointF>();
206 if (!translation.isNull())
207 stream() <<
"Translate { x: " << translation.x() <<
"; y: " << translation.y() <<
" }";
209 hasNonConstantTransform =
true;
210 stream() <<
"Translate { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
213 case QTransform::TxScale:
214 if (animation.isConstant()) {
215 const QPointF scale = parameters.value(0).value<QPointF>();
216 if (scale != QPointF(1, 1))
217 stream() <<
"Scale { xScale: " << scale.x() <<
"; yScale: " << scale.y() <<
" }";
219 hasNonConstantTransform =
true;
220 stream() <<
"Scale { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
"}";
223 case QTransform::TxRotate:
224 if (animation.isConstant()) {
225 const QPointF center = parameters.value(0).value<QPointF>();
226 const qreal angle = parameters.value(1).toReal();
227 if (!qFuzzyIsNull(angle))
228 stream() <<
"Rotation { angle: " << angle <<
"; origin.x: " << center.x() <<
"; origin.y: " << center.y() <<
" }";
230 hasNonConstantTransform =
true;
231 stream() <<
"Rotation { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
234 case QTransform::TxShear:
235 if (animation.isConstant()) {
236 const QPointF skew = parameters.value(0).value<QPointF>();
238 stream() <<
"Shear { xAngle: " << skew.x() <<
"; yAngle: " << skew.y() <<
" }";
240 hasNonConstantTransform =
true;
241 stream() <<
"Shear { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
254 if (info.motionPath.isAnimated()) {
255 QVariantList defaultProps = info.motionPath.defaultValue().value<QVariantList>();
256 const bool adaptAngle = defaultProps.value(1).toBool();
257 const qreal baseRotation = defaultProps.value(2).toReal();
258 QString interpolatorId = idString + QStringLiteral(
"_motion_interpolator");
259 if (adaptAngle || !qFuzzyIsNull(baseRotation)) {
260 stream() <<
"Rotation {";
264 stream() <<
"angle: " << interpolatorId <<
".angle";
265 if (!qFuzzyIsNull(baseRotation))
266 stream(SameLine) <<
" + " << baseRotation;
268 stream() <<
"angle: " << baseRotation;
275 stream() <<
"Translate {";
278 stream() <<
"x: " << interpolatorId <<
".x";
279 stream() <<
"y: " << interpolatorId <<
".y";
286 if (!info.isDefaultTransform) {
287 QTransform xf = info.transform.defaultValue().value<QTransform>();
288 if (xf.type() <= QTransform::TxTranslate) {
289 stream() <<
"Translate { x: " << xf.dx() <<
"; y: " << xf.dy() <<
"}";
291 stream() <<
"Matrix4x4 { matrix: ";
292 generateTransform(xf);
293 stream(SameLine) <<
"}";
297 if (!info.transformReferenceId.isEmpty())
298 stream() <<
"Matrix4x4 { matrix: " << info.transformReferenceId <<
".transformMatrix }";
303 if (hasNonConstantTransform) {
304 generateAnimateTransform(idString, info);
305 }
else if (info.transform.isAnimated() && earliestOverrideGroup >= 0) {
308 stream() <<
"Component.onCompleted: {";
311 stream() << idString <<
"_transform_base_group.activateOverride("
312 << idString <<
"_transform_group_" << earliestOverrideGroup <<
")";
319 generateAnimateMotionPath(idString, info.motionPath);
321 generatePropertyAnimation(info.opacity, idString, QStringLiteral(
"opacity"));
322 generatePropertyAnimation(info.visibility, idString, QStringLiteral(
"visible"));
325void QQuickQmlGenerator::generateShaderUse(
const NodeInfo &info)
327 const bool hasMask = !info.maskId.isEmpty();
328 const bool hasFilters = !info.filterId.isEmpty();
329 if (!hasMask && !hasFilters)
332 const QString effectId = hasFilters
333 ? info.filterId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_effect")
336 QString animatedItemId;
338 stream() <<
"ShaderEffectSource {";
341 const QString seId = info.id + QStringLiteral(
"_se");
342 stream() <<
"id: " << seId;
344 stream() <<
"ItemSpy {";
346 stream() <<
"id: " << info.id <<
"_itemspy";
347 stream() <<
"anchors.fill: parent";
351 stream() <<
"hideSource: true";
352 stream() <<
"wrapMode: " << info.filterId <<
"_filterParameters.wrapMode";
353 stream() <<
"sourceItem: " << info.id;
354 stream() <<
"sourceRect: " << info.filterId
355 <<
"_filterParameters.adaptToFilterRect("
356 << info.id <<
".originalBounds.x, "
357 << info.id <<
".originalBounds.y, "
358 << info.id <<
".originalBounds.width, "
359 << info.id <<
".originalBounds.height)";
360 stream() <<
"textureSize: " << info.id <<
"_itemspy.requiredTextureSize";
361 stream() <<
"width: sourceRect.width";
362 stream() <<
"height: sourceRect.height";
363 stream() <<
"visible: false";
368 stream() <<
"Loader {";
371 animatedItemId = effectId;
372 stream() <<
"id: " << effectId;
374 stream() <<
"property var filterSourceItem: " << seId;
375 stream() <<
"sourceComponent: " << info.filterId <<
"_container";
376 stream() <<
"property real sourceX: " << info.id <<
".originalBounds.x";
377 stream() <<
"property real sourceY: " << info.id <<
".originalBounds.y";
378 stream() <<
"width: " << info.id <<
".originalBounds.width";
379 stream() <<
"height: " << info.id <<
".originalBounds.height";
389 stream() <<
"ShaderEffectSource {";
392 const QString maskId = info.maskId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_mask");
393 stream() <<
"id: " << maskId;
394 stream() <<
"sourceItem: " << info.maskId;
395 stream() <<
"visible: false";
396 stream() <<
"hideSource: true";
398 stream() <<
"ItemSpy {";
400 stream() <<
"id: " << maskId <<
"_itemspy";
401 stream() <<
"anchors.fill: parent";
404 stream() <<
"textureSize: " << maskId <<
"_itemspy.requiredTextureSize";
406 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
407 << info.id <<
".originalBounds.x,"
408 << info.id <<
".originalBounds.y,"
409 << info.id <<
".originalBounds.width,"
410 << info.id <<
".originalBounds.height)";
412 stream() <<
"width: sourceRect.width";
413 stream() <<
"height: sourceRect.height";
419 stream() <<
"ShaderEffectSource {";
422 const QString seId = info.id + QStringLiteral(
"_masked_se");
423 stream() <<
"id: " << seId;
425 stream() <<
"ItemSpy {";
427 stream() <<
"id: " << info.id <<
"_masked_se_itemspy";
428 stream() <<
"anchors.fill: parent";
432 stream() <<
"hideSource: true";
434 stream() <<
"sourceItem: " << effectId;
436 stream() <<
"sourceItem: " << info.id;
437 stream() <<
"textureSize: " << info.id <<
"_masked_se_itemspy.requiredTextureSize";
439 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
440 << info.id <<
".originalBounds.x,"
441 << info.id <<
".originalBounds.y,"
442 << info.id <<
".originalBounds.width,"
443 << info.id <<
".originalBounds.height)";
445 stream() <<
"sourceRect: " << info.maskId <<
".maskRect(0, 0,"
446 << info.id <<
".originalBounds.width,"
447 << info.id <<
".originalBounds.height)";
449 stream() <<
"width: sourceRect.width";
450 stream() <<
"height: sourceRect.height";
451 stream() <<
"smooth: false";
452 stream() <<
"visible: false";
457 stream() <<
"ShaderEffect {";
460 const QString maskShaderId = maskId + QStringLiteral(
"_se");
461 animatedItemId = maskShaderId;
463 stream() <<
"id:" << maskShaderId;
465 stream() <<
"property real sourceX: " << maskId <<
".sourceRect.x";
466 stream() <<
"property real sourceY: " << maskId <<
".sourceRect.y";
467 stream() <<
"width: " << maskId <<
".sourceRect.width";
468 stream() <<
"height: " << maskId <<
".sourceRect.height";
470 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/genericmask.frag.qsb\"";
471 stream() <<
"property var source: " << seId;
472 stream() <<
"property var maskSource: " << maskId;
473 stream() <<
"property bool isAlpha: " << (info.isMaskAlpha ?
"true" :
"false");
474 stream() <<
"property bool isInverted: " << (info.isMaskInverted ?
"true" :
"false");
477 if (!info.isDefaultOpacity)
478 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
480 generateItemAnimations(animatedItemId, info);
486bool QQuickQmlGenerator::generateDefsNode(
const StructureNodeInfo &info)
488 if (Q_UNLIKELY(errorState()))
491 if (info.stage == StructureNodeStage::Start) {
492 m_oldIndentLevel = m_indentLevel;
494 stream() <<
"Component {";
497 stream() <<
"id: " << info.id <<
"_container";
499 stream() <<
"Item {";
502 if (!info.transformReferenceChildId.isEmpty()) {
503 stream() <<
"property alias transformMatrix: "
504 << info.transformReferenceChildId <<
".transformMatrix";
507 generateNodeBase(info, QStringLiteral(
"_defs"));
509 generateNodeEnd(info);
514 stream() << m_defsSuffix;
515 m_defsSuffix.clear();
517 m_indentLevel = m_oldIndentLevel;
523void QQuickQmlGenerator::generateImageNode(
const ImageNodeInfo &info)
525 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
528 const QFileInfo outputFileInfo(outputFileName);
529 const QDir outputDir(outputFileInfo.absolutePath());
533 if (!m_retainFilePaths || info.externalFileReference.isEmpty()) {
534 filePath = m_assetFileDirectory;
535 if (filePath.isEmpty())
536 filePath = outputDir.absolutePath();
538 if (!filePath.isEmpty() && !filePath.endsWith(u'/'))
541 QDir fileDir(filePath);
542 if (!fileDir.exists()) {
543 if (!fileDir.mkpath(QStringLiteral(
".")))
544 qCWarning(lcQuickVectorImage) <<
"Failed to create image resource directory:" << filePath;
547 filePath += QStringLiteral(
"%1%2.png").arg(m_assetFilePrefix.isEmpty()
548 ? QStringLiteral(
"svg_asset_")
550 .arg(info.image.cacheKey());
552 if (!info.image.save(filePath))
553 qCWarning(lcQuickVectorImage) <<
"Unabled to save image resource" << filePath;
554 qCDebug(lcQuickVectorImage) <<
"Saving copy of IMAGE" << filePath;
556 filePath = info.externalFileReference;
559 const QFileInfo assetFileInfo(filePath);
561 stream() <<
"Image {";
564 generateNodeBase(info);
565 stream() <<
"x: " << info.rect.x();
566 stream() <<
"y: " << info.rect.y();
567 stream() <<
"width: " << info.rect.width();
568 stream() <<
"height: " << info.rect.height();
569 stream() <<
"source: \"" << m_urlPrefix << outputDir.relativeFilePath(assetFileInfo.absoluteFilePath()) <<
"\"";
570 generateNodeEnd(info);
573void QQuickQmlGenerator::generateMarkers(
const PathNodeInfo &info)
575 const QPainterPath path = info.path.defaultValue().value<QPainterPath>();
576 for (
int i = 0; i < path.elementCount(); ++i) {
577 const QPainterPath::Element element = path.elementAt(i);
582 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
583 QPointF t1 = p1 - p0;
584 QPointF t2 = p2 - p1;
585 qreal hyp1 = hypot(t1.x(), t1.y());
590 qreal hyp2 = hypot(t2.x(), t2.y());
595 QPointF tangent = t1 + t2;
596 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
600 markerId = info.markerStartId;
601 angle = path.angleAtPercent(0.0);
602 }
else if (i == path.elementCount() - 1) {
603 markerId = info.markerEndId;
604 angle = path.angleAtPercent(1.0);
605 }
else if (path.elementAt(i + 1).type != QPainterPath::CurveToDataElement) {
606 markerId = info.markerMidId;
608 const QPainterPath::Element prevElement = path.elementAt(i - 1);
609 const QPainterPath::Element nextElement = path.elementAt(i + 1);
611 QPointF p1(prevElement.x, prevElement.y);
612 QPointF p2(element.x, element.y);
613 QPointF p3(nextElement.x, nextElement.y);
615 angle = getMeanAngle(p1, p2, p3);
618 if (!markerId.isEmpty()) {
619 stream() <<
"Loader {";
623 stream() <<
"sourceComponent: " << markerId <<
"_container";
624 stream() <<
"property real strokeWidth: " << info.strokeStyle.width;
625 stream() <<
"transform: [";
628 stream() <<
"Scale { "
629 <<
"xScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1; "
630 <<
"yScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1 },";
632 stream() <<
"Rotation { angle: " << markerId <<
"_markerParameters.autoAngle(" << -angle <<
") },";
633 stream() <<
"Translate { x: " << element.x <<
"; y: " << element.y <<
"}";
644void QQuickQmlGenerator::generatePath(
const PathNodeInfo &info,
const QRectF &overrideBoundingRect)
646 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
649 if (m_inShapeItemLevel > 0) {
650 if (!info.isDefaultTransform)
651 qWarning() <<
"Skipped transform for node" << info.nodeId <<
"type" << info.typeName <<
"(this is not supposed to happen)";
652 optimizePaths(info, overrideBoundingRect);
654 m_inShapeItemLevel++;
655 stream() << shapeName() <<
" {";
658 generateNodeBase(info);
660 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
661 stream() <<
"preferredRendererType: Shape.CurveRenderer";
662 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
663 stream() <<
"asynchronous: true";
664 optimizePaths(info, overrideBoundingRect);
667 if (!info.markerStartId.isEmpty()
668 || !info.markerMidId.isEmpty()
669 || !info.markerEndId.isEmpty()) {
670 generateMarkers(info);
673 generateNodeEnd(info);
674 m_inShapeItemLevel--;
678void QQuickQmlGenerator::generateGradient(
const QGradient *grad)
680 if (grad->type() == QGradient::LinearGradient) {
681 auto *linGrad =
static_cast<
const QLinearGradient *>(grad);
682 stream() <<
"fillGradient: LinearGradient {";
685 QRectF gradRect(linGrad->start(), linGrad->finalStop());
687 stream() <<
"x1: " << gradRect.left();
688 stream() <<
"y1: " << gradRect.top();
689 stream() <<
"x2: " << gradRect.right();
690 stream() <<
"y2: " << gradRect.bottom();
691 for (
auto &stop : linGrad->stops())
692 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
693 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
696 }
else if (grad->type() == QGradient::RadialGradient) {
697 auto *radGrad =
static_cast<
const QRadialGradient*>(grad);
698 stream() <<
"fillGradient: RadialGradient {";
701 stream() <<
"centerX: " << radGrad->center().x();
702 stream() <<
"centerY: " << radGrad->center().y();
703 stream() <<
"centerRadius: " << radGrad->radius();
704 stream() <<
"focalX:" << radGrad->focalPoint().x();
705 stream() <<
"focalY:" << radGrad->focalPoint().y();
706 for (
auto &stop : radGrad->stops())
707 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
708 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
714void QQuickQmlGenerator::generateAnimationBindings()
717 if (Q_UNLIKELY(!isRuntimeGenerator()))
718 prefix = QStringLiteral(
".animations");
720 stream() <<
"loops: " << m_topLevelIdString << prefix <<
".loops";
721 stream() <<
"paused: " << m_topLevelIdString << prefix <<
".paused";
722 stream() <<
"running: true";
725 stream() <<
"onLoopsChanged: { if (running) { restart() } }";
728void QQuickQmlGenerator::generateEasing(
const QQuickAnimatedProperty::PropertyAnimation &animation,
729 int time,
int streamFlags)
731 if (animation.easingPerFrame.contains(time)) {
732 QBezier bezier = animation.easingPerFrame.value(time);
733 QPointF c1 = bezier.pt2();
734 QPointF c2 = bezier.pt3();
736 bool isLinear = (c1 == c1.transposed() && c2 == c2.transposed());
738 int nextIdx = m_easings.size();
739 QString &id = m_easings[{c1.x(), c1.y(), c2.x(), c2.y()}];
741 id = QString(QLatin1String(
"easing_%1")).arg(nextIdx, 2, 10, QLatin1Char(
'0'));
742 if (streamFlags & SameLine)
743 stream(streamFlags) <<
"; ";
744 stream(streamFlags) <<
"easing: " << m_topLevelIdString <<
"." << id;
751 static qreal multiplier = qreal(qEnvironmentVariable(
"QT_QUICKVECTORIMAGE_TIME_DILATION", QStringLiteral(
"1.0"))
753 return std::round(multiplier * time);
756void QQuickQmlGenerator::generatePropertyAnimation(
const QQuickAnimatedProperty &property,
757 const QString &targetName,
758 const QString &propertyName,
759 AnimationType animationType)
761 if (!property.isAnimated())
764 if (usingTimelineAnimation())
765 return generatePropertyTimeline(property, targetName, propertyName, animationType);
767 QString mainAnimationId = targetName
768 + QStringLiteral(
"_")
770 + QStringLiteral(
"_animation");
771 mainAnimationId.replace(QLatin1Char(
'.'), QLatin1Char(
'_'));
774 if (Q_UNLIKELY(!isRuntimeGenerator()))
775 prefix = QStringLiteral(
".animations");
777 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
779 stream() <<
"ParallelAnimation {";
782 stream() <<
"id: " << mainAnimationId;
784 generateAnimationBindings();
786 for (
int i = 0; i < property.animationCount(); ++i) {
787 const QQuickAnimatedProperty::PropertyAnimation &animation = property.animation(i);
789 stream() <<
"SequentialAnimation {";
792 const int startOffset = processAnimationTime(animation.startOffset);
794 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
796 stream() <<
"SequentialAnimation {";
799 const int repeatCount = animation.repeatCount;
801 stream() <<
"loops: Animation.Infinite";
803 stream() <<
"loops: " << repeatCount;
805 int previousTime = 0;
806 QVariant previousValue;
807 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
808 const int time = it.key();
809 const int frameTime = processAnimationTime(time - previousTime);
810 const QVariant &value = it.value();
812 if (previousValue.isValid() && previousValue == value) {
814 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
815 }
else if (animationType == AnimationType::Auto && value.typeId() == QMetaType::Bool) {
818 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
819 stream() <<
"ScriptAction {";
822 stream() <<
"script:" << targetName <<
"." << propertyName <<
" = " << value.toString();
827 generateAnimatedPropertySetter(targetName,
837 previousValue = value;
840 if (!(animation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd)) {
841 stream() <<
"ScriptAction {";
843 stream() <<
"script: ";
845 switch (animationType) {
846 case AnimationType::Auto:
847 stream(SameLine) << targetName <<
"." << propertyName <<
" = ";
849 case AnimationType::ColorOpacity:
850 stream(SameLine) << targetName <<
"." << propertyName <<
".a = ";
854 QVariant value = property.defaultValue();
855 if (value.typeId() == QMetaType::QColor)
856 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
858 stream(SameLine) << value.toReal();
875void QQuickQmlGenerator::generateTimelinePropertySetter(
876 const QString &targetName,
877 const QString &propertyName,
878 const QQuickAnimatedProperty::PropertyAnimation &animation,
879 std::function<QVariant(
const QVariant &)>
const& extractValue,
882 if (animation.repeatCount != 1 || animation.startOffset
883 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
884 qCWarning(lcQuickVectorImage) <<
"Animation feature not implemented in timeline mode, for"
885 << targetName << propertyName;
888 stream() <<
"KeyframeGroup {";
890 stream() <<
"target: " << targetName;
891 stream() <<
"property: \"" << propertyName <<
"\"";
893 for (
const auto &[frame, rawValue] : animation.frames.asKeyValueRange()) {
895 if (rawValue.typeId() == QMetaType::QVariantList)
896 value = extractValue(rawValue.toList().value(valueIndex));
898 value = extractValue(rawValue);
900 stream() <<
"Keyframe { frame: " << frame <<
"; value: ";
901 if (value.typeId() == QMetaType::QVector3D) {
902 const QVector3D &v = value.value<QVector3D>();
903 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
904 }
else if (value.typeId() == QMetaType::QColor) {
905 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
907 stream(SameLine) << value.toReal();
909 generateEasing(animation, frame, SameLine);
910 stream(SameLine) <<
" }";
917void QQuickQmlGenerator::generatePropertyTimeline(
const QQuickAnimatedProperty &property,
918 const QString &targetName,
919 const QString &propertyName,
920 AnimationType animationType)
922 if (animationType == QQuickQmlGenerator::AnimationType::ColorOpacity) {
923 qCWarning(lcQuickVectorImage) <<
"ColorOpacity animation not available in timeline mode";
927 if (property.animationGroupCount() > 1 || property.animationCount() > 1) {
928 qCWarning(lcQuickVectorImage) <<
"Property feature not implemented in timeline mode, for"
929 << targetName << propertyName;
932 stream() <<
"Timeline {";
934 stream() <<
"currentFrame: " << property.timelineReferenceId() <<
".frameCounter";
935 stream() <<
"enabled: true";
937 auto extractor = [](
const QVariant &value) {
return value; };
938 generateTimelinePropertySetter(targetName, propertyName, property.animation(0), extractor);
944void QQuickQmlGenerator::generateTransform(
const QTransform &xf)
947 stream(SameLine) <<
"PlanarTransform.fromAffineMatrix("
948 << xf.m11() <<
", " << xf.m12() <<
", "
949 << xf.m21() <<
", " << xf.m22() <<
", "
950 << xf.dx() <<
", " << xf.dy() <<
")";
953 stream(SameLine) <<
"Qt.matrix4x4(";
955 const auto *data = m.data();
956 for (
int i = 0; i < 4; i++) {
957 stream() << data[i] <<
", " << data[i+4] <<
", " << data[i+8] <<
", " << data[i+12];
959 stream(SameLine) <<
", ";
961 stream(SameLine) <<
")";
966void QQuickQmlGenerator::outputShapePath(
const PathNodeInfo &info,
const QPainterPath *painterPath,
const QQuadPath *quadPath, QQuickVectorImageGenerator::PathSelector pathSelector,
const QRectF &boundingRect)
968 Q_UNUSED(pathSelector)
969 Q_ASSERT(painterPath || quadPath);
971 if (Q_UNLIKELY(errorState()))
974 const QColor strokeColor = info.strokeStyle.color.defaultValue().value<QColor>();
975 const bool noPen = strokeColor == QColorConstants::Transparent
976 && !info.strokeStyle.color.isAnimated()
977 && !info.strokeStyle.opacity.isAnimated();
978 if (pathSelector == QQuickVectorImageGenerator::StrokePath && noPen)
981 const QColor fillColor = info.fillColor.defaultValue().value<QColor>();
982 const bool noFill = info.grad.type() == QGradient::NoGradient
983 && fillColor == QColorConstants::Transparent
984 && !info.fillColor.isAnimated()
985 && !info.fillOpacity.isAnimated();
986 if (pathSelector == QQuickVectorImageGenerator::FillPath && noFill)
991 auto fillRule = QQuickShapePath::FillRule(painterPath ? painterPath->fillRule() : quadPath->fillRule());
992 stream() <<
"ShapePath {";
995 QString shapePathId = info.id;
996 if (pathSelector & QQuickVectorImageGenerator::FillPath)
997 shapePathId += QStringLiteral(
"_fill");
998 if (pathSelector & QQuickVectorImageGenerator::StrokePath)
999 shapePathId += QStringLiteral(
"_stroke");
1001 stream() <<
"id: " << shapePathId;
1003 if (!info.nodeId.isEmpty()) {
1004 switch (pathSelector) {
1005 case QQuickVectorImageGenerator::FillPath:
1006 stream() <<
"objectName: \"svg_fill_path:" << info.nodeId <<
"\"";
1008 case QQuickVectorImageGenerator::StrokePath:
1009 stream() <<
"objectName: \"svg_stroke_path:" << info.nodeId <<
"\"";
1011 case QQuickVectorImageGenerator::FillAndStroke:
1012 stream() <<
"objectName: \"svg_path:" << info.nodeId <<
"\"";
1017 if (noPen || !(pathSelector & QQuickVectorImageGenerator::StrokePath)) {
1018 stream() <<
"strokeColor: \"transparent\"";
1020 if (info.strokeStyle.opacity.isAnimated()) {
1021 stream() <<
"property color strokeBase: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1022 stream() <<
"property real strokeOpacity: " << info.strokeStyle.opacity.defaultValue().toReal();
1023 stream() <<
"strokeColor: Qt.rgba(strokeBase.r, strokeBase.g, strokeBase.b, strokeOpacity)";
1025 stream() <<
"strokeColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1027 stream() <<
"strokeWidth: " << info.strokeStyle.width;
1028 stream() <<
"capStyle: " << QQuickVectorImageGenerator::Utils::strokeCapStyleString(info.strokeStyle.lineCapStyle);
1029 stream() <<
"joinStyle: " << QQuickVectorImageGenerator::Utils::strokeJoinStyleString(info.strokeStyle.lineJoinStyle);
1030 stream() <<
"miterLimit: " << info.strokeStyle.miterLimit;
1031 if (info.strokeStyle.dashArray.length() != 0) {
1032 stream() <<
"strokeStyle: " <<
"ShapePath.DashLine";
1033 stream() <<
"dashPattern: " << QQuickVectorImageGenerator::Utils::listString(info.strokeStyle.dashArray);
1034 stream() <<
"dashOffset: " << info.strokeStyle.dashOffset;
1038 QTransform fillTransform = info.fillTransform;
1039 if (!(pathSelector & QQuickVectorImageGenerator::FillPath)) {
1040 stream() <<
"fillColor: \"transparent\"";
1041 }
else if (info.grad.type() != QGradient::NoGradient) {
1042 generateGradient(&info.grad);
1043 if (info.grad.coordinateMode() == QGradient::ObjectMode) {
1044 QTransform objectToUserSpace;
1045 objectToUserSpace.translate(boundingRect.x(), boundingRect.y());
1046 objectToUserSpace.scale(boundingRect.width(), boundingRect.height());
1047 fillTransform *= objectToUserSpace;
1050 if (info.fillOpacity.isAnimated()) {
1051 stream() <<
"property color fillBase: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1052 stream() <<
"property real fillOpacity:" << info.fillOpacity.defaultValue().toReal();
1053 stream() <<
"fillColor: Qt.rgba(fillBase.r, fillBase.g, fillBase.b, fillOpacity)";
1055 stream() <<
"fillColor: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1059 if (!info.patternId.isEmpty()) {
1060 stream() <<
"fillItem: ShaderEffectSource {";
1063 stream() <<
"parent: " << info.id;
1064 stream() <<
"sourceItem: " << info.patternId;
1065 stream() <<
"hideSource: true";
1066 stream() <<
"visible: false";
1067 stream() <<
"width: " << info.patternId <<
".width";
1068 stream() <<
"height: " << info.patternId <<
".height";
1069 stream() <<
"wrapMode: ShaderEffectSource.Repeat";
1070 stream() <<
"textureSize: Qt.size(width * __qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1071 <<
"height * __qt_toplevel_scale_itemspy.requiredTextureSize.height)";;
1072 stream() <<
"sourceRect: " << info.patternId <<
".sourceRect("
1073 << info.id <<
".width, "
1074 << info.id <<
".height)";
1081 stream() <<
"function calculateFillTransform(xScale, yScale) {";
1084 stream() <<
"var m = ";
1085 generateTransform(fillTransform);
1087 stream() <<
"m.translate(" << info.patternId <<
".sourceOffset("
1088 << info.id <<
".width, "
1089 << info.id <<
".height))";
1091 stream() <<
"m.scale(1.0 / xScale, 1.0 / yScale, 1.0)";
1092 stream() <<
"return m";
1097 stream() <<
"fillTransform: calculateFillTransform(__qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1098 <<
"__qt_toplevel_scale_itemspy.requiredTextureSize.height)";
1100 }
else if (!fillTransform.isIdentity()) {
1101 const QTransform &xf = fillTransform;
1102 stream() <<
"fillTransform: ";
1103 if (info.fillTransform.type() == QTransform::TxTranslate)
1104 stream(SameLine) <<
"PlanarTransform.fromTranslate(" << xf.dx() <<
", " << xf.dy() <<
")";
1105 else if (info.fillTransform.type() == QTransform::TxScale && !xf.dx() && !xf.dy())
1106 stream(SameLine) <<
"PlanarTransform.fromScale(" << xf.m11() <<
", " << xf.m22() <<
")";
1108 generateTransform(xf);
1111 if (info.trim.enabled) {
1112 stream() <<
"trim.start: " << info.trim.start.defaultValue().toReal();
1113 stream() <<
"trim.end: " << info.trim.end.defaultValue().toReal();
1114 stream() <<
"trim.offset: " << info.trim.offset.defaultValue().toReal();
1118 if (fillRule == QQuickShapePath::WindingFill)
1119 stream() <<
"fillRule: ShapePath.WindingFill";
1121 stream() <<
"fillRule: ShapePath.OddEvenFill";
1125 hintStr = QQuickVectorImageGenerator::Utils::pathHintString(*quadPath);
1126 if (!hintStr.isEmpty())
1127 stream() << hintStr;
1129 QQuickAnimatedProperty pathFactor(QVariant::fromValue(0));
1130 pathFactor.setTimelineReferenceId(info.path.timelineReferenceId());
1131 QString pathId = shapePathId +
"_ip"_L1;
1132 if (!info.path.isAnimated() || (info.path.animation(0).startOffset == 0 && info.path.animation(0).isConstant())) {
1133 QString svgPathString = painterPath ? QQuickVectorImageGenerator::Utils::toSvgString(*painterPath) : QQuickVectorImageGenerator::Utils::toSvgString(*quadPath);
1134 stream() <<
"PathSvg { path: \"" << svgPathString <<
"\" }";
1136 stream() <<
"PathInterpolated {";
1138 stream() <<
"id: " << pathId;
1139 stream() <<
"svgPaths: [";
1141 QQuickAnimatedProperty::PropertyAnimation pathFactorAnim = info.path.animation(0);
1142 auto &frames = pathFactorAnim.frames;
1145 for (
auto it = frames.begin(); it != frames.end(); ++it) {
1146 QString svg = QQuickVectorImageGenerator::Utils::toSvgString(it->value<QPainterPath>());
1147 if (svg != lastSvg) {
1149 stream(SameLine) <<
",";
1150 stream() <<
"\"" << svg <<
"\"";
1154 *it = QVariant::fromValue(pathIdx);
1156 pathFactor.addAnimation(pathFactorAnim);
1166 if (pathFactor.isAnimated())
1167 generatePropertyAnimation(pathFactor, pathId,
"factor"_L1);
1169 if (info.trim.enabled) {
1170 generatePropertyAnimation(info.trim.start, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"start"));
1171 generatePropertyAnimation(info.trim.end, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"end"));
1172 generatePropertyAnimation(info.trim.offset, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"offset"));
1175 if (info.strokeStyle.opacity.isAnimated()) {
1176 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeBase"));
1177 generatePropertyAnimation(info.strokeStyle.opacity, shapePathId, QStringLiteral(
"strokeOpacity"));
1179 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeColor"));
1181 if (info.fillOpacity.isAnimated()) {
1182 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillBase"));
1183 generatePropertyAnimation(info.fillOpacity, shapePathId, QStringLiteral(
"fillOpacity"));
1185 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillColor"));
1189void QQuickQmlGenerator::generateNode(
const NodeInfo &info)
1191 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1194 stream() <<
"// Missing Implementation for SVG Node: " << info.typeName;
1195 stream() <<
"// Adding an empty Item and skipping";
1196 stream() <<
"Item {";
1198 generateNodeBase(info);
1199 generateNodeEnd(info);
1202void QQuickQmlGenerator::generateTextNode(
const TextNodeInfo &info)
1204 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1207 static int counter = 0;
1208 stream() <<
"Item {";
1210 generateNodeBase(info);
1212 if (!info.isTextArea)
1213 stream() <<
"Item { id: textAlignItem_" << counter <<
"; x: " << info.position.x() <<
"; y: " << info.position.y() <<
"}";
1215 stream() <<
"Text {";
1219 const QString textItemId = QStringLiteral(
"_qt_textItem_%1").arg(counter);
1220 stream() <<
"id: " << textItemId;
1222 generatePropertyAnimation(info.fillColor, textItemId, QStringLiteral(
"color"));
1223 generatePropertyAnimation(info.fillOpacity, textItemId, QStringLiteral(
"color"), AnimationType::ColorOpacity);
1224 generatePropertyAnimation(info.strokeColor, textItemId, QStringLiteral(
"styleColor"));
1225 generatePropertyAnimation(info.strokeOpacity, textItemId, QStringLiteral(
"styleColor"), AnimationType::ColorOpacity);
1227 if (info.isTextArea) {
1228 stream() <<
"x: " << info.position.x();
1229 stream() <<
"y: " << info.position.y();
1230 if (info.size.width() > 0)
1231 stream() <<
"width: " << info.size.width();
1232 if (info.size.height() > 0)
1233 stream() <<
"height: " << info.size.height();
1234 stream() <<
"wrapMode: Text.Wrap";
1235 stream() <<
"clip: true";
1237 QString hAlign = QStringLiteral(
"left");
1238 stream() <<
"anchors.baseline: textAlignItem_" << counter <<
".top";
1239 switch (info.alignment) {
1240 case Qt::AlignHCenter:
1241 hAlign = QStringLiteral(
"horizontalCenter");
1243 case Qt::AlignRight:
1244 hAlign = QStringLiteral(
"right");
1247 qCDebug(lcQuickVectorImage) <<
"Unexpected text alignment" << info.alignment;
1252 stream() <<
"anchors." << hAlign <<
": textAlignItem_" << counter <<
".left";
1256 stream() <<
"color: \"" << info.fillColor.defaultValue().value<QColor>().name(QColor::HexArgb) <<
"\"";
1257 stream() <<
"textFormat:" << (info.needsRichText ?
"Text.RichText" :
"Text.StyledText");
1259 stream() <<
"text: \"" << sanitizeString(info.text) <<
"\"";
1260 stream() <<
"font.family: \"" << sanitizeString(info.font.family()) <<
"\"";
1261 if (info.font.pixelSize() > 0)
1262 stream() <<
"font.pixelSize:" << info.font.pixelSize();
1263 else if (info.font.pointSize() > 0)
1264 stream() <<
"font.pixelSize:" << info.font.pointSizeF();
1265 if (info.font.underline())
1266 stream() <<
"font.underline: true";
1267 if (info.font.weight() != QFont::Normal)
1268 stream() <<
"font.weight: " <<
int(info.font.weight());
1269 if (info.font.italic())
1270 stream() <<
"font.italic: true";
1271 switch (info.font.hintingPreference()) {
1272 case QFont::PreferFullHinting:
1273 stream() <<
"font.hintingPreference: Font.PreferFullHinting";
1275 case QFont::PreferVerticalHinting:
1276 stream() <<
"font.hintingPreference: Font.PreferVerticalHinting";
1278 case QFont::PreferNoHinting:
1279 stream() <<
"font.hintingPreference: Font.PreferNoHinting";
1281 case QFont::PreferDefaultHinting:
1282 stream() <<
"font.hintingPreference: Font.PreferDefaultHinting";
1286 const QColor strokeColor = info.strokeColor.defaultValue().value<QColor>();
1287 if (strokeColor != QColorConstants::Transparent || info.strokeColor.isAnimated()) {
1288 stream() <<
"styleColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1289 stream() <<
"style: Text.Outline";
1295 generateNodeEnd(info);
1298void QQuickQmlGenerator::generateUseNode(
const UseNodeInfo &info)
1300 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1303 if (info.stage == StructureNodeStage::Start) {
1304 stream() <<
"Item {";
1306 generateNodeBase(info);
1308 generateNodeEnd(info);
1312void QQuickQmlGenerator::generatePathContainer(
const StructureNodeInfo &info)
1315 stream() << shapeName() <<
" {";
1317 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
1318 stream() <<
"preferredRendererType: Shape.CurveRenderer";
1319 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
1320 stream() <<
"asynchronous: true";
1323 m_inShapeItemLevel++;
1326void QQuickQmlGenerator::generateAnimateMotionPath(
const QString &targetName,
1327 const QQuickAnimatedProperty &property)
1329 if (!property.isAnimated())
1332 QPainterPath path = property.defaultValue().value<QVariantList>().value(0).value<QPainterPath>();
1333 const QString mainAnimationId = targetName + QStringLiteral(
"_motion_interpolator");
1334 stream() <<
"PathInterpolator {";
1336 stream() <<
"id: " << mainAnimationId;
1337 const QString svgPathString = QQuickVectorImageGenerator::Utils::toSvgString(path);
1338 stream() <<
"path: Path { PathSvg { path: \"" << svgPathString <<
"\" } }";
1342 generatePropertyAnimation(property, mainAnimationId, QStringLiteral(
"progress"));
1345void QQuickQmlGenerator::generateAnimatedPropertySetter(
const QString &targetName,
1346 const QString &propertyName,
1347 const QVariant &value,
1348 const QQuickAnimatedProperty::PropertyAnimation &animation,
1351 AnimationType animationType)
1353 if (frameTime > 0) {
1354 switch (animationType) {
1355 case AnimationType::Auto:
1356 if (value.typeId() == QMetaType::QColor)
1357 stream() <<
"ColorAnimation {";
1359 stream() <<
"PropertyAnimation {";
1361 case AnimationType::ColorOpacity:
1362 stream() <<
"ColorOpacityAnimation {";
1367 stream() <<
"duration: " << frameTime;
1368 stream() <<
"target: " << targetName;
1369 stream() <<
"property: \"" << propertyName <<
"\"";
1371 if (value.typeId() == QMetaType::QVector3D) {
1372 const QVector3D &v = value.value<QVector3D>();
1373 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1374 }
else if (value.typeId() == QMetaType::QColor) {
1375 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1377 stream(SameLine) << value.toReal();
1379 generateEasing(animation, time);
1383 stream() <<
"ScriptAction {";
1385 stream() <<
"script:" << targetName <<
"." << propertyName;
1386 if (animationType == AnimationType::ColorOpacity)
1387 stream(SameLine) <<
".a";
1389 stream(SameLine) <<
" = ";
1390 if (value.typeId() == QMetaType::QVector3D) {
1391 const QVector3D &v = value.value<QVector3D>();
1392 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1393 }
else if (value.typeId() == QMetaType::QColor) {
1394 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1396 stream(SameLine) << value.toReal();
1403void QQuickQmlGenerator::generateAnimateTransform(
const QString &targetName,
const NodeInfo &info)
1405 if (!info.transform.isAnimated())
1408 if (usingTimelineAnimation())
1409 return generateTransformTimeline(targetName, info);
1411 const QString mainAnimationId = targetName
1412 + QStringLiteral(
"_transform_animation");
1415 if (Q_UNLIKELY(!isRuntimeGenerator()))
1416 prefix = QStringLiteral(
".animations");
1417 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
1419 stream() <<
"ParallelAnimation {";
1422 stream() <<
"id:" << mainAnimationId;
1424 generateAnimationBindings();
1425 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
1426 int animationStart = info.transform.animationGroup(groupIndex);
1427 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
1428 ? info.transform.animationGroup(groupIndex + 1)
1429 : info.transform.animationCount();
1432 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
1433 const bool freeze = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1434 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
1436 stream() <<
"SequentialAnimation {";
1439 const int startOffset = processAnimationTime(firstAnimation.startOffset);
1440 if (startOffset > 0)
1441 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
1443 const int repeatCount = firstAnimation.repeatCount;
1444 if (repeatCount < 0)
1445 stream() <<
"loops: Animation.Infinite";
1447 stream() <<
"loops: " << repeatCount;
1450 stream() <<
"ScriptAction {";
1453 stream() <<
"script: " << targetName <<
"_transform_base_group"
1454 <<
".activateOverride(" << targetName <<
"_transform_group_" << groupIndex <<
")";
1460 stream() <<
"ParallelAnimation {";
1463 for (
int i = animationStart; i < nextAnimationStart; ++i) {
1464 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1465 if (animation.isConstant())
1467 bool hasRotationCenter =
false;
1468 if (animation.subtype == QTransform::TxRotate) {
1469 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1470 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1471 if (!center.isNull()) {
1472 hasRotationCenter =
true;
1478 stream() <<
"SequentialAnimation {";
1481 int previousTime = 0;
1482 QVariantList previousParameters;
1483 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1484 const int time = it.key();
1485 const int frameTime = processAnimationTime(time - previousTime);
1486 const QVariantList ¶meters = it.value().value<QVariantList>();
1487 if (parameters.isEmpty())
1490 if (parameters == previousParameters) {
1492 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
1494 stream() <<
"ParallelAnimation {";
1497 const QString propertyTargetName = targetName
1498 + QStringLiteral(
"_transform_")
1499 + QString::number(groupIndex)
1500 + QStringLiteral(
"_")
1501 + QString::number(i);
1503 switch (animation.subtype) {
1504 case QTransform::TxTranslate:
1506 const QPointF translation = parameters.first().value<QPointF>();
1508 generateAnimatedPropertySetter(propertyTargetName,
1509 QStringLiteral(
"x"),
1514 generateAnimatedPropertySetter(propertyTargetName,
1515 QStringLiteral(
"y"),
1522 case QTransform::TxScale:
1524 const QPointF scale = parameters.first().value<QPointF>();
1525 generateAnimatedPropertySetter(propertyTargetName,
1526 QStringLiteral(
"xScale"),
1531 generateAnimatedPropertySetter(propertyTargetName,
1532 QStringLiteral(
"yScale"),
1539 case QTransform::TxRotate:
1541 Q_ASSERT(parameters.size() == 2);
1542 const qreal angle = parameters.value(1).toReal();
1543 if (hasRotationCenter) {
1544 const QPointF center = parameters.value(0).value<QPointF>();
1545 generateAnimatedPropertySetter(propertyTargetName,
1546 QStringLiteral(
"origin"),
1547 QVector3D(center.x(), center.y(), 0.0),
1552 generateAnimatedPropertySetter(propertyTargetName,
1553 QStringLiteral(
"angle"),
1560 case QTransform::TxShear:
1562 const QPointF skew = parameters.first().value<QPointF>();
1564 generateAnimatedPropertySetter(propertyTargetName,
1565 QStringLiteral(
"xAngle"),
1571 generateAnimatedPropertySetter(propertyTargetName,
1572 QStringLiteral(
"yAngle"),
1587 previousTime = time;
1588 previousParameters = parameters;
1600 if (firstAnimation.repeatCount >= 0) {
1601 stream() <<
"ScriptAction {";
1604 stream() <<
"script: {";
1608 stream() << targetName <<
"_transform_base_group.deactivate("
1609 << targetName <<
"_transform_group_" << groupIndex <<
")";
1610 }
else if (!replace) {
1611 stream() << targetName <<
"_transform_base_group.deactivateOverride("
1612 << targetName <<
"_transform_group_" << groupIndex <<
")";
1630void QQuickQmlGenerator::generateTransformTimeline(
const QString &targetName,
const NodeInfo &info)
1632 stream() <<
"Timeline {";
1634 stream() <<
"currentFrame: " << info.transform.timelineReferenceId() <<
".frameCounter";
1635 stream() <<
"enabled: true";
1637 const int groupIndex = 0;
1638 for (
int i = 0; i < info.transform.animationCount(); ++i) {
1639 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1640 if (animation.isConstant())
1642 if (info.transform.animationGroupCount() > 1
1643 || animation.repeatCount != 1 || animation.startOffset
1644 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
1645 qCWarning(lcQuickVectorImage) <<
"Feature not implemented in timeline xf animation mode, for"
1646 << targetName <<
"subtype" << animation.subtype;
1649 bool hasRotationCenter =
false;
1650 if (animation.subtype == QTransform::TxRotate) {
1651 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1652 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1653 if (!center.isNull()) {
1654 hasRotationCenter =
true;
1660 auto pointFxExtractor = [](
const QVariant &value) {
return value.toPointF().x(); };
1661 auto pointFyExtractor = [](
const QVariant &value) {
return value.toPointF().y(); };
1662 auto realExtractor = [](
const QVariant &value) {
return value.toReal(); };
1663 auto pointFtoVector3dExtractor = [](
const QVariant &v) {
return QVector3D(v.toPointF()); };
1665 const QString propertyTargetName = targetName
1666 + QStringLiteral(
"_transform_")
1667 + QString::number(groupIndex)
1668 + QStringLiteral(
"_")
1669 + QString::number(i);
1671 switch (animation.subtype) {
1672 case QTransform::TxTranslate:
1673 generateTimelinePropertySetter(propertyTargetName,
"x"_L1, animation, pointFxExtractor);
1674 generateTimelinePropertySetter(propertyTargetName,
"y"_L1, animation, pointFyExtractor);
1676 case QTransform::TxScale:
1677 generateTimelinePropertySetter(propertyTargetName,
"xScale"_L1, animation, pointFxExtractor);
1678 generateTimelinePropertySetter(propertyTargetName,
"yScale"_L1, animation, pointFyExtractor);
1680 case QTransform::TxRotate:
1681 if (hasRotationCenter)
1682 generateTimelinePropertySetter(propertyTargetName,
"origin"_L1, animation, pointFtoVector3dExtractor);
1683 generateTimelinePropertySetter(propertyTargetName,
"angle"_L1, animation, realExtractor, 1);
1685 case QTransform::TxShear:
1686 generateTimelinePropertySetter(propertyTargetName,
"xAngle"_L1, animation, pointFxExtractor);
1687 generateTimelinePropertySetter(propertyTargetName,
"yAngle"_L1, animation, pointFyExtractor);
1696bool QQuickQmlGenerator::generateStructureNode(
const StructureNodeInfo &info)
1698 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1701 const bool isPathContainer = !info.forceSeparatePaths && info.isPathContainer;
1702 if (info.stage == StructureNodeStage::Start) {
1703 if (!info.clipBox.isEmpty()) {
1704 stream() <<
"Item { // Clip";
1707 stream() <<
"width: " << info.clipBox.width();
1708 stream() <<
"height: " << info.clipBox.height();
1709 stream() <<
"clip: true";
1712 if (isPathContainer) {
1713 generatePathContainer(info);
1714 }
else if (!info.customItemType.isEmpty()) {
1715 stream() << info.customItemType <<
" {";
1717 stream() <<
"Item { // Structure node";
1721 if (usingTimelineAnimation() && info.timelineInfo) {
1722 QString frameCounterRef = info.timelineInfo->frameCounterReference
1723 + QStringLiteral(
".frameCounter");
1724 stream() <<
"visible: " << frameCounterRef <<
" >= " << info.timelineInfo->startFrame
1725 <<
" && " << frameCounterRef <<
" < " << info.timelineInfo->endFrame;
1727 if (info.timelineInfo->generateFrameCounter) {
1728 stream() <<
"property real frameCounter: " << frameCounterRef;
1729 if (info.timelineInfo->frameCounterOffset)
1730 stream(SameLine) <<
" + " << info.timelineInfo->frameCounterOffset;
1734 if (!info.viewBox.isEmpty()) {
1735 stream() <<
"transform: [";
1737 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
1739 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
1740 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
1745 generateNodeBase(info);
1747 generateNodeEnd(info);
1748 if (isPathContainer)
1749 m_inShapeItemLevel--;
1751 if (!info.clipBox.isEmpty()) {
1760bool QQuickQmlGenerator::generateMaskNode(
const MaskNodeInfo &info)
1762 if (Q_UNLIKELY(errorState()))
1766 if (info.stage == StructureNodeStage::End) {
1768 startDefsSuffixBlock();
1769 stream() <<
"Loader {";
1772 stream() <<
"id: " << info.id;
1773 stream() <<
"sourceComponent: " << info.id <<
"_container";
1774 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
1775 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
1777 stream() <<
"property real maskX: " << info.maskRect.left();
1778 stream() <<
"property real maskY: " << info.maskRect.top();
1779 stream() <<
"property real maskWidth: " << info.maskRect.width();
1780 stream() <<
"property real maskHeight: " << info.maskRect.height();
1782 stream() <<
"function maskRect(otherX, otherY, otherWidth, otherHeight) {";
1785 stream() <<
"return ";
1786 if (info.isMaskRectRelativeCoordinates) {
1789 << info.id <<
".maskX * otherWidth + otherX,"
1790 << info.id <<
".maskY * otherHeight + otherY,"
1791 << info.id <<
".maskWidth * otherWidth,"
1792 << info.id <<
".maskHeight * otherHeight)";
1796 << info.id <<
".maskX, "
1797 << info.id <<
".maskY, "
1798 << info.id <<
".maskWidth, "
1799 << info.id <<
".maskHeight)";
1808 endDefsSuffixBlock();
1814void QQuickQmlGenerator::generateFilterNode(
const FilterNodeInfo &info)
1816 if (Q_UNLIKELY(errorState()))
1819 stream() <<
"Item {";
1822 generateNodeBase(info);
1824 stream() <<
"property real originalWidth: filterSourceItem.sourceItem.originalBounds.width";
1825 stream() <<
"property real originalHeight: filterSourceItem.sourceItem.originalBounds.height";
1826 stream() <<
"property rect filterRect: " << info.id <<
"_filterParameters"
1827 <<
".adaptToFilterRect(0, 0, originalWidth, originalHeight)";
1829 for (qsizetype i = 0; i < info.steps.size();)
1830 i = generateFilterStep(info, i);
1833 startDefsSuffixBlock();
1834 stream() <<
"QtObject {";
1837 stream() <<
"id: " << info.id <<
"_filterParameters";
1838 stream() <<
"property int wrapMode: ";
1839 if (info.wrapMode == QSGTexture::Repeat)
1840 stream(SameLine) <<
"ShaderEffectSource.Repeat";
1842 stream(SameLine) <<
"ShaderEffectSource.ClampToEdge";
1844 stream() <<
"property rect filterRect: Qt.rect("
1845 << info.filterRect.x() <<
", "
1846 << info.filterRect.y() <<
", "
1847 << info.filterRect.width() <<
", "
1848 << info.filterRect.height() <<
")";
1850 stream() <<
"function adaptToFilterRect(sx, sy, sw, sh) {";
1853 if (info.csFilterRect == FilterNodeInfo::CoordinateSystem::Absolute) {
1854 stream() <<
"return Qt.rect(filterRect.x, filterRect.y, filterRect.width, filterRect.height)";
1856 stream() <<
"return Qt.rect(sx + sw * filterRect.x, sy + sh * filterRect.y, sw * filterRect.width, sh * filterRect.height)";
1864 endDefsSuffixBlock();
1866 generateNodeEnd(info);
1869qsizetype QQuickQmlGenerator::generateFilterStep(
const FilterNodeInfo &info,
1870 qsizetype stepIndex)
1872 const FilterNodeInfo::FilterStep &step = info.steps.at(stepIndex);
1873 const QString primitiveId = info.id + QStringLiteral(
"_primitive") + QString::number(stepIndex);
1877 QString inputId = step.input1 != FilterNodeInfo::FilterInput::SourceColor
1879 : QStringLiteral(
"filterSourceItem");
1881 bool isComposite =
false;
1882 switch (step.filterType) {
1883 case FilterNodeInfo::Type::Merge:
1885 const int maxNodeCount = 8;
1888 QList<QPair<FilterNodeInfo::FilterInput, QString> > inputs;
1889 for (; stepIndex < info.steps.size(); ++stepIndex) {
1890 const FilterNodeInfo::FilterStep &nodeStep = info.steps.at(stepIndex);
1891 if (nodeStep.filterType != FilterNodeInfo::Type::MergeNode)
1894 inputs.append(qMakePair(nodeStep.input1, nodeStep.namedInput1));
1897 if (inputs.size() > maxNodeCount) {
1898 qCWarning(lcQuickVectorImage) <<
"Maximum of" << maxNodeCount
1899 <<
"nodes exceeded in merge effect.";
1902 if (inputs.isEmpty()) {
1903 qCWarning(lcQuickVectorImage) <<
"Merge effect requires at least one node.";
1907 stream() <<
"ShaderEffect {";
1910 stream() <<
"id: " << primitiveId;
1911 stream() <<
"visible: false";
1913 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/femerge.frag.qsb\"";
1914 stream() <<
"width: source1.width";
1915 stream() <<
"height: source1.height";
1916 stream() <<
"property int sourceCount: " << std::min(qsizetype(8), inputs.size());
1918 for (
int i = 0; i < maxNodeCount; ++i) {
1919 auto input = i < inputs.size()
1921 : qMakePair(FilterNodeInfo::FilterInput::None, QStringLiteral(
"null"));
1923 QString inputId = input.first != FilterNodeInfo::FilterInput::SourceColor
1925 : QStringLiteral(
"filterSourceItem");
1927 stream() <<
"property var source" << (i + 1) <<
": " << inputId;
1935 case FilterNodeInfo::Type::CompositeOver:
1936 case FilterNodeInfo::Type::CompositeOut:
1937 case FilterNodeInfo::Type::CompositeIn:
1938 case FilterNodeInfo::Type::CompositeXor:
1939 case FilterNodeInfo::Type::CompositeAtop:
1940 case FilterNodeInfo::Type::CompositeArithmetic:
1941 case FilterNodeInfo::Type::CompositeLighter:
1945 case FilterNodeInfo::Type::BlendNormal:
1946 case FilterNodeInfo::Type::BlendMultiply:
1947 case FilterNodeInfo::Type::BlendScreen:
1948 case FilterNodeInfo::Type::BlendDarken:
1949 case FilterNodeInfo::Type::BlendLighten:
1951 stream() <<
"ShaderEffect {";
1954 QString input2Id = step.input2 != FilterNodeInfo::FilterInput::SourceColor
1956 : QStringLiteral(
"filterSourceItem");
1958 stream() <<
"id: " << primitiveId;
1959 stream() <<
"visible: false";
1962 switch (step.filterType) {
1963 case FilterNodeInfo::Type::CompositeOver:
1964 shader = QStringLiteral(
"fecompositeover");
1966 case FilterNodeInfo::Type::CompositeOut:
1967 shader = QStringLiteral(
"fecompositeout");
1969 case FilterNodeInfo::Type::CompositeIn:
1970 shader = QStringLiteral(
"fecompositein");
1972 case FilterNodeInfo::Type::CompositeXor:
1973 shader = QStringLiteral(
"fecompositexor");
1975 case FilterNodeInfo::Type::CompositeAtop:
1976 shader = QStringLiteral(
"fecompositeatop");
1978 case FilterNodeInfo::Type::CompositeArithmetic:
1979 shader = QStringLiteral(
"fecompositearithmetic");
1981 case FilterNodeInfo::Type::CompositeLighter:
1982 shader = QStringLiteral(
"fecompositelighter");
1984 case FilterNodeInfo::Type::BlendNormal:
1985 shader = QStringLiteral(
"feblendnormal");
1987 case FilterNodeInfo::Type::BlendMultiply:
1988 shader = QStringLiteral(
"feblendmultiply");
1990 case FilterNodeInfo::Type::BlendScreen:
1991 shader = QStringLiteral(
"feblendscreen");
1993 case FilterNodeInfo::Type::BlendDarken:
1994 shader = QStringLiteral(
"feblenddarken");
1996 case FilterNodeInfo::Type::BlendLighten:
1997 shader = QStringLiteral(
"feblendlighten");
2003 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/"
2004 << shader <<
".frag.qsb\"";
2005 stream() <<
"property var source: " << inputId;
2006 stream() <<
"property var source2: " << input2Id;
2007 stream() <<
"width: source.width";
2008 stream() <<
"height: source.height";
2011 QVector4D k = step.filterParameter.value<QVector4D>();
2012 stream() <<
"property var k: Qt.vector4d("
2025 case FilterNodeInfo::Type::Flood:
2027 stream() <<
"Rectangle {";
2030 stream() <<
"id: " << primitiveId;
2031 stream() <<
"visible: false";
2033 stream() <<
"width: " << inputId <<
".width";
2034 stream() <<
"height: " << inputId <<
".height";
2036 QColor floodColor = step.filterParameter.value<QColor>();
2037 stream() <<
"color: \"" << floodColor.name(QColor::HexArgb) <<
"\"";
2044 case FilterNodeInfo::Type::ColorMatrix:
2046 stream() <<
"ShaderEffect {";
2049 stream() <<
"id: " << primitiveId;
2050 stream() <<
"visible: false";
2052 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/fecolormatrix.frag.qsb\"";
2053 stream() <<
"property var source: " << inputId;
2054 stream() <<
"width: source.width";
2055 stream() <<
"height: source.height";
2057 QGenericMatrix<5, 5, qreal> matrix = step.filterParameter.value<QGenericMatrix<5, 5, qreal> >();
2058 for (
int row = 0; row < 4; ++row) {
2061 for (
int col = 0; col < 5; ++col)
2062 stream() <<
"property real m_" << row <<
"_" << col <<
": " << matrix(col, row);
2071 case FilterNodeInfo::Type::Offset:
2073 stream() <<
"ShaderEffectSource {";
2076 stream() <<
"id: " << primitiveId;
2077 stream() <<
"visible: false";
2078 stream() <<
"sourceItem: " << inputId;
2079 stream() <<
"width: sourceItem.width + offset.x";
2080 stream() <<
"height: sourceItem.height + offset.y";
2082 QVector2D offset = step.filterParameter.value<QVector2D>();
2083 stream() <<
"property vector2d offset: Qt.vector2d(";
2084 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute)
2085 stream(SameLine) << offset.x() <<
" / width, " << offset.y() <<
" / height)";
2087 stream(SameLine) << offset.x() <<
", " << offset.y() <<
")";
2089 stream() <<
"sourceRect: Qt.rect(-offset.x, -offset.y, width, height)";
2091 stream() <<
"ItemSpy {";
2093 stream() <<
"id: " << primitiveId <<
"_offset_itemspy";
2094 stream() <<
"anchors.fill: parent";
2098 stream() <<
"textureSize: " << primitiveId <<
"_offset_itemspy.requiredTextureSize";
2107 case FilterNodeInfo::Type::GaussianBlur:
2110 stream() <<
"MultiEffect {";
2113 stream() <<
"id: " << primitiveId;
2114 stream() <<
"visible: false";
2116 stream() <<
"source: " << inputId;
2117 stream() <<
"blurEnabled: true";
2118 stream() <<
"width: source.width";
2119 stream() <<
"height: source.height";
2121 const qreal maxDeviation(12.0);
2122 const qreal deviation = step.filterParameter.toReal();
2123 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative)
2124 stream() <<
"blur: Math.min(1.0, " << deviation <<
" * filterSourceItem.width / " << maxDeviation <<
")";
2126 stream() <<
"blur: " << std::min(qreal(1.0), deviation / maxDeviation);
2127 stream() <<
"blurMax: 64";
2135 qCWarning(lcQuickVectorImage) <<
"Unhandled filter type: " <<
int(step.filterType);
2137 stream() <<
"Item { id: " << primitiveId <<
" }";
2142 stream() <<
"ShaderEffectSource {";
2145 stream() <<
"id: " << step.outputName;
2146 if (stepIndex < info.steps.size())
2147 stream() <<
"visible: false";
2149 qreal x1, x2, y1, y2;
2150 step.filterPrimitiveRect.getCoords(&x1, &y1, &x2, &y2);
2151 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute) {
2152 stream() <<
"property real fpx1: " << x1;
2153 stream() <<
"property real fpy1: " << y1;
2154 stream() <<
"property real fpx2: " << x2;
2155 stream() <<
"property real fpy2: " << y2;
2156 }
else if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative) {
2160 stream() <<
"property real fpx1: " << x1 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2161 stream() <<
"property real fpy1: " << y1 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2162 stream() <<
"property real fpx2: " << x2 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2163 stream() <<
"property real fpy2: " << y2 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2165 stream() <<
"property real fpx1: parent.filterRect.x";
2166 stream() <<
"property real fpy1: parent.filterRect.y";
2167 stream() <<
"property real fpx2: parent.filterRect.x + parent.filterRect.width";
2168 stream() <<
"property real fpy2: parent.filterRect.y + parent.filterRect.height";
2171 stream() <<
"sourceItem: " << primitiveId;
2172 stream() <<
"sourceRect: Qt.rect(fpx1 - parent.filterRect.x, fpy1 - parent.filterRect.y, width, height)";
2174 stream() <<
"x: fpx1";
2175 stream() <<
"y: fpy1";
2176 stream() <<
"width: " <<
"fpx2 - fpx1";
2177 stream() <<
"height: " <<
"fpy2 - fpy1";
2179 stream() <<
"ItemSpy {";
2181 stream() <<
"id: " << primitiveId <<
"_itemspy";
2182 stream() <<
"anchors.fill: parent";
2186 stream() <<
"textureSize: " << primitiveId <<
"_itemspy.requiredTextureSize";
2194bool QQuickQmlGenerator::generatePatternNode(
const PatternNodeInfo &info)
2196 if (info.stage == StructureNodeStage::Start) {
2199 startDefsSuffixBlock();
2200 stream() <<
"Loader {";
2203 stream() <<
"id: " << info.id;
2204 stream() <<
"sourceComponent: " << info.id <<
"_container";
2205 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
2206 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
2207 stream() <<
"visible: false";
2208 stream() <<
"function sourceRect(targetWidth, targetHeight) {";
2211 stream() <<
"return Qt.rect(0, 0, ";
2212 if (!info.isPatternRectRelativeCoordinates) {
2213 stream(SameLine) << info.patternRect.width() <<
", "
2214 << info.patternRect.height();
2216 stream(SameLine) << info.patternRect.width() <<
" * targetWidth, "
2217 << info.patternRect.height() <<
" * targetHeight";
2219 stream(SameLine) <<
")";
2223 stream() <<
"function sourceOffset(targetWidth, targetHeight) {";
2226 stream() <<
"return Qt.vector3d(";
2227 if (!info.isPatternRectRelativeCoordinates) {
2228 stream(SameLine) << info.patternRect.x() <<
", "
2229 << info.patternRect.y() <<
", ";
2231 stream(SameLine) << info.patternRect.x() <<
" * targetWidth, "
2232 << info.patternRect.y() <<
" * targetHeight, ";
2234 stream(SameLine) <<
"0.0)";
2242 endDefsSuffixBlock();
2248bool QQuickQmlGenerator::generateMarkerNode(
const MarkerNodeInfo &info)
2250 if (info.stage == StructureNodeStage::Start) {
2251 startDefsSuffixBlock();
2252 stream() <<
"QtObject {";
2255 stream() <<
"id: " << info.id <<
"_markerParameters";
2257 stream() <<
"property bool startReversed: ";
2258 if (info.orientation == MarkerNodeInfo::Orientation::AutoStartReverse)
2259 stream(SameLine) <<
"true";
2261 stream(SameLine) <<
"false";
2263 stream() <<
"function autoAngle(adaptedAngle) {";
2265 if (info.orientation == MarkerNodeInfo::Orientation::Value)
2266 stream() <<
"return " << info.angle;
2268 stream() <<
"return adaptedAngle";
2274 endDefsSuffixBlock();
2276 if (!info.clipBox.isEmpty()) {
2277 stream() <<
"Item {";
2280 stream() <<
"x: " << info.clipBox.x();
2281 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2282 stream(SameLine) <<
" * strokeWidth";
2283 stream() <<
"y: " << info.clipBox.y();
2284 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2285 stream(SameLine) <<
" * strokeWidth";
2286 stream() <<
"width: " << info.clipBox.width();
2287 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2288 stream(SameLine) <<
" * strokeWidth";
2289 stream() <<
"height: " << info.clipBox.height();
2290 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2291 stream(SameLine) <<
" * strokeWidth";
2292 stream() <<
"clip: true";
2295 stream() <<
"Item {";
2298 if (!info.clipBox.isEmpty()) {
2299 stream() <<
"x: " << -info.clipBox.x();
2300 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2301 stream(SameLine) <<
" * strokeWidth";
2302 stream() <<
"y: " << -info.clipBox.y();
2303 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2304 stream(SameLine) <<
" * strokeWidth";
2307 stream() <<
"id: " << info.id;
2309 stream() <<
"property real markerWidth: " << info.markerSize.width();
2310 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2311 stream(SameLine) <<
" * strokeWidth";
2313 stream() <<
"property real markerHeight: " << info.markerSize.height();
2314 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2315 stream(SameLine) <<
" * strokeWidth";
2317 stream() <<
"function calculateMarkerScale(w, h) {";
2320 stream() <<
"var scaleX = 1.0";
2321 stream() <<
"var scaleY = 1.0";
2322 stream() <<
"var offsetX = 0.0";
2323 stream() <<
"var offsetY = 0.0";
2324 if (info.viewBox.width() > 0)
2325 stream() <<
"if (w > 0) scaleX = w / " << info.viewBox.width();
2326 if (info.viewBox.height() > 0)
2327 stream() <<
"if (h > 0) scaleY = h / " << info.viewBox.height();
2329 if (info.preserveAspectRatio & MarkerNodeInfo::xyMask) {
2330 stream() <<
"if (scaleX != scaleY) {";
2333 if (info.preserveAspectRatio & MarkerNodeInfo::meet)
2334 stream() <<
"scaleX = scaleY = Math.min(scaleX, scaleY)";
2336 stream() <<
"scaleX = scaleY = Math.max(scaleX, scaleY)";
2338 QString overflowX = QStringLiteral(
"scaleX * %1 - w").arg(info.viewBox.width());
2339 QString overflowY = QStringLiteral(
"scaleY * %1 - h").arg(info.viewBox.height());
2341 const quint8 xRatio = info.preserveAspectRatio & MarkerNodeInfo::xMask;
2342 if (xRatio == MarkerNodeInfo::xMid)
2343 stream() <<
"offsetX -= " << overflowX <<
" / 2";
2344 else if (xRatio == MarkerNodeInfo::xMax)
2345 stream() <<
"offsetX -= " << overflowX;
2347 const quint8 yRatio = info.preserveAspectRatio & MarkerNodeInfo::yMask;
2348 if (yRatio == MarkerNodeInfo::yMid)
2349 stream() <<
"offsetY -= " << overflowY <<
" / 2";
2350 else if (yRatio == MarkerNodeInfo::yMax)
2351 stream() <<
"offsetY -= " << overflowY;
2357 stream() <<
"return Qt.vector4d("
2358 <<
"offsetX - " << info.anchorPoint.x() <<
" * scaleX, "
2359 <<
"offsetY - " << info.anchorPoint.y() <<
" * scaleY, "
2366 stream() <<
"property vector4d markerScale: calculateMarkerScale(markerWidth, markerHeight)";
2368 stream() <<
"transform: [";
2371 stream() <<
"Scale { xScale: " << info.id <<
".markerScale.z; yScale: " << info.id <<
".markerScale.w },";
2372 stream() <<
"Translate { x: " << info.id <<
".markerScale.x; y: " << info.id <<
".markerScale.y }";
2378 generateNodeEnd(info);
2380 if (!info.clipBox.isEmpty()) {
2389bool QQuickQmlGenerator::generateRootNode(
const StructureNodeInfo &info)
2391 if (Q_UNLIKELY(errorState()))
2394 const QStringList comments = m_commentString.split(u'\n');
2396 if (!isNodeVisible(info)) {
2399 if (comments.isEmpty()) {
2400 stream() <<
"// Generated from SVG";
2402 for (
const auto &comment : comments)
2403 stream() <<
"// " << comment;
2406 stream() <<
"import QtQuick";
2407 stream() <<
"import QtQuick.Shapes" << Qt::endl;
2408 stream() <<
"Item {";
2411 double w = info.size.width();
2412 double h = info.size.height();
2414 stream() <<
"implicitWidth: " << w;
2416 stream() <<
"implicitHeight: " << h;
2424 if (info.stage == StructureNodeStage::Start) {
2427 if (comments.isEmpty())
2428 stream() <<
"// Generated from SVG";
2430 for (
const auto &comment : comments)
2431 stream() <<
"// " << comment;
2433 stream() <<
"import QtQuick";
2434 stream() <<
"import QtQuick.VectorImage";
2435 stream() <<
"import QtQuick.VectorImage.Helpers";
2436 stream() <<
"import QtQuick.Shapes";
2437 stream() <<
"import QtQuick.Effects";
2438 if (usingTimelineAnimation())
2439 stream() <<
"import QtQuick.Timeline";
2441 for (
const auto &import : std::as_const(m_extraImports))
2442 stream() <<
"import " << import;
2444 stream() << Qt::endl <<
"Item {";
2447 double w = info.size.width();
2448 double h = info.size.height();
2450 stream() <<
"implicitWidth: " << w;
2452 stream() <<
"implicitHeight: " << h;
2454 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2455 stream() <<
"component AnimationsInfo : QtObject";
2460 stream() <<
"property bool paused: false";
2461 stream() <<
"property int loops: 1";
2462 stream() <<
"signal restart()";
2464 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2467 stream() <<
"property AnimationsInfo animations : AnimationsInfo {}";
2470 stream() <<
"Item {";
2472 stream() <<
"width: 1";
2473 stream() <<
"height: 1";
2475 stream() <<
"ItemSpy { id: __qt_toplevel_scale_itemspy; anchors.fill: parent }";
2480 if (!info.viewBox.isEmpty()) {
2481 stream() <<
"transform: [";
2483 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
2485 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
2486 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
2491 if (!info.forceSeparatePaths && info.isPathContainer) {
2492 m_topLevelIdString = QStringLiteral(
"__qt_toplevel");
2493 stream() <<
"id: " << m_topLevelIdString;
2495 generatePathContainer(info);
2498 generateNodeBase(info);
2500 m_topLevelIdString = generateNodeBase(info);
2501 if (m_topLevelIdString.isEmpty())
2502 qCWarning(lcQuickVectorImage) <<
"No ID specified for top level item";
2505 if (usingTimelineAnimation() && info.timelineInfo) {
2506 stream() <<
"property real frameCounter: " << info.timelineInfo->startFrame;
2507 stream() <<
"NumberAnimation on frameCounter {";
2509 stream() <<
"from: " << info.timelineInfo->startFrame;
2510 stream() <<
"to: " << info.timelineInfo->endFrame - 0.01;
2511 stream() <<
"duration: " << processAnimationTime(info.timelineInfo->duration);
2512 generateAnimationBindings();
2515 stream() <<
"visible: frameCounter >= " << info.timelineInfo->startFrame
2516 <<
" && frameCounter < " << info.timelineInfo->endFrame;
2519 if (m_inShapeItemLevel > 0) {
2520 m_inShapeItemLevel--;
2525 for (
const auto [coords, id] : m_easings.asKeyValueRange()) {
2526 stream() <<
"readonly property easingCurve " << id <<
": ({ type: Easing.BezierSpline, bezierCurve: [ ";
2527 for (
auto coord : coords)
2528 stream(SameLine) << coord <<
", ";
2529 stream(SameLine) <<
"1, 1 ] })";
2532 generateNodeEnd(info);
2539void QQuickQmlGenerator::startDefsSuffixBlock()
2541 std::swap(m_indentLevel, m_oldIndentLevel);
2542 m_stream.setString(&m_defsSuffix);
2545void QQuickQmlGenerator::endDefsSuffixBlock()
2547 std::swap(m_indentLevel, m_oldIndentLevel);
2548 m_stream.setDevice(&m_result);
2551QStringView QQuickQmlGenerator::indent()
2553 static QString indentString;
2554 int indentWidth = m_indentLevel * 4;
2555 if (indentWidth > indentString.size())
2556 indentString.fill(QLatin1Char(
' '), indentWidth * 2);
2557 return QStringView(indentString).first(indentWidth);
2560QTextStream &QQuickQmlGenerator::stream(
int flags)
2562 if (m_stream.device() ==
nullptr && m_stream.string() ==
nullptr)
2563 m_stream.setDevice(&m_result);
2564 else if (!(flags & StreamFlags::SameLine))
2565 m_stream << Qt::endl << indent();
2567 static qint64 maxBufferSize = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_BUFFER").value_or(64 << 20);
2568 if (m_stream.device()) {
2569 if (Q_UNLIKELY(!checkSanityLimit(m_stream.device()->size(), maxBufferSize,
"buffer size"_L1)))
2570 m_stream.device()->reset();
2572 if (Q_UNLIKELY(!checkSanityLimit(m_stream.string()->size(), maxBufferSize,
"buffer string size"_L1)))
2573 m_stream.string()->clear();
2579const char *QQuickQmlGenerator::shapeName()
const
2581 return m_shapeTypeName.isEmpty() ?
"Shape" : m_shapeTypeName.constData();
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QString sanitizeString(const QString &input)
static int processAnimationTime(int time)