Warning, file /network/ruqola/src/core/model/teamroomsmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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 "teamroomsmodel.h" 0008 #include <KLocalizedString> 0009 0010 TeamRoomsModel::TeamRoomsModel(QObject *parent) 0011 : QAbstractListModel(parent) 0012 { 0013 } 0014 0015 TeamRoomsModel::~TeamRoomsModel() = default; 0016 0017 int TeamRoomsModel::rowCount(const QModelIndex &parent) const 0018 { 0019 Q_UNUSED(parent); 0020 return mTeamRooms.count(); 0021 } 0022 0023 QVariant TeamRoomsModel::data(const QModelIndex &index, int role) const 0024 { 0025 if (index.row() < 0 || index.row() >= mTeamRooms.count()) { 0026 return {}; 0027 } 0028 const TeamRoom &teamroom = mTeamRooms.at(index.row()); 0029 switch (role) { 0030 case Qt::DisplayRole: 0031 case TeamRoomsRoles::Name: { 0032 const QString name = teamroom.name() + (teamroom.autoJoin() ? QLatin1Char(' ') + i18n("(Autojoin)") : QString()); 0033 return name; 0034 } 0035 case TeamRoomsRoles::AutoJoin: 0036 return teamroom.autoJoin(); 0037 case TeamRoomsRoles::Identifier: 0038 return teamroom.identifier(); 0039 case Qt::CheckStateRole: { 0040 if (mIsCheckable) { 0041 const QString roomId = data(index, TeamRoomsModel::Identifier).toString(); 0042 return mRoomSelected.contains(roomId) ? Qt::Checked : Qt::Unchecked; 0043 } 0044 } 0045 } 0046 return {}; 0047 } 0048 0049 QVector<TeamRoom> TeamRoomsModel::teamRooms() const 0050 { 0051 return mTeamRooms; 0052 } 0053 0054 void TeamRoomsModel::setTeamRooms(const QVector<TeamRoom> &teamRooms) 0055 { 0056 if (!mTeamRooms.isEmpty()) { 0057 beginResetModel(); 0058 mTeamRooms.clear(); 0059 endResetModel(); 0060 } 0061 if (!teamRooms.isEmpty()) { 0062 beginInsertRows(QModelIndex(), 0, teamRooms.count() - 1); 0063 mTeamRooms = teamRooms; 0064 endInsertRows(); 0065 } 0066 } 0067 0068 void TeamRoomsModel::setRoomChanged(const TeamRoom &t) 0069 { 0070 const int roomCount = mTeamRooms.count(); 0071 for (int i = 0; i < roomCount; ++i) { 0072 TeamRoom &teamRoom = mTeamRooms[i]; 0073 if (teamRoom.identifier() == t.identifier()) { 0074 teamRoom.setAutoJoin(t.autoJoin()); 0075 const QModelIndex idx = createIndex(i, 0); 0076 Q_EMIT dataChanged(idx, idx); 0077 break; 0078 } 0079 } 0080 } 0081 0082 void TeamRoomsModel::insertRooms(const QVector<TeamRoom> &teamRooms) 0083 { 0084 const int count = mTeamRooms.count(); 0085 beginInsertRows(QModelIndex(), count, count + teamRooms.count() - 1); 0086 mTeamRooms.append(teamRooms); 0087 endInsertRows(); 0088 } 0089 0090 bool TeamRoomsModel::setData(const QModelIndex &index, const QVariant &value, int role) 0091 { 0092 if (mIsCheckable) { 0093 if (role == Qt::CheckStateRole) { 0094 if (index.isValid()) { 0095 Q_EMIT dataChanged(index, index); 0096 const QString roomId = data(index, TeamRoomsModel::Identifier).toString(); 0097 if (value == Qt::Checked) { 0098 mRoomSelected.append(std::move(roomId)); 0099 } else { 0100 mRoomSelected.removeAll(roomId); 0101 } 0102 return true; 0103 } 0104 } 0105 } 0106 return QAbstractListModel::setData(index, value, role); 0107 } 0108 0109 Qt::ItemFlags TeamRoomsModel::flags(const QModelIndex &index) const 0110 { 0111 if (mIsCheckable) { 0112 if (index.isValid()) { 0113 return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable; 0114 } else { 0115 return QAbstractListModel::flags(index); 0116 } 0117 } 0118 return QAbstractListModel::flags(index); 0119 } 0120 0121 bool TeamRoomsModel::isCheckable() const 0122 { 0123 return mIsCheckable; 0124 } 0125 0126 void TeamRoomsModel::setIsCheckable(bool isCheckable) 0127 { 0128 mIsCheckable = isCheckable; 0129 } 0130 0131 #include "moc_teamroomsmodel.cpp"