dolphinsortfilterproxymodel.cpp 9.17 KB
Newer Older
1
/***************************************************************************
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
*   Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>                  *
*   Copyright (C) 2006 by Dominic Battre <dominic battre de>              *
*   Copyright (C) 2006 by Martin Pool <mbp canonical com>                 *
*                                                                         *
*   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            *
***************************************************************************/
21
22
23
24
25
26

#include "dolphinsortfilterproxymodel.h"

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

27
static const int dolphinMapSize = 6;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
static int dolphinViewToDirModelColumn[] =
    {
        KDirModel::Name,         // DolphinView::SortByName
        KDirModel::Size,         // DolphinView::SortBySize
        KDirModel::ModifiedTime, // DolphinView::SortByDate
        KDirModel::Permissions,  // DolphinView::SortByPermissions
        KDirModel::Owner,        // DolphinView::SortByOwner
        KDirModel::Group         // DolphinView::SortByGroup
    };

static DolphinView::Sorting dirModelColumnToDolphinView[] =
    {
        DolphinView::SortByName,        // KDirModel::Name
        DolphinView::SortBySize,        // KDirModel::Size
        DolphinView::SortByDate,        // KDirModel::ModifiedTime
        DolphinView::SortByPermissions, // KDirModel::Permissions
        DolphinView::SortByOwner,       // KDirModel::Owner
        DolphinView::SortByGroup        // KDirModel::Group
    };
47
48


49
DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
50
51
52
53
        QSortFilterProxyModel(parent),
        m_sortColumn(0),
        m_sorting(DolphinView::SortByName),
        m_sortOrder(Qt::AscendingOrder)
54
{
55
56
    setDynamicSortFilter(true);

57
    // sort by the user visible string for now
58
59
    setSortRole(Qt::DisplayRole);
    setSortCaseSensitivity(Qt::CaseInsensitive);
60
    sort(KDirModel::Name, Qt::AscendingOrder);
61
62
63
}

DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
64
{}
65
66
67

void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting)
{
68
69
    // Update the sort column by mapping DolpginView::Sorting to
    // KDirModel::ModelColumns. We will keep the sortOrder.
70
    Q_ASSERT(static_cast<int>(sorting) >= 0 && static_cast<int>(sorting) < dolphinMapSize);
71
    sort(dolphinViewToDirModelColumn[static_cast<int>(sorting)],
72
         m_sortOrder);
73
74
}

75
76
77
78
79
80
void DolphinSortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
{
    // change the sort order by keeping the current column
    sort(dolphinViewToDirModelColumn[m_sorting], sortOrder);
}

81
82
void DolphinSortFilterProxyModel::sort(int column, Qt::SortOrder sortOrder)
{
83
    m_sortColumn = column;
84
    m_sortOrder = sortOrder;
85
    m_sorting = (column >= 0) && (column < dolphinMapSize) ?
86
87
                dirModelColumnToDolphinView[column]  :
                DolphinView::SortByName;
88
    QSortFilterProxyModel::sort(column, sortOrder);
89
90
}

91
92
93
94
95
96
97
98
99
100
101
102
bool DolphinSortFilterProxyModel::hasChildren(const QModelIndex& parent) const
{
    const QModelIndex sourceParent = mapToSource(parent);
    return sourceModel()->hasChildren(sourceParent);
}

bool DolphinSortFilterProxyModel::canFetchMore(const QModelIndex& parent) const
{
    const QModelIndex sourceParent = mapToSource(parent);
    return sourceModel()->canFetchMore(sourceParent);
}

103
104
DolphinView::Sorting DolphinSortFilterProxyModel::sortingForColumn(int column)
{
105
    if ((column >= 0) && (column < dolphinMapSize)) {
106
107
108
109
110
        return dirModelColumnToDolphinView[column];
    }
    return DolphinView::SortByName;
}

111
bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
112
        const QModelIndex& right) const
