File indexing completed on 2024-12-01 04:37:05

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 "teamsearchroomforteamwidget.h"
0008 #include "common/flowlayout.h"
0009 #include "misc/clickablewidget.h"
0010 #include <QVBoxLayout>
0011 
0012 TeamSearchRoomForTeamWidget::TeamSearchRoomForTeamWidget(RocketChatAccount *account, QWidget *parent)
0013     : QWidget(parent)
0014     , mSearchRoomLineEdit(new AddTeamRoomCompletionLineEdit(account, this))
0015     , mFlowLayout(new FlowLayout)
0016 {
0017     auto mainLayout = new QVBoxLayout(this);
0018     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0019     mainLayout->setContentsMargins({});
0020 
0021     mSearchRoomLineEdit->setObjectName(QStringLiteral("mSearchRoomLineEdit"));
0022     connect(mSearchRoomLineEdit, &AddTeamRoomCompletionLineEdit::newRoomName, this, &TeamSearchRoomForTeamWidget::slotAddNewName);
0023     mainLayout->addWidget(mSearchRoomLineEdit);
0024 
0025     mFlowLayout->setObjectName(QStringLiteral("mFlowLayout"));
0026     mainLayout->addLayout(mFlowLayout);
0027     mainLayout->addStretch();
0028 }
0029 
0030 TeamSearchRoomForTeamWidget::~TeamSearchRoomForTeamWidget()
0031 {
0032     delete mFlowLayout;
0033 }
0034 
0035 void TeamSearchRoomForTeamWidget::slotAddNewName(const AddTeamRoomCompletionLineEdit::RoomCompletionInfo &info)
0036 {
0037     const QString &roomName = info.roomName;
0038     if (mMap.contains(roomName)) {
0039         return;
0040     }
0041     auto clickableWidget = new ClickableWidget(roomName, this);
0042     clickableWidget->setIdentifier(info.roomId);
0043     connect(clickableWidget, &ClickableWidget::removeClickableWidget, this, &TeamSearchRoomForTeamWidget::slotRemoveRoom);
0044     mFlowLayout->addWidget(clickableWidget);
0045     mMap.insert(roomName, clickableWidget);
0046     Q_EMIT userListChanged(!mMap.isEmpty());
0047 }
0048 
0049 void TeamSearchRoomForTeamWidget::slotRemoveRoom(const QString &name)
0050 {
0051     ClickableWidget *userWidget = mMap.value(name);
0052     if (userWidget) {
0053         const int index = mFlowLayout->indexOf(userWidget);
0054         if (index != -1) {
0055             delete mFlowLayout->takeAt(index);
0056             mMap.remove(name);
0057             delete userWidget;
0058         }
0059     }
0060     Q_EMIT userListChanged(!mMap.isEmpty());
0061 }
0062 
0063 QStringList TeamSearchRoomForTeamWidget::roomIds() const
0064 {
0065     QStringList addUsers;
0066     QMapIterator<QString, ClickableWidget *> i(mMap);
0067     addUsers.reserve(mMap.count());
0068     while (i.hasNext()) {
0069         i.next();
0070         addUsers << i.value()->identifier();
0071     }
0072     return addUsers;
0073 }
0074 
0075 QStringList TeamSearchRoomForTeamWidget::names() const
0076 {
0077     QStringList addUsers;
0078     QMapIterator<QString, ClickableWidget *> i(mMap);
0079     addUsers.reserve(mMap.count());
0080     while (i.hasNext()) {
0081         i.next();
0082         addUsers << i.value()->name();
0083     }
0084     return addUsers;
0085 }
0086 
0087 #include "moc_teamsearchroomforteamwidget.cpp"