forked from mirror/nheko
parent
8e611abe87
commit
8b5c7b2f2f
@ -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); |
||||||
|
} |
@ -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; |
||||||
|
}; |
||||||
|
|
@ -1,10 +1,52 @@ |
|||||||
#include "TimelineViewManager.h" |
#include "TimelineViewManager.h" |
||||||
|
|
||||||
|
#include <QMetaType> |
||||||
|
#include <QQmlContext> |
||||||
|
|
||||||
|
#include "Logging.h" |
||||||
|
|
||||||
TimelineViewManager::TimelineViewManager(QWidget *parent) |
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->setSource(QUrl("qrc:///qml/TimelineView.qml")); |
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); |
||||||
|
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue