KQuickCharts

LineGridNode.cpp
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
14LineGridNode::LineGridNode()
15{
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
26}
27
28LineGridNode::~LineGridNode()
29{
30}
31
32void LineGridNode::setVisible(bool visible)
33{
34 if (visible == m_visible) {
35 return;
36 }
37
38 m_visible = visible;
40}
41
42void LineGridNode::setVertical(bool vertical)
43{
44 if (vertical == m_vertical) {
45 return;
46 }
47
48 m_vertical = vertical;
49}
50
51void LineGridNode::setRect(const QRectF &rect)
52{
53 if (rect == m_rect) {
54 return;
55 }
56
57 m_rect = rect;
58}
59
60void LineGridNode::setColor(const QColor &color)
61{
62 if (color == m_material->color()) {
63 return;
64 }
65
66 m_material->setColor(color);
68}
69
70void LineGridNode::setSpacing(float spacing)
71{
72 if (qFuzzyCompare(spacing, m_spacing)) {
73 return;
74 }
75
76 m_spacing = spacing;
77}
78
79void LineGridNode::setLineWidth(float lineWidth)
80{
81 if (qFuzzyCompare(lineWidth, m_lineWidth)) {
82 return;
83 }
84
85 m_lineWidth = lineWidth;
86 m_geometry->setLineWidth(lineWidth);
88}
89
90bool LineGridNode::isSubtreeBlocked() const
91{
92 return !m_visible;
93}
94
95void LineGridNode::update()
96{
97 if (!m_rect.isValid()) {
98 return;
99 }
100
101 int totalVertices = 0;
102 if (!m_vertical) {
103 totalVertices = std::floor(m_rect.width() / std::ceil(m_spacing)) * 2 + 4;
104 } else {
105 totalVertices = std::floor(m_rect.height() / std::ceil(m_spacing)) * 2 + 4;
106 }
107
108 if (totalVertices < 4) {
109 return;
110 }
111
112 if (totalVertices != m_geometry->vertexCount()) {
113 m_geometry->allocate(totalVertices, 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, m_rect.left(), m_rect.top(), m_rect.right(), m_rect.top());
126
127 auto y = m_spacing;
128 for (auto i = 0; i < (totalVertices - 4) / 2; ++i) {
129 line(vertices, indices, index, m_rect.left(), y, m_rect.right(), y);
130 y += m_spacing;
131 }
132
133 line(vertices, indices, index, m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
134 } else {
135 line(vertices, indices, index, m_rect.left(), m_rect.top(), m_rect.left(), m_rect.bottom());
136
137 auto x = m_spacing;
138 for (auto i = 0; i < (totalVertices - 4) / 2; ++i) {
139 line(vertices, indices, index, x, m_rect.top(), x, m_rect.bottom());
140 x += m_spacing;
141 }
142
143 line(vertices, indices, index, m_rect.right(), m_rect.top(), m_rect.right(), m_rect.bottom());
144 }
145
146 m_geometry->markVertexDataDirty();
147 m_geometry->markIndexDataDirty();
149}
150
151void 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(fromX, fromY);
155 indices[index] = index;
156 vertices[index++].set(toX, toY);
157}
qreal bottom() const const
qreal height() const const
bool isValid() const const
qreal left() const const
qreal right() const const
qreal top() const const
qreal width() const const
void setGeometry(QSGGeometry *geometry)
const QColor & color() const const
void setColor(const QColor &color)
void set(float x, float y)
void allocate(int vertexCount, int indexCount)
const AttributeSet & defaultAttributes_Point2D()
quint16 * indexDataAsUShort()
void markIndexDataDirty()
void markVertexDataDirty()
void setDrawingMode(unsigned int mode)
void setLineWidth(float width)
int vertexCount() const const
Point2D * vertexDataAsPoint2D()
void setMaterial(QSGMaterial *material)
DirtySubtreeBlocked
void markDirty(DirtyState bits)
void setFlags(Flags f, bool enabled)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.