From deb1a6e2925acb4ab339fc071e5ea58ab96b5bb4 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Thu, 30 Nov 2017 12:55:30 +0200 Subject: [PATCH] Simplify timeline item creation --- include/TimelineView.h | 36 ++++++++++++++++++++-------- src/TimelineView.cc | 54 ++++++++---------------------------------- 2 files changed, 36 insertions(+), 54 deletions(-) diff --git a/include/TimelineView.h b/include/TimelineView.h index 715d8a9a..5453ea5c 100644 --- a/include/TimelineView.h +++ b/include/TimelineView.h @@ -90,16 +90,14 @@ public: const QString &room_id, QWidget *parent = 0); - TimelineItem *createTimelineItem(const events::MessageEvent &e, - bool with_sender); - TimelineItem *createTimelineItem(const events::MessageEvent &e, - bool with_sender); - TimelineItem *createTimelineItem(const events::MessageEvent &e, - bool with_sender); - TimelineItem *createTimelineItem(const events::MessageEvent &e, - bool with_sender); - TimelineItem *createTimelineItem(const events::MessageEvent &e, - bool with_sender); + // For events with custom display widgets. + template + TimelineItem *createTimelineItem(const Event &event, bool withSender); + + // For events without custom display widgets. + // TODO: All events should have custom widgets. + template + TimelineItem *createTimelineItem(const Event &event, bool withSender); // Add new events at the end of the timeline. int addEvents(const Timeline &timeline); @@ -222,3 +220,21 @@ TimelineView::addUserMessage(const QString &url, const QString &filename) PendingMessage message(MsgType, txn_id, url, filename, "", view_item); handleNewUserMessage(message); } + +template +TimelineItem * +TimelineView::createTimelineItem(const Event &event, bool withSender) +{ + TimelineItem *item = new TimelineItem(event, withSender, scroll_widget_); + return item; +} + +template +TimelineItem * +TimelineView::createTimelineItem(const Event &event, bool withSender) +{ + auto eventWidget = new Widget(client_, event); + auto item = new TimelineItem(eventWidget, event, withSender, scroll_widget_); + + return item; +} diff --git a/src/TimelineView.cc b/src/TimelineView.cc index 346ecc52..e61307fa 100644 --- a/src/TimelineView.cc +++ b/src/TimelineView.cc @@ -255,7 +255,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire updateLastSender(text.sender(), direction); - return createTimelineItem(text, with_sender); + using Text = events::MessageEvent; + return createTimelineItem(text, with_sender); } else if (msg_type == events::MessageEventType::Notice) { events::MessageEvent notice; @@ -275,7 +276,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire updateLastSender(notice.sender(), direction); - return createTimelineItem(notice, with_sender); + using Notice = events::MessageEvent; + return createTimelineItem(notice, with_sender); } else if (msg_type == events::MessageEventType::Image) { events::MessageEvent img; @@ -302,7 +304,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire updateLastSender(img.sender(), direction); - return createTimelineItem(img, with_sender); + using Image = events::MessageEvent; + return createTimelineItem(img, with_sender); } else if (msg_type == events::MessageEventType::Emote) { events::MessageEvent emote; @@ -329,7 +332,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire updateLastSender(emote.sender(), direction); - return createTimelineItem(emote, with_sender); + using Emote = events::MessageEvent; + return createTimelineItem(emote, with_sender); } else if (msg_type == events::MessageEventType::File) { events::MessageEvent file; @@ -357,7 +361,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire updateLastSender(file.sender(), direction); - return createTimelineItem(file, withSender); + using File = events::MessageEvent; + return createTimelineItem(file, withSender); } else if (msg_type == events::MessageEventType::Unknown) { // TODO Handle redacted messages. // Silenced for now. @@ -487,45 +492,6 @@ TimelineView::isSenderRendered(const QString &user_id, TimelineDirection directi return firstSender_ != user_id; } -TimelineItem * -TimelineView::createTimelineItem(const events::MessageEvent &event, bool with_sender) -{ - auto image = new ImageItem(client_, event); - auto item = new TimelineItem(image, event, with_sender, scroll_widget_); - - return item; -} - -TimelineItem * -TimelineView::createTimelineItem(const events::MessageEvent &event, bool withSender) -{ - auto file = new FileItem(client_, event); - auto item = new TimelineItem(file, event, withSender, scroll_widget_); - - return item; -} - -TimelineItem * -TimelineView::createTimelineItem(const events::MessageEvent &event, bool with_sender) -{ - TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_); - return item; -} - -TimelineItem * -TimelineView::createTimelineItem(const events::MessageEvent &event, bool with_sender) -{ - TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_); - return item; -} - -TimelineItem * -TimelineView::createTimelineItem(const events::MessageEvent &event, bool with_sender) -{ - TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_); - return item; -} - void TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction) {