113
{
114
115
116
117
118
119
120
    KDirModel* dirModel = static_cast<KDirModel*>(sourceModel());

    QVariant leftData  = dirModel->data(left,  sortRole());
    QVariant rightData = dirModel->data(right, sortRole());

    if ((leftData.type() == QVariant::String) && (rightData.type() == QVariant::String)) {
        // assure that directories are always sorted before files
121
122
123
124
125
126
127
128
129
130
131
        // if the sorting is done by the 'Name' column
        if (m_sortColumn == KDirModel::Name) {
            const bool leftIsDir  = dirModel->itemForIndex(left)->isDir();
            const bool rightIsDir = dirModel->itemForIndex(right)->isDir();
            if (leftIsDir && !rightIsDir) {
                return true;
            }

            if (!leftIsDir && rightIsDir) {
                return false;
            }
132
133
        }

134
135
        const QString leftStr  = leftData.toString();
        const QString rightStr = rightData.toString();
136
137

        return sortCaseSensitivity() ? (naturalCompare(leftStr, rightStr) < 0) :
138
               (naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
139
140
141
142
143
    }

    // We have set a SortRole and trust the ProxyModel to do
    // the right thing for now.
    return QSortFilterProxyModel::lessThan(left, right);
144
145
}

146
int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
147
        const QString& b)
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
    // This method chops the input a and b into pieces of
    // digits and non-digits (a1.05 becomes a | 1 | . | 05)
    // and compares these pieces of a and b to each other
    // (first with first, second with second, ...).
    //
    // This is based on the natural sort order code code by Martin Pool
    // http://sourcefrog.net/projects/natsort/
    // Martin Pool agreed to license this under LGPL or GPL.

    const QChar* currA = a.unicode(); // iterator over a
    const QChar* currB = b.unicode(); // iterator over b

    if (currA == currB) {
        return 0;
    }

    const QChar* begSeqA = currA; // beginning of a new character sequence of a
    const QChar* begSeqB = currB;

    while (!currA->isNull() && !currB->isNull()) {
        // find sequence of characters ending at the first non-character
        while (!currA->isNull() && !currA->isDigit()) {
            ++currA;
        }

        while (!currB->isNull() && !currB->isDigit()) {
            ++currB;
        }

        // compare these sequences
179
180
181
        const QString subA(begSeqA, currA - begSeqA);
        const QString subB(begSeqB, currB - begSeqB);
        const int cmp = QString::localeAwareCompare(subA, subB);
182
183
184
185
186
187
188
189
190
191
192
        if (cmp != 0) {
            return cmp;
        }

        if (currA->isNull() || currB->isNull()) {
            break;
        }

        // now some digits follow...
        if ((*currA == '0') || (*currB == '0')) {
            // one digit-sequence starts with 0 -> assume we are in a fraction part
193
            // do left aligned comparison (numbers are considered left aligned)
194
            while (1) {
195
196
                if (!currA->isDigit() && !currB->isDigit()) {
                    break;
197
                } else if (!currA->isDigit()) {
198
                    return -1;
199
200
201
                } else if (!currB->isDigit()) {
                    return + 1;
                } else if (*currA < *currB) {
202
                    return -1;
203
204
                } else if (*currA > *currB) {
                    return + 1;
205
206
207
208
                }
                ++currA;
                ++currB;
            }
209
        } else {
210
211
212
213
            // No digit-sequence starts with 0 -> assume we are looking at some integer
            // do right aligned comparison.
            //
            // The longest run of digits wins. That aside, the greatest
214
            // value wins, but we can't know that it will until we've scanned
215
            // both numbers to know that they have the same magnitude.
216

217
            int weight = 0;
218
219
            while (1) {
                if (!currA->isDigit() && !currB->isDigit()) {
220
221
                    if (weight != 0) {
                        return weight;
222
223
                    }
                    break;
224
                } else if (!currA->isDigit()) {
225
                    return -1;
226
227
228
                } else if (!currB->isDigit()) {
                    return + 1;
                } else if ((*currA < *currB) && (weight == 0)) {
229
                    weight = -1;
230
231
                } else if ((*currA > *currB) && (weight == 0)) {
                    weight = + 1;
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                }
                ++currA;
                ++currB;
            }
        }

        begSeqA = currA;
        begSeqB = currB;
    }

    if (currA->isNull() && currB->isNull()) {
        return 0;
    }

246
    return currA->isNull() ? -1 : + 1;
247
248
}

249
#include "dolphinsortfilterproxymodel.moc"