Use a QSortFilterProxyModel instead of resetting the model

pull/655/head
Loren Burkholder 3 years ago
parent 1777a1b52f
commit 7e538851d6
  1. 4
      CMakeLists.txt
  2. 4
      resources/qml/ReadReceipts.qml
  3. 53
      src/ReadReceiptsModel.cpp
  4. 27
      src/ReadReceiptsModel.h
  5. 2
      src/timeline/TimelineModel.cpp
  6. 2
      src/timeline/TimelineModel.h
  7. 6
      src/timeline/TimelineViewManager.cpp

@ -350,7 +350,7 @@ set(SRC_FILES
src/MemberList.cpp src/MemberList.cpp
src/MxcImageProvider.cpp src/MxcImageProvider.cpp
src/Olm.cpp src/Olm.cpp
src/ReadReceiptsModel.cpp src/ReadReceiptsModel.cpp
src/RegisterPage.cpp src/RegisterPage.cpp
src/SSOHandler.cpp src/SSOHandler.cpp
src/CombinedImagePackModel.cpp src/CombinedImagePackModel.cpp
@ -555,7 +555,7 @@ qt5_wrap_cpp(MOC_HEADERS
src/MainWindow.h src/MainWindow.h
src/MemberList.h src/MemberList.h
src/MxcImageProvider.h src/MxcImageProvider.h
src/ReadReceiptsModel.h src/ReadReceiptsModel.h
src/RegisterPage.h src/RegisterPage.h
src/SSOHandler.h src/SSOHandler.h
src/CombinedImagePackModel.h src/CombinedImagePackModel.h

@ -10,7 +10,7 @@ import im.nheko 1.0
ApplicationWindow { ApplicationWindow {
id: readReceiptsRoot id: readReceiptsRoot
property ReadReceiptsModel readReceipts property ReadReceiptsProxy readReceipts
x: MainWindow.x + (MainWindow.width / 2) - (width / 2) x: MainWindow.x + (MainWindow.width / 2) - (width / 2)
y: MainWindow.y + (MainWindow.height / 2) - (height / 2) y: MainWindow.y + (MainWindow.height / 2) - (height / 2)
@ -86,7 +86,7 @@ ApplicationWindow {
ToolTip.text: model.mxid ToolTip.text: model.mxid
TapHandler { TapHandler {
onSingleTapped: chat.model.openUserProfile(userId) onSingleTapped: Rooms.currentRoom.openUserProfile(userId)
} }
CursorShape { CursorShape {

@ -46,10 +46,13 @@ ReadReceiptsModel::update()
QHash<int, QByteArray> QHash<int, QByteArray>
ReadReceiptsModel::roleNames() const ReadReceiptsModel::roleNames() const
{ {
return {{Mxid, "mxid"}, // Note: RawTimestamp is purposely not included here
{DisplayName, "displayName"}, return {
{AvatarUrl, "avatarUrl"}, {Mxid, "mxid"},
{Timestamp, "timestamp"}}; {DisplayName, "displayName"},
{AvatarUrl, "avatarUrl"},
{Timestamp, "timestamp"},
};
} }
QVariant QVariant
@ -67,6 +70,8 @@ ReadReceiptsModel::data(const QModelIndex &index, int role) const
return cache::avatarUrl(room_id_, readReceipts_[index.row()].first); return cache::avatarUrl(room_id_, readReceipts_[index.row()].first);
case Timestamp: case Timestamp:
return dateFormat(readReceipts_[index.row()].second); return dateFormat(readReceipts_[index.row()].second);
case RawTimestamp:
return readReceipts_[index.row()].second;
default: default:
return {}; return {};
} }
@ -76,21 +81,22 @@ void
ReadReceiptsModel::addUsers( ReadReceiptsModel::addUsers(
const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users) const std::multimap<uint64_t, std::string, std::greater<uint64_t>> &users)
{ {
beginResetModel(); auto newReceipts = users.size() - readReceipts_.size();
readReceipts_.clear(); if (newReceipts > 0) {
for (const auto &user : users) { beginInsertRows(
readReceipts_.push_back({QString::fromStdString(user.second), QModelIndex{}, readReceipts_.size(), readReceipts_.size() + newReceipts - 1);
QDateTime::fromMSecsSinceEpoch(user.first)});
}
std::sort(readReceipts_.begin(), for (const auto &user : users) {
readReceipts_.end(), QPair<QString, QDateTime> item = {
[](const QPair<QString, QDateTime> &a, const QPair<QString, QDateTime> &b) { QString::fromStdString(user.second),
return a.second > b.second; QDateTime::fromMSecsSinceEpoch(user.first)};
}); if (!readReceipts_.contains(item))
readReceipts_.push_back(item);
}
endResetModel(); endInsertRows();
}
} }
QString QString
@ -112,3 +118,18 @@ ReadReceiptsModel::dateFormat(const QDateTime &then) const
return QLocale::system().toString(then.time(), QLocale::ShortFormat); 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();
}

@ -8,15 +8,13 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QDateTime> #include <QDateTime>
#include <QObject> #include <QObject>
#include <QSortFilterProxyModel>
#include <QString> #include <QString>
class ReadReceiptsModel : public QAbstractListModel class ReadReceiptsModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString eventId READ eventId CONSTANT)
Q_PROPERTY(QString roomId READ roomId CONSTANT)
public: public:
enum Roles enum Roles
{ {
@ -24,6 +22,7 @@ public:
DisplayName, DisplayName,
AvatarUrl, AvatarUrl,
Timestamp, Timestamp,
RawTimestamp,
}; };
explicit ReadReceiptsModel(QString event_id, QString room_id, QObject *parent = nullptr); explicit ReadReceiptsModel(QString event_id, QString room_id, QObject *parent = nullptr);
@ -51,4 +50,26 @@ private:
QVector<QPair<QString, QDateTime>> readReceipts_; QVector<QPair<QString, QDateTime>> readReceipts_;
}; };
class ReadReceiptsProxy : public QSortFilterProxyModel
{
Q_OBJECT
Q_PROPERTY(QString eventId READ eventId CONSTANT)
Q_PROPERTY(QString roomId READ roomId CONSTANT)
public:
explicit ReadReceiptsProxy(QString event_id, QString room_id, QObject *parent = nullptr);
QString eventId() const { return event_id_; }
QString roomId() const { return room_id_; }
bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const;
private:
QString event_id_;
QString room_id_;
ReadReceiptsModel model_;
};
#endif // READRECEIPTSMODEL_H #endif // READRECEIPTSMODEL_H

@ -1092,7 +1092,7 @@ TimelineModel::relatedInfo(QString id)
void void
TimelineModel::showReadReceipts(QString id) TimelineModel::showReadReceipts(QString id)
{ {
emit openReadReceiptsDialog(new ReadReceiptsModel{id, roomId(), this}); emit openReadReceiptsDialog(new ReadReceiptsProxy{id, roomId(), this});
} }
void void

@ -349,7 +349,7 @@ signals:
void typingUsersChanged(std::vector<QString> users); void typingUsersChanged(std::vector<QString> users);
void replyChanged(QString reply); void replyChanged(QString reply);
void editChanged(QString reply); void editChanged(QString reply);
void openReadReceiptsDialog(ReadReceiptsModel *rr); void openReadReceiptsDialog(ReadReceiptsProxy *rr);
void paginationInProgressChanged(const bool); void paginationInProgressChanged(const bool);
void newCallEvent(const mtx::events::collections::TimelineEvents &event); void newCallEvent(const mtx::events::collections::TimelineEvents &event);
void scrollToIndex(int index); void scrollToIndex(int index);

@ -206,12 +206,12 @@ TimelineViewManager::TimelineViewManager(CallManager *callManager, ChatPage *par
0, 0,
"InviteesModel", "InviteesModel",
"InviteesModel needs to be instantiated on the C++ side"); "InviteesModel needs to be instantiated on the C++ side");
qmlRegisterUncreatableType<ReadReceiptsModel>( qmlRegisterUncreatableType<ReadReceiptsProxy>(
"im.nheko", "im.nheko",
1, 1,
0, 0,
"ReadReceiptsModel", "ReadReceiptsProxy",
"ReadReceiptsModel needs to be instantiated on the C++ side"); "ReadReceiptsProxy needs to be instantiated on the C++ side");
static auto self = this; static auto self = this;
qmlRegisterSingletonType<MainWindow>( qmlRegisterSingletonType<MainWindow>(

Loading…
Cancel
Save