File: LineGridNode.cpp

package info (click to toggle)
kf6-kquickcharts 6.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 852 kB
  • sloc: cpp: 5,663; makefile: 11; sh: 5
file content (157 lines) | stat: -rw-r--r-- 3,752 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * This file is part of KQuickCharts
 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include "LineGridNode.h"

#include <cmath>

#include <QSGFlatColorMaterial>

LineGridNode::LineGridNode()
{
    m_geometry = new QSGGeometry{QSGGeometry::defaultAttributes_Point2D(), 0};
    m_geometry->setDrawingMode(QSGGeometry::DrawLines);
    m_geometry->setLineWidth(m_lineWidth);
    setGeometry(m_geometry);

    m_material = new QSGFlatColorMaterial{};
    m_material->setColor(QColor(255, 0, 0, 255));
    setMaterial(m_material);

    setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial);
}

LineGridNode::~LineGridNode()
{
}

void LineGridNode::setVisible(bool visible)
{
    if (visible == m_visible) {
        return;
    }

    m_visible = visible;
    markDirty(QSGNode::DirtySubtreeBlocked);
}

void LineGridNode::setVertical(bool vertical)
{
    if (vertical == m_vertical) {
        return;
    }

    m_vertical = vertical;
}

void LineGridNode::setRect(const QRectF &rect)
{
    if (rect == m_rect) {
        return;
    }

    m_rect = rect;
}

void LineGridNode::setColor(const QColor &color)
{
    if (color == m_material->color()) {
        return;
    }

    m_material->setColor(color);
    markDirty(QSGNode::DirtyMaterial);
}

void LineGridNode::setSpacing(float spacing)
{
    if (qFuzzyCompare(spacing, m_spacing)) {
        return;
    }

    m_spacing = spacing;
}

void LineGridNode::setLineWidth(float lineWidth)
{
    if (qFuzzyCompare(lineWidth, m_lineWidth)) {
        return;
    }

    m_lineWidth = lineWidth;
    m_geometry->setLineWidth(lineWidth);
    markDirty(QSGNode::DirtyGeometry);
}

bool LineGridNode::isSubtreeBlocked() const
{
    return !m_visible;
}

void LineGridNode::update()
{
    if (!m_rect.isValid()) {
        return;
    }

    int totalVertices = 0;
    if (!m_vertical) {
        totalVertices = std::floor(m_rect.width() / std::ceil(m_spacing)) * 2 + 4;
    } else {
        totalVertices = std::floor(m_rect.height() / std::ceil(m_spacing)) * 2 + 4;
    }

    if (totalVertices < 4) {
        return;
    }

    if (totalVertices != m_geometry->vertexCount()) {
        m_geometry->allocate(totalVertices, totalVertices);
    }

    auto vertices = m_geometry->vertexDataAsPoint2D();
    auto indices = m_geometry->indexDataAsUShort();

    if (!vertices || !indices) {
        return;
    }

    int index = 0;
    if (m_vertical) {
        line(vertices, indices, index, m_rect.left(), m_rect.top(), m_rect.right(), m_rect.top());

        auto y = m_spacing;
        for (auto i = 0; i < (totalVertices - 4) / 2; ++i) {
            line(vertices, indices, index, m_rect.left(), y, m_rect.right(), y);
            y += m_spacing;
        }

        line(vertices, indices, index, m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
    } else {
        line(vertices, indices, index, m_rect.left(), m_rect.top(), m_rect.left(), m_rect.bottom());

        auto x = m_spacing;
        for (auto i = 0; i < (totalVertices - 4) / 2; ++i) {
            line(vertices, indices, index, x, m_rect.top(), x, m_rect.bottom());
            x += m_spacing;
        }

        line(vertices, indices, index, m_rect.right(), m_rect.top(), m_rect.right(), m_rect.bottom());
    }

    m_geometry->markVertexDataDirty();
    m_geometry->markIndexDataDirty();
    markDirty(QSGNode::DirtyGeometry);
}

void LineGridNode::line(QSGGeometry::Point2D *vertices, quint16 *indices, int &index, qreal fromX, qreal fromY, qreal toX, qreal toY)
{
    indices[index] = index;
    vertices[index++].set(fromX, fromY);
    indices[index] = index;
    vertices[index++].set(toX, toY);
}