Warning, file /libraries/kqtquickcharts/src/barchartsegment.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 "barchartsegment.h"
0021 
0022 #include <QAbstractTableModel>
0023 
0024 #include "dimension.h"
0025 #include "barchartcore.h"
0026 
0027 BarChartSegment::BarChartSegment(QQuickItem* parent) :
0028     QQuickItem(parent),
0029     m_barChartCore(nullptr),
0030     m_dimension(-1),
0031     m_row(-1)
0032 {
0033     connect(this, &QQuickItem::heightChanged, this, &BarChartSegment::triggerUpdate);
0034 }
0035 
0036 BarChartCore* BarChartSegment::barChartCore() const
0037 {
0038     return m_barChartCore;
0039 }
0040 
0041 void BarChartSegment::setBarChartCore(BarChartCore* barChartCore)
0042 {
0043     if (barChartCore != m_barChartCore)
0044     {
0045         if (m_barChartCore)
0046         {
0047             m_barChartCore->disconnect(this);
0048         }
0049 
0050         m_barChartCore = barChartCore;
0051 
0052         if (m_barChartCore)
0053         {
0054             connect(m_barChartCore, &ChartCore::updated, this, &BarChartSegment::triggerUpdate);
0055         }
0056 
0057         triggerUpdate();
0058         emit barChartCoreChanged();
0059     }
0060 }
0061 
0062 int BarChartSegment::dimension() const
0063 {
0064     return m_dimension;
0065 }
0066 
0067 void BarChartSegment::setDimension(int dimension)
0068 {
0069     if (dimension != m_dimension)
0070     {
0071         m_dimension = dimension;
0072         triggerUpdate();
0073         emit dimensionChanged();
0074     }
0075 }
0076 
0077 int BarChartSegment::row() const
0078 {
0079     return m_row;
0080 }
0081 
0082 void BarChartSegment::setRow(int row)
0083 {
0084     if (row != m_row)
0085     {
0086         m_row = row;
0087         triggerUpdate();
0088         emit rowChanged();
0089     }
0090 }
0091 
0092 qreal BarChartSegment::barHeight() const
0093 {
0094     if (!valid())
0095         return 0.0;
0096 
0097     QAbstractTableModel* model = m_barChartCore->model();
0098     Dimension* dimension = m_barChartCore->dimensionsList().at(m_dimension);
0099     const qreal minValue = dimension->minimumValue();
0100     const qreal maxValue = dimension->maximumValue();
0101     const int column = dimension->dataColumn();
0102     const qreal value = model->data(model->index(m_row, column)).toReal();
0103 
0104     return height() * (value - minValue) / (maxValue - minValue);
0105 }
0106 
0107 QString BarChartSegment::text() const
0108 {
0109     if (!m_barChartCore)
0110         return QString();
0111 
0112     const int role = m_barChartCore->textRole();
0113 
0114     if (role == -1)
0115         return QString();
0116 
0117     QAbstractTableModel* model = m_barChartCore->model();
0118     Dimension* dimension = m_barChartCore->dimensionsList().at(m_dimension);
0119     const int column = dimension->dataColumn();
0120 
0121     return model->data(model->index(m_row, column), role).toString();
0122 }
0123 
0124 void BarChartSegment::triggerUpdate()
0125 {
0126     if (!valid())
0127         return;
0128     update();
0129     emit barHeightChanged();
0130 }
0131 
0132 bool BarChartSegment::valid() const
0133 {
0134     if (!m_barChartCore)
0135         return false;
0136     if (m_dimension == -1)
0137         return false;
0138     if (m_row == -1)
0139         return false;
0140     if (m_row >= m_barChartCore->model()->rowCount())
0141         return false;
0142     return true;
0143 }