File indexing completed on 2025-03-02 05:11:58

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Fredrik Höglund <fredrik@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QSortFilterProxyModel>
0010 #include <thememodel.h>
0011 
0012 /**
0013  * SortProxyModel is a sorting proxy model intended to be used in combination
0014  * with the ItemDelegate class.
0015  *
0016  * First it compares the Qt::DisplayRoles, and if they match it compares
0017  * the CursorTheme::DisplayDetailRoles.
0018  *
0019  * The model assumes both display roles are QStrings.
0020  */
0021 class SortProxyModel : public QSortFilterProxyModel
0022 {
0023     Q_OBJECT
0024 public:
0025     explicit SortProxyModel(QObject *parent = nullptr)
0026         : QSortFilterProxyModel(parent)
0027     {
0028     }
0029     ~SortProxyModel() override
0030     {
0031     }
0032     QHash<int, QByteArray> roleNames() const override;
0033     inline const CursorTheme *theme(const QModelIndex &index) const;
0034     inline QModelIndex findIndex(const QString &name) const;
0035     inline QModelIndex defaultIndex() const;
0036     inline void removeTheme(const QModelIndex &index);
0037 
0038 private:
0039     int compare(const QModelIndex &left, const QModelIndex &right, int role) const;
0040 
0041 protected:
0042     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0043 };
0044 
0045 const CursorTheme *SortProxyModel::theme(const QModelIndex &index) const
0046 {
0047     CursorThemeModel *model = static_cast<CursorThemeModel *>(sourceModel());
0048     return model->theme(mapToSource(index));
0049 }
0050 
0051 QModelIndex SortProxyModel::findIndex(const QString &name) const
0052 {
0053     CursorThemeModel *model = static_cast<CursorThemeModel *>(sourceModel());
0054     return mapFromSource(model->findIndex(name));
0055 }
0056 
0057 QModelIndex SortProxyModel::defaultIndex() const
0058 {
0059     CursorThemeModel *model = static_cast<CursorThemeModel *>(sourceModel());
0060     return mapFromSource(model->defaultIndex());
0061 }
0062 
0063 void SortProxyModel::removeTheme(const QModelIndex &index)
0064 {
0065     CursorThemeModel *model = static_cast<CursorThemeModel *>(sourceModel());
0066     model->removeTheme(mapToSource(index));
0067 }