File indexing completed on 2024-12-08 07:33:44
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 #include "mediamessagefiltermodel.h" 0005 0006 #include <Quotient/room.h> 0007 0008 #include "enums/delegatetype.h" 0009 #include "messageeventmodel.h" 0010 #include "messagefiltermodel.h" 0011 0012 MediaMessageFilterModel::MediaMessageFilterModel(QObject *parent, MessageFilterModel *sourceMediaModel) 0013 : QSortFilterProxyModel(parent) 0014 { 0015 Q_ASSERT(sourceMediaModel); 0016 setSourceModel(sourceMediaModel); 0017 } 0018 0019 bool MediaMessageFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const 0020 { 0021 const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); 0022 0023 if (index.data(MessageEventModel::DelegateTypeRole).toInt() == DelegateType::Image 0024 || index.data(MessageEventModel::DelegateTypeRole).toInt() == DelegateType::Video) { 0025 return true; 0026 } 0027 return false; 0028 } 0029 0030 QVariant MediaMessageFilterModel::data(const QModelIndex &index, int role) const 0031 { 0032 if (role == SourceRole) { 0033 if (mapToSource(index).data(MessageEventModel::DelegateTypeRole).toInt() == DelegateType::Image) { 0034 return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()[QStringLiteral("source")].toUrl(); 0035 } else if (mapToSource(index).data(MessageEventModel::DelegateTypeRole).toInt() == DelegateType::Video) { 0036 auto progressInfo = mapToSource(index).data(MessageEventModel::ProgressInfoRole).value<Quotient::FileTransferInfo>(); 0037 0038 if (progressInfo.completed()) { 0039 return mapToSource(index).data(MessageEventModel::ProgressInfoRole).value<Quotient::FileTransferInfo>().localPath; 0040 } else { 0041 return QUrl(); 0042 } 0043 } else { 0044 return QUrl(); 0045 } 0046 } 0047 if (role == TempSourceRole) { 0048 return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()[QStringLiteral("tempInfo")].toMap()[QStringLiteral("source")].toUrl(); 0049 } 0050 if (role == TypeRole) { 0051 if (mapToSource(index).data(MessageEventModel::DelegateTypeRole).toInt() == DelegateType::Image) { 0052 return MediaType::Image; 0053 } else { 0054 return MediaType::Video; 0055 } 0056 } 0057 if (role == CaptionRole) { 0058 return mapToSource(index).data(Qt::DisplayRole); 0059 } 0060 if (role == SourceWidthRole) { 0061 return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()[QStringLiteral("width")].toFloat(); 0062 } 0063 if (role == SourceHeightRole) { 0064 return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()[QStringLiteral("height")].toFloat(); 0065 } 0066 // We need to catch this one and return true if the next media object was 0067 // on a different day. 0068 if (role == MessageEventModel::ShowSectionRole) { 0069 const auto day = mapToSource(index).data(MessageEventModel::TimeRole).toDateTime().toLocalTime().date(); 0070 const auto previousEventDay = mapToSource(this->index(index.row() + 1, 0)).data(MessageEventModel::TimeRole).toDateTime().toLocalTime().date(); 0071 return day != previousEventDay; 0072 } 0073 0074 return sourceModel()->data(mapToSource(index), role); 0075 } 0076 0077 QHash<int, QByteArray> MediaMessageFilterModel::roleNames() const 0078 { 0079 auto roles = sourceModel()->roleNames(); 0080 roles[SourceRole] = "source"; 0081 roles[TempSourceRole] = "tempSource"; 0082 roles[TypeRole] = "type"; 0083 roles[CaptionRole] = "caption"; 0084 roles[SourceWidthRole] = "sourceWidth"; 0085 roles[SourceHeightRole] = "sourceHeight"; 0086 return roles; 0087 } 0088 0089 int MediaMessageFilterModel::getRowForSourceItem(int sourceRow) const 0090 { 0091 return mapFromSource(sourceModel()->index(sourceRow, 0)).row(); 0092 } 0093 0094 #include "moc_mediamessagefiltermodel.cpp"