File indexing completed on 2024-06-09 05:30:57
0001 /* 0002 SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "funnelmodel.h" 0008 0009 FunnelModel::FunnelModel(QObject *parent) 0010 : ForwardingModel(parent) 0011 { 0012 } 0013 0014 FunnelModel::~FunnelModel() 0015 { 0016 } 0017 0018 void FunnelModel::setSourceModel(QAbstractItemModel *model) 0019 { 0020 if (model && m_sourceModel == model) { 0021 return; 0022 } 0023 0024 if (!model) { 0025 reset(); 0026 0027 return; 0028 } 0029 0030 connect(model, SIGNAL(destroyed(QObject *)), this, SLOT(reset())); 0031 0032 if (!m_sourceModel) { 0033 beginResetModel(); 0034 0035 m_sourceModel = model; 0036 0037 connectSignals(); 0038 0039 endResetModel(); 0040 0041 Q_EMIT countChanged(); 0042 0043 Q_EMIT sourceModelChanged(); 0044 Q_EMIT descriptionChanged(); 0045 0046 return; 0047 } 0048 0049 int oldCount = m_sourceModel->rowCount(); 0050 int newCount = model->rowCount(); 0051 0052 auto setNewModel = [this, model]() { 0053 disconnectSignals(); 0054 m_sourceModel = model; 0055 connectSignals(); 0056 }; 0057 0058 if (newCount > oldCount) { 0059 beginInsertRows(QModelIndex(), oldCount, newCount - 1); 0060 setNewModel(); 0061 endInsertRows(); 0062 } else if (newCount < oldCount) { 0063 if (newCount == 0) { 0064 beginResetModel(); 0065 setNewModel(); 0066 endResetModel(); 0067 } else { 0068 beginRemoveRows(QModelIndex(), newCount, oldCount - 1); 0069 setNewModel(); 0070 endRemoveRows(); 0071 } 0072 } else { 0073 setNewModel(); 0074 } 0075 0076 if (newCount > 0) { 0077 Q_EMIT dataChanged(index(0, 0), index(qMin(oldCount, newCount) - 1, 0)); 0078 } 0079 0080 if (oldCount != newCount) { 0081 Q_EMIT countChanged(); 0082 } 0083 0084 Q_EMIT sourceModelChanged(); 0085 Q_EMIT descriptionChanged(); 0086 }