summary refs log tree commit diff
path: root/src/dialogs/InviteUsers.cc
blob: 71cfdf20ecbcd32c54eac95a711487f5d4e25ad2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <QDebug>
#include <QIcon>
#include <QListWidget>
#include <QListWidgetItem>
#include <QStyleOption>
#include <QTimer>
#include <QVBoxLayout>

#include "Config.h"
#include "FlatButton.h"
#include "TextField.h"

#include "InviteeItem.h"
#include "dialogs/InviteUsers.h"

#include "mtx.hpp"

using namespace dialogs;

InviteUsers::InviteUsers(QWidget *parent)
  : QFrame(parent)
{
        setMaximumSize(400, 350);

        auto layout = new QVBoxLayout(this);
        layout->setSpacing(30);
        layout->setMargin(20);

        auto buttonLayout = new QHBoxLayout();
        buttonLayout->setSpacing(0);
        buttonLayout->setMargin(0);

        confirmBtn_ = new FlatButton("INVITE", this);
        confirmBtn_->setFontSize(conf::btn::fontSize);

        cancelBtn_ = new FlatButton(tr("CANCEL"), this);
        cancelBtn_->setFontSize(conf::btn::fontSize);

        buttonLayout->addStretch(1);
        buttonLayout->addWidget(confirmBtn_);
        buttonLayout->addWidget(cancelBtn_);

        QFont font;
        font.setPixelSize(conf::headerFontSize);

        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);
        font.setPixelSize(12);
        errorLabel_->setFont(font);

        layout->addWidget(inviteeInput_);
        layout->addWidget(errorLabel_);
        layout->addWidget(inviteeList_);
        layout->addLayout(buttonLayout);

        connect(inviteeInput_, &TextField::returnPressed, this, &InviteUsers::addUser);
        connect(confirmBtn_, &QPushButton::clicked, [this]() {
                emit closing(true, invitedUsers());

                inviteeInput_->clear();
                inviteeList_->clear();
                errorLabel_->hide();
        });

        connect(cancelBtn_, &QPushButton::clicked, [this]() {
                QStringList emptyList;
                emit closing(false, emptyList);

                inviteeInput_->clear();
                inviteeList_->clear();
                errorLabel_->hide();
        });
}

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);
}

void
InviteUsers::paintEvent(QPaintEvent *)
{
        QStyleOption opt;
        opt.init(this);
        QPainter p(this);
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

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);
}