2022-03-17 03:20:36 +00:00
|
|
|
// SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
|
|
|
|
|
// SPDX-License-Identifier: LGPL-2.0-or-later
|
|
|
|
|
|
|
|
|
|
#include "savedquicksettingsmodel.h"
|
|
|
|
|
|
|
|
|
|
SavedQuickSettingsModel::SavedQuickSettingsModel(QObject *parent)
|
|
|
|
|
: QAbstractListModel{parent}
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SavedQuickSettingsModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid() || index.row() >= m_data.count()) {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (role == NameRole) {
|
2022-03-17 19:34:27 +00:00
|
|
|
return m_data[index.row()]->name();
|
2022-03-17 03:20:36 +00:00
|
|
|
} else if (role == IconRole) {
|
2022-03-17 19:34:27 +00:00
|
|
|
return m_data[index.row()]->iconName();
|
2022-03-17 03:20:36 +00:00
|
|
|
} else if (role == IdRole) {
|
2022-03-17 19:34:27 +00:00
|
|
|
return m_data[index.row()]->pluginId();
|
2022-03-17 03:20:36 +00:00
|
|
|
}
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SavedQuickSettingsModel::rowCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
return m_data.count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QHash<int, QByteArray> SavedQuickSettingsModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
return {{NameRole, "name"}, {IdRole, "id"}, {IconRole, "icon"}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SavedQuickSettingsModel::moveRow(int oldIndex, int newIndex)
|
|
|
|
|
{
|
|
|
|
|
if (oldIndex < 0 || oldIndex >= m_data.count() || newIndex < 0 || newIndex >= m_data.count()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:34:27 +00:00
|
|
|
if (oldIndex < newIndex) {
|
|
|
|
|
++newIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 03:20:36 +00:00
|
|
|
Q_EMIT beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
|
2022-03-17 19:34:27 +00:00
|
|
|
std::iter_swap(m_data.begin() + oldIndex, m_data.begin() + newIndex);
|
2022-03-17 03:20:36 +00:00
|
|
|
Q_EMIT endMoveRows();
|
|
|
|
|
|
|
|
|
|
Q_EMIT dataUpdated(m_data);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:34:27 +00:00
|
|
|
void SavedQuickSettingsModel::insertRow(KPluginMetaData *metaData, int index)
|
2022-03-17 03:20:36 +00:00
|
|
|
{
|
|
|
|
|
Q_EMIT beginInsertRows(QModelIndex(), index, index);
|
|
|
|
|
m_data.insert(index, metaData);
|
|
|
|
|
Q_EMIT endInsertRows();
|
|
|
|
|
|
|
|
|
|
Q_EMIT dataUpdated(m_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SavedQuickSettingsModel::removeRow(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index < 0 || index >= m_data.size()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_EMIT beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
|
m_data.erase(m_data.begin() + index);
|
|
|
|
|
Q_EMIT endRemoveRows();
|
|
|
|
|
|
|
|
|
|
Q_EMIT dataUpdated(m_data);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:34:27 +00:00
|
|
|
QList<KPluginMetaData *> SavedQuickSettingsModel::list() const
|
2022-03-17 03:20:36 +00:00
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:34:27 +00:00
|
|
|
void SavedQuickSettingsModel::updateData(QList<KPluginMetaData *> data)
|
2022-03-17 03:20:36 +00:00
|
|
|
{
|
|
|
|
|
Q_EMIT beginResetModel();
|
|
|
|
|
|
|
|
|
|
m_data.clear();
|
|
|
|
|
for (auto metaData : data) {
|
|
|
|
|
m_data.push_back(metaData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_EMIT endResetModel();
|
|
|
|
|
|
|
|
|
|
Q_EMIT dataUpdated(m_data);
|
|
|
|
|
}
|