Add placeholder timeline model

This commit is contained in:
Nicolas Werner 2019-08-30 23:20:53 +02:00
parent 8e611abe87
commit 8b5c7b2f2f
6 changed files with 155 additions and 6 deletions

View file

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

View file

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

View file

@ -0,0 +1,47 @@
#include "TimelineModel.h"
#include "Utils.h"
QHash<int, QByteArray>
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);
}

View file

@ -0,0 +1,42 @@
#pragma once
#include <map>
#include <vector>
#include <QAbstractListModel>
#include <QColor>
#include <mtx/events/collections.hpp>
class TimelineModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit TimelineModel(QObject *parent = 0)
: QAbstractListModel(parent)
{}
enum Roles
{
Type,
Body,
FormattedBody,
UserId,
UserName,
Timestamp,
};
QHash<int, QByteArray> 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<QString, mtx::events::collections::TimelineEvents> events;
std::vector<QString> eventOrder;
std::map<QString, QColor> userColors;
};

View file

@ -1,10 +1,52 @@
#include "TimelineViewManager.h"
#include <QMetaType>
#include <QQmlContext>
#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<TimelineModel>(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<QString, mtx::responses::Timeline> &msgs)
{
for (const auto &e : msgs) {
addRoom(e.first);
}
}

View file

@ -1,11 +1,13 @@
#pragma once
#include <QQuickView>
#include <QSharedPointer>
#include <QWidget>
#include <mtx/responses.hpp>
#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<QString> &event_ids) {}
void removeTimelineEvent(const QString &room_id, const QString &event_id) {}
void initWithMessages(const std::map<QString, mtx::responses::Timeline> &msgs) {}
void initWithMessages(const std::map<QString, mtx::responses::Timeline> &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<QString, QSharedPointer<TimelineModel>> models;
};
#pragma GCC diagnostic pop