nheko/src/RoomInfoListItem.cc

266 lines
6.4 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 <QDebug>
2017-04-09 02:54:39 +02:00
#include <QMouseEvent>
2017-05-19 02:28:15 +02:00
#include <QPainter>
2017-04-06 01:06:42 +02:00
#include "Config.h"
2017-04-06 01:06:42 +02:00
#include "Ripple.h"
#include "RoomInfoListItem.h"
#include "RoomState.h"
2017-05-19 18:55:38 +02:00
#include "Theme.h"
2017-04-06 01:06:42 +02:00
2017-05-31 18:42:07 +02:00
RoomInfoListItem::RoomInfoListItem(QSharedPointer<RoomSettings> settings,
RoomState state,
QString room_id,
QWidget *parent)
2017-04-06 01:06:42 +02:00
: QWidget(parent)
, state_(state)
2017-05-19 02:28:15 +02:00
, roomId_(room_id)
2017-05-31 18:42:07 +02:00
, roomSettings_{settings}
2017-05-19 02:28:15 +02:00
, isPressed_(false)
, maxHeight_(IconSize + 2 * Padding)
2017-05-19 02:28:15 +02:00
, unreadMsgCount_(0)
2017-04-06 01:06:42 +02:00
{
2017-05-19 02:28:15 +02:00
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
2017-05-19 02:45:46 +02:00
setMouseTracking(true);
setAttribute(Qt::WA_Hover);
2017-04-06 01:06:42 +02:00
2017-05-19 02:28:15 +02:00
setFixedHeight(maxHeight_);
2017-04-06 01:06:42 +02:00
2017-05-19 02:28:15 +02:00
QPainterPath path;
path.addRect(0, 0, parent->width(), height());
2017-04-06 01:06:42 +02:00
2017-05-19 02:28:15 +02:00
ripple_overlay_ = new RippleOverlay(this);
ripple_overlay_->setClipPath(path);
ripple_overlay_->setClipping(true);
2017-05-31 18:42:07 +02:00
menu_ = new Menu(this);
toggleNotifications_ = new QAction(notificationText(), this);
connect(toggleNotifications_, &QAction::triggered, this, [=]() {
roomSettings_->toggleNotifications();
});
menu_->addAction(toggleNotifications_);
}
QString RoomInfoListItem::notificationText()
{
if (roomSettings_.isNull() || roomSettings_->isNotificationsEnabled())
return QString(tr("Disable notifications"));
return tr("Enable notifications");
2017-05-19 02:28:15 +02:00
}
2017-04-06 01:06:42 +02:00
2017-05-19 02:28:15 +02:00
void RoomInfoListItem::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
2017-05-19 02:28:15 +02:00
QPainter p(this);
p.setRenderHint(QPainter::TextAntialiasing);
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.setRenderHint(QPainter::Antialiasing);
2017-04-06 01:06:42 +02:00
2017-05-19 02:28:15 +02:00
if (isPressed_)
p.fillRect(rect(), QColor("#38A3D8"));
2017-05-19 02:45:46 +02:00
else if (underMouse())
p.fillRect(rect(), QColor(200, 200, 200, 128));
2017-05-19 02:28:15 +02:00
else
p.fillRect(rect(), QColor("#F8FBFE"));
2017-04-06 01:06:42 +02:00
QFont font;
font.setPixelSize(conf::fontSize);
2017-05-19 02:28:15 +02:00
QFontMetrics metrics(font);
2017-05-21 21:52:38 +02:00
p.setPen(QColor("#333"));
2017-04-06 01:06:42 +02:00
2017-05-19 02:28:15 +02:00
QRect avatarRegion(Padding, Padding, IconSize, IconSize);
// Description line with the default font.
int bottom_y = maxHeight_ - Padding - Padding / 3 - metrics.ascent() / 2;
2017-05-19 18:55:38 +02:00
if (width() > ui::sidebar::SmallSize) {
if (isPressed_) {
QPen pen(QColor("white"));
p.setPen(pen);
}
font.setPixelSize(conf::roomlist::fonts::heading);
p.setFont(font);
// Name line.
QFontMetrics fontNameMetrics(font);
int top_y = 2 * Padding + fontNameMetrics.ascent() / 2;
auto name = metrics.elidedText(state_.getName(), Qt::ElideRight, (width() - IconSize - 2 * Padding) * 0.8);
p.drawText(QPoint(2 * Padding + IconSize, top_y), name);
2017-04-06 01:06:42 +02:00
2017-05-19 18:55:38 +02:00
if (!isPressed_) {
QPen pen(QColor("#5d6565"));
p.setPen(pen);
}
2017-04-06 01:06:42 +02:00
double descPercentage = 0.90;
2017-04-06 01:06:42 +02:00
2017-05-19 18:55:38 +02:00
if (unreadMsgCount_ > 0)
descPercentage = 0.8;
font.setPixelSize(conf::fontSize);
p.setFont(font);
auto description = metrics.elidedText(state_.getTopic(), Qt::ElideRight, width() * descPercentage - 2 * Padding - IconSize);
2017-05-19 18:55:38 +02:00
p.drawText(QPoint(2 * Padding + IconSize, bottom_y), description);
}
2017-04-06 01:06:42 +02:00
2017-05-19 02:28:15 +02:00
p.setPen(Qt::NoPen);
2017-04-06 01:06:42 +02:00
2017-05-19 18:55:38 +02:00
// We using the first letter of room's name.
if (roomAvatar_.isNull()) {
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor("#eee");
p.setPen(Qt::NoPen);
p.setBrush(brush);
p.drawEllipse(avatarRegion.center(), IconSize / 2, IconSize / 2);
font.setPixelSize(conf::roomlist::fonts::bubble);
2017-05-19 18:55:38 +02:00
p.setFont(font);
p.setPen(QColor("#333"));
p.setBrush(Qt::NoBrush);
p.drawText(avatarRegion.translated(0, -1), Qt::AlignCenter, QChar(state_.getName()[0]));
2017-05-19 18:55:38 +02:00
} else {
p.save();
QPainterPath path;
path.addEllipse(Padding, Padding, IconSize, IconSize);
p.setClipPath(path);
p.drawPixmap(avatarRegion, roomAvatar_);
p.restore();
}
2017-05-19 02:28:15 +02:00
if (unreadMsgCount_ > 0) {
QColor textColor("white");
QColor backgroundColor("#38A3D8");
2017-05-19 02:28:15 +02:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(backgroundColor);
if (isPressed_)
brush.setColor(textColor);
2017-04-06 01:06:42 +02:00
QFont unreadCountFont;
unreadCountFont.setPixelSize(conf::roomlist::fonts::badge);
unreadCountFont.setBold(true);
2017-05-19 02:28:15 +02:00
p.setBrush(brush);
p.setPen(Qt::NoPen);
p.setFont(unreadCountFont);
2017-05-19 02:28:15 +02:00
int diameter = 20;
QRectF r(width() - diameter - Padding, bottom_y - diameter / 2 - 5, diameter, diameter);
2017-05-19 18:55:38 +02:00
if (width() == ui::sidebar::SmallSize)
r = QRectF(width() - diameter - 5, height() - diameter - 5, diameter, diameter);
2017-05-19 02:28:15 +02:00
p.setPen(Qt::NoPen);
p.drawEllipse(r);
p.setPen(QPen(textColor));
if (isPressed_)
p.setPen(QPen(backgroundColor));
2017-05-19 02:28:15 +02:00
p.setBrush(Qt::NoBrush);
p.drawText(r.translated(0, -0.5), Qt::AlignCenter, QString::number(unreadMsgCount_));
}
2017-04-06 01:06:42 +02:00
}
void RoomInfoListItem::updateUnreadMessageCount(int count)
{
2017-05-19 02:28:15 +02:00
unreadMsgCount_ += count;
update();
}
void RoomInfoListItem::clearUnreadMessageCount()
{
2017-05-19 02:28:15 +02:00
unreadMsgCount_ = 0;
update();
}
2017-04-06 01:06:42 +02:00
void RoomInfoListItem::setPressedState(bool state)
{
2017-05-19 02:28:15 +02:00
if (!isPressed_ && state) {
isPressed_ = state;
update();
} else if (isPressed_ && !state) {
isPressed_ = state;
update();
2017-04-06 01:06:42 +02:00
}
}
void RoomInfoListItem::setState(const RoomState &new_state)
{
state_ = new_state;
update();
}
2017-05-31 18:42:07 +02:00
void RoomInfoListItem::contextMenuEvent(QContextMenuEvent *event)
{
Q_UNUSED(event);
toggleNotifications_->setText(notificationText());
menu_->popup(event->globalPos());
}
2017-04-06 01:06:42 +02:00
void RoomInfoListItem::mousePressEvent(QMouseEvent *event)
{
2017-05-31 18:42:07 +02:00
if (event->buttons() == Qt::RightButton) {
QWidget::mousePressEvent(event);
return;
}
2017-05-19 02:28:15 +02:00
emit clicked(roomId_);
2017-04-06 01:06:42 +02:00
setPressedState(true);
// Ripple on mouse position by default.
QPoint pos = event->pos();
2017-05-14 00:27:30 +02:00
qreal radiusEndValue = static_cast<qreal>(width()) / 3;
2017-04-06 01:06:42 +02:00
Ripple *ripple = new Ripple(pos);
ripple->setRadiusEndValue(radiusEndValue);
2017-04-14 14:13:09 +02:00
ripple->setOpacityStartValue(0.15);
2017-05-19 02:28:15 +02:00
ripple->setColor(QColor("white"));
ripple->radiusAnimation()->setDuration(200);
ripple->opacityAnimation()->setDuration(400);
2017-04-06 01:06:42 +02:00
ripple_overlay_->addRipple(ripple);
}
RoomInfoListItem::~RoomInfoListItem()
{
}