2018-01-09 16:07:32 +03:00
|
|
|
#include "CommunitiesList.h"
|
2018-05-08 18:43:56 +03:00
|
|
|
#include "MatrixClient.h"
|
2018-01-09 16:07:32 +03:00
|
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
2018-05-08 18:43:56 +03:00
|
|
|
CommunitiesList::CommunitiesList(QWidget *parent)
|
2018-01-09 16:07:32 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
|
|
|
|
sizePolicy.setHorizontalStretch(0);
|
|
|
|
sizePolicy.setVerticalStretch(1);
|
|
|
|
setSizePolicy(sizePolicy);
|
|
|
|
|
|
|
|
setStyleSheet("border-style: none;");
|
|
|
|
|
|
|
|
topLayout_ = new QVBoxLayout(this);
|
|
|
|
topLayout_->setSpacing(0);
|
|
|
|
topLayout_->setMargin(0);
|
|
|
|
|
|
|
|
setFixedWidth(ui::sidebar::CommunitiesSidebarSize);
|
|
|
|
|
|
|
|
scrollArea_ = new QScrollArea(this);
|
|
|
|
scrollArea_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
scrollArea_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
scrollArea_->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
|
|
|
|
scrollArea_->setWidgetResizable(true);
|
|
|
|
scrollArea_->setAlignment(Qt::AlignLeading | Qt::AlignTop | Qt::AlignVCenter);
|
|
|
|
|
|
|
|
scrollAreaContents_ = new QWidget();
|
|
|
|
|
|
|
|
contentsLayout_ = new QVBoxLayout(scrollAreaContents_);
|
|
|
|
contentsLayout_->setSpacing(0);
|
|
|
|
contentsLayout_->setMargin(0);
|
|
|
|
|
2018-04-28 15:27:12 +03:00
|
|
|
addGlobalItem();
|
2018-01-09 16:07:32 +03:00
|
|
|
contentsLayout_->addStretch(1);
|
|
|
|
|
|
|
|
scrollArea_->setWidget(scrollAreaContents_);
|
|
|
|
topLayout_->addWidget(scrollArea_);
|
|
|
|
|
2018-05-08 18:43:56 +03:00
|
|
|
connect(http::client(),
|
2018-01-09 16:07:32 +03:00
|
|
|
&MatrixClient::communityProfileRetrieved,
|
|
|
|
this,
|
2018-05-08 18:43:56 +03:00
|
|
|
[](QString communityId, QJsonObject profile) {
|
|
|
|
http::client()->fetchCommunityAvatar(
|
|
|
|
communityId, QUrl(profile["avatar_url"].toString()));
|
2018-01-09 16:07:32 +03:00
|
|
|
});
|
2018-05-08 18:43:56 +03:00
|
|
|
connect(http::client(),
|
2018-01-09 16:07:32 +03:00
|
|
|
SIGNAL(communityAvatarRetrieved(const QString &, const QPixmap &)),
|
|
|
|
this,
|
|
|
|
SLOT(updateCommunityAvatar(const QString &, const QPixmap &)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-24 21:46:37 +03:00
|
|
|
CommunitiesList::setCommunities(const std::map<QString, QSharedPointer<Community>> &communities)
|
2018-01-09 16:07:32 +03:00
|
|
|
{
|
|
|
|
communities_.clear();
|
|
|
|
|
2018-04-28 15:27:12 +03:00
|
|
|
addGlobalItem();
|
2018-01-09 16:07:32 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
for (const auto &community : communities) {
|
|
|
|
addCommunity(community.second, community.first);
|
2018-01-09 16:07:32 +03:00
|
|
|
|
2018-05-08 18:43:56 +03:00
|
|
|
http::client()->fetchCommunityProfile(community.first);
|
|
|
|
http::client()->fetchCommunityRooms(community.first);
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
|
2018-04-28 15:27:12 +03:00
|
|
|
communities_["world"]->setPressedState(true);
|
2018-01-09 16:07:32 +03:00
|
|
|
emit communityChanged("world");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CommunitiesList::addCommunity(QSharedPointer<Community> community, const QString &community_id)
|
|
|
|
{
|
|
|
|
CommunitiesListItem *list_item =
|
|
|
|
new CommunitiesListItem(community, community_id, scrollArea_);
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
communities_.emplace(community_id, QSharedPointer<CommunitiesListItem>(list_item));
|
2018-01-09 16:07:32 +03:00
|
|
|
|
2018-05-08 18:43:56 +03:00
|
|
|
http::client()->fetchCommunityAvatar(community_id, community->getAvatar());
|
2018-01-09 16:07:32 +03:00
|
|
|
|
|
|
|
contentsLayout_->insertWidget(contentsLayout_->count() - 1, list_item);
|
|
|
|
|
|
|
|
connect(list_item,
|
|
|
|
&CommunitiesListItem::clicked,
|
|
|
|
this,
|
|
|
|
&CommunitiesList::highlightSelectedCommunity);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CommunitiesList::updateCommunityAvatar(const QString &community_id, const QPixmap &img)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!communityExists(community_id)) {
|
2018-01-09 16:07:32 +03:00
|
|
|
qWarning() << "Avatar update on nonexistent community" << community_id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
communities_.find(community_id)->second->setAvatar(img.toImage());
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CommunitiesList::highlightSelectedCommunity(const QString &community_id)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!communityExists(community_id)) {
|
2018-01-09 16:07:32 +03:00
|
|
|
qDebug() << "CommunitiesList: clicked unknown community";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
emit communityChanged(community_id);
|
|
|
|
|
|
|
|
for (const auto &community : communities_) {
|
|
|
|
if (community.first != community_id) {
|
|
|
|
community.second->setPressedState(false);
|
2018-01-09 16:07:32 +03:00
|
|
|
} else {
|
2018-01-24 21:46:37 +03:00
|
|
|
community.second->setPressedState(true);
|
|
|
|
scrollArea_->ensureWidgetVisible(community.second.data());
|
2018-01-09 16:07:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|