File indexing completed on 2024-09-08 05:00:58
0001 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org> 0002 // SPDX-License-Identifier: LGPL-2.0-or-later 0003 0004 #include "savedquicksettingsmodel.h" 0005 0006 SavedQuickSettingsModel::SavedQuickSettingsModel(QObject *parent) 0007 : QAbstractListModel{parent} 0008 { 0009 } 0010 0011 QVariant SavedQuickSettingsModel::data(const QModelIndex &index, int role) const 0012 { 0013 if (!index.isValid() || index.row() >= m_data.count()) { 0014 return QVariant(); 0015 } 0016 0017 if (role == NameRole) { 0018 return m_data[index.row()].name(); 0019 } else if (role == IconRole) { 0020 return m_data[index.row()].iconName(); 0021 } else if (role == IdRole) { 0022 return m_data[index.row()].pluginId(); 0023 } 0024 return QVariant(); 0025 } 0026 0027 int SavedQuickSettingsModel::rowCount(const QModelIndex &parent) const 0028 { 0029 Q_UNUSED(parent); 0030 return m_data.count(); 0031 } 0032 0033 QHash<int, QByteArray> SavedQuickSettingsModel::roleNames() const 0034 { 0035 return {{NameRole, "name"}, {IdRole, "id"}, {IconRole, "icon"}}; 0036 } 0037 0038 void SavedQuickSettingsModel::moveRow(int oldIndex, int newIndex) 0039 { 0040 if (oldIndex < 0 || oldIndex >= m_data.count() || newIndex < 0 || newIndex >= m_data.count()) { 0041 return; 0042 } 0043 0044 Q_EMIT beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex + (oldIndex < newIndex ? 1 : 0)); 0045 std::iter_swap(m_data.begin() + oldIndex, m_data.begin() + newIndex); 0046 Q_EMIT endMoveRows(); 0047 0048 Q_EMIT dataUpdated(m_data); 0049 } 0050 0051 void SavedQuickSettingsModel::insertRow(KPluginMetaData metaData, int index) 0052 { 0053 Q_EMIT beginInsertRows(QModelIndex(), index, index); 0054 m_data.insert(index, metaData); 0055 Q_EMIT endInsertRows(); 0056 0057 Q_EMIT dataUpdated(m_data); 0058 } 0059 0060 KPluginMetaData SavedQuickSettingsModel::takeRow(int index) 0061 { 0062 if (index < 0 || index >= m_data.size()) { 0063 return {}; 0064 } 0065 0066 Q_EMIT beginRemoveRows(QModelIndex(), index, index); 0067 KPluginMetaData tmp = m_data.takeAt(index); 0068 Q_EMIT endRemoveRows(); 0069 0070 Q_EMIT dataUpdated(m_data); 0071 0072 return tmp; 0073 } 0074 0075 void SavedQuickSettingsModel::removeRow(int index) 0076 { 0077 if (index < 0 || index >= m_data.size()) { 0078 return; 0079 } 0080 0081 Q_EMIT beginRemoveRows(QModelIndex(), index, index); 0082 m_data.erase(m_data.begin() + index); 0083 Q_EMIT endRemoveRows(); 0084 0085 Q_EMIT dataUpdated(m_data); 0086 } 0087 0088 QList<KPluginMetaData> SavedQuickSettingsModel::list() const 0089 { 0090 return m_data; 0091 } 0092 0093 void SavedQuickSettingsModel::updateData(QList<KPluginMetaData> data) 0094 { 0095 Q_EMIT beginResetModel(); 0096 0097 m_data.clear(); 0098 for (auto metaData : data) { 0099 m_data.push_back(metaData); 0100 } 0101 0102 Q_EMIT endResetModel(); 0103 0104 Q_EMIT dataUpdated(m_data); 0105 }