2021-03-05 00:35:15 +01:00
|
|
|
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2017-04-06 01:06:42 +02:00
|
|
|
|
2020-06-08 01:45:24 +02:00
|
|
|
#include <QInputDialog>
|
2020-05-25 13:03:49 +02:00
|
|
|
#include <QLabel>
|
2020-06-08 01:45:24 +02:00
|
|
|
#include <QMenu>
|
2020-01-31 06:12:02 +01:00
|
|
|
#include <QPainter>
|
2020-05-25 13:03:49 +02:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOption>
|
2017-06-02 17:04:51 +02:00
|
|
|
#include <QTimer>
|
2017-05-19 18:55:38 +02:00
|
|
|
|
2018-10-01 16:56:46 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-06-08 01:45:24 +02:00
|
|
|
#include "ChatPage.h"
|
2017-07-15 16:11:46 +02:00
|
|
|
#include "Config.h"
|
2017-06-02 17:04:51 +02:00
|
|
|
#include "MainWindow.h"
|
2020-01-31 06:12:02 +01:00
|
|
|
#include "Splitter.h"
|
2017-05-19 18:55:38 +02:00
|
|
|
#include "UserInfoWidget.h"
|
2020-06-08 20:26:37 +02:00
|
|
|
#include "UserSettingsPage.h"
|
2018-07-17 15:37:25 +02:00
|
|
|
#include "ui/Avatar.h"
|
|
|
|
#include "ui/FlatButton.h"
|
|
|
|
#include "ui/OverlayModal.h"
|
2017-04-06 01:06:42 +02:00
|
|
|
|
|
|
|
UserInfoWidget::UserInfoWidget(QWidget *parent)
|
2017-08-20 12:47:22 +02:00
|
|
|
: QWidget(parent)
|
|
|
|
, display_name_("User")
|
|
|
|
, user_id_("@user:homeserver.org")
|
2017-04-06 01:06:42 +02:00
|
|
|
{
|
2018-10-03 23:32:13 +02:00
|
|
|
QFont f;
|
|
|
|
f.setPointSizeF(f.pointSizeF());
|
|
|
|
|
|
|
|
const int fontHeight = QFontMetrics(f).height();
|
2018-10-01 16:56:46 +02:00
|
|
|
const int widgetMargin = fontHeight / 3;
|
|
|
|
const int contentHeight = fontHeight * 3;
|
|
|
|
|
2018-10-03 23:32:13 +02:00
|
|
|
logoutButtonSize_ = std::min(fontHeight, 20);
|
2018-10-01 16:56:46 +02:00
|
|
|
|
2018-10-03 23:32:13 +02:00
|
|
|
setFixedHeight(contentHeight + widgetMargin);
|
2017-09-10 11:59:21 +02:00
|
|
|
|
|
|
|
topLayout_ = new QHBoxLayout(this);
|
|
|
|
topLayout_->setSpacing(0);
|
2018-10-01 16:56:46 +02:00
|
|
|
topLayout_->setMargin(widgetMargin);
|
2017-09-10 11:59:21 +02:00
|
|
|
|
|
|
|
avatarLayout_ = new QHBoxLayout();
|
|
|
|
textLayout_ = new QVBoxLayout();
|
2018-10-06 16:21:03 +02:00
|
|
|
textLayout_->setSpacing(widgetMargin / 2);
|
|
|
|
textLayout_->setContentsMargins(widgetMargin * 2, widgetMargin, widgetMargin, widgetMargin);
|
2017-09-10 11:59:21 +02:00
|
|
|
|
2019-08-26 01:24:56 +02:00
|
|
|
userAvatar_ = new Avatar(this, fontHeight * 2.5);
|
2017-11-16 15:33:52 +01:00
|
|
|
userAvatar_->setObjectName("userAvatar");
|
2017-09-10 11:59:21 +02:00
|
|
|
userAvatar_->setLetter(QChar('?'));
|
|
|
|
|
2018-09-30 12:24:36 +02:00
|
|
|
QFont nameFont;
|
2018-10-03 23:32:13 +02:00
|
|
|
nameFont.setPointSizeF(nameFont.pointSizeF() * 1.1);
|
2018-09-30 12:24:36 +02:00
|
|
|
nameFont.setWeight(QFont::Medium);
|
2017-09-10 11:59:21 +02:00
|
|
|
|
|
|
|
displayNameLabel_ = new QLabel(this);
|
|
|
|
displayNameLabel_->setFont(nameFont);
|
2017-11-16 15:33:52 +01:00
|
|
|
displayNameLabel_->setObjectName("displayNameLabel");
|
2017-09-10 11:59:21 +02:00
|
|
|
displayNameLabel_->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignTop);
|
|
|
|
|
|
|
|
userIdLabel_ = new QLabel(this);
|
2018-10-03 23:32:13 +02:00
|
|
|
userIdLabel_->setFont(f);
|
2017-11-16 15:33:52 +01:00
|
|
|
userIdLabel_->setObjectName("userIdLabel");
|
2017-09-10 11:59:21 +02:00
|
|
|
userIdLabel_->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
|
|
|
|
avatarLayout_->addWidget(userAvatar_);
|
2018-10-03 23:32:13 +02:00
|
|
|
textLayout_->addWidget(displayNameLabel_, 0, Qt::AlignBottom);
|
|
|
|
textLayout_->addWidget(userIdLabel_, 0, Qt::AlignTop);
|
2017-09-10 11:59:21 +02:00
|
|
|
|
|
|
|
topLayout_->addLayout(avatarLayout_);
|
|
|
|
topLayout_->addLayout(textLayout_);
|
|
|
|
topLayout_->addStretch(1);
|
|
|
|
|
|
|
|
buttonLayout_ = new QHBoxLayout();
|
|
|
|
buttonLayout_->setSpacing(0);
|
|
|
|
buttonLayout_->setMargin(0);
|
|
|
|
|
|
|
|
logoutButton_ = new FlatButton(this);
|
2018-07-25 22:28:37 +02:00
|
|
|
logoutButton_->setToolTip(tr("Logout"));
|
2017-09-10 11:59:21 +02:00
|
|
|
logoutButton_->setCornerRadius(logoutButtonSize_ / 2);
|
|
|
|
|
|
|
|
QIcon icon;
|
2017-10-15 21:08:51 +02:00
|
|
|
icon.addFile(":/icons/icons/ui/power-button-off.png");
|
2017-09-10 11:59:21 +02:00
|
|
|
|
|
|
|
logoutButton_->setIcon(icon);
|
2017-10-15 21:08:51 +02:00
|
|
|
logoutButton_->setIconSize(QSize(logoutButtonSize_, logoutButtonSize_));
|
2017-09-10 11:59:21 +02:00
|
|
|
|
|
|
|
buttonLayout_->addWidget(logoutButton_);
|
|
|
|
|
|
|
|
topLayout_->addLayout(buttonLayout_);
|
|
|
|
|
|
|
|
// Show the confirmation dialog.
|
2018-08-31 08:10:47 +02:00
|
|
|
connect(logoutButton_, &QPushButton::clicked, this, []() {
|
|
|
|
MainWindow::instance()->openLogoutDialog();
|
2017-09-10 11:59:21 +02:00
|
|
|
});
|
2020-06-08 01:45:24 +02:00
|
|
|
|
|
|
|
menu = new QMenu(this);
|
|
|
|
|
|
|
|
auto setStatusAction = menu->addAction(tr("Set custom status message"));
|
|
|
|
connect(setStatusAction, &QAction::triggered, this, [this]() {
|
|
|
|
bool ok = false;
|
|
|
|
QString text = QInputDialog::getText(this,
|
|
|
|
tr("Custom status message"),
|
|
|
|
tr("Status:"),
|
|
|
|
QLineEdit::Normal,
|
|
|
|
ChatPage::instance()->status(),
|
|
|
|
&ok);
|
2020-06-11 18:28:00 +02:00
|
|
|
if (ok)
|
2020-06-08 01:45:24 +02:00
|
|
|
ChatPage::instance()->setStatus(text);
|
|
|
|
});
|
2020-06-09 12:49:36 +02:00
|
|
|
|
2021-01-28 15:33:50 +01:00
|
|
|
auto userProfileAction = menu->addAction(tr("User Profile Settings"));
|
|
|
|
connect(
|
|
|
|
userProfileAction, &QAction::triggered, this, [this]() { emit openGlobalUserProfile(); });
|
|
|
|
|
2020-06-09 12:49:36 +02:00
|
|
|
#if 0 // disable presence menu until issues in synapse are resolved
|
2020-06-08 20:26:37 +02:00
|
|
|
auto setAutoPresence = menu->addAction(tr("Set presence automatically"));
|
2020-06-08 22:35:47 +02:00
|
|
|
connect(setAutoPresence, &QAction::triggered, this, []() {
|
2020-06-08 20:26:37 +02:00
|
|
|
ChatPage::instance()->userSettings()->setPresence(
|
|
|
|
UserSettings::Presence::AutomaticPresence);
|
|
|
|
ChatPage::instance()->setStatus(ChatPage::instance()->status());
|
|
|
|
});
|
|
|
|
auto setOnline = menu->addAction(tr("Online"));
|
2020-06-08 22:35:47 +02:00
|
|
|
connect(setOnline, &QAction::triggered, this, []() {
|
2020-06-08 20:26:37 +02:00
|
|
|
ChatPage::instance()->userSettings()->setPresence(UserSettings::Presence::Online);
|
|
|
|
ChatPage::instance()->setStatus(ChatPage::instance()->status());
|
|
|
|
});
|
|
|
|
auto setUnavailable = menu->addAction(tr("Unavailable"));
|
2020-06-08 22:35:47 +02:00
|
|
|
connect(setUnavailable, &QAction::triggered, this, []() {
|
2020-06-08 20:26:37 +02:00
|
|
|
ChatPage::instance()->userSettings()->setPresence(
|
|
|
|
UserSettings::Presence::Unavailable);
|
|
|
|
ChatPage::instance()->setStatus(ChatPage::instance()->status());
|
|
|
|
});
|
|
|
|
auto setOffline = menu->addAction(tr("Offline"));
|
2020-06-08 22:35:47 +02:00
|
|
|
connect(setOffline, &QAction::triggered, this, []() {
|
2020-06-08 20:26:37 +02:00
|
|
|
ChatPage::instance()->userSettings()->setPresence(UserSettings::Presence::Offline);
|
|
|
|
ChatPage::instance()->setStatus(ChatPage::instance()->status());
|
|
|
|
});
|
2020-06-09 12:49:36 +02:00
|
|
|
#endif
|
2020-06-08 01:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UserInfoWidget::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
menu->popup(event->globalPos());
|
2017-06-02 17:04:51 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 12:47:22 +02:00
|
|
|
void
|
|
|
|
UserInfoWidget::resizeEvent(QResizeEvent *event)
|
2017-05-19 18:55:38 +02:00
|
|
|
{
|
2017-09-10 11:59:21 +02:00
|
|
|
Q_UNUSED(event);
|
|
|
|
|
2020-01-31 06:12:02 +01:00
|
|
|
const auto sz = splitter::calculateSidebarSizes(QFont{});
|
2018-10-07 13:09:47 +02:00
|
|
|
|
|
|
|
if (width() <= sz.small) {
|
|
|
|
topLayout_->setContentsMargins(0, 0, logoutButtonSize_, 0);
|
2017-09-10 11:59:21 +02:00
|
|
|
|
|
|
|
userAvatar_->hide();
|
|
|
|
displayNameLabel_->hide();
|
|
|
|
userIdLabel_->hide();
|
|
|
|
} else {
|
|
|
|
topLayout_->setMargin(5);
|
|
|
|
userAvatar_->show();
|
|
|
|
displayNameLabel_->show();
|
|
|
|
userIdLabel_->show();
|
|
|
|
}
|
2017-11-08 22:09:15 +01:00
|
|
|
|
|
|
|
QWidget::resizeEvent(event);
|
2017-05-19 18:55:38 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 12:47:22 +02:00
|
|
|
void
|
|
|
|
UserInfoWidget::reset()
|
2017-04-09 01:17:04 +02:00
|
|
|
{
|
2017-09-10 11:59:21 +02:00
|
|
|
displayNameLabel_->setText("");
|
|
|
|
userIdLabel_->setText("");
|
|
|
|
userAvatar_->setLetter(QChar('?'));
|
2017-04-09 01:17:04 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 12:47:22 +02:00
|
|
|
void
|
|
|
|
UserInfoWidget::setDisplayName(const QString &name)
|
2017-04-06 01:06:42 +02:00
|
|
|
{
|
2017-09-10 11:59:21 +02:00
|
|
|
if (name.isEmpty())
|
|
|
|
display_name_ = user_id_.split(':')[0].split('@')[1];
|
|
|
|
else
|
|
|
|
display_name_ = name;
|
2017-05-16 14:43:29 +02:00
|
|
|
|
2017-09-10 11:59:21 +02:00
|
|
|
displayNameLabel_->setText(display_name_);
|
|
|
|
userAvatar_->setLetter(QChar(display_name_[0]));
|
2017-09-30 19:43:57 +02:00
|
|
|
update();
|
2017-04-06 01:06:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 12:47:22 +02:00
|
|
|
void
|
|
|
|
UserInfoWidget::setUserId(const QString &userid)
|
2017-04-06 01:06:42 +02:00
|
|
|
{
|
2017-09-10 11:59:21 +02:00
|
|
|
user_id_ = userid;
|
|
|
|
userIdLabel_->setText(userid);
|
2019-08-26 01:24:56 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UserInfoWidget::setAvatar(const QString &url)
|
|
|
|
{
|
|
|
|
userAvatar_->setImage(url);
|
|
|
|
update();
|
2017-04-06 01:06:42 +02:00
|
|
|
}
|
2017-11-16 15:33:52 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
UserInfoWidget::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
QPainter p(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
|
|
|
}
|