Warning, file /libraries/kqtquickcharts/src/linechartbackgroundpainter.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 2014 Sebastian Gottfried <sebastiangottfried@web.de> 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 "linechartbackgroundpainter.h" 0021 0022 #include <QAbstractTableModel> 0023 #include <QPainter> 0024 0025 #include "linechartcore.h" 0026 #include "dimension.h" 0027 0028 LineChartBackgroundPainter::LineChartBackgroundPainter(QQuickItem* parent) : 0029 QQuickPaintedItem(parent), 0030 m_lineChartCore(nullptr) 0031 { 0032 setFlag(QQuickItem::ItemHasContents, true); 0033 0034 connect(this, &QQuickItem::heightChanged, this, &LineChartBackgroundPainter::triggerUpdate); 0035 } 0036 0037 LineChartCore* LineChartBackgroundPainter::lineChartCore() const 0038 { 0039 return m_lineChartCore; 0040 } 0041 0042 void LineChartBackgroundPainter::setLineChartCore(LineChartCore* lineChartCore) 0043 { 0044 if (lineChartCore != m_lineChartCore) 0045 { 0046 if (m_lineChartCore) 0047 { 0048 m_lineChartCore->disconnect(this); 0049 } 0050 0051 m_lineChartCore = lineChartCore; 0052 0053 if (m_lineChartCore) 0054 { 0055 connect(m_lineChartCore, &ChartCore::updated, this, &LineChartBackgroundPainter::triggerUpdate); 0056 } 0057 0058 update(); 0059 emit lineChartCoreChanged(); 0060 } 0061 } 0062 0063 const QList<QPolygonF>& LineChartBackgroundPainter::linePolygons() const 0064 { 0065 return m_linePolygons; 0066 } 0067 0068 void LineChartBackgroundPainter::paint(QPainter* painter) 0069 { 0070 if (m_lineChartCore->model()->rowCount() == 0) 0071 return; 0072 0073 QList<Dimension*> dimensions = m_lineChartCore->dimensionsList(); 0074 const qreal radius = m_lineChartCore->pointRadius(); 0075 const qreal maxY = height(); 0076 0077 for (int i = 0; i < dimensions.length(); i++) 0078 { 0079 0080 QPolygonF line = m_linePolygons.at(i); 0081 0082 line << QPointF(line.last().x(), maxY - radius); 0083 line << QPointF(line.first().x(), maxY - radius); 0084 0085 QColor bgColor = dimensions.at(i)->color(); 0086 bgColor.setAlphaF(0.4); 0087 painter->setBrush(bgColor); 0088 painter->setPen(Qt::NoPen); 0089 0090 painter->drawPolygon(line); 0091 } 0092 } 0093 0094 void LineChartBackgroundPainter::triggerUpdate() 0095 { 0096 if (!m_lineChartCore->model()) 0097 return; 0098 0099 updateLinePolygons(); 0100 updateWidth(); 0101 update(); 0102 } 0103 0104 void LineChartBackgroundPainter::updateWidth() 0105 { 0106 QAbstractTableModel* model = m_lineChartCore->model(); 0107 0108 if (!model) 0109 { 0110 setWidth(0); 0111 return; 0112 } 0113 0114 setWidth(model->rowCount() * m_lineChartCore->pitch()); 0115 } 0116 0117 void LineChartBackgroundPainter::updateLinePolygons() 0118 { 0119 m_linePolygons.clear(); 0120 0121 QList<Dimension*> dimensions = m_lineChartCore->dimensionsList(); 0122 QAbstractTableModel* model = m_lineChartCore->model(); 0123 const qreal pitch = m_lineChartCore->pitch(); 0124 const qreal radius = m_lineChartCore->pointRadius(); 0125 0126 foreach(Dimension* dimension, dimensions) 0127 { 0128 const int column = dimension->dataColumn(); 0129 const qreal minValue = dimension->minimumValue(); 0130 const qreal maxValue = dimension->maximumValue(); 0131 const qreal maxY = height(); 0132 0133 QPolygonF line; 0134 0135 for (int row = 0; row < model->rowCount(); row++) 0136 { 0137 const qreal value = model->data(model->index(row, column)).toReal(); 0138 if (qIsNaN(value)) 0139 continue; 0140 0141 const qreal x = (qreal(row) + 0.5) * pitch; 0142 const qreal y = maxY - ((maxY - 2 * radius) * (value - minValue) / (maxValue - minValue)) - radius; 0143 line << QPointF(x, y); 0144 } 0145 0146 m_linePolygons << line; 0147 } 0148 0149 emit linePolygonsUpdated(); 0150 } 0151 0152