File: GridLines.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 (209 lines) | stat: -rw-r--r-- 4,522 bytes parent folder | download | duplicates (2)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * 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 "GridLines.h"

#include "LineGridNode.h"
#include "XYChart.h"

LinePropertiesGroup::LinePropertiesGroup(GridLines *parent)
    : QObject(parent)
{
    m_parent = parent;
}

bool LinePropertiesGroup::visible() const
{
    return m_visible;
}

void LinePropertiesGroup::setVisible(bool newVisible)
{
    if (newVisible == m_visible) {
        return;
    }

    m_visible = newVisible;
    Q_EMIT propertiesChanged();
}

QColor LinePropertiesGroup::color() const
{
    return m_color;
}

void LinePropertiesGroup::setColor(const QColor &newColor)
{
    if (newColor == m_color) {
        return;
    }

    m_color = newColor;
    Q_EMIT propertiesChanged();
}

float LinePropertiesGroup::lineWidth() const
{
    return m_lineWidth;
}

void LinePropertiesGroup::setLineWidth(float newLineWidth)
{
    if (newLineWidth == m_lineWidth) {
        return;
    }

    m_lineWidth = newLineWidth;
    Q_EMIT propertiesChanged();
}

int LinePropertiesGroup::frequency() const
{
    return m_frequency;
}

void LinePropertiesGroup::setFrequency(int newFrequency)
{
    if (newFrequency == m_frequency) {
        return;
    }

    m_frequency = newFrequency;
    Q_EMIT propertiesChanged();
}

int LinePropertiesGroup::count() const
{
    return m_count;
}

void LinePropertiesGroup::setCount(int newCount)
{
    if (newCount == m_count) {
        return;
    }

    m_count = newCount;
    Q_EMIT propertiesChanged();
}

GridLines::GridLines(QQuickItem *parent)
    : QQuickItem(parent)
{
    setFlag(QQuickItem::ItemHasContents);

    m_major = std::make_unique<LinePropertiesGroup>(this);
    connect(m_major.get(), &LinePropertiesGroup::propertiesChanged, this, &GridLines::update);
    m_minor = std::make_unique<LinePropertiesGroup>(this);
    connect(m_minor.get(), &LinePropertiesGroup::propertiesChanged, this, &GridLines::update);
}

GridLines::Direction GridLines::direction() const
{
    return m_direction;
}

void GridLines::setDirection(GridLines::Direction newDirection)
{
    if (newDirection == m_direction) {
        return;
    }

    m_direction = newDirection;
    update();
    Q_EMIT directionChanged();
}

XYChart *GridLines::chart() const
{
    return m_chart;
}

void GridLines::setChart(XYChart *newChart)
{
    if (newChart == m_chart) {
        return;
    }

    if (m_chart) {
        disconnect(m_chart, &XYChart::computedRangeChanged, this, &GridLines::update);
    }

    m_chart = newChart;

    if (m_chart) {
        connect(m_chart, &XYChart::computedRangeChanged, this, &GridLines::update);
    }

    update();
    Q_EMIT chartChanged();
}

float GridLines::spacing() const
{
    return m_spacing;
}

void GridLines::setSpacing(float newSpacing)
{
    if (newSpacing == m_spacing || m_chart != nullptr) {
        return;
    }

    m_spacing = newSpacing;
    update();
    Q_EMIT spacingChanged();
}

LinePropertiesGroup *GridLines::majorGroup() const
{
    return m_major.get();
}

LinePropertiesGroup *GridLines::minorGroup() const
{
    return m_minor.get();
}

QSGNode *GridLines::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *)
{
    if (!node) {
        node = new QSGNode{};
        node->appendChildNode(new LineGridNode{});
        node->appendChildNode(new LineGridNode{});
    }

    if (m_chart) {
        if (m_direction == Direction::Horizontal) {
            m_spacing = width() / (m_chart->computedRange().distanceX - 1);
        } else {
            m_spacing = height() / (m_chart->computedRange().distanceY);
        }
    }

    updateLines(static_cast<LineGridNode *>(node->childAtIndex(0)), m_minor.get());
    updateLines(static_cast<LineGridNode *>(node->childAtIndex(1)), m_major.get());

    return node;
}

void GridLines::updateLines(LineGridNode *node, LinePropertiesGroup *properties)
{
    node->setVisible(properties->visible());
    node->setRect(boundingRect());
    node->setVertical(m_direction == Direction::Vertical);
    node->setColor(properties->color());
    node->setLineWidth(properties->lineWidth());
    if (properties->count() > 0) {
        node->setSpacing(m_direction == Direction::Horizontal ? width() / (properties->count() + 1) : height() / (properties->count() + 1));
    } else {
        node->setSpacing(m_spacing * properties->frequency());
    }
    node->update();
}

#include "moc_GridLines.cpp"