Add items to timline

This commit is contained in:
Nicolas Werner 2019-08-31 22:43:31 +02:00
parent 8b5c7b2f2f
commit 47fbfd3f44
5 changed files with 89 additions and 19 deletions

View file

@ -4,19 +4,28 @@ Rectangle {
anchors.fill: parent anchors.fill: parent
Text { Text {
visible: !timeline visible: !timelineManager.timeline
anchors.centerIn: parent anchors.centerIn: parent
text: qsTr("No room open") text: qsTr("No room open")
font.pointSize: 24 font.pointSize: 24
} }
Text {
visible: timelineManager.timeline != null
anchors.centerIn: parent
text: qsTr("room open")
font.pointSize: 24
}
ListView { ListView {
visible: timeline != undefined visible: timelineManager.timeline != null
anchors.fill: parent anchors.fill: parent
model: timeline id: chat
model: timelineManager.timeline
delegate: Text { delegate: Text {
text: userId height: contentHeight
text: model.userId
} }
} }
} }

View file

@ -1,7 +1,29 @@
#include "TimelineModel.h" #include "TimelineModel.h"
#include "Logging.h"
#include "Utils.h" #include "Utils.h"
namespace {
template<class T>
QString
eventId(const T &event)
{
return QString::fromStdString(event.event_id);
}
template<class T>
QString
roomId(const T &event)
{
return QString::fromStdString(event.room_id);
}
template<class T>
QString
senderId(const T &event)
{
return QString::fromStdString(event.sender);
}
}
QHash<int, QByteArray> QHash<int, QByteArray>
TimelineModel::roleNames() const TimelineModel::roleNames() const
{ {
@ -18,12 +40,14 @@ int
TimelineModel::rowCount(const QModelIndex &parent) const TimelineModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
nhlog::ui()->info("current order size: {}", eventOrder.size());
return (int)this->eventOrder.size(); return (int)this->eventOrder.size();
} }
QVariant QVariant
TimelineModel::data(const QModelIndex &index, int role) const TimelineModel::data(const QModelIndex &index, int role) const
{ {
nhlog::ui()->info("data");
if (index.row() < 0 && index.row() >= (int)eventOrder.size()) if (index.row() < 0 && index.row() >= (int)eventOrder.size())
return QVariant(); return QVariant();
@ -31,17 +55,39 @@ TimelineModel::data(const QModelIndex &index, int role) const
switch (role) { switch (role) {
case UserId: case UserId:
return QVariant(QString("")); return QVariant(boost::apply_visitor(
[](const auto &e) -> QString { return senderId(e); }, events.value(id)));
default: default:
return QVariant(); return QVariant();
} }
} }
void
TimelineModel::addEvents(const mtx::responses::Timeline &events)
{
nhlog::ui()->info("add {} events", events.events.size());
std::vector<QString> 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<int>(this->events.size()),
static_cast<int>(this->events.size() + ids.size() - 1));
this->eventOrder.insert(this->eventOrder.end(), ids.begin(), ids.end());
endInsertRows();
}
QColor QColor
TimelineModel::userColor(QString id, QColor background) TimelineModel::userColor(QString id, QColor background)
{ {
if (!userColors.count(id)) if (!userColors.contains(id))
userColors.insert( userColors.insert(
{id, QColor(utils::generateContrastingHexColor(id, background.name()))}); id, QColor(utils::generateContrastingHexColor(id, background.name())));
return userColors.at(id); return userColors.value(id);
} }

View file

@ -1,12 +1,10 @@
#pragma once #pragma once
#include <map>
#include <vector>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QColor> #include <QColor>
#include <QHash>
#include <mtx/events/collections.hpp> #include <mtx/responses.hpp>
class TimelineModel : public QAbstractListModel class TimelineModel : public QAbstractListModel
{ {
@ -33,10 +31,12 @@ public:
Q_INVOKABLE QColor userColor(QString id, QColor background); Q_INVOKABLE QColor userColor(QString id, QColor background);
void addEvents(const mtx::responses::Timeline &events);
private: private:
std::map<QString, mtx::events::collections::TimelineEvents> events; QHash<QString, mtx::events::collections::TimelineEvents> events;
std::vector<QString> eventOrder; std::vector<QString> eventOrder;
std::map<QString, QColor> userColors; QHash<QString, QColor> userColors;
}; };

