matrixion/src/CommunitiesListItem.cpp

201 lines
6.2 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2018-01-09 16:07:32 +03:00
#include "CommunitiesListItem.h"
2020-01-31 08:12:02 +03:00
#include <QMenu>
2020-01-31 08:12:02 +03:00
#include <QMouseEvent>
2018-04-30 21:41:47 +03:00
#include "Utils.h"
2018-07-17 16:37:25 +03:00
#include "ui/Painter.h"
#include "ui/Ripple.h"
#include "ui/RippleOverlay.h"
2018-01-09 16:07:32 +03:00
2018-07-14 12:08:16 +03:00
CommunitiesListItem::CommunitiesListItem(QString group_id, QWidget *parent)
2018-01-09 16:07:32 +03:00
: QWidget(parent)
2018-07-14 12:08:16 +03:00
, groupId_(group_id)
2018-01-09 16:07:32 +03:00
{
setMouseTracking(true);
setAttribute(Qt::WA_Hover);
2018-01-09 16:07:32 +03:00
QPainterPath path;
path.addRect(0, 0, parent->width(), height());
rippleOverlay_ = new RippleOverlay(this);
rippleOverlay_->setClipPath(path);
rippleOverlay_->setClipping(true);
menu_ = new QMenu(this);
hideRoomsWithTagAction_ =
new QAction(tr("Hide rooms with this tag or from this community"), this);
hideRoomsWithTagAction_->setCheckable(true);
menu_->addAction(hideRoomsWithTagAction_);
connect(menu_, &QMenu::aboutToShow, this, [this]() {
hideRoomsWithTagAction_->setChecked(isDisabled_);
});
connect(hideRoomsWithTagAction_, &QAction::triggered, this, [this](bool checked) {
this->setDisabled(checked);
});
updateTooltip();
}
void
CommunitiesListItem::contextMenuEvent(QContextMenuEvent *event)
{
menu_->popup(event->globalPos());
}
void
CommunitiesListItem::setName(QString name)
{
name_ = name;
updateTooltip();
2018-01-09 16:07:32 +03:00
}
void
CommunitiesListItem::setPressedState(bool state)
{
if (isPressed_ != state) {
isPressed_ = state;
update();
}
}
void
CommunitiesListItem::setDisabled(bool state)
{
if (isDisabled_ != state) {
isDisabled_ = state;
update();
emit isDisabledChanged();
}
}
2018-01-09 16:07:32 +03:00
void
CommunitiesListItem::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::RightButton) {
QWidget::mousePressEvent(event);
return;
}
2018-07-14 12:08:16 +03:00
emit clicked(groupId_);
2018-01-09 16:07:32 +03:00
setPressedState(true);
QPoint pos = event->pos();
qreal radiusEndValue = static_cast<qreal>(width()) / 3;
auto ripple = new Ripple(pos);
ripple->setRadiusEndValue(radiusEndValue);
ripple->setOpacityStartValue(0.15);
ripple->setColor("white");
ripple->radiusAnimation()->setDuration(200);
ripple->opacityAnimation()->setDuration(400);
rippleOverlay_->addRipple(ripple);
2018-01-09 16:07:32 +03:00
}
void
CommunitiesListItem::paintEvent(QPaintEvent *)
2018-01-09 16:07:32 +03:00
{
Painter p(this);
PainterHighQualityEnabler hq(p);
2018-01-09 16:07:32 +03:00
if (isPressed_)
p.fillRect(rect(), highlightedBackgroundColor_);
else if (isDisabled_)
p.fillRect(rect(), disabledBackgroundColor_);
2018-01-09 16:07:32 +03:00
else if (underMouse())
p.fillRect(rect(), hoverBackgroundColor_);
else
p.fillRect(rect(), backgroundColor_);
2018-07-14 12:08:16 +03:00
if (avatar_.isNull()) {
QPixmap source;
if (groupId_ == "world")
source = QPixmap(":/icons/icons/ui/world.png");
else if (groupId_ == "tag:m.favourite")
source = QPixmap(":/icons/icons/ui/star.png");
else if (groupId_ == "tag:m.lowpriority")
source = QPixmap(":/icons/icons/ui/lowprio.png");
else if (groupId_.startsWith("tag:"))
source = QPixmap(":/icons/icons/ui/tag.png");
if (source.isNull()) {
QFont font;
font.setPointSizeF(font.pointSizeF() * 1.3);
p.setFont(font);
p.drawLetterAvatar(utils::firstChar(resolveName()),
avatarFgColor_,
avatarBgColor_,
width(),
height(),
IconSize);
} else {
QPainter painter(&source);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.fillRect(source.rect(), avatarFgColor_);
painter.end();
const int imageSz = 32;
p.drawPixmap(
QRect(
(width() - imageSz) / 2, (height() - imageSz) / 2, imageSz, imageSz),
source);
}
2018-01-09 16:07:32 +03:00
} else {
p.save();
2018-07-14 12:08:16 +03:00
p.drawAvatar(avatar_, width(), height(), IconSize);
2018-01-09 16:07:32 +03:00
p.restore();
}
}
void
CommunitiesListItem::setAvatar(const QImage &img)
{
2018-07-14 12:08:16 +03:00
avatar_ = utils::scaleImageToPixmap(img, IconSize);
update();
}
2018-07-14 12:08:16 +03:00
QString
CommunitiesListItem::resolveName() const
{
if (!name_.isEmpty())
return name_;
if (groupId_.startsWith("tag:"))
2020-12-25 03:08:06 +03:00
return groupId_.right(static_cast<int>(groupId_.size() - strlen("tag:")));
2018-07-14 12:08:16 +03:00
if (!groupId_.startsWith("+"))
return QString("Group"); // Group with no name or id.
// Extract the localpart of the group.
auto firstPart = groupId_.split(':').at(0);
return firstPart.right(firstPart.size() - 1);
}
void
CommunitiesListItem::updateTooltip()
{
if (groupId_ == "world")
setToolTip(tr("All rooms"));
else if (is_tag()) {
2020-12-25 03:08:06 +03:00
QString tag = groupId_.right(static_cast<int>(groupId_.size() - strlen("tag:")));
if (tag == "m.favourite")
setToolTip(tr("Favourite rooms"));
else if (tag == "m.lowpriority")
setToolTip(tr("Low priority rooms"));
2020-05-18 04:30:04 +03:00
else if (tag == "m.server_notice")
setToolTip(tr("Server Notices", "Tag translation for m.server_notice"));
else if (tag.startsWith("u."))
setToolTip(tag.right(tag.size() - 2) + tr(" (tag)"));
else
setToolTip(tag + tr(" (tag)"));
} else {
QString name = resolveName();
setToolTip(name + tr(" (community)"));
}
2018-09-30 13:24:36 +03:00
}