matrixion/src/timeline2/TimelineModel.cpp

94 lines
2.4 KiB
C++
Raw Normal View History

2019-08-31 00:20:53 +03:00
#include "TimelineModel.h"
2019-08-31 23:43:31 +03:00
#include "Logging.h"
2019-08-31 00:20:53 +03:00
#include "Utils.h"
2019-08-31 23:43:31 +03:00
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);
}
}
2019-08-31 00:20:53 +03:00
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);
2019-08-31 23:43:31 +03:00
nhlog::ui()->info("current order size: {}", eventOrder.size());
2019-08-31 00:20:53 +03:00
return (int)this->eventOrder.size();
}
QVariant
TimelineModel::data(const QModelIndex &index, int role) const
{
2019-08-31 23:43:31 +03:00
nhlog::ui()->info("data");
2019-08-31 00:20:53 +03:00
if (index.row() < 0 && index.row() >= (int)eventOrder.size())
return QVariant();
QString id = eventOrder[index.row()];
switch (role) {
case UserId:
2019-08-31 23:43:31 +03:00
return QVariant(boost::apply_visitor(
[](const auto &e) -> QString { return senderId(e); }, events.value(id)));
2019-08-31 00:20:53 +03:00
default:
return QVariant();
}
}
2019-08-31 23:43:31 +03:00
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();
}
2019-08-31 00:20:53 +03:00
QColor
TimelineModel::userColor(QString id, QColor background)
{
2019-08-31 23:43:31 +03:00
if (!userColors.contains(id))
2019-08-31 00:20:53 +03:00
userColors.insert(
2019-08-31 23:43:31 +03:00
id, QColor(utils::generateContrastingHexColor(id, background.name())));
return userColors.value(id);
2019-08-31 00:20:53 +03:00
}