nheko/src/RoomInfoListItem.cpp

391 lines
13 KiB
C++
Raw Normal View History

2017-04-06 01:06:42 +02:00
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDateTime>
2017-04-09 02:54:39 +02:00
#include <QMouseEvent>
2017-05-19 02:28:15 +02:00
#include <QPainter>
#include <QSettings>
2019-09-04 04:59:08 +02:00
#include <QtGlobal>
2017-04-06 01:06:42 +02:00
#include "AvatarProvider.h"
2018-04-21 15:34:50 +02:00
#include "Cache.h"
#include "Config.h"
2017-04-06 01:06:42 +02:00
#include "RoomInfoListItem.h"
2020-01-31 06:12:02 +01:00
#include "Splitter.h"
2018-01-12 09:21:53 +01:00
#include "Utils.h"
2018-07-17 15:37:25 +02:00
#include "ui/Menu.h"
#include "ui/Ripple.h"
#include "ui/RippleOverlay.h"
2017-04-06 01:06:42 +02:00
constexpr int MaxUnreadCountDisplayed = 99;
2018-03-20 20:23:01 +01:00
struct WidgetMetrics
{
int maxHeight;
int iconSize;
int padding;
int unit;
int unreadLineWidth;
int unreadLineOffset;
int inviteBtnX;
int inviteBtnY;
};
WidgetMetrics
getMetrics(const QFont &font)
{
WidgetMetrics m;
const int height = QFontMetrics(font).lineSpacing();
m.unit = height;
m.maxHeight = std::ceil((double)height * 3.8);
m.iconSize = std::ceil((double)height * 2.8);
m.padding = std::ceil((double)height / 2.0);
m.unreadLineWidth = m.padding - m.padding / 3;
m.unreadLineOffset = m.padding - m.padding / 4;
m.inviteBtnX = m.iconSize + 2 * m.padding;
m.inviteBtnY = m.iconSize / 2.0 + m.padding + m.padding / 3.0;
return m;
}
2017-12-19 21:36:12 +01:00
void
RoomInfoListItem::init(QWidget *parent)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setMouseTracking(true);
setAttribute(Qt::WA_Hover);
2017-04-06 01:06:42 +02:00
2020-02-28 03:20:27 +01:00
auto wm = getMetrics(QFont{});
setFixedHeight(wm.maxHeight);
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
QPainterPath path;
path.addRect(0, 0, parent->width(), height());
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
ripple_overlay_ = new RippleOverlay(this);
ripple_overlay_->setClipPath(path);
ripple_overlay_->setClipping(true);
2020-02-28 03:20:27 +01:00
avatar_ = new Avatar(this, wm.iconSize);
avatar_->setLetter(utils::firstChar(roomName_));
avatar_->move(wm.padding, wm.padding);
unreadCountFont_.setPointSizeF(unreadCountFont_.pointSizeF() * 0.8);
unreadCountFont_.setBold(true);
2018-09-30 12:24:36 +02:00
bubbleDiameter_ = QFontMetrics(unreadCountFont_).averageCharWidth() * 3;
2018-04-21 15:34:50 +02:00
menu_ = new Menu(this);
leaveRoom_ = new QAction(tr("Leave room"), this);
connect(leaveRoom_, &QAction::triggered, this, [this]() { emit leaveRoom(roomId_); });
menu_->addAction(leaveRoom_);
2017-12-19 21:36:12 +01:00
}
RoomInfoListItem::RoomInfoListItem(QString room_id, const RoomInfo &info, QWidget *parent)
2017-12-19 21:36:12 +01:00
: QWidget(parent)
2018-04-21 15:34:50 +02:00
, roomType_{info.is_invite ? RoomType::Invited : RoomType::Joined}
, roomId_(std::move(room_id))
, roomName_{QString::fromStdString(std::move(info.name))}
, isPressed_(false)
, unreadMsgCount_(0)
, unreadHighlightedMsgCount_(0)
2017-12-19 21:36:12 +01:00
{
init(parent);
2017-05-19 02:28:15 +02:00
}
2017-04-06 01:06:42 +02:00
2017-08-20 12:47:22 +02:00
void
RoomInfoListItem::resizeEvent(QResizeEvent *)
{
2017-09-10 11:59:21 +02:00
// Update ripple's clipping path.
QPainterPath path;
path.addRect(0, 0, width(), height());
2020-01-31 06:12:02 +01:00
const auto sidebarSizes = splitter::calculateSidebarSizes(QFont{});
2018-10-07 13:09:47 +02:00
if (width() > sidebarSizes.small)
setToolTip("");
else
setToolTip(roomName_);
2017-09-10 11:59:21 +02:00
ripple_overlay_->setClipPath(path);
ripple_overlay_->setClipping(true);
}
2017-08-20 12:47:22 +02:00
void
RoomInfoListItem::paintEvent(QPaintEvent *event)
2017-05-19 02:28:15 +02:00
{
2017-09-10 11:59:21 +02:00
Q_UNUSED(event);
QPainter p(this);
p.setRenderHint(QPainter::TextAntialiasing);
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.setRenderHint(QPainter::Antialiasing);
QFontMetrics metrics(QFont{});
2017-09-10 11:59:21 +02:00
2017-12-19 21:36:12 +01:00
QPen titlePen(titleColor_);
QPen subtitlePen(subtitleColor_);
auto wm = getMetrics(QFont{});
if (isPressed_) {
p.fillRect(rect(), highlightedBackgroundColor_);
2017-12-19 21:36:12 +01:00
titlePen.setColor(highlightedTitleColor_);
subtitlePen.setColor(highlightedSubtitleColor_);
} else if (underMouse()) {
p.fillRect(rect(), hoverBackgroundColor_);
titlePen.setColor(hoverTitleColor_);
subtitlePen.setColor(hoverSubtitleColor_);
} else {
p.fillRect(rect(), backgroundColor_);
titlePen.setColor(titleColor_);
subtitlePen.setColor(subtitleColor_);
}
2017-09-10 11:59:21 +02:00
// Description line with the default font.
int bottom_y = wm.maxHeight - wm.padding - metrics.ascent() / 2;
2017-09-10 11:59:21 +02:00
2020-01-31 06:12:02 +01:00
const auto sidebarSizes = splitter::calculateSidebarSizes(QFont{});
2018-10-07 13:09:47 +02:00
if (width() > sidebarSizes.small) {
QFont headingFont;
headingFont.setWeight(QFont::Medium);
p.setFont(headingFont);
2017-12-19 21:36:12 +01:00
p.setPen(titlePen);
2017-09-10 11:59:21 +02:00
QFont tsFont;
tsFont.setPointSizeF(tsFont.pointSizeF() * 0.9);
2019-07-05 04:58:06 +02:00
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
const int msgStampWidth = QFontMetrics(tsFont).width(lastMsgInfo_.timestamp) + 4;
#else
2019-07-05 03:31:28 +02:00
const int msgStampWidth =
QFontMetrics(tsFont).horizontalAdvance(lastMsgInfo_.timestamp) + 4;
2019-07-05 04:58:06 +02:00
#endif
// We use the full width of the widget if there is no unread msg bubble.
const int bottomLineWidthLimit = (unreadMsgCount_ > 0) ? msgStampWidth : 0;
2017-09-10 11:59:21 +02:00
// Name line.
QFontMetrics fontNameMetrics(headingFont);
int top_y = 2 * wm.padding + fontNameMetrics.ascent() / 2;
2017-09-10 11:59:21 +02:00
const auto name = metrics.elidedText(
roomName(),
Qt::ElideRight,
(width() - wm.iconSize - 2 * wm.padding - msgStampWidth) * 0.8);
p.drawText(QPoint(2 * wm.padding + wm.iconSize, top_y), name);
2017-09-10 11:59:21 +02:00
2017-12-19 21:36:12 +01:00
if (roomType_ == RoomType::Joined) {
p.setFont(QFont{});
2017-12-19 21:36:12 +01:00
p.setPen(subtitlePen);
int descriptionLimit = std::max(
0, width() - 3 * wm.padding - bottomLineWidthLimit - wm.iconSize);
2017-12-19 21:36:12 +01:00
auto description =
metrics.elidedText(lastMsgInfo_.body, Qt::ElideRight, descriptionLimit);
p.drawText(QPoint(2 * wm.padding + wm.iconSize, bottom_y), description);
2017-12-19 21:36:12 +01:00
// We show the last message timestamp.
2018-03-18 22:38:04 +01:00
p.save();
if (isPressed_) {
2018-03-18 22:38:04 +01:00
p.setPen(QPen(highlightedTimestampColor_));
} else if (underMouse()) {
p.setPen(QPen(hoverTimestampColor_));
} else {
2018-03-18 22:38:04 +01:00
p.setPen(QPen(timestampColor_));
}
2018-03-18 22:38:04 +01:00
p.setFont(tsFont);
p.drawText(QPoint(width() - wm.padding - msgStampWidth, top_y),
lastMsgInfo_.timestamp);
2018-03-18 22:38:04 +01:00
p.restore();
2017-12-19 21:36:12 +01:00
} else {
int btnWidth = (width() - wm.iconSize - 6 * wm.padding) / 2;
acceptBtnRegion_ = QRectF(wm.inviteBtnX, wm.inviteBtnY, btnWidth, 20);
declineBtnRegion_ = QRectF(
wm.inviteBtnX + btnWidth + 2 * wm.padding, wm.inviteBtnY, btnWidth, 20);
2017-04-06 01:06:42 +02:00
2017-12-19 21:36:12 +01:00
QPainterPath acceptPath;
acceptPath.addRoundedRect(acceptBtnRegion_, 10, 10);
2017-12-19 21:36:12 +01:00
p.setPen(Qt::NoPen);
p.fillPath(acceptPath, btnColor_);
p.drawPath(acceptPath);
2017-12-19 21:36:12 +01:00
QPainterPath declinePath;
declinePath.addRoundedRect(declineBtnRegion_, 10, 10);
2017-12-19 21:36:12 +01:00
p.setPen(Qt::NoPen);
p.fillPath(declinePath, btnColor_);
p.drawPath(declinePath);
p.setPen(QPen(btnTextColor_));
p.setFont(QFont{});
p.drawText(acceptBtnRegion_,
Qt::AlignCenter,
metrics.elidedText(tr("Accept"), Qt::ElideRight, btnWidth));
p.drawText(declineBtnRegion_,
Qt::AlignCenter,
metrics.elidedText(tr("Decline"), Qt::ElideRight, btnWidth));
2017-09-10 11:59:21 +02:00
}
}
2017-09-10 11:59:21 +02:00
p.setPen(Qt::NoPen);
2017-09-10 11:59:21 +02:00
if (unreadMsgCount_ > 0) {
QBrush brush;
brush.setStyle(Qt::SolidPattern);
if (unreadHighlightedMsgCount_ > 0) {
brush.setColor(mentionedColor());
} else {
brush.setColor(bubbleBgColor());
}
2017-09-10 11:59:21 +02:00
if (isPressed_)
brush.setColor(bubbleFgColor());
2017-09-10 11:59:21 +02:00
p.setBrush(brush);
p.setPen(Qt::NoPen);
p.setFont(unreadCountFont_);
// Extra space on the x-axis to accomodate the extra character space
// inside the bubble.
const int x_width = unreadMsgCount_ > MaxUnreadCountDisplayed
? QFontMetrics(p.font()).averageCharWidth()
: 0;
QRectF r(width() - bubbleDiameter_ - wm.padding - x_width,
bottom_y - bubbleDiameter_ / 2 - 5,
bubbleDiameter_ + x_width,
bubbleDiameter_);
2017-09-10 11:59:21 +02:00
2018-10-07 13:09:47 +02:00
if (width() == sidebarSizes.small)
r = QRectF(width() - bubbleDiameter_ - 5,
height() - bubbleDiameter_ - 5,
bubbleDiameter_ + x_width,
bubbleDiameter_);
2017-09-10 11:59:21 +02:00
p.setPen(Qt::NoPen);
p.drawEllipse(r);
2017-04-06 01:06:42 +02:00
p.setPen(QPen(bubbleFgColor()));
2017-09-10 11:59:21 +02:00
if (isPressed_)
p.setPen(QPen(bubbleBgColor()));
2017-09-10 11:59:21 +02:00
auto countTxt = unreadMsgCount_ > MaxUnreadCountDisplayed
? QString("99+")
: QString::number(unreadMsgCount_);
2017-09-10 11:59:21 +02:00
p.setBrush(Qt::NoBrush);
p.drawText(r.translated(0, -0.5), Qt::AlignCenter, countTxt);
2017-09-10 11:59:21 +02:00
}
if (!isPressed_ && hasUnreadMessages_) {
QPen pen;
pen.setWidth(wm.unreadLineWidth);
pen.setColor(highlightedBackgroundColor_);
p.setPen(pen);
p.drawLine(0, wm.unreadLineOffset, 0, height() - wm.unreadLineOffset);
}
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RoomInfoListItem::updateUnreadMessageCount(int count, int highlightedCount)
{
unreadMsgCount_ = count;
unreadHighlightedMsgCount_ = highlightedCount;
2017-09-10 11:59:21 +02:00
update();
}
2017-08-20 12:47:22 +02:00
void
RoomInfoListItem::setPressedState(bool state)
2017-04-06 01:06:42 +02:00
{
2018-01-09 14:07:32 +01:00
if (isPressed_ != state) {
2017-09-10 11:59:21 +02:00
isPressed_ = state;
update();
}
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RoomInfoListItem::contextMenuEvent(QContextMenuEvent *event)
2017-05-31 18:42:07 +02:00
{
2017-09-10 11:59:21 +02:00
Q_UNUSED(event);
2017-05-31 18:42:07 +02:00
2017-12-19 21:36:12 +01:00
if (roomType_ == RoomType::Invited)
return;
2017-09-10 11:59:21 +02:00
menu_->popup(event->globalPos());
2017-05-31 18:42:07 +02:00
}
2017-08-20 12:47:22 +02:00
void
RoomInfoListItem::mousePressEvent(QMouseEvent *event)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
if (event->buttons() == Qt::RightButton) {
QWidget::mousePressEvent(event);
return;
}
2017-05-31 18:42:07 +02:00
2017-12-19 21:36:12 +01:00
if (roomType_ == RoomType::Invited) {
const auto point = event->pos();
if (acceptBtnRegion_.contains(point))
emit acceptInvite(roomId_);
if (declineBtnRegion_.contains(point))
emit declineInvite(roomId_);
return;
}
2017-09-10 11:59:21 +02:00
emit clicked(roomId_);
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
setPressedState(true);
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
// Ripple on mouse position by default.
QPoint pos = event->pos();
qreal radiusEndValue = static_cast<qreal>(width()) / 3;
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
Ripple *ripple = new Ripple(pos);
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
ripple->setRadiusEndValue(radiusEndValue);
ripple->setOpacityStartValue(0.15);
ripple->setColor(QColor("white"));
ripple->radiusAnimation()->setDuration(200);
ripple->opacityAnimation()->setDuration(400);
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
ripple_overlay_->addRipple(ripple);
2017-04-06 01:06:42 +02:00
}
2017-10-22 18:03:55 +02:00
void
RoomInfoListItem::setAvatar(const QString &avatar_url)
2017-10-22 18:03:55 +02:00
{
2020-02-28 03:20:27 +01:00
avatar_->setImage(avatar_url);
2017-10-22 18:03:55 +02:00
}
void
RoomInfoListItem::setDescriptionMessage(const DescInfo &info)
{
lastMsgInfo_ = info;
update();
}