diff --git a/src/models/messageeventmodel.cpp b/src/models/messageeventmodel.cpp index 3b718c71d..9ea0ad5be 100644 --- a/src/models/messageeventmodel.cpp +++ b/src/models/messageeventmodel.cpp @@ -24,6 +24,7 @@ #include "events/pollevent.h" #include "linkpreviewer.h" #include "messagecontentmodel.h" +#include "models/messagefiltermodel.h" #include "models/reactionmodel.h" #include "texthandler.h" @@ -43,7 +44,6 @@ QHash MessageEventModel::roleNames() const roles[ProgressInfoRole] = "progressInfo"; roles[IsThreadedRole] = "isThreaded"; roles[ThreadRootRole] = "threadRoot"; - roles[ShowAuthorRole] = "showAuthor"; roles[ShowSectionRole] = "showSection"; roles[ReadMarkersRole] = "readMarkers"; roles[ExcessReadMarkersRole] = "excessReadMarkers"; @@ -176,7 +176,7 @@ void MessageEventModel::setRoom(NeoChatRoom *room) } if (biggest < m_currentRoom->maxTimelineIndex()) { auto rowBelowInserted = m_currentRoom->maxTimelineIndex() - biggest + timelineBaseIndex() - 1; - refreshEventRoles(rowBelowInserted, {ShowAuthorRole}); + refreshEventRoles(rowBelowInserted, {MessageFilterModel::ShowAuthorRole}); } for (auto i = m_currentRoom->maxTimelineIndex() - biggest; i <= m_currentRoom->maxTimelineIndex() - lowest; ++i) { refreshLastUserEvents(i); @@ -206,7 +206,7 @@ void MessageEventModel::setRoom(NeoChatRoom *room) refreshRow(timelineBaseIndex()); // Refresh the looks refreshLastUserEvents(0); if (timelineBaseIndex() > 0) { // Refresh below, see #312 - refreshEventRoles(timelineBaseIndex() - 1, {ShowAuthorRole}); + refreshEventRoles(timelineBaseIndex() - 1, {MessageFilterModel::ShowAuthorRole}); } }); connect(m_currentRoom, &Room::pendingEventChanged, this, &MessageEventModel::refreshRow); @@ -549,25 +549,6 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const return eventHandler.threadRoot(); } - if (role == ShowAuthorRole) { - for (auto r = row + 1; r < rowCount(); ++r) { - auto i = index(r); - // Note !itemData(i).empty() is a check for instances where rows have been removed, e.g. when the read marker is moved. - // While the row is removed the subsequent row indexes are not changed so we need to skip over the removed index. - // See - https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows - if (data(i, SpecialMarksRole) != EventStatus::Hidden && !itemData(i).empty()) { - return data(i, AuthorRole) != data(idx, AuthorRole) || data(i, DelegateTypeRole) == DelegateType::State - || data(i, TimeRole).toDateTime().msecsTo(data(idx, TimeRole).toDateTime()) > 600000 - || data(i, TimeRole).toDateTime().toLocalTime().date().day() != data(idx, TimeRole).toDateTime().toLocalTime().date().day() - // FIXME: This should not be necessary; the proper fix is to calculate this role in MessageFilterModel with the knowledge about the filtered - // events. - || data(i, IsRedactedRole).toBool(); - } - } - - return true; - } - if (role == ShowSectionRole) { for (auto r = row + 1; r < rowCount(); ++r) { auto i = index(r); diff --git a/src/models/messageeventmodel.h b/src/models/messageeventmodel.h index 26d133b2a..0032ac0aa 100644 --- a/src/models/messageeventmodel.h +++ b/src/models/messageeventmodel.h @@ -56,7 +56,6 @@ public: IsThreadedRole, ThreadRootRole, - ShowAuthorRole, /**< Whether the author's name should be shown. */ ShowSectionRole, /**< Whether the section header should be shown. */ ReadMarkersRole, /**< The first 5 other users at the event for read marker tracking. */ diff --git a/src/models/messagefiltermodel.cpp b/src/models/messagefiltermodel.cpp index 20abe8853..e75f98038 100644 --- a/src/models/messagefiltermodel.cpp +++ b/src/models/messagefiltermodel.cpp @@ -80,8 +80,24 @@ QVariant MessageFilterModel::data(const QModelIndex &index, int role) const return authorList(mapToSource(index).row()); } else if (role == ExcessAuthorsRole) { return excessAuthors(mapToSource(index).row()); + } else if (role == ShowAuthorRole) { + for (auto r = index.row() + 1; r < rowCount(); ++r) { + auto i = this->index(r, 0); + // Note !itemData(i).empty() is a check for instances where rows have been removed, e.g. when the read marker is moved. + // While the row is removed the subsequent row indexes are not changed so we need to skip over the removed index. + // See - https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows + if (data(i, MessageEventModel::SpecialMarksRole) != EventStatus::Hidden && !itemData(i).empty()) { + return data(i, MessageEventModel::AuthorRole) != data(index, MessageEventModel::AuthorRole) + || data(i, MessageEventModel::DelegateTypeRole) == DelegateType::State + || data(i, MessageEventModel::TimeRole).toDateTime().msecsTo(data(index, MessageEventModel::TimeRole).toDateTime()) > 600000 + || data(i, MessageEventModel::TimeRole).toDateTime().toLocalTime().date().day() + != data(index, MessageEventModel::TimeRole).toDateTime().toLocalTime().date().day(); + } + } + + return true; } - return sourceModel()->data(mapToSource(index), role); + return QSortFilterProxyModel::data(index, role); } QHash MessageFilterModel::roleNames() const @@ -91,6 +107,7 @@ QHash MessageFilterModel::roleNames() const roles[StateEventsRole] = "stateEvents"; roles[AuthorListRole] = "authorList"; roles[ExcessAuthorsRole] = "excessAuthors"; + roles[ShowAuthorRole] = "showAuthor"; return roles; } diff --git a/src/models/messagefiltermodel.h b/src/models/messagefiltermodel.h index 768eb95bb..87a50a3a0 100644 --- a/src/models/messagefiltermodel.h +++ b/src/models/messagefiltermodel.h @@ -34,6 +34,7 @@ public: StateEventsRole, /**< List of state events in the aggregated state. */ AuthorListRole, /**< List of the first 5 unique authors of the aggregated state event. */ ExcessAuthorsRole, /**< The number of unique authors beyond the first 5. */ + ShowAuthorRole, /**< Whether the author (name and avatar) should be shown at this message. */ LastRole, // Keep this last };