2018-05-01 19:35:28 +03:00
|
|
|
#include <QListWidgetItem>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "dialogs/MemberList.h"
|
|
|
|
|
2018-05-04 17:59:29 +03:00
|
|
|
#include "AvatarProvider.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "Cache.h"
|
2018-05-04 17:59:29 +03:00
|
|
|
#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"
|
|
|
|
#include "ui/FlatButton.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);
|
2018-07-22 18:03:12 +03:00
|
|
|
avatar_->setSize(46);
|
2018-05-01 19:35:28 +03:00
|
|
|
avatar_->setLetter(utils::firstChar(member.display_name));
|
|
|
|
|
|
|
|
if (!member.avatar.isNull())
|
|
|
|
avatar_->setImage(member.avatar);
|
2018-05-04 17:59:29 +03:00
|
|
|
else
|
|
|
|
AvatarProvider::resolve(ChatPage::instance()->currentRoom(),
|
|
|
|
member.user_id,
|
|
|
|
this,
|
|
|
|
[this](const QImage &img) { avatar_->setImage(img); });
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2018-07-22 18:03:12 +03:00
|
|
|
QFont nameFont;
|
2018-05-01 19:35:28 +03:00
|
|
|
nameFont.setWeight(65);
|
2018-07-22 18:03:12 +03:00
|
|
|
nameFont.setPointSizeF(nameFont.pointSizeF() * 1.1);
|
2018-05-01 19:35:28 +03:00
|
|
|
|
2018-07-22 18:03:12 +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-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-07-22 18:03:12 +03:00
|
|
|
QFont doubleFont;
|
|
|
|
doubleFont.setPointSizeF(doubleFont.pointSizeF() * 2);
|
|
|
|
setMinimumWidth(QFontMetrics(doubleFont).averageCharWidth() * 30);
|
|
|
|
setMinimumHeight(conf::modals::MIN_WIDGET_HEIGHT);
|
|
|
|
|
|
|
|
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
2018-05-01 19:35:28 +03:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose, true);
|
|
|
|
|
|
|
|
auto layout = new QVBoxLayout(this);
|
2018-07-22 18:03:12 +03:00
|
|
|
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_->setAttribute(Qt::WA_MacShowFocusRect, 0);
|
|
|
|
list_->setSpacing(5);
|
|
|
|
|
|
|
|
QFont font;
|
2018-07-22 18:03:12 +03:00
|
|
|
font.setPointSizeF(font.pointSizeF() * conf::modals::LABEL_BIG_SIZE_RATIO);
|
2018-05-01 19:35:28 +03:00
|
|
|
|
|
|
|
topLabel_ = new QLabel(tr("Room members"), this);
|
|
|
|
topLabel_->setAlignment(Qt::AlignCenter);
|
|
|
|
topLabel_->setFont(font);
|
|
|
|
|
|
|
|
layout->addWidget(topLabel_);
|
|
|
|
layout->addWidget(list_);
|
|
|
|
|
|
|
|
list_->clear();
|
|
|
|
|
|
|
|
// Add button at the bottom.
|
|
|
|
moreBtn_ = new FlatButton(tr("SHOW MORE"), this);
|
|
|
|
auto item = new QListWidgetItem;
|
|
|
|
item->setSizeHint(moreBtn_->minimumSizeHint());
|
|
|
|
item->setFlags(Qt::NoItemFlags);
|
|
|
|
item->setTextAlignment(Qt::AlignCenter);
|
|
|
|
list_->insertItem(0, item);
|
|
|
|
list_->setItemWidget(item, moreBtn_);
|
|
|
|
|
|
|
|
connect(moreBtn_, &FlatButton::clicked, this, [this]() {
|
|
|
|
const size_t numMembers = list_->count() - 1;
|
|
|
|
|
|
|
|
if (numMembers > 0)
|
2018-05-08 20:30:09 +03:00
|
|
|
addUsers(cache::client()->getMembers(room_id_.toStdString(), numMembers));
|
2018-05-01 19:35:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2018-05-08 20:30:09 +03:00
|
|
|
addUsers(cache::client()->getMembers(room_id_.toStdString()));
|
2018-05-01 19:35:28 +03:00
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MemberList::moveButtonToBottom()
|
|
|
|
{
|
|
|
|
auto item = new QListWidgetItem(list_);
|
|
|
|
item->setSizeHint(moreBtn_->minimumSizeHint());
|
|
|
|
item->setFlags(Qt::NoItemFlags);
|
|
|
|
item->setTextAlignment(Qt::AlignCenter);
|
|
|
|
list_->setItemWidget(item, moreBtn_);
|
|
|
|
list_->addItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MemberList::addUsers(const std::vector<RoomMember> &members)
|
|
|
|
{
|
|
|
|
if (members.size() == 0) {
|
|
|
|
moreBtn_->hide();
|
|
|
|
} else {
|
|
|
|
moreBtn_->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MemberList::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
QPainter p(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
|
|
|
}
|