1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
/*
SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "funnelmodel.h"
FunnelModel::FunnelModel(QObject *parent)
: ForwardingModel(parent)
{
}
FunnelModel::~FunnelModel()
{
}
void FunnelModel::setSourceModel(QAbstractItemModel *model)
{
if (model && m_sourceModel == model) {
return;
}
if (!model) {
reset();
return;
}
connect(model, &QObject::destroyed, this, &ForwardingModel::reset);
if (!m_sourceModel) {
beginResetModel();
m_sourceModel = model;
connectSignals();
endResetModel();
Q_EMIT countChanged();
Q_EMIT sourceModelChanged();
Q_EMIT descriptionChanged();
return;
}
int oldCount = m_sourceModel->rowCount();
int newCount = model->rowCount();
auto setNewModel = [this, model]() {
disconnectSignals();
m_sourceModel = model;
connectSignals();
};
if (newCount > oldCount) {
beginInsertRows(QModelIndex(), oldCount, newCount - 1);
setNewModel();
endInsertRows();
} else if (newCount < oldCount) {
if (newCount == 0) {
beginResetModel();
setNewModel();
endResetModel();
} else {
beginRemoveRows(QModelIndex(), newCount, oldCount - 1);
setNewModel();
endRemoveRows();
}
} else {
setNewModel();
}
if (newCount > 0) {
Q_EMIT dataChanged(index(0, 0), index(qMin(oldCount, newCount) - 1, 0));
}
if (oldCount != newCount) {
Q_EMIT countChanged();
}
Q_EMIT sourceModelChanged();
Q_EMIT descriptionChanged();
}
#include "moc_funnelmodel.cpp"
|