File indexing completed on 2024-05-12 07:52:10

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 "LineChartMaterial.h"
0009 
0010 LineChartMaterial::LineChartMaterial()
0011 {
0012     setFlag(QSGMaterial::Blending);
0013 }
0014 
0015 LineChartMaterial::~LineChartMaterial()
0016 {
0017 }
0018 
0019 QSGMaterialType *LineChartMaterial::type() const
0020 {
0021     static QSGMaterialType type;
0022     return &type;
0023 }
0024 
0025 QSGMaterialShader *LineChartMaterial::createShader(QSGRendererInterface::RenderMode) const
0026 {
0027     return new LineChartShader();
0028 }
0029 
0030 int LineChartMaterial::compare(const QSGMaterial *other) const
0031 {
0032     auto material = static_cast<const LineChartMaterial *>(other);
0033 
0034     /* clang-format off */
0035     if (qFuzzyCompare(material->aspect, aspect)
0036         && qFuzzyCompare(material->lineWidth, lineWidth)
0037         && qFuzzyCompare(material->smoothing, smoothing)) { /* clang-format on */
0038         return 0;
0039     }
0040 
0041     return QSGMaterial::compare(other);
0042 }
0043 
0044 LineChartShader::LineChartShader()
0045 {
0046     setShaders(QStringLiteral("linechart.vert"), QStringLiteral("linechart.frag"));
0047 }
0048 
0049 LineChartShader::~LineChartShader()
0050 {
0051 }
0052 
0053 bool LineChartShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
0054 {
0055     bool changed = false;
0056     UniformDataStream uniformData(state);
0057 
0058     if (state.isMatrixDirty()) {
0059         uniformData << state.combinedMatrix();
0060         changed = true;
0061     } else {
0062         uniformData.skip<QMatrix4x4>();
0063     }
0064 
0065     if (state.isOpacityDirty()) {
0066         uniformData << state.opacity();
0067         changed = true;
0068     } else {
0069         uniformData.skip<float>();
0070     }
0071 
0072     if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) {
0073         const auto material = static_cast<LineChartMaterial *>(newMaterial);
0074         uniformData << material->lineWidth;
0075         uniformData << material->aspect;
0076         uniformData << material->smoothing;
0077         changed = true;
0078     }
0079 
0080     return changed;
0081 }