diff --git a/src/core/model/usercompletermodel.cpp b/src/core/model/usercompletermodel.cpp index 61a700ad..9f233109 100644 --- a/src/core/model/usercompletermodel.cpp +++ b/src/core/model/usercompletermodel.cpp @@ -1,92 +1,104 @@ /* Copyright (c) 2017-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "usercompletermodel.h" #include "ruqola_debug.h" #include #include UserCompleterModel::UserCompleterModel(QObject *parent) : QAbstractListModel(parent) { } UserCompleterModel::~UserCompleterModel() { } void UserCompleterModel::clear() { if (!mUsers.isEmpty()) { beginRemoveRows(QModelIndex(), 0, rowCount() - 1); mUsers.clear(); endRemoveRows(); } } void UserCompleterModel::insertUsers(const QVector &users) { if (rowCount() != 0) { beginRemoveRows(QModelIndex(), 0, mUsers.count() - 1); mUsers.clear(); endRemoveRows(); } if (!users.isEmpty()) { beginInsertRows(QModelIndex(), 0, users.count() - 1); mUsers = users; endInsertRows(); } } int UserCompleterModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return mUsers.count(); } QVariant UserCompleterModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= mUsers.count()) { return QVariant(); } const User user = mUsers.at(index.row()); switch (role) { - case UserName: case Qt::DisplayRole: + return displayUserName(user); + case UserName: return user.userName(); case UserId: return user.userId(); case Qt::DecorationRole: return QIcon::fromTheme(user.iconFromStatus()); case UserIconStatus: return user.iconFromStatus(); } return {}; } QHash UserCompleterModel::roleNames() const { QHash roles; roles[UserName] = QByteArrayLiteral("username"); roles[UserId] = QByteArrayLiteral("userid"); roles[UserIconStatus] = QByteArrayLiteral("iconstatus"); return roles; } + +QString UserCompleterModel::displayUserName(const User &user) const +{ + QString text = user.userName(); + const QString name = user.name(); + if (!name.isEmpty()) { + text += QLatin1String(" (") + name + QLatin1Char(')'); + } + return text; +} + diff --git a/src/core/model/usercompletermodel.h b/src/core/model/usercompletermodel.h index 5e2044e3..df586116 100644 --- a/src/core/model/usercompletermodel.h +++ b/src/core/model/usercompletermodel.h @@ -1,55 +1,56 @@ /* Copyright (c) 2017-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef USERCOMPLETERMODEL_H #define USERCOMPLETERMODEL_H #include "user.h" #include "libruqola_private_export.h" #include class LIBRUQOLACORE_TESTS_EXPORT UserCompleterModel : public QAbstractListModel { Q_OBJECT public: enum UserRoles { UserName = Qt::UserRole + 1, UserId, UserIconStatus }; Q_ENUM(UserRoles) explicit UserCompleterModel(QObject *parent = nullptr); ~UserCompleterModel() override; Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent = QModelIndex()) const override; Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; void clear(); void insertUsers(const QVector &users); Q_REQUIRED_RESULT QHash roleNames() const override; private: + Q_REQUIRED_RESULT QString displayUserName(const User &user) const; Q_DISABLE_COPY(UserCompleterModel) QVector mUsers; }; #endif // USERCOMPLETERMODEL_H