Add date separator in the timeline

fixes #157
This commit is contained in:
Konstantinos Sideris 2017-12-25 23:02:33 +02:00
parent 6835a97b15
commit 1ab55d4732
6 changed files with 80 additions and 19 deletions

View file

@ -79,7 +79,8 @@ static constexpr int headerSpacing = 7;
static constexpr int headerLeftMargin = 15; static constexpr int headerLeftMargin = 15;
namespace fonts { namespace fonts {
static constexpr int timestamp = 10; static constexpr int timestamp = 10;
static constexpr int dateSeparator = conf::fontSize - 2;
} // namespace fonts } // namespace fonts
} // namespace timeline } // namespace timeline

View file

@ -18,6 +18,7 @@
#pragma once #pragma once
#include <QAction> #include <QAction>
#include <QDateTime>
#include <QSharedPointer> #include <QSharedPointer>
#include <QWidget> #include <QWidget>
@ -35,6 +36,7 @@ struct DescInfo
QString userid; QString userid;
QString body; QString body;
QString timestamp; QString timestamp;
QDateTime datetime;
}; };
class RoomInfoListItem : public QWidget class RoomInfoListItem : public QWidget

View file

@ -142,7 +142,7 @@ TimelineItem::setupLocalWidgetLayout(Widget *widget,
auto timestamp = QDateTime::currentDateTime(); auto timestamp = QDateTime::currentDateTime();
descriptionMsg_ = { descriptionMsg_ = {
"You", userid, QString(" %1").arg(msgDescription), descriptiveTime(timestamp)}; "You", userid, QString(" %1").arg(msgDescription), descriptiveTime(timestamp), timestamp};
generateTimestamp(timestamp); generateTimestamp(timestamp);
@ -183,7 +183,8 @@ TimelineItem::setupWidgetLayout(Widget *widget,
descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName, descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName,
sender, sender,
msgDescription, msgDescription,
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.origin_server_ts))}; descriptiveTime(timestamp),
timestamp};
generateTimestamp(timestamp); generateTimestamp(timestamp);

View file

@ -91,6 +91,7 @@ public:
void addUserMessage(const QString &url, const QString &filename); void addUserMessage(const QString &url, const QString &filename);
void updatePendingMessage(int txn_id, QString event_id); void updatePendingMessage(int txn_id, QString event_id);
void scrollDown(); void scrollDown();
void addDateSeparator(QDateTime datetime, int position);
public slots: public slots:
void sliderRangeChanged(int min, int max); void sliderRangeChanged(int min, int max);

View file

@ -73,10 +73,9 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
if (ty == mtx::events::MessageType::Emote) { if (ty == mtx::events::MessageType::Emote) {
body = QString("* %1 %2").arg(displayName).arg(body); body = QString("* %1 %2").arg(displayName).arg(body);
descriptionMsg_ = {"", userid, body, descriptiveTime(timestamp)}; descriptionMsg_ = {"", userid, body, descriptiveTime(timestamp), timestamp};
} else { } else {
descriptionMsg_ = { descriptionMsg_ = {"You: ", userid, body, descriptiveTime(timestamp), timestamp};
"You: ", userid, body, descriptiveTime(QDateTime::currentDateTime())};
} }
body = body.toHtmlEscaped(); body = body.toHtmlEscaped();
@ -189,16 +188,16 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
{ {
init(); init();
event_id_ = QString::fromStdString(event.event_id); event_id_ = QString::fromStdString(event.event_id);
const auto sender = QString::fromStdString(event.sender); const auto sender = QString::fromStdString(event.sender);
const auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
auto body = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
descriptionMsg_ = {TimelineViewManager::displayName(sender), descriptionMsg_ = {TimelineViewManager::displayName(sender),
sender, sender,
" sent a notification", " sent a notification",
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.origin_server_ts))}; descriptiveTime(timestamp),
timestamp};
auto body = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
generateTimestamp(timestamp); generateTimestamp(timestamp);
@ -241,10 +240,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
auto displayName = TimelineViewManager::displayName(sender); auto displayName = TimelineViewManager::displayName(sender);
auto emoteMsg = QString("* %1 %2").arg(displayName).arg(body); auto emoteMsg = QString("* %1 %2").arg(displayName).arg(body);
descriptionMsg_ = {"", descriptionMsg_ = {"", sender, emoteMsg, descriptiveTime(timestamp), timestamp};
sender,
emoteMsg,
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.origin_server_ts))};
generateTimestamp(timestamp); generateTimestamp(timestamp);
emoteMsg = emoteMsg.toHtmlEscaped(); emoteMsg = emoteMsg.toHtmlEscaped();
@ -286,7 +282,8 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName, descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName,
sender, sender,
QString(": %1").arg(body), QString(": %1").arg(body),
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.origin_server_ts))}; descriptiveTime(timestamp),
timestamp};
generateTimestamp(timestamp); generateTimestamp(timestamp);

View file

@ -19,6 +19,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QTimer> #include <QTimer>
#include "Config.h"
#include "FloatingButton.h" #include "FloatingButton.h"
#include "RoomMessages.h" #include "RoomMessages.h"
@ -368,10 +369,38 @@ TimelineView::isSenderRendered(const QString &user_id, TimelineDirection directi
void void
TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction) TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction)
{ {
if (direction == TimelineDirection::Bottom) const auto newDate = item->descriptionMessage().datetime;
if (direction == TimelineDirection::Bottom) {
const auto lastItemPosition = scroll_layout_->count() - 1;
auto lastItem =
static_cast<TimelineItem *>(scroll_layout_->itemAt(lastItemPosition)->widget());
if (lastItem) {
auto oldDate = lastItem->descriptionMessage().datetime;
if (oldDate.daysTo(newDate) != 0)
addDateSeparator(newDate, lastItemPosition);
}
scroll_layout_->addWidget(item); scroll_layout_->addWidget(item);
else } else {
// The first item (position 0) is a stretch widget that pushes
// the widgets to the bottom of the page.
if (scroll_layout_->count() > 1) {
auto firstItem =
static_cast<TimelineItem *>(scroll_layout_->itemAt(1)->widget());
if (firstItem) {
auto oldDate = firstItem->descriptionMessage().datetime;
if (newDate.daysTo(oldDate) != 0)
addDateSeparator(oldDate, 1);
}
}
scroll_layout_->insertWidget(1, item); scroll_layout_->insertWidget(1, item);
}
} }
void void
@ -563,6 +592,36 @@ TimelineView::event(QEvent *event)
return QWidget::event(event); return QWidget::event(event);
} }
void
TimelineView::addDateSeparator(QDateTime datetime, int position)
{
auto now = QDateTime::currentDateTime();
auto days = now.daysTo(datetime);
QString fmt;
QLabel *separator;
if (now.date().year() != datetime.date().year())
fmt = QString("ddd d MMMM yy");
else
fmt = QString("ddd d MMMM");
if (days == 0)
separator = new QLabel(tr("Today"));
else if (std::abs(days) == 1)
separator = new QLabel(tr("Yesterday"));
else
separator = new QLabel(datetime.toString(fmt));
if (separator) {
separator->setStyleSheet(
QString("font-size: %1px").arg(conf::timeline::fonts::dateSeparator));
separator->setAlignment(Qt::AlignCenter);
separator->setContentsMargins(0, 15, 0, 15);
scroll_layout_->insertWidget(position, separator);
}
}
QString QString
TimelineView::getEventSender(const mtx::events::collections::TimelineEvents &event) const TimelineView::getEventSender(const mtx::events::collections::TimelineEvents &event) const
{ {