matrixion/src/dialogs/InviteUsers.cpp

148 lines
4.2 KiB
C++
Raw Normal View History

2017-12-11 00:59:50 +03:00
#include <QDebug>
#include <QIcon>
#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
#include "mtx.hpp"
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);
setMinimumWidth(conf::modals::MIN_WIDGET_WIDTH);
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]() {
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();
});
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);
}