matrixion/src/dialogs/MemberList.cpp

142 lines
4.4 KiB
C++
Raw Normal View History

#include <QAbstractSlider>
2019-11-09 05:06:10 +03:00
#include <QLabel>
2018-05-01 19:35:28 +03:00
#include <QListWidgetItem>
#include <QPainter>
2018-09-19 22:42:26 +03:00
#include <QPushButton>
#include <QScrollBar>
2018-09-19 22:42:26 +03:00
#include <QShortcut>
2018-05-01 19:35:28 +03:00
#include <QStyleOption>
#include <QVBoxLayout>
2018-07-17 16:37:25 +03:00
#include "dialogs/MemberList.h"
#include "Cache.h"
#include "ChatPage.h"
2018-05-01 19:35:28 +03:00
#include "Config.h"
#include "Utils.h"
2018-07-17 16:37:25 +03:00
#include "ui/Avatar.h"
2018-05-01 19:35:28 +03:00
using namespace dialogs;
MemberItem::MemberItem(const RoomMember &member, QWidget *parent)
: QWidget(parent)
{
topLayout_ = new QHBoxLayout(this);
topLayout_->setMargin(0);
textLayout_ = new QVBoxLayout;
textLayout_->setMargin(0);
textLayout_->setSpacing(0);
avatar_ = new Avatar(this, 44);
2018-05-01 19:35:28 +03:00
avatar_->setLetter(utils::firstChar(member.display_name));
avatar_->setImage(ChatPage::instance()->currentRoom(), member.user_id);
2018-05-01 19:35:28 +03:00
QFont nameFont;
nameFont.setPointSizeF(nameFont.pointSizeF() * 1.1);
2018-05-01 19:35:28 +03:00
userId_ = new QLabel(member.user_id, this);
2018-05-01 19:35:28 +03:00
userName_ = new QLabel(member.display_name, this);
userName_->setFont(nameFont);
textLayout_->addWidget(userName_);
textLayout_->addWidget(userId_);
topLayout_->addWidget(avatar_);
topLayout_->addLayout(textLayout_, 1);
}
2018-09-19 22:42:26 +03:00
void
MemberItem::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
2018-05-08 20:30:09 +03:00
MemberList::MemberList(const QString &room_id, QWidget *parent)
2018-05-01 19:35:28 +03:00
: QFrame(parent)
, room_id_{room_id}
{
2018-09-19 22:42:26 +03:00
setAutoFillBackground(true);
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
setWindowModality(Qt::WindowModal);
2018-05-01 19:35:28 +03:00
setAttribute(Qt::WA_DeleteOnClose, true);
auto layout = new QVBoxLayout(this);
layout->setSpacing(conf::modals::WIDGET_SPACING);
layout->setMargin(conf::modals::WIDGET_MARGIN);
2018-05-01 19:35:28 +03:00
list_ = new QListWidget;
list_->setFrameStyle(QFrame::NoFrame);
list_->setSelectionMode(QAbstractItemView::NoSelection);
list_->setSpacing(5);
2018-09-30 13:24:36 +03:00
QFont largeFont;
largeFont.setPointSizeF(largeFont.pointSizeF() * 1.5);
2018-08-11 17:55:44 +03:00
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
setMinimumHeight(list_->sizeHint().height() * 2);
setMinimumWidth(std::max(list_->sizeHint().width() + 4 * conf::modals::WIDGET_MARGIN,
2018-09-30 13:24:36 +03:00
QFontMetrics(largeFont).averageCharWidth() * 30 -
2018-08-11 17:55:44 +03:00
2 * conf::modals::WIDGET_MARGIN));
2018-05-01 19:35:28 +03:00
QFont font;
2018-07-22 20:01:12 +03:00
font.setPointSizeF(font.pointSizeF() * conf::modals::LABEL_MEDIUM_SIZE_RATIO);
2018-05-01 19:35:28 +03:00
topLabel_ = new QLabel(tr("Room members"), this);
topLabel_->setAlignment(Qt::AlignCenter);
topLabel_->setFont(font);
auto okBtn = new QPushButton(tr("OK"), this);
2018-09-21 13:56:39 +03:00
auto buttonLayout = new QHBoxLayout();
buttonLayout->setSpacing(15);
buttonLayout->addStretch(1);
buttonLayout->addWidget(okBtn);
2018-05-01 19:35:28 +03:00
layout->addWidget(topLabel_);
layout->addWidget(list_);
2018-09-21 13:56:39 +03:00
layout->addLayout(buttonLayout);
2018-05-01 19:35:28 +03:00
list_->clear();
connect(list_->verticalScrollBar(), &QAbstractSlider::valueChanged, this, [this](int pos) {
if (pos != list_->verticalScrollBar()->maximum())
return;
2018-05-01 19:35:28 +03:00
const size_t numMembers = list_->count() - 1;
if (numMembers > 0)
2019-12-15 04:56:04 +03:00
addUsers(cache::getMembers(room_id_.toStdString(), numMembers));
2018-05-01 19:35:28 +03:00
});
try {
2019-12-15 04:56:04 +03:00
addUsers(cache::getMembers(room_id_.toStdString()));
2018-05-01 19:35:28 +03:00
} catch (const lmdb::error &e) {
qCritical() << e.what();
}
2018-09-19 22:42:26 +03:00
auto closeShortcut = new QShortcut(QKeySequence(QKeySequence::Cancel), this);
2018-09-19 22:42:26 +03:00
connect(closeShortcut, &QShortcut::activated, this, &MemberList::close);
2018-09-21 13:56:39 +03:00
connect(okBtn, &QPushButton::clicked, this, &MemberList::close);
2018-05-01 19:35:28 +03:00
}
void
MemberList::addUsers(const std::vector<RoomMember> &members)
{
for (const auto &member : members) {
auto user = new MemberItem(member, this);
auto item = new QListWidgetItem;
item->setSizeHint(user->minimumSizeHint());
item->setFlags(Qt::NoItemFlags);
item->setTextAlignment(Qt::AlignCenter);
list_->insertItem(list_->count() - 1, item);
list_->setItemWidget(item, user);
}
}