8#include "ModelSource.h"
10#include <QMetaProperty>
12#include "charts_datasource_logging.h"
14ModelSource::ModelSource(
QObject *parent)
17 connect(
this, &ModelSource::modelChanged,
this, &ModelSource::dataChanged);
18 connect(
this, &ModelSource::columnChanged,
this, &ModelSource::dataChanged);
19 connect(
this, &ModelSource::roleChanged,
this, &ModelSource::dataChanged);
20 connect(
this, &ModelSource::indexColumnsChanged,
this, &ModelSource::dataChanged);
23int ModelSource::role()
const
29 if (m_role < 0 && !m_roleName.isEmpty()) {
30 m_role = m_model->roleNames().key(m_roleName.toLatin1(), -1);
36QString ModelSource::roleName()
const
41int ModelSource::column()
const
51bool ModelSource::indexColumns()
const
53 return m_indexColumns;
56int ModelSource::itemCount()
const
62 return m_indexColumns ? m_model->
columnCount() : m_model->rowCount();
65QVariant ModelSource::item(
int index)
const
75 if (m_roleName.isEmpty()) {
79 m_role = m_model->roleNames().key(m_roleName.toLatin1(), -1);
81 qCWarning(DATASOURCE) <<
"ModelSource: Invalid role " << m_role << m_roleName;
86 if (!m_indexColumns && (m_column < 0 || m_column > m_model->columnCount())) {
87 qCDebug(DATASOURCE) <<
"ModelSource: Invalid column" << m_column;
91 auto modelIndex = m_indexColumns ? m_model->index(0, index) : m_model->index(index, m_column);
92 if (modelIndex.isValid()) {
93 return m_model->data(modelIndex, m_role);
101 if (!m_model || itemCount() <= 0) {
105 if (m_minimum.isValid()) {
109 auto minProperty = m_model->property(
"minimum");
110 auto maxProperty = m_model->property(
"maximum");
111 if (minProperty.isValid() && minProperty != maxProperty) {
115 QVariant result = std::numeric_limits<float>::max();
116 for (
int i = 0; i < itemCount(); ++i) {
117 result = std::min(result, item(i), variantCompare);
122QVariant ModelSource::maximum()
const
124 if (!m_model || itemCount() <= 0) {
128 if (m_maximum.isValid()) {
132 auto minProperty = m_model->property(
"minimum");
133 auto maxProperty = m_model->property(
"maximum");
134 if (maxProperty.isValid() && maxProperty != minProperty) {
138 QVariant result = std::numeric_limits<float>::min();
139 for (
int i = 0; i < itemCount(); ++i) {
140 result = std::max(result, item(i), variantCompare);
145void ModelSource::setRole(
int role)
147 if (role == m_role) {
159void ModelSource::setRoleName(
const QString &name)
161 if (name == m_roleName) {
167 m_role = m_model->roleNames().key(m_roleName.toLatin1(), -1);
173void ModelSource::setColumn(
int column)
175 if (column == m_column) {
183void ModelSource::setIndexColumns(
bool index)
185 if (index == m_indexColumns) {
189 m_indexColumns = index;
190 Q_EMIT indexColumnsChanged();
195 if (m_model == model) {
200 m_model->disconnect(
this);
201 m_minimum = QVariant{};
202 m_maximum = QVariant{};
215 m_minimum = QVariant{};
216 m_maximum = QVariant{};
220 auto minimumIndex = m_model->metaObject()->indexOfProperty(
"minimum");
221 if (minimumIndex != -1) {
222 auto minimum = m_model->metaObject()->property(minimumIndex);
223 if (minimum.hasNotifySignal()) {
225 connect(m_model, minimum.notifySignal(),
this, slot);
226 m_minimum = minimum.read(m_model);
230 auto maximumIndex = m_model->metaObject()->indexOfProperty(
"maximum");
231 if (maximumIndex != -1) {
232 auto maximum = m_model->metaObject()->property(maximumIndex);
233 if (maximum.hasNotifySignal()) {
235 connect(m_model, maximum.notifySignal(),
this, slot);
236 m_maximum = maximum.read(m_model);
244void ModelSource::onMinimumChanged()
246 auto newMinimum = m_model->property(
"minimum");
247 if (newMinimum.isValid() && newMinimum != m_minimum) {
248 m_minimum = newMinimum;
252void ModelSource::onMaximumChanged()
254 auto newMaximum = m_model->property(
"maximum");
255 if (newMaximum.isValid() && newMaximum != m_maximum) {
256 m_maximum = newMaximum;
260#include "moc_ModelSource.cpp"
Abstract base class for data sources.
QString name(StandardAction id)
virtual int columnCount(const QModelIndex &parent) const const=0
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void rowsInserted(const QModelIndex &parent, int first, int last)
void rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
void rowsRemoved(const QModelIndex &parent, int first, int last)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
QString fromLatin1(QByteArrayView str)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)