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 "PieChartMaterial.h"
9
10constexpr int MaximumSegmentCount = 100;
11
12PieChartMaterial::PieChartMaterial()
13{
14 setFlag(flags: QSGMaterial::Blending);
15}
16
17PieChartMaterial::~PieChartMaterial()
18{
19}
20
21QSGMaterialType *PieChartMaterial::type() const
22{
23 static QSGMaterialType type;
24 return &type;
25}
26
27QSGMaterialShader *PieChartMaterial::createShader(QSGRendererInterface::RenderMode) const
28{
29 return new PieChartShader();
30}
31
32QVector2D PieChartMaterial::aspectRatio() const
33{
34 return m_aspectRatio;
35}
36
37float PieChartMaterial::innerRadius() const
38{
39 return m_innerRadius;
40}
41
42float PieChartMaterial::outerRadius() const
43{
44 return m_outerRadius;
45}
46
47QColor PieChartMaterial::backgroundColor() const
48{
49 return m_backgroundColor;
50}
51
52QList<QVector2D> PieChartMaterial::segments() const
53{
54 return m_segments;
55}
56
57QList<QVector4D> PieChartMaterial::colors() const
58{
59 return m_colors;
60}
61
62bool PieChartMaterial::smoothEnds() const
63{
64 return m_smoothEnds;
65}
66
67float PieChartMaterial::fromAngle() const
68{
69 return m_fromAngle;
70}
71
72float PieChartMaterial::toAngle() const
73{
74 return m_toAngle;
75}
76
77void PieChartMaterial::setAspectRatio(const QVector2D &aspect)
78{
79 m_aspectRatio = aspect;
80}
81
82void PieChartMaterial::setInnerRadius(float radius)
83{
84 m_innerRadius = radius;
85}
86
87void PieChartMaterial::setOuterRadius(float radius)
88{
89 m_outerRadius = radius;
90}
91
92void PieChartMaterial::setBackgroundColor(const QColor &color)
93{
94 m_backgroundColor = color;
95}
96
97void PieChartMaterial::setSegments(const QList<QVector2D> &segments)
98{
99 m_segments = segments;
100}
101
102void PieChartMaterial::setColors(const QList<QVector4D> &colors)
103{
104 m_colors = colors;
105}
106
107void PieChartMaterial::setSmoothEnds(bool smooth)
108{
109 m_smoothEnds = smooth;
110}
111
112void PieChartMaterial::setFromAngle(float angle)
113{
114 m_fromAngle = angle;
115}
116
117void PieChartMaterial::setToAngle(float angle)
118{
119 m_toAngle = angle;
120}
121
122PieChartShader::PieChartShader()
123{
124 setShaders(QStringLiteral("piechart.vert"), QStringLiteral("piechart.frag"));
125}
126
127PieChartShader::~PieChartShader()
128{
129}
130
131bool PieChartShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
132{
133 bool changed = false;
134
135 UniformDataStream uniformData(state);
136
137 if (state.isMatrixDirty()) {
138 uniformData << state.combinedMatrix();
139 changed = true;
140 } else {
141 uniformData.skip<QMatrix4x4>();
142 }
143
144 if (state.isOpacityDirty()) {
145 uniformData << state.opacity();
146 changed = true;
147 } else {
148 uniformData.skip<float>();
149 }
150
151 if (!oldMaterial || newMaterial->compare(other: oldMaterial) != 0) {
152 const auto material = static_cast<PieChartMaterial *>(newMaterial);
153
154 uniformData << material->aspectRatio() << material->innerRadius() << material->outerRadius() << material->backgroundColor() //
155 << int(material->smoothEnds()) << material->fromAngle() << material->toAngle();
156
157 const auto segmentCount = uint(material->segments().size());
158 uniformData << segmentCount;
159
160 uniformData << material->segments();
161 uniformData.skipComponents(count: (MaximumSegmentCount - segmentCount) * 4);
162
163 uniformData << material->colors();
164 uniformData.skipComponents(count: (MaximumSegmentCount - segmentCount) * 4);
165
166 changed = true;
167 }
168
169 return changed;
170}
171

source code of kquickcharts/src/scenegraph/PieChartMaterial.cpp