From 8b5c7b2f2fb43b4f1884e683d60dd2553b9aa994 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Fri, 30 Aug 2019 23:20:53 +0200 Subject: [PATCH] Add placeholder timeline model --- CMakeLists.txt | 2 ++ resources/qml/TimelineView.qml | 11 +++++++ src/timeline2/TimelineModel.cpp | 47 +++++++++++++++++++++++++++ src/timeline2/TimelineModel.h | 42 ++++++++++++++++++++++++ src/timeline2/TimelineViewManager.cpp | 44 ++++++++++++++++++++++++- src/timeline2/TimelineViewManager.h | 15 ++++++--- 6 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 src/timeline2/TimelineModel.cpp create mode 100644 src/timeline2/TimelineModel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b9726dd8..8013fed9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,6 +192,7 @@ set(SRC_FILES # Timeline src/timeline2/TimelineViewManager.cpp + src/timeline2/TimelineModel.cpp #src/timeline/TimelineViewManager.cpp #src/timeline/TimelineItem.cpp #src/timeline/TimelineView.cpp @@ -335,6 +336,7 @@ qt5_wrap_cpp(MOC_HEADERS # Timeline src/timeline2/TimelineViewManager.h + src/timeline2/TimelineModel.h #src/timeline/TimelineItem.h #src/timeline/TimelineView.h #src/timeline/TimelineViewManager.h diff --git a/resources/qml/TimelineView.qml b/resources/qml/TimelineView.qml index d81e3ae1..a53c8a94 100644 --- a/resources/qml/TimelineView.qml +++ b/resources/qml/TimelineView.qml @@ -4,8 +4,19 @@ Rectangle { anchors.fill: parent Text { + visible: !timeline anchors.centerIn: parent text: qsTr("No room open") font.pointSize: 24 } + + ListView { + visible: timeline != undefined + anchors.fill: parent + + model: timeline + delegate: Text { + text: userId + } + } } diff --git a/src/timeline2/TimelineModel.cpp b/src/timeline2/TimelineModel.cpp new file mode 100644 index 00000000..592064dd --- /dev/null +++ b/src/timeline2/TimelineModel.cpp @@ -0,0 +1,47 @@ +#include "TimelineModel.h" + +#include "Utils.h" + +QHash +TimelineModel::roleNames() const +{ + return { + {Type, "type"}, + {Body, "body"}, + {FormattedBody, "formattedBody"}, + {UserId, "userId"}, + {UserName, "userName"}, + {Timestamp, "timestamp"}, + }; +} +int +TimelineModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return (int)this->eventOrder.size(); +} + +QVariant +TimelineModel::data(const QModelIndex &index, int role) const +{ + if (index.row() < 0 && index.row() >= (int)eventOrder.size()) + return QVariant(); + + QString id = eventOrder[index.row()]; + + switch (role) { + case UserId: + return QVariant(QString("")); + default: + return QVariant(); + } +} + +QColor +TimelineModel::userColor(QString id, QColor background) +{ + if (!userColors.count(id)) + userColors.insert( + {id, QColor(utils::generateContrastingHexColor(id, background.name()))}); + return userColors.at(id); +} diff --git a/src/timeline2/TimelineModel.h b/src/timeline2/TimelineModel.h new file mode 100644 index 00000000..c281056d --- /dev/null +++ b/src/timeline2/TimelineModel.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +#include +#include + +#include + +class TimelineModel : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit TimelineModel(QObject *parent = 0) + : QAbstractListModel(parent) + {} + + enum Roles + { + Type, + Body, + FormattedBody, + UserId, + UserName, + Timestamp, + }; + + QHash roleNames() const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + + Q_INVOKABLE QColor userColor(QString id, QColor background); + +private: + std::map events; + std::vector eventOrder; + + std::map userColors; +}; + diff --git a/src/timeline2/TimelineViewManager.cpp b/src/timeline2/TimelineViewManager.cpp index b932b6ac..711dfcad 100644 --- a/src/timeline2/TimelineViewManager.cpp +++ b/src/timeline2/TimelineViewManager.cpp @@ -1,10 +1,52 @@ #include "TimelineViewManager.h" +#include +#include + +#include "Logging.h" + TimelineViewManager::TimelineViewManager(QWidget *parent) { view = new QQuickView(); container = QWidget::createWindowContainer(view, parent); container->setMinimumSize(200, 200); view->setSource(QUrl("qrc:///qml/TimelineView.qml")); - // view->rootContext()->setContextProperty(room); +} + +void +TimelineViewManager::initialize(const mtx::responses::Rooms &rooms) +{ + for (auto it = rooms.join.cbegin(); it != rooms.join.cend(); ++it) { + addRoom(QString::fromStdString(it->first)); + } + + sync(rooms); +} + +void +TimelineViewManager::addRoom(const QString &room_id) +{ + if (!models.contains(room_id)) + models.insert(room_id, QSharedPointer(new TimelineModel())); +} + +void +TimelineViewManager::setHistoryView(const QString &room_id) +{ + nhlog::ui()->info("Trying to activate room {}", room_id.toStdString()); + + auto room = models.find(room_id); + if (room != models.end()) { + view->rootContext()->setContextProperty("timeline", + QVariant::fromValue(room.value().data())); + nhlog::ui()->info("Activated room {}", room_id.toStdString()); + } +} + +void +TimelineViewManager::initWithMessages(const std::map &msgs) +{ + for (const auto &e : msgs) { + addRoom(e.first); + } } diff --git a/src/timeline2/TimelineViewManager.h b/src/timeline2/TimelineViewManager.h index 23d30065..80948148 100644 --- a/src/timeline2/TimelineViewManager.h +++ b/src/timeline2/TimelineViewManager.h @@ -1,11 +1,13 @@ #pragma once #include +#include #include #include #include "Cache.h" +#include "TimelineModel.h" #include "Utils.h" // temporary for stubs @@ -19,11 +21,11 @@ public: TimelineViewManager(QWidget *parent = 0); QWidget *getWidget() const { return container; } - void initialize(const mtx::responses::Rooms &rooms) {} - void addRoom(const QString &room_id) {} + void initialize(const mtx::responses::Rooms &rooms); + void addRoom(const QString &room_id); void sync(const mtx::responses::Rooms &rooms) {} - void clearAll() {} + void clearAll() { models.clear(); } signals: void clearRoomMessageCount(QString roomid); @@ -32,9 +34,10 @@ signals: public slots: void updateReadReceipts(const QString &room_id, const std::vector &event_ids) {} void removeTimelineEvent(const QString &room_id, const QString &event_id) {} - void initWithMessages(const std::map &msgs) {} + void initWithMessages(const std::map &msgs); + + void setHistoryView(const QString &room_id); - void setHistoryView(const QString &room_id) {} void queueTextMessage(const QString &msg) {} void queueReplyMessage(const QString &reply, const RelatedInfo &related) {} void queueEmoteMessage(const QString &msg) {} @@ -67,6 +70,8 @@ public slots: private: QQuickView *view; QWidget *container; + + QHash> models; }; #pragma GCC diagnostic pop