| 1 | // Copyright (C) 2025 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qsvgstyleselector_p.h" |
| 5 | #include <QtSvg/private/qsvgnode_p.h> |
| 6 | #include <QtSvg/private/qsvgstructure_p.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | QSvgStyleSelector::QSvgStyleSelector() |
| 11 | { |
| 12 | nameCaseSensitivity = Qt::CaseInsensitive; |
| 13 | } |
| 14 | |
| 15 | QSvgStyleSelector::~QSvgStyleSelector() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | QString QSvgStyleSelector::nodeToName(QSvgNode *node) const |
| 20 | { |
| 21 | return node->typeName(); |
| 22 | } |
| 23 | |
| 24 | QSvgNode *QSvgStyleSelector::svgNode(NodePtr node) const |
| 25 | { |
| 26 | return (QSvgNode*)node.ptr; |
| 27 | } |
| 28 | |
| 29 | QSvgStructureNode *QSvgStyleSelector::nodeToStructure(QSvgNode *n) const |
| 30 | { |
| 31 | if (n && |
| 32 | (n->type() == QSvgNode::Doc || |
| 33 | n->type() == QSvgNode::Group || |
| 34 | n->type() == QSvgNode::Defs || |
| 35 | n->type() == QSvgNode::Switch)) { |
| 36 | return (QSvgStructureNode*)n; |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | QSvgStructureNode *QSvgStyleSelector::svgStructure(NodePtr node) const |
| 42 | { |
| 43 | QSvgNode *n = svgNode(node); |
| 44 | QSvgStructureNode *st = nodeToStructure(n); |
| 45 | return st; |
| 46 | } |
| 47 | |
| 48 | bool QSvgStyleSelector::nodeNameEquals(NodePtr node, const QString &nodeName) const |
| 49 | { |
| 50 | QSvgNode *n = svgNode(node); |
| 51 | if (!n) |
| 52 | return false; |
| 53 | QString name = nodeToName(node: n); |
| 54 | return QString::compare(s1: name, s2: nodeName, cs: Qt::CaseInsensitive) == 0; |
| 55 | } |
| 56 | |
| 57 | QString QSvgStyleSelector::attributeValue(NodePtr node, const QCss::AttributeSelector &asel) const |
| 58 | { |
| 59 | const QString &name = asel.name; |
| 60 | QSvgNode *n = svgNode(node); |
| 61 | if ((!n->nodeId().isEmpty() && (name == QLatin1String("id") || |
| 62 | name == QLatin1String("xml:id")))) |
| 63 | return n->nodeId(); |
| 64 | if (!n->xmlClass().isEmpty() && name == QLatin1String("class")) |
| 65 | return n->xmlClass(); |
| 66 | return QString(); |
| 67 | } |
| 68 | |
| 69 | bool QSvgStyleSelector::hasAttributes(NodePtr node) const |
| 70 | { |
| 71 | QSvgNode *n = svgNode(node); |
| 72 | return (n && |
| 73 | (!n->nodeId().isEmpty() || !n->xmlClass().isEmpty())); |
| 74 | } |
| 75 | |
| 76 | QStringList QSvgStyleSelector::nodeIds(NodePtr node) const |
| 77 | { |
| 78 | QSvgNode *n = svgNode(node); |
| 79 | QString nid; |
| 80 | if (n) |
| 81 | nid = n->nodeId(); |
| 82 | QStringList lst; lst.append(t: nid); |
| 83 | return lst; |
| 84 | } |
| 85 | |
| 86 | QStringList QSvgStyleSelector::nodeNames(NodePtr node) const |
| 87 | { |
| 88 | QSvgNode *n = svgNode(node); |
| 89 | if (n) |
| 90 | return QStringList(nodeToName(node: n)); |
| 91 | return QStringList(); |
| 92 | } |
| 93 | |
| 94 | bool QSvgStyleSelector::isNullNode(NodePtr node) const |
| 95 | { |
| 96 | return !node.ptr; |
| 97 | } |
| 98 | |
| 99 | QCss::StyleSelector::NodePtr QSvgStyleSelector::parentNode(NodePtr node) const |
| 100 | { |
| 101 | QSvgNode *n = svgNode(node); |
| 102 | NodePtr newNode; |
| 103 | newNode.ptr = 0; |
| 104 | newNode.id = 0; |
| 105 | if (n) { |
| 106 | QSvgNode *svgParent = n->parent(); |
| 107 | if (svgParent) { |
| 108 | newNode.ptr = svgParent; |
| 109 | } |
| 110 | } |
| 111 | return newNode; |
| 112 | } |
| 113 | |
| 114 | QCss::StyleSelector::NodePtr QSvgStyleSelector::previousSiblingNode(NodePtr node) const |
| 115 | { |
| 116 | NodePtr newNode; |
| 117 | newNode.ptr = 0; |
| 118 | newNode.id = 0; |
| 119 | |
| 120 | QSvgNode *n = svgNode(node); |
| 121 | if (!n) |
| 122 | return newNode; |
| 123 | QSvgStructureNode *svgParent = nodeToStructure(n: n->parent()); |
| 124 | |
| 125 | if (svgParent) { |
| 126 | newNode.ptr = svgParent->previousSiblingNode(n); |
| 127 | } |
| 128 | return newNode; |
| 129 | } |
| 130 | |
| 131 | QCss::StyleSelector::NodePtr QSvgStyleSelector::duplicateNode(NodePtr node) const |
| 132 | { |
| 133 | NodePtr n; |
| 134 | n.ptr = node.ptr; |
| 135 | n.id = node.id; |
| 136 | return n; |
| 137 | } |
| 138 | |
| 139 | void QSvgStyleSelector::freeNode(NodePtr node) const |
| 140 | { |
| 141 | Q_UNUSED(node); |
| 142 | } |
| 143 | |
| 144 | QT_END_NAMESPACE |
| 145 |
