File indexing completed on 2024-10-06 04:33:34

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
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 "models/messagefiltermodel.h"
0010 
0011 class MessageFilterModel;
0012 
0013 /**
0014  * @class MediaMessageFilterModel
0015  *
0016  * This model filters a MessageEventModel for image and video messages.
0017  *
0018  * @sa MessageEventModel
0019  */
0020 class MediaMessageFilterModel : public QSortFilterProxyModel
0021 {
0022     Q_OBJECT
0023     QML_ELEMENT
0024 
0025 public:
0026     enum MediaType {
0027         Image = 0,
0028         Video,
0029     };
0030     Q_ENUM(MediaType)
0031 
0032     /**
0033      * @brief Defines the model roles.
0034      */
0035     enum Roles {
0036         SourceRole = MessageFilterModel::LastRole + 1, /**< The mxc source URL for the item. */
0037         TempSourceRole, /**< Source for the temporary content (either blurhash or mxc URL). */
0038         TypeRole, /**< The type of the media (image or video). */
0039         CaptionRole, /**< The caption for the item. */
0040         SourceWidthRole, /**< The width of the source item. */
0041         SourceHeightRole, /**< The height of the source item. */
0042     };
0043     Q_ENUM(Roles)
0044 
0045     explicit MediaMessageFilterModel(QObject *parent = nullptr, MessageFilterModel *sourceMediaModel = nullptr);
0046 
0047     /**
0048      * @brief Custom filter to show only image and video messages.
0049      */
0050     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
0051 
0052     /**
0053      * @brief Get the given role value at the given index.
0054      *
0055      * @sa QSortFilterProxyModel::data
0056      */
0057     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0058 
0059     /**
0060      * @brief Returns a mapping from Role enum values to role names.
0061      *
0062      * @sa Roles, QAbstractProxyModel::roleNames()
0063      */
0064     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0065 
0066     Q_INVOKABLE int getRowForSourceItem(int sourceRow) const;
0067 };