File indexing completed on 2024-12-01 04:36:50

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "adduserswidget.h"
0008 #include "common/flowlayout.h"
0009 #include "misc/clickablewidget.h"
0010 #include <QVBoxLayout>
0011 
0012 AddUsersWidget::AddUsersWidget(RocketChatAccount *account, QWidget *parent)
0013     : QWidget(parent)
0014     , mSearchUserLineEdit(new AddUsersCompletionLineEdit(account, this))
0015     , mFlowLayout(new FlowLayout)
0016 {
0017     auto mainLayout = new QVBoxLayout(this);
0018     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0019     mainLayout->setContentsMargins({});
0020 
0021     mSearchUserLineEdit->setObjectName(QStringLiteral("mSearchUserLineEdit"));
0022     connect(mSearchUserLineEdit, &AddUsersCompletionLineEdit::newUserName, this, &AddUsersWidget::slotAddNewName);
0023     mainLayout->addWidget(mSearchUserLineEdit);
0024 
0025     mFlowLayout->setObjectName(QStringLiteral("mFlowLayout"));
0026     mainLayout->addLayout(mFlowLayout);
0027 }
0028 
0029 AddUsersWidget::~AddUsersWidget()
0030 {
0031     delete mFlowLayout;
0032 }
0033 
0034 void AddUsersWidget::slotAddNewName(const AddUsersCompletionLineEdit::UserCompletionInfo &info)
0035 {
0036     const QString &userName = info.username;
0037     if (mMap.contains(userName)) {
0038         return;
0039     }
0040     auto clickableUserWidget = new ClickableWidget(userName, this);
0041     clickableUserWidget->setIdentifier(info.userId);
0042     connect(clickableUserWidget, &ClickableWidget::removeClickableWidget, this, &AddUsersWidget::slotRemoveUser);
0043     mFlowLayout->addWidget(clickableUserWidget);
0044     mMap.insert(userName, clickableUserWidget);
0045     Q_EMIT userListChanged(!mMap.isEmpty());
0046 }
0047 
0048 void AddUsersWidget::slotRemoveUser(const QString &username)
0049 {
0050     ClickableWidget *userWidget = mMap.value(username);
0051     if (userWidget) {
0052         const int index = mFlowLayout->indexOf(userWidget);
0053         if (index != -1) {
0054             userWidget->deleteLater();
0055             delete mFlowLayout->takeAt(index);
0056             mMap.remove(username);
0057         }
0058     }
0059     Q_EMIT userListChanged(!mMap.isEmpty());
0060 }
0061 
0062 QStringList AddUsersWidget::userIds() const
0063 {
0064     QStringList addUsers;
0065     QMapIterator<QString, ClickableWidget *> i(mMap);
0066     while (i.hasNext()) {
0067         i.next();
0068         addUsers << i.value()->identifier();
0069     }
0070     return addUsers;
0071 }
0072 
0073 QStringList AddUsersWidget::userNames() const
0074 {
0075     QStringList addUsers;
0076     QMapIterator<QString, ClickableWidget *> i(mMap);
0077     while (i.hasNext()) {
0078         i.next();
0079         addUsers << i.value()->name();
0080     }
0081     return addUsers;
0082 }
0083 
0084 void AddUsersWidget::setPlaceholderText(const QString &str)
0085 {
0086     mSearchUserLineEdit->setPlaceholderText(str);
0087 }
0088 
0089 QString AddUsersWidget::placeHolderText() const
0090 {
0091     return mSearchUserLineEdit->placeholderText();
0092 }
0093 
0094 int AddUsersWidget::numberOfUsers() const
0095 {
0096     return mMap.count();
0097 }
0098 
0099 QLineEdit *AddUsersWidget::lineEdit() const
0100 {
0101     return mSearchUserLineEdit;
0102 }
0103 
0104 #include "moc_adduserswidget.cpp"