File indexing completed on 2024-04-07 12:39:42

0001 /*
0002  *   Copyright 2017  Jos van den Oever <jos@vandenoever.info>
0003  *
0004  *   This program is free software; you can redistribute it and/or
0005  *   modify it under the terms of the GNU General Public License as
0006  *   published by the Free Software Foundation; either version 2 of
0007  *   the License or (at your option) version 3 or any later version
0008  *   accepted by the membership of KDE e.V. (or its successor approved
0009  *   by the membership of KDE e.V.), which shall act as a proxy
0010  *   defined in Section 14 of version 3 of the license.
0011  *
0012  *   This program is distributed in the hope that it will be useful,
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  *   GNU General Public License for more details.
0016  *
0017  *   You should have received a copy of the GNU General Public License
0018  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #ifndef SORTED_MODEL
0022 #define SORTED_MODEL
0023 
0024 #include <QSortFilterProxyModel>
0025 #include <QDebug>
0026 
0027 class SortedModel : public QSortFilterProxyModel {
0028 Q_OBJECT
0029 public:
0030     SortedModel() :QSortFilterProxyModel() {}
0031     bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const override;
0032     Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
0033         return QSortFilterProxyModel::data(index, role);
0034     }
0035 public slots:
0036     void sortByRole(const QString& role, Qt::SortOrder order) {
0037         QHashIterator<int, QByteArray> i(roleNames());
0038         while (i.hasNext()) {
0039             i.next();
0040             if (i.value() == role) {
0041                 setSortRole(i.key());
0042             }
0043         }
0044         sort(0, order);
0045     }
0046 };
0047 
0048 #endif