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

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 "usersinroomlabel.h"
0008 #include "rocketchataccount.h"
0009 #include "room.h"
0010 #include "roomutil.h"
0011 #include "ruqola.h"
0012 #include "usersinroommenu.h"
0013 #include <QHBoxLayout>
0014 #include <QIcon>
0015 
0016 UsersInRoomLabel::UsersInRoomLabel(QWidget *parent)
0017     : QWidget(parent)
0018     , mIconLabel(new QLabel(this))
0019     , mUserNameLabel(new UserLabel(this))
0020 {
0021     auto mainLayout = new QHBoxLayout(this);
0022     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0023     mainLayout->setContentsMargins({});
0024     mainLayout->setSpacing(0);
0025     mIconLabel->setObjectName(QStringLiteral("mIconLabel"));
0026     mainLayout->addWidget(mIconLabel);
0027 
0028     mUserNameLabel->setObjectName(QStringLiteral("mUserNameLabel"));
0029     mainLayout->addWidget(mUserNameLabel);
0030 }
0031 
0032 UsersInRoomLabel::~UsersInRoomLabel() = default;
0033 
0034 void UsersInRoomLabel::setUserInfo(const UsersInRoomLabel::UserInfo &info)
0035 {
0036     mUserNameLabel->setText(info.userDisplayName);
0037     mIconLabel->setPixmap(QIcon::fromTheme(info.iconStatus).pixmap(18, 18));
0038     mUserNameLabel->setUserId(info.userId);
0039     mUserNameLabel->setUserName(info.userName);
0040 }
0041 
0042 void UsersInRoomLabel::setRoom(Room *room)
0043 {
0044     mUserNameLabel->setRoom(room);
0045 }
0046 
0047 UserLabel::UserLabel(QWidget *parent)
0048     : QLabel(parent)
0049 {
0050     setTextFormat(Qt::RichText);
0051     setContextMenuPolicy(Qt::CustomContextMenu);
0052     connect(this, &UserLabel::customContextMenuRequested, this, &UserLabel::slotCustomContextMenuRequested);
0053     connect(this, &QLabel::linkActivated, this, &UserLabel::slotOpenConversation);
0054 }
0055 
0056 UserLabel::~UserLabel() = default;
0057 
0058 void UserLabel::setRoom(Room *room)
0059 {
0060     mRoom = room;
0061 }
0062 
0063 void UserLabel::setUserName(const QString &userName)
0064 {
0065     mUserName = userName;
0066 }
0067 
0068 void UserLabel::setUserId(const QString &userId)
0069 {
0070     mUserId = userId;
0071 }
0072 
0073 void UserLabel::slotOpenConversation()
0074 {
0075     Q_EMIT Ruqola::self()->rocketChatAccount()->openLinkRequested(RoomUtil::generateUserLink(mUserName));
0076 }
0077 
0078 void UserLabel::slotCustomContextMenuRequested(const QPoint &pos)
0079 {
0080     if (!mMenu) {
0081         mMenu = new UsersInRoomMenu(this);
0082         mMenu->setParentWidget(this);
0083         mMenu->setRoom(mRoom);
0084         mMenu->setUserId(mUserId);
0085         mMenu->setUserName(mUserName);
0086     }
0087     mMenu->slotCustomContextMenuRequested(pos);
0088 }
0089 
0090 #include "moc_usersinroomlabel.cpp"