matrixion/src/dialogs/InviteUsers.cpp

159 lines
4.5 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-12-11 00:59:50 +03:00
#include <QDebug>
#include <QIcon>
2020-05-25 14:03:49 +03:00
#include <QLabel>
2017-12-11 00:59:50 +03:00
#include <QListWidget>
#include <QListWidgetItem>
2018-09-19 22:42:26 +03:00
#include <QPushButton>
2017-12-11 00:59:50 +03:00
#include <QStyleOption>
#include <QTimer>
#include <QVBoxLayout>
2018-07-17 16:37:25 +03:00
#include "dialogs/InviteUsers.h"
2017-12-11 00:59:50 +03:00
2018-07-17 16:37:25 +03:00
#include "Config.h"
2017-12-11 00:59:50 +03:00
#include "InviteeItem.h"
2018-07-17 16:37:25 +03:00
#include "ui/TextField.h"
2017-12-11 00:59:50 +03:00
2020-01-31 19:00:13 +03:00
#include <mtx/identifiers.hpp>
2017-12-11 00:59:50 +03:00
using namespace dialogs;
InviteUsers::InviteUsers(QWidget *parent)
: QFrame(parent)
{
2018-09-19 22:42:26 +03:00
setAutoFillBackground(true);
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
setWindowModality(Qt::WindowModal);
setAttribute(Qt::WA_DeleteOnClose, true);
2018-09-30 13:37:38 +03:00
setMinimumWidth(conf::window::minModalWidth);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
2017-12-11 00:59:50 +03:00
auto layout = new QVBoxLayout(this);
layout->setSpacing(conf::modals::WIDGET_SPACING);
layout->setMargin(conf::modals::WIDGET_MARGIN);
2017-12-11 00:59:50 +03:00
auto buttonLayout = new QHBoxLayout();
buttonLayout->setSpacing(0);
buttonLayout->setMargin(0);
2018-09-19 22:42:26 +03:00
confirmBtn_ = new QPushButton("Invite", this);
2018-09-20 08:52:31 +03:00
confirmBtn_->setDefault(true);
cancelBtn_ = new QPushButton(tr("Cancel"), this);
2017-12-11 00:59:50 +03:00
buttonLayout->addStretch(1);
2018-09-19 22:42:26 +03:00
buttonLayout->setSpacing(15);
2017-12-11 00:59:50 +03:00
buttonLayout->addWidget(cancelBtn_);
2018-09-19 22:42:26 +03:00
buttonLayout->addWidget(confirmBtn_);
2017-12-11 00:59:50 +03:00
inviteeInput_ = new TextField(this);
inviteeInput_->setLabel(tr("User ID to invite"));
inviteeList_ = new QListWidget;
inviteeList_->setFrameStyle(QFrame::NoFrame);
inviteeList_->setSelectionMode(QAbstractItemView::NoSelection);
inviteeList_->setAttribute(Qt::WA_MacShowFocusRect, 0);
inviteeList_->setSpacing(5);
errorLabel_ = new QLabel(this);
errorLabel_->setAlignment(Qt::AlignCenter);
layout->addWidget(inviteeInput_);
layout->addWidget(errorLabel_);
layout->addWidget(inviteeList_);
layout->addLayout(buttonLayout);
connect(inviteeInput_, &TextField::returnPressed, this, &InviteUsers::addUser);
connect(confirmBtn_, &QPushButton::clicked, [this]() {
2021-02-15 22:30:32 +03:00
if (!inviteeInput_->text().trimmed().isEmpty()) {
addUser();
}
2018-09-19 22:42:26 +03:00
emit sendInvites(invitedUsers());
2017-12-11 00:59:50 +03:00
inviteeInput_->clear();
inviteeList_->clear();
errorLabel_->hide();
2018-09-20 09:59:14 +03:00
emit close();
2017-12-11 00:59:50 +03:00
});
connect(cancelBtn_, &QPushButton::clicked, [this]() {
2017-12-11 00:59:50 +03:00
inviteeInput_->clear();
inviteeList_->clear();
errorLabel_->hide();
2018-09-19 22:42:26 +03:00
emit close();
2017-12-11 00:59:50 +03:00
});
}
void
InviteUsers::addUser()
{
auto user_id = inviteeInput_->text();
try {
namespace ids = mtx::identifiers;
auto user = ids::parse<ids::User>(user_id.toStdString());
auto item = new QListWidgetItem(inviteeList_);
auto invitee = new InviteeItem(user, this);
item->setSizeHint(invitee->minimumSizeHint());
item->setFlags(Qt::NoItemFlags);
item->setTextAlignment(Qt::AlignCenter);
inviteeList_->setItemWidget(item, invitee);
connect(invitee, &InviteeItem::removeItem, this, [this, item]() {
emit removeInvitee(item);
});
errorLabel_->hide();
inviteeInput_->clear();
} catch (std::exception &e) {
errorLabel_->setText(e.what());
errorLabel_->show();
}
}
void
InviteUsers::removeInvitee(QListWidgetItem *item)
{
int row = inviteeList_->row(item);
auto widget = inviteeList_->takeItem(row);
inviteeList_->removeItemWidget(widget);
}
QStringList
InviteUsers::invitedUsers() const
{
QStringList users;
for (int ii = 0; ii < inviteeList_->count(); ++ii) {
auto item = inviteeList_->item(ii);
auto widget = inviteeList_->itemWidget(item);
auto invitee = qobject_cast<InviteeItem *>(widget);
if (invitee)
users << invitee->userID();
else
qDebug() << "Cast InviteeItem failed";
}
return users;
}
void
InviteUsers::showEvent(QShowEvent *event)
{
inviteeInput_->setFocus();
QFrame::showEvent(event);
}