From 47fbfd3f44154faf796c0be47dddfcba1b509a12 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 31 Aug 2019 22:43:31 +0200 Subject: [PATCH] Add items to timline --- resources/qml/TimelineView.qml | 19 +++++++--- src/timeline2/TimelineModel.cpp | 54 +++++++++++++++++++++++++-- src/timeline2/TimelineModel.h | 12 +++--- src/timeline2/TimelineViewManager.cpp | 10 +++-- src/timeline2/TimelineViewManager.h | 13 +++++++ 5 files changed, 89 insertions(+), 19 deletions(-) diff --git a/resources/qml/TimelineView.qml b/resources/qml/TimelineView.qml index a53c8a94..fcf88167 100644 --- a/resources/qml/TimelineView.qml +++ b/resources/qml/TimelineView.qml @@ -4,19 +4,28 @@ Rectangle { anchors.fill: parent Text { - visible: !timeline + visible: !timelineManager.timeline anchors.centerIn: parent text: qsTr("No room open") font.pointSize: 24 } + Text { + visible: timelineManager.timeline != null + anchors.centerIn: parent + text: qsTr("room open") + font.pointSize: 24 + } ListView { - visible: timeline != undefined + visible: timelineManager.timeline != null anchors.fill: parent - model: timeline + id: chat + + model: timelineManager.timeline delegate: Text { - text: userId + height: contentHeight + text: model.userId } - } + } } diff --git a/src/timeline2/TimelineModel.cpp b/src/timeline2/TimelineModel.cpp index 592064dd..b13a1e6a 100644 --- a/src/timeline2/TimelineModel.cpp +++ b/src/timeline2/TimelineModel.cpp @@ -1,7 +1,29 @@ #include "TimelineModel.h" +#include "Logging.h" #include "Utils.h" +namespace { +template +QString +eventId(const T &event) +{ + return QString::fromStdString(event.event_id); +} +template +QString +roomId(const T &event) +{ + return QString::fromStdString(event.room_id); +} +template +QString +senderId(const T &event) +{ + return QString::fromStdString(event.sender); +} +} + QHash TimelineModel::roleNames() const { @@ -18,12 +40,14 @@ int TimelineModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); + nhlog::ui()->info("current order size: {}", eventOrder.size()); return (int)this->eventOrder.size(); } QVariant TimelineModel::data(const QModelIndex &index, int role) const { + nhlog::ui()->info("data"); if (index.row() < 0 && index.row() >= (int)eventOrder.size()) return QVariant(); @@ -31,17 +55,39 @@ TimelineModel::data(const QModelIndex &index, int role) const switch (role) { case UserId: - return QVariant(QString("")); + return QVariant(boost::apply_visitor( + [](const auto &e) -> QString { return senderId(e); }, events.value(id))); default: return QVariant(); } } +void +TimelineModel::addEvents(const mtx::responses::Timeline &events) +{ + nhlog::ui()->info("add {} events", events.events.size()); + std::vector ids; + for (const auto &e : events.events) { + QString id = + boost::apply_visitor([](const auto &e) -> QString { return eventId(e); }, e); + + this->events.insert(id, e); + ids.push_back(id); + nhlog::ui()->info("add event {}", id.toStdString()); + } + + beginInsertRows(QModelIndex(), + static_cast(this->events.size()), + static_cast(this->events.size() + ids.size() - 1)); + this->eventOrder.insert(this->eventOrder.end(), ids.begin(), ids.end()); + endInsertRows(); +} + QColor TimelineModel::userColor(QString id, QColor background) { - if (!userColors.count(id)) + if (!userColors.contains(id)) userColors.insert( - {id, QColor(utils::generateContrastingHexColor(id, background.name()))}); - return userColors.at(id); + id, QColor(utils::generateContrastingHexColor(id, background.name()))); + return userColors.value(id); } diff --git a/src/timeline2/TimelineModel.h b/src/timeline2/TimelineModel.h index c281056d..2252621c 100644 --- a/src/timeline2/TimelineModel.h +++ b/src/timeline2/TimelineModel.h @@ -1,12 +1,10 @@ #pragma once -#include -#include - #include #include +#include -#include +#include class TimelineModel : public QAbstractListModel { @@ -33,10 +31,12 @@ public: Q_INVOKABLE QColor userColor(QString id, QColor background); + void addEvents(const mtx::responses::Timeline &events); + private: - std::map events; + QHash events; std::vector eventOrder; - std::map userColors; + QHash userColors; }; diff --git a/src/timeline2/TimelineViewManager.cpp b/src/timeline2/TimelineViewManager.cpp index 711dfcad..0468fc2a 100644 --- a/src/timeline2/TimelineViewManager.cpp +++ b/src/timeline2/TimelineViewManager.cpp @@ -10,6 +10,7 @@ TimelineViewManager::TimelineViewManager(QWidget *parent) view = new QQuickView(); container = QWidget::createWindowContainer(view, parent); container->setMinimumSize(200, 200); + view->rootContext()->setContextProperty("timelineManager", this); view->setSource(QUrl("qrc:///qml/TimelineView.qml")); } @@ -18,9 +19,8 @@ TimelineViewManager::initialize(const mtx::responses::Rooms &rooms) { for (auto it = rooms.join.cbegin(); it != rooms.join.cend(); ++it) { addRoom(QString::fromStdString(it->first)); + models.value(QString::fromStdString(it->first))->addEvents(it->second.timeline); } - - sync(rooms); } void @@ -37,8 +37,8 @@ TimelineViewManager::setHistoryView(const QString &room_id) auto room = models.find(room_id); if (room != models.end()) { - view->rootContext()->setContextProperty("timeline", - QVariant::fromValue(room.value().data())); + timeline_ = room.value().get(); + emit activeTimelineChanged(timeline_); nhlog::ui()->info("Activated room {}", room_id.toStdString()); } } @@ -48,5 +48,7 @@ TimelineViewManager::initWithMessages(const std::mapaddEvents(e.second); } } diff --git a/src/timeline2/TimelineViewManager.h b/src/timeline2/TimelineViewManager.h index 80948148..7f760eac 100644 --- a/src/timeline2/TimelineViewManager.h +++ b/src/timeline2/TimelineViewManager.h @@ -7,6 +7,7 @@ #include #include "Cache.h" +#include "Logging.h" #include "TimelineModel.h" #include "Utils.h" @@ -17,6 +18,10 @@ class TimelineViewManager : public QObject { Q_OBJECT + + Q_PROPERTY( + TimelineModel *timeline MEMBER timeline_ READ activeTimeline NOTIFY activeTimelineChanged) + public: TimelineViewManager(QWidget *parent = 0); QWidget *getWidget() const { return container; } @@ -27,9 +32,16 @@ public: void sync(const mtx::responses::Rooms &rooms) {} void clearAll() { models.clear(); } + Q_INVOKABLE TimelineModel *activeTimeline() const + { + nhlog::ui()->info("aaaa"); + return timeline_; + } + signals: void clearRoomMessageCount(QString roomid); void updateRoomsLastMessage(const QString &user, const DescInfo &info); + void activeTimelineChanged(TimelineModel *timeline); public slots: void updateReadReceipts(const QString &room_id, const std::vector &event_ids) {} @@ -70,6 +82,7 @@ public slots: private: QQuickView *view; QWidget *container; + TimelineModel *timeline_ = nullptr; QHash> models; };