| 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 | |
| 10 | constexpr int MaximumSegmentCount = 100; |
| 11 | |
| 12 | PieChartMaterial::PieChartMaterial() |
| 13 | { |
| 14 | setFlag(flags: QSGMaterial::Blending); |
| 15 | } |
| 16 | |
| 17 | PieChartMaterial::~PieChartMaterial() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | QSGMaterialType *PieChartMaterial::type() const |
| 22 | { |
| 23 | static QSGMaterialType type; |
| 24 | return &type; |
| 25 | } |
| 26 | |
| 27 | QSGMaterialShader *PieChartMaterial::createShader(QSGRendererInterface::RenderMode) const |
| 28 | { |
| 29 | return new PieChartShader(); |
| 30 | } |
| 31 | |
| 32 | QVector2D PieChartMaterial::aspectRatio() const |
| 33 | { |
| 34 | return m_aspectRatio; |
| 35 | } |
| 36 | |
| 37 | float PieChartMaterial::innerRadius() const |
| 38 | { |
| 39 | return m_innerRadius; |
| 40 | } |
| 41 | |
| 42 | float PieChartMaterial::outerRadius() const |
| 43 | { |
| 44 | return m_outerRadius; |
| 45 | } |
| 46 | |
| 47 | QColor PieChartMaterial::backgroundColor() const |
| 48 | { |
| 49 | return m_backgroundColor; |
| 50 | } |
| 51 | |
| 52 | QList<QVector2D> PieChartMaterial::segments() const |
| 53 | { |
| 54 | return m_segments; |
| 55 | } |
| 56 | |
| 57 | QList<QVector4D> PieChartMaterial::colors() const |
| 58 | { |
| 59 | return m_colors; |
| 60 | } |
| 61 | |
| 62 | bool PieChartMaterial::smoothEnds() const |
| 63 | { |
| 64 | return m_smoothEnds; |
| 65 | } |
| 66 | |
| 67 | float PieChartMaterial::fromAngle() const |
| 68 | { |
| 69 | return m_fromAngle; |
| 70 | } |
| 71 | |
| 72 | float PieChartMaterial::toAngle() const |
| 73 | { |
| 74 | return m_toAngle; |
| 75 | } |
| 76 | |
| 77 | void PieChartMaterial::setAspectRatio(const QVector2D &aspect) |
| 78 | { |
| 79 | m_aspectRatio = aspect; |
| 80 | } |
| 81 | |
| 82 | void PieChartMaterial::setInnerRadius(float radius) |
| 83 | { |
| 84 | m_innerRadius = radius; |
| 85 | } |
| 86 | |
| 87 | void PieChartMaterial::setOuterRadius(float radius) |
| 88 | { |
| 89 | m_outerRadius = radius; |
| 90 | } |
| 91 | |
| 92 | void PieChartMaterial::setBackgroundColor(const QColor &color) |
| 93 | { |
| 94 | m_backgroundColor = color; |
| 95 | } |
| 96 | |
| 97 | void PieChartMaterial::setSegments(const QList<QVector2D> &segments) |
| 98 | { |
| 99 | m_segments = segments; |
| 100 | } |
| 101 | |
| 102 | void PieChartMaterial::setColors(const QList<QVector4D> &colors) |
| 103 | { |
| 104 | m_colors = colors; |
| 105 | } |
| 106 | |
| 107 | void PieChartMaterial::setSmoothEnds(bool smooth) |
| 108 | { |
| 109 | m_smoothEnds = smooth; |
| 110 | } |
| 111 | |
| 112 | void PieChartMaterial::setFromAngle(float angle) |
| 113 | { |
| 114 | m_fromAngle = angle; |
| 115 | } |
| 116 | |
| 117 | void PieChartMaterial::setToAngle(float angle) |
| 118 | { |
| 119 | m_toAngle = angle; |
| 120 | } |
| 121 | |
| 122 | PieChartShader::PieChartShader() |
| 123 | { |
| 124 | setShaders(QStringLiteral("piechart.vert"), QStringLiteral( "piechart.frag")); |
| 125 | } |
| 126 | |
| 127 | PieChartShader::~PieChartShader() |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | bool 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 |
