File indexing completed on 2024-12-08 12:53:34
0001 /* 0002 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "permissionsmodel.h" 0008 #include <KLocalizedString> 0009 0010 PermissionsModel::PermissionsModel(QObject *parent) 0011 : QAbstractListModel(parent) 0012 { 0013 } 0014 0015 PermissionsModel::~PermissionsModel() = default; 0016 0017 int PermissionsModel::rowCount(const QModelIndex &parent) const 0018 { 0019 if (parent.isValid()) { 0020 return 0; // flat model 0021 } 0022 return mPermissions.count(); 0023 } 0024 0025 QVariant PermissionsModel::headerData(int section, Qt::Orientation orientation, int role) const 0026 { 0027 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { 0028 switch (static_cast<AdminPermissionsRoles>(section)) { 0029 case PermissionsModel::IdentifierRole: 0030 return i18n("Name"); 0031 case PermissionsModel::RolesStrRole: 0032 case PermissionsModel::RolesRole: 0033 return i18n("Roles"); 0034 } 0035 } 0036 return {}; 0037 } 0038 0039 int PermissionsModel::columnCount(const QModelIndex &parent) const 0040 { 0041 Q_UNUSED(parent) 0042 constexpr int val = static_cast<int>(PermissionsModel::LastColumn) + 1; 0043 return val; 0044 } 0045 0046 Permissions PermissionsModel::permissions() const 0047 { 0048 return mPermissions; 0049 } 0050 0051 void PermissionsModel::setPermissions(const Permissions &newPermissions) 0052 { 0053 if (!mPermissions.isEmpty()) { 0054 beginResetModel(); 0055 mPermissions.clear(); 0056 endResetModel(); 0057 } 0058 if (!newPermissions.isEmpty()) { 0059 beginInsertRows(QModelIndex(), 0, newPermissions.count() - 1); 0060 mPermissions = newPermissions; 0061 endInsertRows(); 0062 } 0063 } 0064 0065 QVariant PermissionsModel::data(const QModelIndex &index, int role) const 0066 { 0067 if (index.row() < 0 || index.row() >= mPermissions.count()) { 0068 return {}; 0069 } 0070 if (role != Qt::DisplayRole) { 0071 return {}; 0072 } 0073 0074 const Permission &permissionInfo = mPermissions.at(index.row()); 0075 const int col = index.column(); 0076 switch (col) { 0077 case PermissionsModel::IdentifierRole: 0078 return permissionInfo.identifier(); 0079 case PermissionsModel::RolesRole: 0080 return permissionInfo.roles(); 0081 case PermissionsModel::RolesStrRole: 0082 return permissionInfo.rolesStr().join(QLatin1Char(',')); 0083 } 0084 return {}; 0085 } 0086 0087 #include "moc_permissionsmodel.cpp"