Use a QSortFilterProxyModel instead of resetting the model
This commit is contained in:
parent
1777a1b52f
commit
7e538851d6
7 changed files with 71 additions and 29 deletions
|
|
@ -46,10 +46,13 @@ ReadReceiptsModel::update()
|
|||
QHash<int, QByteArray>
|
||||
ReadReceiptsModel::roleNames() const
|
||||
{
|
||||
return {{Mxid, "mxid"},
|
||||
{DisplayName, "displayName"},
|
||||
{AvatarUrl, "avatarUrl"},
|
||||
{Timestamp, "timestamp"}};
|
||||
// Note: RawTimestamp is purposely not included here
|
||||
return {
|
||||
{Mxid, "mxid"},
|
||||
{DisplayName, "displayName"},
|
||||
{AvatarUrl, "avatarUrl"},
|
||||
{Timestamp, "timestamp"},
|
||||
};
|
||||
}
|
||||
|
||||
QVariant
|
||||
|
|
@ -67,6 +70,8 @@ ReadReceiptsModel::data(const QModelIndex &index, int role) const
|
|||
return cache::avatarUrl(room_id_, readReceipts_[index.row()].first);
|
||||
case Timestamp:
|
||||
return dateFormat(readReceipts_[index.row()].second);
|
||||
case RawTimestamp:
|
||||
return readReceipts_[index.row()].second;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
|
|
@ -76,21 +81,22 @@ void
|
|||
ReadReceiptsModel::addUsers(
|
||||
const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users)
|
||||
{
|
||||
beginResetModel();
|
||||
auto newReceipts = users.size() - readReceipts_.size();
|
||||
|
||||
readReceipts_.clear();
|
||||
for (const auto &user : users) {
|
||||
readReceipts_.push_back({QString::fromStdString(user.second),
|
||||
QDateTime::fromMSecsSinceEpoch(user.first)});
|
||||
if (newReceipts > 0) {
|
||||
beginInsertRows(
|
||||
QModelIndex{}, readReceipts_.size(), readReceipts_.size() + newReceipts - 1);
|
||||
|
||||
for (const auto &user : users) {
|
||||
QPair<QString, QDateTime> item = {
|
||||
QString::fromStdString(user.second),
|
||||
QDateTime::fromMSecsSinceEpoch(user.first)};
|
||||
if (!readReceipts_.contains(item))
|
||||
readReceipts_.push_back(item);
|
||||
}
|
||||
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
std::sort(readReceipts_.begin(),
|
||||
readReceipts_.end(),
|
||||
[](const QPair<QString, QDateTime> &a, const QPair<QString, QDateTime> &b) {
|
||||
return a.second > b.second;
|
||||
});
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QString
|
||||
|
|
@ -112,3 +118,18 @@ ReadReceiptsModel::dateFormat(const QDateTime &then) const
|
|||
|
||||
return QLocale::system().toString(then.time(), QLocale::ShortFormat);
|
||||
}
|
||||
|
||||
ReadReceiptsProxy::ReadReceiptsProxy(QString event_id, QString room_id, QObject *parent)
|
||||
: QSortFilterProxyModel{parent}
|
||||
, model_{event_id, room_id, this}
|
||||
{
|
||||
setSourceModel(&model_);
|
||||
setSortRole(ReadReceiptsModel::RawTimestamp);
|
||||
}
|
||||
|
||||
bool
|
||||
ReadReceiptsProxy::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
|
||||
{
|
||||
// since we are sorting from greatest to least timestamp, return something that looks totally backwards!
|
||||
return source_left.data().toULongLong() > source_right.data().toULongLong();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue