Warning, file /libraries/kqtquickcharts/src/chartcore.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 "chartcore.h" 0021 0022 #include <QAbstractTableModel> 0023 #include <QPainter> 0024 0025 ChartCore::ChartCore(QQuickItem *parent) : 0026 QQuickPaintedItem(parent), 0027 m_model(nullptr), 0028 m_pitch(50.0), 0029 m_textRole(-1) 0030 { 0031 setFlag(QQuickItem::ItemHasContents, true); 0032 } 0033 0034 QAbstractTableModel* ChartCore::model() const 0035 { 0036 return m_model; 0037 } 0038 0039 void ChartCore::setModel(QAbstractTableModel* model) 0040 { 0041 if (model != m_model) 0042 { 0043 if (m_model) 0044 { 0045 m_model->disconnect(this); 0046 } 0047 0048 m_model = model; 0049 0050 if (m_model) 0051 { 0052 connect(m_model, &QAbstractItemModel::modelReset, this, &ChartCore::triggerUpdate); 0053 connect(m_model, &QAbstractItemModel::rowsInserted, this, &ChartCore::triggerUpdate); 0054 connect(m_model, &QAbstractItemModel::rowsRemoved, this, &ChartCore::triggerUpdate); 0055 connect(m_model, &QAbstractItemModel::rowsMoved, this, &ChartCore::triggerUpdate); 0056 connect(m_model, &QAbstractItemModel::layoutChanged, this, &ChartCore::triggerUpdate); 0057 connect(m_model, &QAbstractItemModel::dataChanged, this, &ChartCore::triggerUpdate); 0058 } 0059 0060 triggerUpdate(); 0061 emit modelChanged(); 0062 } 0063 } 0064 0065 QQmlListProperty<Dimension> ChartCore::dimensions() { 0066 return QQmlListProperty<Dimension>(this, nullptr, &ChartCore::appendDimension, &ChartCore::countDimensions, &ChartCore::dimensionAt, &ChartCore::clearDimensions); 0067 } 0068 0069 QList<Dimension*> ChartCore::dimensionsList() const 0070 { 0071 return m_dimensions; 0072 } 0073 0074 qreal ChartCore::pitch() const 0075 { 0076 return m_pitch; 0077 } 0078 0079 void ChartCore::setPitch(qreal pitch) 0080 { 0081 if (pitch != m_pitch) 0082 { 0083 m_pitch = pitch; 0084 triggerUpdate(); 0085 emit pitchChanged(); 0086 } 0087 } 0088 0089 int ChartCore::textRole() const 0090 { 0091 return m_textRole; 0092 } 0093 0094 void ChartCore::setTextRole(int textRole) 0095 { 0096 if (textRole != m_textRole) 0097 { 0098 m_textRole = textRole; 0099 0100 triggerUpdate(); 0101 0102 emit textRoleChanged(); 0103 } 0104 } 0105 0106 void ChartCore::triggerUpdate() 0107 { 0108 emit updated(); 0109 update(); 0110 } 0111 0112 void ChartCore::paint(QPainter*) 0113 { 0114 } 0115 0116 void ChartCore::paintAxisAndLines(QPainter* painter, qreal offset) 0117 { 0118 const int minY = qRound(offset); 0119 const int maxY = height() - offset; 0120 const int distance = (maxY - minY) / 4; 0121 const qreal x1 = 0.0; 0122 const qreal x2 = x1 + width(); 0123 int y = minY; 0124 0125 painter->setBrush(QBrush("#808080")); 0126 painter->setPen(Qt::NoPen); 0127 0128 for (int i = 0; i < 4; i++, y += distance) 0129 { 0130 painter->drawRect(QRectF(QPointF(x1, y), QPointF(x2, y + 1))); 0131 } 0132 0133 painter->setBrush(QBrush("#000000")); 0134 painter->drawRect(QRectF(QPointF(x1, maxY), QPointF(x2, maxY + 1))); 0135 } 0136 0137 void ChartCore::appendDimension(QQmlListProperty<Dimension>* list, Dimension *dimension) { 0138 ChartCore* chartCore = qobject_cast<ChartCore*>(list->object); 0139 if (chartCore) { 0140 dimension->setParent(chartCore); 0141 chartCore->m_dimensions.append(dimension); 0142 connect(dimension, &Dimension::updated, chartCore, &ChartCore::triggerUpdate); 0143 chartCore->triggerUpdate(); 0144 } 0145 } 0146 0147 int ChartCore::countDimensions(QQmlListProperty<Dimension>* list) { 0148 ChartCore* chartCore = qobject_cast<ChartCore*>(list->object); 0149 if (chartCore) { 0150 return chartCore->m_dimensions.count(); 0151 } 0152 return -1; 0153 } 0154 0155 Dimension* ChartCore::dimensionAt(QQmlListProperty<Dimension>* list, int index) { 0156 ChartCore* chartCore = qobject_cast<ChartCore*>(list->object); 0157 if (chartCore) { 0158 return chartCore->m_dimensions.at(index); 0159 } 0160 return nullptr; 0161 } 0162 0163 void ChartCore::clearDimensions(QQmlListProperty<Dimension>* list) { 0164 ChartCore* chartCore = qobject_cast<ChartCore*>(list->object); 0165 if (chartCore) { 0166 foreach (Dimension* dimension, chartCore->m_dimensions) 0167 { 0168 dimension->disconnect(chartCore); 0169 } 0170 chartCore->m_dimensions.clear(); 0171 chartCore->triggerUpdate(); 0172 } 0173 }