File indexing completed on 2024-05-05 04:50:35

0001 /*
0002    SPDX-FileCopyrightText: 2020 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
0003 
0004    SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "viewsproxymodel.h"
0008 #include "viewsmodel.h"
0009 
0010 class ViewsProxyModelPrivate
0011 {
0012 public:
0013     ElisaUtils::PlayListEntryType mEmbeddedCategory = ElisaUtils::Unknown;
0014 };
0015 
0016 ViewsProxyModel::ViewsProxyModel(QObject *parent)
0017     : QSortFilterProxyModel(parent)
0018     , d{std::make_unique<ViewsProxyModelPrivate>()}
0019 {
0020     setSortCaseSensitivity(Qt::CaseInsensitive);
0021     sort(0, Qt::AscendingOrder);
0022 }
0023 
0024 ViewsProxyModel::~ViewsProxyModel() = default;
0025 
0026 ElisaUtils::PlayListEntryType ViewsProxyModel::embeddedCategory() const
0027 {
0028     return d->mEmbeddedCategory;
0029 }
0030 
0031 void ViewsProxyModel::setEmbeddedCategory(const ElisaUtils::PlayListEntryType category)
0032 {
0033     if (d->mEmbeddedCategory != category) {
0034         d->mEmbeddedCategory = category;
0035         invalidate();
0036         Q_EMIT embeddedCategoryChanged();
0037     }
0038 }
0039 
0040 int ViewsProxyModel::mapRowToSource(int row) const
0041 {
0042     return mapToSource(index(row, 0)).row();
0043 }
0044 
0045 int ViewsProxyModel::mapRowFromSource(int row) const
0046 {
0047     return mapFromSource(sourceModel()->index(row, 0)).row();
0048 }
0049 
0050 bool ViewsProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
0051 {
0052     if (source_right.data(ViewsModel::EntryCategoryRole).toString() == QStringLiteral("default")) {
0053         switch (sortOrder())
0054         {
0055         case Qt::AscendingOrder:
0056             return false;
0057         case Qt::DescendingOrder:
0058             return true;
0059         }
0060     }
0061 
0062     if (source_left.data(ViewsModel::EntryCategoryRole).toString() == QStringLiteral("default")) {
0063         switch (sortOrder())
0064         {
0065         case Qt::AscendingOrder:
0066             return true;
0067         case Qt::DescendingOrder:
0068             return false;
0069         }
0070     }
0071 
0072     return QSortFilterProxyModel::lessThan(source_left, source_right);
0073 }
0074 
0075 bool ViewsProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0076 {
0077     const auto currentIndex = sourceModel()->index(source_row, 0, source_parent);
0078 
0079     const bool isBaseView = sourceModel()->data(currentIndex, ViewsModel::EntryCategoryRole).toString() == QStringLiteral("default");
0080     if (!isBaseView) {
0081         return true;
0082     }
0083 
0084     const bool haveEmbeddedCategory = d->mEmbeddedCategory != ElisaUtils::Unknown;
0085     if (!haveEmbeddedCategory) {
0086         return true;
0087     }
0088 
0089     const auto dataType = sourceModel()->data(currentIndex, ViewsModel::DataTypeRole).value<ElisaUtils::PlayListEntryType>();
0090     const bool isEmbeddedCategory = dataType != d->mEmbeddedCategory;
0091     return isEmbeddedCategory;
0092 }
0093 
0094 
0095 #include "moc_viewsproxymodel.cpp"