Warning, file /frameworks/kquickcharts/src/scenegraph/PieChartMaterial.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * This file is part of KQuickCharts
0003  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 #include "PieChartMaterial.h"
0009 
0010 constexpr int MaximumSegmentCount = 100;
0011 
0012 PieChartMaterial::PieChartMaterial()
0013 {
0014     setFlag(QSGMaterial::Blending);
0015 }
0016 
0017 PieChartMaterial::~PieChartMaterial()
0018 {
0019 }
0020 
0021 QSGMaterialType *PieChartMaterial::type() const
0022 {
0023     static QSGMaterialType type;
0024     return &type;
0025 }
0026 
0027 QSGMaterialShader *PieChartMaterial::createShader(QSGRendererInterface::RenderMode) const
0028 {
0029     return new PieChartShader();
0030 }
0031 
0032 QVector2D PieChartMaterial::aspectRatio() const
0033 {
0034     return m_aspectRatio;
0035 }
0036 
0037 float PieChartMaterial::innerRadius() const
0038 {
0039     return m_innerRadius;
0040 }
0041 
0042 float PieChartMaterial::outerRadius() const
0043 {
0044     return m_outerRadius;
0045 }
0046 
0047 QColor PieChartMaterial::backgroundColor() const
0048 {
0049     return m_backgroundColor;
0050 }
0051 
0052 QList<QVector2D> PieChartMaterial::segments() const
0053 {
0054     return m_segments;
0055 }
0056 
0057 QList<QVector4D> PieChartMaterial::colors() const
0058 {
0059     return m_colors;
0060 }
0061 
0062 bool PieChartMaterial::smoothEnds() const
0063 {
0064     return m_smoothEnds;
0065 }
0066 
0067 float PieChartMaterial::fromAngle() const
0068 {
0069     return m_fromAngle;
0070 }
0071 
0072 float PieChartMaterial::toAngle() const
0073 {
0074     return m_toAngle;
0075 }
0076 
0077 void PieChartMaterial::setAspectRatio(const QVector2D &aspect)
0078 {
0079     m_aspectRatio = aspect;
0080 }
0081 
0082 void PieChartMaterial::setInnerRadius(float radius)
0083 {
0084     m_innerRadius = radius;
0085 }
0086 
0087 void PieChartMaterial::setOuterRadius(float radius)
0088 {
0089     m_outerRadius = radius;
0090 }
0091 
0092 void PieChartMaterial::setBackgroundColor(const QColor &color)
0093 {
0094     m_backgroundColor = color;
0095 }
0096 
0097 void PieChartMaterial::setSegments(const QList<QVector2D> &segments)
0098 {
0099     m_segments = segments;
0100 }
0101 
0102 void PieChartMaterial::setColors(const QList<QVector4D> &colors)
0103 {
0104     m_colors = colors;
0105 }
0106 
0107 void PieChartMaterial::setSmoothEnds(bool smooth)
0108 {
0109     m_smoothEnds = smooth;
0110 }
0111 
0112 void PieChartMaterial::setFromAngle(float angle)
0113 {
0114     m_fromAngle = angle;
0115 }
0116 
0117 void PieChartMaterial::setToAngle(float angle)
0118 {
0119     m_toAngle = angle;
0120 }
0121 
0122 PieChartShader::PieChartShader()
0123 {
0124     setShaders(QStringLiteral("piechart.vert"), QStringLiteral("piechart.frag"));
0125 }
0126 
0127 PieChartShader::~PieChartShader()
0128 {
0129 }
0130 
0131 bool PieChartShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
0132 {
0133     bool changed = false;
0134 
0135     UniformDataStream uniformData(state);
0136 
0137     if (state.isMatrixDirty()) {
0138         uniformData << state.combinedMatrix();
0139         changed = true;
0140     } else {
0141         uniformData.skip<QMatrix4x4>();
0142     }
0143 
0144     if (state.isOpacityDirty()) {
0145         uniformData << state.opacity();
0146         changed = true;
0147     } else {
0148         uniformData.skip<float>();
0149     }
0150 
0151     if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) {
0152         const auto material = static_cast<PieChartMaterial *>(newMaterial);
0153 
0154         uniformData << material->aspectRatio() << material->innerRadius() << material->outerRadius() << material->backgroundColor() //
0155                     << material->smoothEnds() << material->fromAngle() << material->toAngle();
0156 
0157         const auto segmentCount = uint(material->segments().size());
0158         uniformData << segmentCount;
0159 
0160         uniformData << material->segments();
0161         uniformData.skipComponents((MaximumSegmentCount - segmentCount) * 4);
0162 
0163         uniformData << material->colors();
0164         uniformData.skipComponents((MaximumSegmentCount - segmentCount) * 4);
0165 
0166         changed = true;
0167     }
0168 
0169     return changed;
0170 }