From ddcc0f7f60d51f588afc0a4975fca038bc9392b9 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 10 Feb 2018 01:09:30 +0200 Subject: [PATCH] Generate a date separator before local messages --- include/timeline/TimelineView.h | 7 ++++--- src/timeline/TimelineView.cc | 25 +++++++++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/timeline/TimelineView.h b/include/timeline/TimelineView.h index f771a236..fb7c6485 100644 --- a/include/timeline/TimelineView.h +++ b/include/timeline/TimelineView.h @@ -93,7 +93,7 @@ public: const QSharedPointer data = QSharedPointer(nullptr)); void updatePendingMessage(int txn_id, QString event_id); void scrollDown(); - void addDateSeparator(QDateTime datetime, int position); + QLabel *createDateSeparator(QDateTime datetime); public slots: void sliderRangeChanged(int min, int max); @@ -135,7 +135,8 @@ private: //! Decides whether or not to show or hide the scroll down button. void toggleScrollDownButton(); void init(); - void addTimelineItem(TimelineItem *item, TimelineDirection direction); + void addTimelineItem(TimelineItem *item, + TimelineDirection direction = TimelineDirection::Bottom); void updateLastSender(const QString &user_id, TimelineDirection direction); void notifyForLastEvent(); void notifyForLastEvent(const TimelineEvent &event); @@ -245,7 +246,7 @@ TimelineView::addUserMessage(const QString &url, TimelineItem *view_item = new TimelineItem(widget, local_user_, with_sender, scroll_widget_); - pushTimelineItem(view_item); + addTimelineItem(view_item); lastMessageDirection_ = TimelineDirection::Bottom; diff --git a/src/timeline/TimelineView.cc b/src/timeline/TimelineView.cc index 253a4267..7ec5023b 100644 --- a/src/timeline/TimelineView.cc +++ b/src/timeline/TimelineView.cc @@ -450,8 +450,12 @@ TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction) if (lastItem) { auto oldDate = lastItem->descriptionMessage().datetime; - if (oldDate.daysTo(newDate) != 0) - addDateSeparator(newDate, lastItemPosition); + if (oldDate.daysTo(newDate) != 0) { + auto separator = createDateSeparator(newDate); + + if (separator) + scroll_layout_->addWidget(separator); + } } pushTimelineItem(item); @@ -465,8 +469,12 @@ TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction) if (firstItem) { auto oldDate = firstItem->descriptionMessage().datetime; - if (newDate.daysTo(oldDate) != 0) - addDateSeparator(oldDate, 1); + if (newDate.daysTo(oldDate) != 0) { + auto separator = createDateSeparator(oldDate); + + if (separator) + scroll_layout_->insertWidget(1, separator); + } } } @@ -501,7 +509,7 @@ TimelineView::addUserMessage(mtx::events::MessageType ty, const QString &body) TimelineItem *view_item = new TimelineItem(ty, local_user_, body, with_sender, scroll_widget_); - pushTimelineItem(view_item); + addTimelineItem(view_item); lastMessageDirection_ = TimelineDirection::Bottom; @@ -687,8 +695,8 @@ TimelineView::event(QEvent *event) return QWidget::event(event); } -void -TimelineView::addDateSeparator(QDateTime datetime, int position) +QLabel * +TimelineView::createDateSeparator(QDateTime datetime) { auto now = QDateTime::currentDateTime(); auto days = now.daysTo(datetime); @@ -713,8 +721,9 @@ TimelineView::addDateSeparator(QDateTime datetime, int position) QString("font-size: %1px").arg(conf::timeline::fonts::dateSeparator)); separator->setAlignment(Qt::AlignCenter); separator->setContentsMargins(0, 15, 0, 15); - scroll_layout_->insertWidget(position, separator); } + + return separator; } QString