Warning, file /network/ruqola/src/core/model/usercompletermodel.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: 2017-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "usercompletermodel.h" 0008 #include <QIcon> 0009 #include <QModelIndex> 0010 0011 UserCompleterModel::UserCompleterModel(QObject *parent) 0012 : QAbstractListModel(parent) 0013 { 0014 } 0015 0016 UserCompleterModel::~UserCompleterModel() = default; 0017 0018 void UserCompleterModel::clear() 0019 { 0020 if (!mUsers.isEmpty()) { 0021 beginResetModel(); 0022 mUsers.clear(); 0023 endResetModel(); 0024 } 0025 } 0026 0027 void UserCompleterModel::addUsers(const QVector<User> &users) 0028 { 0029 // qDebug() << " users " << users; 0030 clear(); 0031 if (!users.isEmpty()) { 0032 beginInsertRows(QModelIndex(), 0, users.count() - 1); 0033 mUsers = users; 0034 endInsertRows(); 0035 } 0036 } 0037 0038 int UserCompleterModel::rowCount(const QModelIndex &parent) const 0039 { 0040 Q_UNUSED(parent) 0041 return mUsers.count(); 0042 } 0043 0044 QVariant UserCompleterModel::data(const QModelIndex &index, int role) const 0045 { 0046 if (index.row() < 0 || index.row() >= mUsers.count()) { 0047 return {}; 0048 } 0049 const User user = mUsers.at(index.row()); 0050 switch (role) { 0051 case Qt::DisplayRole: 0052 case DisplayName: 0053 return displayUserName(user); 0054 case UserName: 0055 return user.userName(); 0056 case UserId: 0057 return user.userId(); 0058 case UserIconStatus: 0059 case Qt::DecorationRole: 0060 return QIcon::fromTheme(user.iconFromStatus()); 0061 case AvatarInfo: 0062 return QVariant::fromValue(avatarInfo(user)); 0063 } 0064 0065 return {}; 0066 } 0067 0068 Utils::AvatarInfo UserCompleterModel::avatarInfo(const User &user) const 0069 { 0070 Utils::AvatarInfo info; 0071 info.avatarType = Utils::AvatarType::User; 0072 info.identifier = user.userName(); 0073 return info; 0074 } 0075 0076 QString UserCompleterModel::displayUserName(const User &user) const 0077 { 0078 return user.name(); 0079 } 0080 0081 #include "moc_usercompletermodel.cpp"