nheko/src/RoomInfoListItem.cc

183 lines
5.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-04-06 01:06:42 +02:00
#include "Ripple.h"
#include "RoomInfoListItem.h"
#include "RoomState.h"
2017-04-06 01:06:42 +02:00
RoomInfoListItem::RoomInfoListItem(RoomState state, QString room_id, QWidget *parent)
2017-04-06 01:06:42 +02:00
: QWidget(parent)
, state_(state)
, room_id_(room_id)
2017-04-06 01:06:42 +02:00
, is_pressed_(false)
2017-04-10 00:34:53 +02:00
, max_height_(60)
, unread_msg_count_(0)
2017-04-06 01:06:42 +02:00
{
normal_style_ =
2017-04-14 14:13:09 +02:00
"QWidget { color: black; background-color: #f8fbfe}"
2017-04-06 01:06:42 +02:00
"QLabel { border: none; }";
pressed_style_ =
2017-04-14 14:13:09 +02:00
"QWidget { background-color: #acc7dc; color: black;}"
2017-04-06 01:06:42 +02:00
"QLabel { border: none; }";
setStyleSheet(normal_style_);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setAutoFillBackground(true);
setMaximumSize(parent->width(), max_height_);
2017-04-06 01:06:42 +02:00
QString room_name = state_.resolveName();
QString room_topic = state_.topic.content().topic().simplified();
2017-04-06 01:06:42 +02:00
topLayout_ = new QHBoxLayout(this);
topLayout_->setSpacing(0);
topLayout_->setMargin(0);
avatarWidget_ = new QWidget(this);
avatarWidget_->setMaximumSize(max_height_, max_height_);
textWidget_ = new QWidget(this);
avatarLayout_ = new QVBoxLayout(avatarWidget_);
avatarLayout_->setSpacing(0);
avatarLayout_->setContentsMargins(0, 5, 0, 5);
textLayout_ = new QVBoxLayout(textWidget_);
textLayout_->setSpacing(0);
textLayout_->setContentsMargins(0, 5, 0, 5);
roomAvatar_ = new Avatar(avatarWidget_);
roomAvatar_->setLetter(QChar(room_name[0]));
2017-04-10 00:34:53 +02:00
roomAvatar_->setSize(max_height_ - 20);
2017-04-14 14:13:09 +02:00
roomAvatar_->setTextColor("#555459");
roomAvatar_->setBackgroundColor("#d6dde3");
unreadMessagesBadge_ = new Badge(roomAvatar_);
unreadMessagesBadge_->setRelativePosition(12, 10);
unreadMessagesBadge_->setDiameter(5);
unreadMessagesBadge_->setBackgroundColor("#f8fbfe");
unreadMessagesBadge_->setTextColor("black");
// TODO: Initialize when nheko can restore messages from previous session.
unreadMessagesBadge_->hide();
2017-04-06 01:06:42 +02:00
avatarLayout_->addWidget(roomAvatar_);
roomName_ = new QLabel(room_name, textWidget_);
roomName_->setMaximumSize(parent->width() - max_height_, 20);
roomName_->setFont(QFont("Open Sans", 11));
2017-04-10 00:34:53 +02:00
roomName_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
2017-04-06 01:06:42 +02:00
roomTopic_ = new QLabel(room_topic, textWidget_);
roomTopic_->setMaximumSize(parent->width() - max_height_, 20);
roomTopic_->setFont(QFont("Open Sans", 10));
roomTopic_->setStyleSheet("color: #171919");
2017-04-10 00:34:53 +02:00
roomTopic_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
2017-04-06 01:06:42 +02:00
textLayout_->addWidget(roomName_);
textLayout_->addWidget(roomTopic_);
topLayout_->addWidget(avatarWidget_);
topLayout_->addWidget(textWidget_);
setElidedText(roomName_, room_name, parent->width() - max_height_);
setElidedText(roomTopic_, room_topic, parent->width() - max_height_);
2017-04-06 01:06:42 +02:00
QPainterPath path;
2017-05-14 00:27:30 +02:00
path.addRect(0, 0, parent->width(), max_height_);
2017-04-06 01:06:42 +02:00
ripple_overlay_ = new RippleOverlay(this);
ripple_overlay_->setClipPath(path);
ripple_overlay_->setClipping(true);
setLayout(topLayout_);
}
void RoomInfoListItem::updateUnreadMessageCount(int count)
{
unread_msg_count_ += count;
unreadMessagesBadge_->setText(QString::number(unread_msg_count_));
unreadMessagesBadge_->show();
}
void RoomInfoListItem::clearUnreadMessageCount()
{
unread_msg_count_ = 0;
unreadMessagesBadge_->setText("");
unreadMessagesBadge_->hide();
}
2017-04-06 01:06:42 +02:00
void RoomInfoListItem::setPressedState(bool state)
{
if (!is_pressed_ && state) {
is_pressed_ = state;
setStyleSheet(pressed_style_);
} else if (is_pressed_ && !state) {
is_pressed_ = state;
setStyleSheet(normal_style_);
}
}
void RoomInfoListItem::setState(const RoomState &new_state)
{
if (state_.resolveName() != new_state.resolveName())
setElidedText(roomName_, new_state.resolveName(), parentWidget()->width() - max_height_);
if (state_.resolveTopic() != new_state.resolveTopic())
setElidedText(roomTopic_, new_state.resolveTopic(), parentWidget()->width() - max_height_);
if (new_state.avatar.content().url().toString().isEmpty())
roomAvatar_->setLetter(QChar(new_state.resolveName()[0]));
state_ = new_state;
}
2017-04-06 01:06:42 +02:00
void RoomInfoListItem::mousePressEvent(QMouseEvent *event)
{
emit clicked(room_id_);
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-14 00:27:30 +02:00
ripple->setColor(QColor("#052B49"));
2017-04-06 01:06:42 +02:00
ripple->radiusAnimation()->setDuration(300);
ripple->opacityAnimation()->setDuration(500);
ripple_overlay_->addRipple(ripple);
}
void RoomInfoListItem::setElidedText(QLabel *label, QString text, int width)
{
QFontMetrics metrics(label->font());
QString elidedText = metrics.elidedText(text, Qt::ElideRight, width);
label->setText(elidedText);
}
RoomInfoListItem::~RoomInfoListItem()
{
}