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
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
}
}
}
}

View File

@ -1,7 +1,29 @@
#include "TimelineModel.h"
#include "Logging.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>
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<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
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);
}

View File

@ -1,12 +1,10 @@
#pragma once
#include <map>
#include <vector>
#include <QAbstractListModel>
#include <QColor>
#include <QHash>
#include <mtx/events/collections.hpp>
#include <mtx/responses.hpp>
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<QString, mtx::events::collections::TimelineEvents> events;
QHash<QString, mtx::events::collections::TimelineEvents> events;
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();
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::map<QString, mtx::responses::Ti
{
for (const auto &e : msgs) {
addRoom(e.first);
models.value(e.first)->addEvents(e.second);
}
}

View File

@ -7,6 +7,7 @@
#include <mtx/responses.hpp>
#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<QString> &event_ids) {}
@ -70,6 +82,7 @@ public slots:
private:
QQuickView *view;
QWidget *container;
TimelineModel *timeline_ = nullptr;
QHash<QString, QSharedPointer<TimelineModel>> models;
};