File indexing completed on 2024-05-05 16:16:43

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 #ifndef PIECHARTMATERIAL_H
0009 #define PIECHARTMATERIAL_H
0010 
0011 #include <QColor>
0012 #include <QSGMaterial>
0013 #include <QSGMaterialShader>
0014 
0015 #include "SDFShader.h"
0016 
0017 class PieChartMaterial : public QSGMaterial
0018 {
0019 public:
0020     PieChartMaterial();
0021     ~PieChartMaterial();
0022 
0023     QSGMaterialType *type() const override;
0024 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0025     QSGMaterialShader *createShader() const override;
0026 #else
0027     QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const override;
0028 #endif
0029 
0030     QVector2D aspectRatio() const;
0031     float innerRadius() const;
0032     float outerRadius() const;
0033     QColor backgroundColor() const;
0034     bool smoothEnds() const;
0035     float fromAngle() const;
0036     float toAngle() const;
0037 
0038     QVector<QVector2D> segments() const;
0039     QVector<QVector4D> colors() const;
0040 
0041     void setAspectRatio(const QVector2D &aspect);
0042     void setInnerRadius(float radius);
0043     void setOuterRadius(float radius);
0044     void setBackgroundColor(const QColor &color);
0045     void setSmoothEnds(bool smooth);
0046     void setFromAngle(float angle);
0047     void setToAngle(float angle);
0048 
0049     void setSegments(const QVector<QVector2D> &triangles);
0050     void setColors(const QVector<QVector4D> &colors);
0051 
0052 private:
0053     QVector2D m_aspectRatio;
0054     float m_innerRadius = 0.0f;
0055     float m_outerRadius = 0.0f;
0056     QColor m_backgroundColor;
0057     bool m_smoothEnds = false;
0058     float m_fromAngle = 0.0;
0059     float m_toAngle = 6.28318; // 2 * pi
0060 
0061     QVector<QVector2D> m_segments;
0062     QVector<QVector4D> m_colors;
0063 };
0064 
0065 class PieChartShader : public SDFShader
0066 {
0067 public:
0068     PieChartShader();
0069     ~PieChartShader();
0070 
0071 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0072     char const *const *attributeNames() const override;
0073 
0074     void initialize() override;
0075     void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
0076 
0077 private:
0078     int m_matrixLocation = 0;
0079     int m_opacityLocation = 0;
0080     int m_innerRadiusLocation = 0;
0081     int m_outerRadiusLocation = 0;
0082     int m_aspectLocation = 0;
0083     int m_backgroundColorLocation = 0;
0084     int m_colorsLocation = 0;
0085     int m_segmentsLocation = 0;
0086     int m_segmentCountLocation = 0;
0087     int m_smoothEndsLocation = 0;
0088     int m_fromAngleLocation = 0;
0089     int m_toAngleLocation = 0;
0090 #else
0091     bool updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
0092 #endif
0093 };
0094 
0095 #endif // PIECHARTMATERIAL_H