1 | /* |
2 | * This file is part of KQuickCharts |
3 | * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> |
4 | * |
5 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
6 | */ |
7 | |
8 | #include "LineGridNode.h" |
9 | |
10 | #include <cmath> |
11 | |
12 | #include <QSGFlatColorMaterial> |
13 | |
14 | LineGridNode::LineGridNode() |
15 | { |
16 | m_geometry = new QSGGeometry{QSGGeometry::defaultAttributes_Point2D(), 0}; |
17 | m_geometry->setDrawingMode(QSGGeometry::DrawLines); |
18 | m_geometry->setLineWidth(m_lineWidth); |
19 | setGeometry(m_geometry); |
20 | |
21 | m_material = new QSGFlatColorMaterial{}; |
22 | m_material->setColor(QColor(255, 0, 0, 255)); |
23 | setMaterial(m_material); |
24 | |
25 | setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial); |
26 | } |
27 | |
28 | LineGridNode::~LineGridNode() |
29 | { |
30 | } |
31 | |
32 | void LineGridNode::setVisible(bool visible) |
33 | { |
34 | if (visible == m_visible) { |
35 | return; |
36 | } |
37 | |
38 | m_visible = visible; |
39 | markDirty(bits: QSGNode::DirtySubtreeBlocked); |
40 | } |
41 | |
42 | void LineGridNode::setVertical(bool vertical) |
43 | { |
44 | if (vertical == m_vertical) { |
45 | return; |
46 | } |
47 | |
48 | m_vertical = vertical; |
49 | } |
50 | |
51 | void LineGridNode::setRect(const QRectF &rect) |
52 | { |
53 | if (rect == m_rect) { |
54 | return; |
55 | } |
56 | |
57 | m_rect = rect; |
58 | } |
59 | |
60 | void LineGridNode::setColor(const QColor &color) |
61 | { |
62 | if (color == m_material->color()) { |
63 | return; |
64 | } |
65 | |
66 | m_material->setColor(color); |
67 | markDirty(bits: QSGNode::DirtyMaterial); |
68 | } |
69 | |
70 | void LineGridNode::setSpacing(float spacing) |
71 | { |
72 | if (qFuzzyCompare(p1: spacing, p2: m_spacing)) { |
73 | return; |
74 | } |
75 | |
76 | m_spacing = spacing; |
77 | } |
78 | |
79 | void LineGridNode::setLineWidth(float lineWidth) |
80 | { |
81 | if (qFuzzyCompare(p1: lineWidth, p2: m_lineWidth)) { |
82 | return; |
83 | } |
84 | |
85 | m_lineWidth = lineWidth; |
86 | m_geometry->setLineWidth(lineWidth); |
87 | markDirty(bits: QSGNode::DirtyGeometry); |
88 | } |
89 | |
90 | bool LineGridNode::isSubtreeBlocked() const |
91 | { |
92 | return !m_visible; |
93 | } |
94 | |
95 | void LineGridNode::update() |
96 | { |
97 | if (!m_rect.isValid()) { |
98 | return; |
99 | } |
100 | |
101 | int totalVertices = 0; |
102 | if (!m_vertical) { |
103 | totalVertices = std::floor(x: m_rect.width() / std::ceil(x: m_spacing)) * 2 + 4; |
104 | } else { |
105 | totalVertices = std::floor(x: m_rect.height() / std::ceil(x: m_spacing)) * 2 + 4; |
106 | } |
107 | |
108 | if (totalVertices < 4) { |
109 | return; |
110 | } |
111 | |
112 | if (totalVertices != m_geometry->vertexCount()) { |
113 | m_geometry->allocate(vertexCount: totalVertices, indexCount: totalVertices); |
114 | } |
115 | |
116 | auto vertices = m_geometry->vertexDataAsPoint2D(); |
117 | auto indices = m_geometry->indexDataAsUShort(); |
118 | |
119 | if (!vertices || !indices) { |
120 | return; |
121 | } |
122 | |
123 | int index = 0; |
124 | if (m_vertical) { |
125 | line(vertices, indices, index, fromX: m_rect.left(), fromY: m_rect.top(), toX: m_rect.right(), toY: m_rect.top()); |
126 | |
127 | auto y = m_spacing; |
128 | for (auto i = 0; i < (totalVertices - 4) / 2; ++i) { |
129 | line(vertices, indices, index, fromX: m_rect.left(), fromY: y, toX: m_rect.right(), toY: y); |
130 | y += m_spacing; |
131 | } |
132 | |
133 | line(vertices, indices, index, fromX: m_rect.left(), fromY: m_rect.bottom(), toX: m_rect.right(), toY: m_rect.bottom()); |
134 | } else { |
135 | line(vertices, indices, index, fromX: m_rect.left(), fromY: m_rect.top(), toX: m_rect.left(), toY: m_rect.bottom()); |
136 | |
137 | auto x = m_spacing; |
138 | for (auto i = 0; i < (totalVertices - 4) / 2; ++i) { |
139 | line(vertices, indices, index, fromX: x, fromY: m_rect.top(), toX: x, toY: m_rect.bottom()); |
140 | x += m_spacing; |
141 | } |
142 | |
143 | line(vertices, indices, index, fromX: m_rect.right(), fromY: m_rect.top(), toX: m_rect.right(), toY: m_rect.bottom()); |
144 | } |
145 | |
146 | m_geometry->markVertexDataDirty(); |
147 | m_geometry->markIndexDataDirty(); |
148 | markDirty(bits: QSGNode::DirtyGeometry); |
149 | } |
150 | |
151 | void LineGridNode::line(QSGGeometry::Point2D *vertices, quint16 *indices, int &index, qreal fromX, qreal fromY, qreal toX, qreal toY) |
152 | { |
153 | indices[index] = index; |
154 | vertices[index++].set(nx: fromX, ny: fromY); |
155 | indices[index] = index; |
156 | vertices[index++].set(nx: toX, ny: toY); |
157 | } |
158 | |