Warning, file /libraries/kqtquickcharts/src/xychartbackgroundpainter.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * Copyright 2015 Jesper Hellesø Hansen <jesperhh@gmail.com> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) version 3, or any 0008 * later version accepted by the membership of KDE e.V. (or its 0009 * successor approved by the membership of KDE e.V.), which shall 0010 * act as a proxy defined in Section 6 of version 3 of the license. 0011 * 0012 * This library is distributed in the hope that it will be useful, 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 * Lesser General Public License for more details. 0016 * 0017 * You should have received a copy of the GNU Lesser General Public 0018 */ 0019 0020 #include "xychartbackgroundpainter.h" 0021 0022 #include <QAbstractTableModel> 0023 #include <QPainter> 0024 0025 #include "xychartcore.h" 0026 #include "dimension.h" 0027 0028 XYChartBackgroundPainter::XYChartBackgroundPainter(QQuickItem* parent) : 0029 QQuickPaintedItem(parent), 0030 m_xyChartCore(nullptr) 0031 { 0032 setFlag(QQuickItem::ItemHasContents, true); 0033 0034 connect(this, &QQuickItem::widthChanged, this, &XYChartBackgroundPainter::triggerUpdate); 0035 connect(this, &QQuickItem::heightChanged, this, &XYChartBackgroundPainter::triggerUpdate); 0036 } 0037 0038 XYChartCore* XYChartBackgroundPainter::xyChartCore() const 0039 { 0040 return m_xyChartCore; 0041 } 0042 0043 void XYChartBackgroundPainter::setXYChartCore(XYChartCore* xyChartCore) 0044 { 0045 if (xyChartCore != m_xyChartCore) 0046 { 0047 if (m_xyChartCore) 0048 { 0049 m_xyChartCore->disconnect(this); 0050 } 0051 0052 m_xyChartCore = xyChartCore; 0053 0054 if (m_xyChartCore) 0055 { 0056 connect(m_xyChartCore, &ChartCore::updated, this, &XYChartBackgroundPainter::triggerUpdate); 0057 } 0058 0059 update(); 0060 emit xyChartCoreChanged(); 0061 } 0062 } 0063 0064 const QList<QPolygonF>& XYChartBackgroundPainter::linePolygons() const 0065 { 0066 return m_linePolygons; 0067 } 0068 0069 void XYChartBackgroundPainter::paint(QPainter* painter) 0070 { 0071 Q_UNUSED(painter); 0072 } 0073 0074 void XYChartBackgroundPainter::triggerUpdate() 0075 { 0076 if (!m_xyChartCore->model()) 0077 return; 0078 0079 updateLinePolygons(); 0080 update(); 0081 } 0082 0083 void XYChartBackgroundPainter::updateLinePolygons() 0084 { 0085 m_linePolygons.clear(); 0086 0087 Dimension* xAxis = m_xyChartCore->xAxis(); 0088 QList<Dimension*> dimensions = m_xyChartCore->dimensionsList(); 0089 QAbstractTableModel* model = m_xyChartCore->model(); 0090 0091 const int xAxisColumn = xAxis->dataColumn(); 0092 0093 foreach(Dimension* dimension, dimensions) 0094 { 0095 const int column = dimension->dataColumn(); 0096 QPolygonF line; 0097 for (int row = 0; row < model->rowCount(); row++) 0098 { 0099 const qreal key = model->data(model->index(row, xAxisColumn)).toReal(); 0100 const qreal value = model->data(model->index(row, column)).toReal(); 0101 // Skip "NULL" values 0102 if (qIsNaN(value)) 0103 continue; 0104 0105 line << m_xyChartCore->translatePoint(QPointF(key, value)); 0106 } 0107 0108 m_linePolygons << line; 0109 } 0110 0111 emit linePolygonsUpdated(); 0112 }