File indexing completed on 2024-05-19 05:38:22
0001 /* 0002 * SPDX-FileCopyrightText: 2023 Ismael Asensio <isma.af@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "eventsproxymodel.h" 0008 0009 EventsProxyModel::EventsProxyModel(QObject *parent) 0010 : QAbstractProxyModel(parent) 0011 { 0012 connect(this, &QAbstractProxyModel::sourceModelChanged, this, &EventsProxyModel::updateDataConnection); 0013 } 0014 0015 void EventsProxyModel::updateDataConnection() 0016 { 0017 connect(sourceModel(), &QAbstractItemModel::dataChanged, this, [this](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles) { 0018 if (topLeft.parent() == m_rootIndex && bottomRight.parent() == m_rootIndex) { 0019 Q_EMIT dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight), roles); 0020 } 0021 }); 0022 connect(sourceModel(), &QAbstractItemModel::modelAboutToBeReset, this, &EventsProxyModel::beginResetModel); 0023 connect(sourceModel(), &QAbstractItemModel::modelReset, this, &EventsProxyModel::endResetModel); 0024 } 0025 0026 int EventsProxyModel::rowCount(const QModelIndex &index) const 0027 { 0028 return sourceModel()->rowCount(mapToSource(index)); 0029 } 0030 0031 int EventsProxyModel::columnCount(const QModelIndex &index) const 0032 { 0033 return sourceModel()->columnCount(mapToSource(index)); 0034 } 0035 0036 QModelIndex EventsProxyModel::index(int row, int column, const QModelIndex &parent) const 0037 { 0038 return createIndex(row, column, parent.internalId()); 0039 } 0040 0041 QModelIndex EventsProxyModel::parent(const QModelIndex &index) const 0042 { 0043 Q_UNUSED(index); 0044 return QModelIndex(); 0045 } 0046 0047 QModelIndex EventsProxyModel::mapFromSource(const QModelIndex &sourceIndex) const 0048 { 0049 if (!sourceIndex.isValid() || !m_rootIndex.isValid() || sourceIndex.parent() != m_rootIndex) { 0050 return QModelIndex(); 0051 } 0052 0053 return index(sourceIndex.row(), sourceIndex.column(), QModelIndex()); 0054 } 0055 0056 QModelIndex EventsProxyModel::mapToSource(const QModelIndex &proxyIndex) const 0057 { 0058 if (!m_rootIndex.isValid()) { 0059 return QModelIndex(); 0060 } 0061 if (!proxyIndex.isValid()) { // root index, we want to return the parent's leaf 0062 return m_rootIndex; 0063 } 0064 return sourceModel()->index(proxyIndex.row(), proxyIndex.column(), m_rootIndex); 0065 } 0066 0067 QModelIndex EventsProxyModel::rootIndex() const 0068 { 0069 return m_rootIndex; 0070 } 0071 0072 void EventsProxyModel::setRootIndex(const QModelIndex &index) 0073 { 0074 if (m_rootIndex == index) { 0075 return; 0076 } 0077 beginResetModel(); 0078 m_rootIndex = index; 0079 Q_EMIT rootIndexChanged(); 0080 endResetModel(); 0081 }