File indexing completed on 2024-12-08 07:33:45

0001 // SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003 
0004 #pragma once
0005 
0006 #include <QQmlEngine>
0007 #include <QSortFilterProxyModel>
0008 
0009 #include "messageeventmodel.h"
0010 #include "timelinemodel.h"
0011 
0012 /**
0013  * @class MessageFilterModel
0014  *
0015  * This model filters out any messages that should be hidden.
0016  *
0017  * Deleted messages are only hidden if the user hasn't set them to be shown.
0018  *
0019  * The model also contains the roles and functions to support aggregating multiple
0020  * consecutive state events into a single delegate. The state events must all happen
0021  * on the same day to be aggregated.
0022  */
0023 class MessageFilterModel : public QSortFilterProxyModel
0024 {
0025     Q_OBJECT
0026     QML_ELEMENT
0027 
0028 public:
0029     /**
0030      * @brief Defines the model roles.
0031      */
0032     enum Roles {
0033         AggregateDisplayRole = MessageEventModel::LastRole + 1, /**< Single line aggregation of all the state events. */
0034         StateEventsRole, /**< List of state events in the aggregated state. */
0035         AuthorListRole, /**< List of the first 5 unique authors of the aggregated state event. */
0036         ExcessAuthorsRole, /**< The number of unique authors beyond the first 5. */
0037         LastRole, // Keep this last
0038     };
0039 
0040     explicit MessageFilterModel(QObject *parent = nullptr, TimelineModel *sourceModel = nullptr);
0041 
0042     /**
0043      * @brief Custom filter function to remove hidden messages.
0044      */
0045     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
0046 
0047     /**
0048      * @brief Get the given role value at the given index.
0049      *
0050      * @sa QSortFilterProxyModel::data
0051      */
0052     [[nodiscard]] QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override;
0053 
0054     /**
0055      * @brief Returns a mapping from Role enum values to role names.
0056      *
0057      * @sa Roles, QAbstractProxyModel::roleNames()
0058      */
0059     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0060 
0061 private:
0062     /**
0063      * @brief Aggregation of the text of consecutive state events starting at row.
0064      *
0065      * If state events happen on different days they will be split into two aggregate
0066      * events.
0067      */
0068     [[nodiscard]] QString aggregateEventToString(int row) const;
0069 
0070     /**
0071      * @brief Return a list of consecutive state events starting at row.
0072      *
0073      * If state events happen on different days they will be split into two aggregate
0074      * events.
0075      */
0076     [[nodiscard]] QVariantList stateEventsList(int row) const;
0077 
0078     /**
0079      * @brief List of the first 5 unique authors for the aggregate state events starting at row.
0080      */
0081     [[nodiscard]] QVariantList authorList(int row) const;
0082 
0083     /**
0084      * @brief The number of unique authors beyond the first 5 for the aggregate state events starting at row.
0085      */
0086     [[nodiscard]] QString excessAuthors(int row) const;
0087 };