Warning, file /libraries/kqtquickcharts/src/linechartpoint.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 "linechartpoint.h"
0021 
0022 #include <QAbstractTableModel>
0023 #include <QPainter>
0024 
0025 #include "linechartcore.h"
0026 #include "dimension.h"
0027 #include "linechartbackgroundpainter.h"
0028 
0029 LineChartPoint::LineChartPoint(QQuickItem* parent) :
0030     QQuickPaintedItem(parent),
0031     m_lineChartCore(nullptr),
0032     m_backgroundPainter(nullptr),
0033     m_dimension(-1),
0034     m_row(-1)
0035 {
0036     setFlag(QQuickItem::ItemHasContents, true);
0037 }
0038 
0039 LineChartCore* LineChartPoint::lineChartCore() const
0040 {
0041     return m_lineChartCore;
0042 }
0043 
0044 void LineChartPoint::setLineChartCore(LineChartCore* lineChartCore)
0045 {
0046     if (lineChartCore != m_lineChartCore)
0047     {
0048         if (m_lineChartCore)
0049         {
0050             m_lineChartCore->disconnect(this);
0051         }
0052 
0053         m_lineChartCore = lineChartCore;
0054 
0055         if (m_lineChartCore)
0056         {
0057             connect(m_lineChartCore, &ChartCore::updated, this, &LineChartPoint::triggerUpdate);
0058         }
0059 
0060         triggerUpdate();
0061         emit lineChartCoreChanged();
0062     }
0063 }
0064 
0065 LineChartBackgroundPainter* LineChartPoint::backgroundPainter() const
0066 {
0067     return m_backgroundPainter;
0068 }
0069 
0070 void LineChartPoint::setBackgroundPainter(LineChartBackgroundPainter* backgroundPainter)
0071 {
0072     if (backgroundPainter != m_backgroundPainter)
0073     {
0074         if (m_backgroundPainter)
0075         {
0076             m_backgroundPainter->disconnect(this);
0077         }
0078 
0079         m_backgroundPainter = backgroundPainter;
0080 
0081         if (m_backgroundPainter)
0082         {
0083             connect(m_backgroundPainter, &LineChartBackgroundPainter::linePolygonsUpdated, this, &LineChartPoint::triggerUpdate);
0084         }
0085 
0086         triggerUpdate();
0087         emit backgroundPainterChanged();
0088     }
0089 }
0090 
0091 int LineChartPoint::dimension() const
0092 {
0093     return m_dimension;
0094 }
0095 
0096 void LineChartPoint::setDimension(int dimension)
0097 {
0098     if (dimension != m_dimension)
0099     {
0100         m_dimension = dimension;
0101         triggerUpdate();
0102         emit dimensionChanged();
0103     }
0104 }
0105 
0106 int LineChartPoint::row() const
0107 {
0108     return m_row;
0109 }
0110 
0111 void LineChartPoint::setRow(int row)
0112 {
0113     if (row != m_row)
0114     {
0115         m_row = row;
0116         triggerUpdate();
0117         emit rowChanged();
0118     }
0119 }
0120 
0121 QString LineChartPoint::text() const
0122 {
0123     if (!m_lineChartCore)
0124         return QString();
0125 
0126     const int role = m_lineChartCore->textRole();
0127 
0128     if (role == -1)
0129         return QString();
0130 
0131     QAbstractTableModel* model = m_lineChartCore->model();
0132     Dimension* dimension = m_lineChartCore->dimensionsList().at(m_dimension);
0133     const int column = dimension->dataColumn();
0134 
0135     return model->data(model->index(m_row, column), role).toString();
0136 }
0137 
0138 void LineChartPoint::paint(QPainter* painter)
0139 {
0140     if (!valid())
0141         return;
0142 
0143     Dimension* dimension = m_lineChartCore->dimensionsList().at(m_dimension);
0144 
0145     painter->setRenderHints(QPainter::Antialiasing, true);
0146     painter->setBrush(QBrush(dimension->color()));
0147     painter->setPen(Qt::NoPen);
0148     const qreal radius = m_lineChartCore->pointRadius();
0149     painter->drawEllipse(QPointF(radius, radius), radius, radius);
0150 }
0151 
0152 void LineChartPoint::triggerUpdate()
0153 {
0154     if (!valid())
0155         return;
0156     updateGeometry();
0157     update();
0158 }
0159 
0160 void LineChartPoint::updateGeometry()
0161 {
0162     const qreal radius = m_lineChartCore->pointRadius();
0163     setWidth(2 * radius);
0164     setHeight(2 * radius);
0165     QPointF center = m_backgroundPainter->linePolygons().at(m_dimension).at(m_row);
0166 
0167     setX(center.x() - radius);
0168     setY(center.y() - radius);
0169 }
0170 
0171 bool LineChartPoint::valid() const
0172 {
0173     if (!m_lineChartCore || !m_backgroundPainter || m_row == -1 || m_dimension == -1)
0174         return false;
0175     if (m_dimension >= m_backgroundPainter->linePolygons().count())
0176         return false;
0177     if (m_row >= m_backgroundPainter->linePolygons().at(m_dimension).count())
0178         return false;
0179     return true;
0180 }