Simplify timeline item creation

This commit is contained in:
Konstantinos Sideris 2017-11-30 12:55:30 +02:00
parent fdb76bb5c1
commit deb1a6e292
2 changed files with 36 additions and 54 deletions

View file

@ -90,16 +90,14 @@ public:
const QString &room_id, const QString &room_id,
QWidget *parent = 0); QWidget *parent = 0);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Image> &e, // For events with custom display widgets.
bool with_sender); template<class Event, class Widget>
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Notice> &e, TimelineItem *createTimelineItem(const Event &event, bool withSender);
bool with_sender);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Text> &e, // For events without custom display widgets.
bool with_sender); // TODO: All events should have custom widgets.
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::Emote> &e, template<class Event>
bool with_sender); TimelineItem *createTimelineItem(const Event &event, bool withSender);
TimelineItem *createTimelineItem(const events::MessageEvent<msgs::File> &e,
bool with_sender);
// Add new events at the end of the timeline. // Add new events at the end of the timeline.
int addEvents(const Timeline &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); PendingMessage message(MsgType, txn_id, url, filename, "", view_item);
handleNewUserMessage(message); handleNewUserMessage(message);
} }
template<class Event>
TimelineItem *
TimelineView::createTimelineItem(const Event &event, bool withSender)
{
TimelineItem *item = new TimelineItem(event, withSender, scroll_widget_);
return item;
}
template<class Event, class Widget>
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;
}

View file

@ -255,7 +255,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
updateLastSender(text.sender(), direction); updateLastSender(text.sender(), direction);
return createTimelineItem(text, with_sender); using Text = events::MessageEvent<msgs::Text>;
return createTimelineItem<Text>(text, with_sender);
} else if (msg_type == events::MessageEventType::Notice) { } else if (msg_type == events::MessageEventType::Notice) {
events::MessageEvent<msgs::Notice> notice; events::MessageEvent<msgs::Notice> notice;
@ -275,7 +276,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
updateLastSender(notice.sender(), direction); updateLastSender(notice.sender(), direction);
return createTimelineItem(notice, with_sender); using Notice = events::MessageEvent<msgs::Notice>;
return createTimelineItem<Notice>(notice, with_sender);
} else if (msg_type == events::MessageEventType::Image) { } else if (msg_type == events::MessageEventType::Image) {
events::MessageEvent<msgs::Image> img; events::MessageEvent<msgs::Image> img;
@ -302,7 +304,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
updateLastSender(img.sender(), direction); updateLastSender(img.sender(), direction);
return createTimelineItem(img, with_sender); using Image = events::MessageEvent<msgs::Image>;
return createTimelineItem<Image, ImageItem>(img, with_sender);
} else if (msg_type == events::MessageEventType::Emote) { } else if (msg_type == events::MessageEventType::Emote) {
events::MessageEvent<msgs::Emote> emote; events::MessageEvent<msgs::Emote> emote;
@ -329,7 +332,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
updateLastSender(emote.sender(), direction); updateLastSender(emote.sender(), direction);
return createTimelineItem(emote, with_sender); using Emote = events::MessageEvent<msgs::Emote>;
return createTimelineItem<Emote>(emote, with_sender);
} else if (msg_type == events::MessageEventType::File) { } else if (msg_type == events::MessageEventType::File) {
events::MessageEvent<msgs::File> file; events::MessageEvent<msgs::File> file;
@ -357,7 +361,8 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
updateLastSender(file.sender(), direction); updateLastSender(file.sender(), direction);
return createTimelineItem(file, withSender); using File = events::MessageEvent<msgs::File>;
return createTimelineItem<File, FileItem>(file, withSender);
} else if (msg_type == events::MessageEventType::Unknown) { } else if (msg_type == events::MessageEventType::Unknown) {
// TODO Handle redacted messages. // TODO Handle redacted messages.
// Silenced for now. // Silenced for now.
@ -487,45 +492,6 @@ TimelineView::isSenderRendered(const QString &user_id, TimelineDirection directi
return firstSender_ != user_id; return firstSender_ != user_id;
} }
TimelineItem *
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Image> &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<msgs::File> &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<msgs::Notice> &event, bool with_sender)
{
TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_);
return item;
}
TimelineItem *
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Text> &event, bool with_sender)
{
TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_);
return item;
}
TimelineItem *
TimelineView::createTimelineItem(const events::MessageEvent<msgs::Emote> &event, bool with_sender)
{
TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_);
return item;
}
void void
TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction) TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction)
{ {