File indexing completed on 2024-05-12 05:36:50
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> 0003 * SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "PageSortModel.h" 0009 0010 PageSortModel::PageSortModel(QObject *parent) 0011 : QAbstractProxyModel(parent) 0012 { 0013 } 0014 0015 QHash<int, QByteArray> PageSortModel::roleNames() const 0016 { 0017 if (!sourceModel()) { 0018 return {}; 0019 } 0020 auto sourceNames = sourceModel()->roleNames(); 0021 sourceNames.insert(ShouldRemoveFilesRole, "shouldRemoveFiles"); 0022 return sourceNames; 0023 } 0024 QVariant PageSortModel::data(const QModelIndex &index, int role) const 0025 { 0026 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { 0027 return QVariant(); 0028 } 0029 0030 switch (role) { 0031 case PagesModel::HiddenRole: 0032 return m_hiddenProxy[mapToSource(index).row()]; 0033 case ShouldRemoveFilesRole: 0034 return m_removeFiles[mapToSource(index).row()]; 0035 } 0036 0037 return QAbstractProxyModel::data(index, role); 0038 } 0039 0040 bool PageSortModel::setData(const QModelIndex &index, const QVariant &value, int role) 0041 { 0042 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { 0043 return false; 0044 } 0045 0046 switch (role) { 0047 case PagesModel::HiddenRole: 0048 m_hiddenProxy[mapToSource(index).row()] = value.toBool(); 0049 break; 0050 case ShouldRemoveFilesRole: 0051 m_removeFiles[mapToSource(index).row()] = value.toBool(); 0052 break; 0053 default: 0054 return false; 0055 } 0056 0057 Q_EMIT dataChanged(index, index, {role}); 0058 return true; 0059 } 0060 0061 void PageSortModel::setSourceModel(QAbstractItemModel *newSourceModel) 0062 { 0063 beginResetModel(); 0064 m_rowMapping.clear(); 0065 m_hiddenProxy.clear(); 0066 m_removeFiles.clear(); 0067 if (newSourceModel) { 0068 for (int i = 0; i < newSourceModel->rowCount(); ++i) { 0069 m_rowMapping.append(i); 0070 m_hiddenProxy.append(newSourceModel->index(i, 0).data(PagesModel::HiddenRole).toBool()); 0071 m_removeFiles.append(false); 0072 } 0073 } 0074 QAbstractProxyModel::setSourceModel(newSourceModel); 0075 endResetModel(); 0076 } 0077 0078 int PageSortModel::rowCount(const QModelIndex &parent) const 0079 { 0080 if (parent.isValid()) { 0081 return 0; 0082 } 0083 return m_rowMapping.size(); 0084 } 0085 0086 int PageSortModel::columnCount(const QModelIndex &parent) const 0087 { 0088 if (parent.isValid()) { 0089 return 0; 0090 } 0091 return 1; 0092 } 0093 0094 QModelIndex PageSortModel::index(int row, int column, const QModelIndex &parent) const 0095 { 0096 if (parent.isValid()) { 0097 return QModelIndex(); 0098 } else if (m_rowMapping.size() <= row || column != 0) { 0099 return QModelIndex(); 0100 } 0101 return createIndex(row, column); 0102 } 0103 0104 QModelIndex PageSortModel::parent(const QModelIndex &child) const 0105 { 0106 Q_UNUSED(child) 0107 return QModelIndex(); 0108 } 0109 0110 QModelIndex PageSortModel::mapFromSource(const QModelIndex &sourceIndex) const 0111 { 0112 if (!checkIndex(sourceIndex, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { 0113 return QModelIndex(); 0114 } 0115 const int row = m_rowMapping.indexOf(sourceIndex.row()); 0116 return row != -1 ? createIndex(row, sourceIndex.column()) : QModelIndex(); 0117 } 0118 0119 QModelIndex PageSortModel::mapToSource(const QModelIndex &proxyIndex) const 0120 { 0121 if (!checkIndex(proxyIndex, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { 0122 return QModelIndex(); 0123 } 0124 return sourceModel()->index(m_rowMapping.at(proxyIndex.row()), proxyIndex.column()); 0125 } 0126 0127 void PageSortModel::move(int fromRow, int toRow) 0128 { 0129 beginMoveRows(QModelIndex(), fromRow, fromRow, QModelIndex(), fromRow < toRow ? toRow + 1 : toRow); 0130 m_rowMapping.move(fromRow, toRow); 0131 endMoveRows(); 0132 } 0133 0134 void PageSortModel::applyChangesToSourceModel() const 0135 { 0136 auto *pagesModel = static_cast<PagesModel *>(sourceModel()); 0137 QStringList newOrder, hiddenPages, toRemove; 0138 for (int i = 0; i < m_rowMapping.size(); ++i) { 0139 const QModelIndex sourceIndex = pagesModel->index(m_rowMapping[i]); 0140 const QString name = sourceIndex.data(PagesModel::FileNameRole).toString(); 0141 newOrder.append(name); 0142 if (m_hiddenProxy[m_rowMapping[i]]) { 0143 hiddenPages.append(name); 0144 } 0145 if (m_removeFiles[m_rowMapping[i]]) { 0146 toRemove.append(name); 0147 } 0148 } 0149 pagesModel->setPageOrder(newOrder); 0150 pagesModel->setHiddenPages(hiddenPages); 0151 for (const auto &name : toRemove) { 0152 pagesModel->removeLocalPageFiles(name); 0153 } 0154 } 0155 0156 #include "moc_PageSortModel.cpp"