dolphinsortfilterproxymodel.cpp 3.7 KB
Newer Older
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
/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>                  *
 *   Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net>          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "dolphinsortfilterproxymodel.h"

#include <kdirmodel.h>
#include <kfileitem.h>

26
27
28
29
30
31
32
33
34
35
36
37
38
39
static const int dolphin_map_size = 3;
static int dolphin_view_to_dir_model_column[] = {
    /* SortByName */ KDirModel::Name,
    /* SortBySize */ KDirModel::Size,
    /* SortByDate */ KDirModel::ModifiedTime 
};

static DolphinView::Sorting dir_model_column_to_dolphin_view[] = {
    /* KDirModel::Name */ DolphinView::SortByName,
    /* KDirModel::Size */ DolphinView::SortBySize,
    /* KDirModel::ModifiedTime */ DolphinView::SortByDate
};


40
DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
41
    QSortFilterProxyModel(parent)
42
{
43
44
45
46
47
48
49
50
    setDynamicSortFilter(true);

    /*
     * sort by the user visible string for now
     */
    setSortRole(Qt::DisplayRole);
    setSortCaseSensitivity(Qt::CaseInsensitive);
    sort(KDirModel::Name, Qt::Ascending);
51
52
53
54
55
56
}

DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
{
}

57
58
59
60
61
/*
 * Update the sort column by mapping DolpginView::Sorting to
 * KDirModel::ModelColumns.
 * We will keep the sortOrder
 */
62
63
void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting)
{
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    Q_ASSERT( static_cast<int>(sorting) >= 0 && static_cast<int>(sorting) <= dolphin_map_size );
    sort(dolphin_view_to_dir_model_column[static_cast<int>(sorting)],
         m_sortOrder );
}

/**
 * @reimplemented, @internal
 *
 * If the view 'forces' sorting order to change we will
 * notice now.
 */
void DolphinSortFilterProxyModel::sort(int column, Qt::SortOrder sortOrder)
{
    m_sortOrder = sortOrder;
    m_sorting = column >= 0 && column <= dolphin_map_size ?
                dir_model_column_to_dolphin_view[column]  :
                DolphinView::SortByName; 
    QSortFilterProxyModel::sort(column,sortOrder);
82
83
}

84
85
86
/*
 * change the sort order by keeping the current column
 */
87
88
void DolphinSortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
{
89
    sort(dolphin_view_to_dir_model_column[m_sorting], sortOrder);
90
91
92
93
94
}

bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
                                           const QModelIndex& right) const
{
95
96
97
98
99
    /*
     * We have set a SortRole and trust the ProxyModel to do
     * the right thing for now
     */
    return QSortFilterProxyModel::lessThan(left,right);
100
101
102
}

#include "dolphinsortfilterproxymodel.moc"