View file

@ -10,6 +10,7 @@ TimelineViewManager::TimelineViewManager(QWidget *parent)
view = new QQuickView(); view = new QQuickView();
container = QWidget::createWindowContainer(view, parent); container = QWidget::createWindowContainer(view, parent);
container->setMinimumSize(200, 200); container->setMinimumSize(200, 200);
view->rootContext()->setContextProperty("timelineManager", this);
view->setSource(QUrl("qrc:///qml/TimelineView.qml")); 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) { for (auto it = rooms.join.cbegin(); it != rooms.join.cend(); ++it) {
addRoom(QString::fromStdString(it->first)); addRoom(QString::fromStdString(it->first));
models.value(QString::fromStdString(it->first))->addEvents(it->second.timeline);
} }
sync(rooms);
} }
void void
@ -37,8 +37,8 @@ TimelineViewManager::setHistoryView(const QString &room_id)
auto room = models.find(room_id); auto room = models.find(room_id);
if (room != models.end()) { if (room != models.end()) {
view->rootContext()->setContextProperty("timeline", timeline_ = room.value().get();
QVariant::fromValue(room.value().data())); emit activeTimelineChanged(timeline_);
nhlog::ui()->info("Activated room {}", room_id.toStdString()); nhlog::ui()->info("Activated room {}", room_id.toStdString());
} }
} }
@ -48,5 +48,7 @@ TimelineViewManager::initWithMessages(const std::map<QString, mtx::responses::Ti
{ {
for (const auto &e : msgs) { for (const auto &e : msgs) {
addRoom(e.first); addRoom(e.first);
models.value(e.first)->addEvents(e.second);
} }
} }

View file

@ -7,6 +7,7 @@
#include <mtx/responses.hpp> #include <mtx/responses.hpp>
#include "Cache.h" #include "Cache.h"
#include "Logging.h"
#include "TimelineModel.h" #include "TimelineModel.h"
#include "Utils.h" #include "Utils.h"
@ -17,6 +18,10 @@
class TimelineViewManager : public QObject class TimelineViewManager : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(
TimelineModel *timeline MEMBER timeline_ READ activeTimeline NOTIFY activeTimelineChanged)
public: public:
TimelineViewManager(QWidget *parent = 0); TimelineViewManager(QWidget *parent = 0);
QWidget *getWidget() const { return container; } QWidget *getWidget() const { return container; }
@ -27,9 +32,16 @@ public:
void sync(const mtx::responses::Rooms &rooms) {} void sync(const mtx::responses::Rooms &rooms) {}
void clearAll() { models.clear(); } void clearAll() { models.clear(); }
Q_INVOKABLE TimelineModel *activeTimeline() const
{
nhlog::ui()->info("aaaa");
return timeline_;
}
signals: signals:
void clearRoomMessageCount(QString roomid); void clearRoomMessageCount(QString roomid);
void updateRoomsLastMessage(const QString &user, const DescInfo &info); void updateRoomsLastMessage(const QString &user, const DescInfo &info);
void activeTimelineChanged(TimelineModel *timeline);
public slots: public slots:
void updateReadReceipts(const QString &room_id, const std::vector<QString> &event_ids) {} void updateReadReceipts(const QString &room_id, const std::vector<QString> &event_ids) {}
@ -70,6 +82,7 @@ public slots:
private: private:
QQuickView *view; QQuickView *view;
QWidget *container; QWidget *container;
TimelineModel *timeline_ = nullptr;
QHash<QString, QSharedPointer<TimelineModel>> models; QHash<QString, QSharedPointer<TimelineModel>> models;
}; };