~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright © 2006-2007 Fredrik Höglund <fredrik@kde.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License version 2 or at your option version 3 as published 
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef SORTPROXYMODEL_H
#define SORTPROXYMODEL_H

#include <QSortFilterProxyModel>
#include <thememodel.h>

/**
 * SortProxyModel is a sorting proxy model intended to be used in combination
 * with the ItemDelegate class.
 *
 * First it compares the Qt::DisplayRoles, and if they match it compares
 * the CursorTheme::DisplayDetailRoles.
 *
 * The model assumes both display roles are QStrings.
 */
class SortProxyModel : public QSortFilterProxyModel
{
    public:
        SortProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
        ~SortProxyModel() {}
        inline const CursorTheme *theme(const QModelIndex &index) const;
        inline QModelIndex findIndex(const QString &name) const;
        inline QModelIndex defaultIndex() const;
        inline void removeTheme(const QModelIndex &index);

    private:
        int compare(const QModelIndex &left, const QModelIndex &right, int role) const;

    protected:
        bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
};


const CursorTheme *SortProxyModel::theme(const QModelIndex &index) const
{
    CursorThemeModel *model = static_cast<CursorThemeModel*>(sourceModel());
    return model->theme(mapToSource(index));
}

QModelIndex SortProxyModel::findIndex(const QString &name) const
{
    CursorThemeModel *model = static_cast<CursorThemeModel*>(sourceModel());
    return mapFromSource(model->findIndex(name));
}

QModelIndex SortProxyModel::defaultIndex() const
{
    CursorThemeModel *model = static_cast<CursorThemeModel*>(sourceModel());
    return mapFromSource(model->defaultIndex());
}

void SortProxyModel::removeTheme(const QModelIndex &index)
{
    CursorThemeModel *model = static_cast<CursorThemeModel*>(sourceModel());
    model->removeTheme(mapToSource(index));
}

#endif // SORTPROXYMODEL_H