dolphinsortfilterproxymodel.cpp 14 KB
Newer Older
1
/***************************************************************************
2
3
4
 *   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>                 *
5
 *   Copyright (C) 2007 by Rafael Fernández López <ereslibre@gmail.com>    *
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *                                                                         *
 *   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            *
 ***************************************************************************/
22
23
24
25
26
27

#include "dolphinsortfilterproxymodel.h"

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

Peter Penz's avatar
Peter Penz committed
28
static const int dolphinMapSize = 7;
29
static int dolphinViewToDirModelColumn[] =
Peter Penz's avatar
Peter Penz committed
30
31
32
33
34
35
36
37
38
{
    KDirModel::Name,         // DolphinView::SortByName
    KDirModel::Size,         // DolphinView::SortBySize
    KDirModel::ModifiedTime, // DolphinView::SortByDate
    KDirModel::Permissions,  // DolphinView::SortByPermissions
    KDirModel::Owner,        // DolphinView::SortByOwner
    KDirModel::Group,        // DolphinView::SortByGroup
    KDirModel::Type          // DolphinView::SortByType
};
39
40

static DolphinView::Sorting dirModelColumnToDolphinView[] =
Peter Penz's avatar
Peter Penz committed
41
42
43
44
45
46
47
48
49
{
    DolphinView::SortByName,        // KDirModel::Name
    DolphinView::SortBySize,        // KDirModel::Size
    DolphinView::SortByDate,        // KDirModel::ModifiedTime
    DolphinView::SortByPermissions, // KDirModel::Permissions
    DolphinView::SortByOwner,       // KDirModel::Owner
    DolphinView::SortByGroup,       // KDirModel::Group
    DolphinView::SortByType         // KDirModel::Type
};
50
51


52
DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
53
    KSortFilterProxyModel(parent),
54
55
56
    m_sortColumn(0),
    m_sorting(DolphinView::SortByName),
    m_sortOrder(Qt::AscendingOrder)
57
{
58
59
    setDynamicSortFilter(true);

60
    // sort by the user visible string for now
61
    setSortRole(DolphinView::SortByName);
62
    setSortCaseSensitivity(Qt::CaseInsensitive);
63
    sort(KDirModel::Name, Qt::AscendingOrder);
64
65
66
}

DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
67
{}
68
69
70

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

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

84
85
void DolphinSortFilterProxyModel::sort(int column, Qt::SortOrder sortOrder)
{
86
    m_sortColumn = column;
87
    m_sortOrder = sortOrder;
88
    m_sorting = (column >= 0) && (column < dolphinMapSize) ?
89
90
                dirModelColumnToDolphinView[column]  :
                DolphinView::SortByName;
91
    setSortRole(m_sorting);
92
    KSortFilterProxyModel::sort(column, sortOrder);
93
94
}

95
96
97
98
99
100
101
102
103
104
105
106
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);
}

107
108
DolphinView::Sorting DolphinSortFilterProxyModel::sortingForColumn(int column)
{
109
    if ((column >= 0) && (column < dolphinMapSize)) {
110
111
112
113
114
        return dirModelColumnToDolphinView[column];
    }
    return DolphinView::SortByName;
}

115
116
117
118
119
120
121
122
123
124
125
126
127
bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left,
                                                         const QModelIndex &right) const
{
    KDirModel* dirModel = static_cast<KDirModel*>(sourceModel());

    const KFileItem *leftFileItem  = dirModel->itemForIndex(left);
     const KFileItem *rightFileItem = dirModel->itemForIndex(right);

    if (sortRole() == DolphinView::SortByName) // If we are sorting by name
    {
        const QVariant leftData  = dirModel->data(left,  sortRole());
        const QVariant rightData = dirModel->data(right, sortRole());

128
129
        QString leftStr = leftData.toString();
        QString rightStr = rightData.toString();
130

131
132
        // We don't care about case for building categories
        return naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0;
133
134
135
136
137
138
139
140
141
142
143
144
    }
    else if (sortRole() == DolphinView::SortBySize) // If we are sorting by size
    {
        // If we are sorting by size, show folders first. We will sort them
        // correctly later
        if (leftFileItem->isDir() && !rightFileItem->isDir())
            return true;

        return false;
    }
}

145
bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
146
                                           const QModelIndex& right) const
147
{
148
149
150
151
152
    KDirModel* dirModel = static_cast<KDirModel*>(sourceModel());

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

153
154
155
156
157
158
159
160
161
162
    const KFileItem *leftFileItem  = dirModel->itemForIndex(left);
    const KFileItem *rightFileItem = dirModel->itemForIndex(right);

    if (sortRole() == DolphinView::SortByName) { // If we are sorting by name
        if ((leftData.type() == QVariant::String) && (rightData.type() ==
                                                            QVariant::String)) {
            // Priority: hidden > folders > regular files. If an item is
            // hidden (doesn't matter if file or folder) will have higher
            // preference than a non-hidden item
            if (leftFileItem->isHidden() && !rightFileItem->isHidden()) {
163
164
                return true;
            }
165
166
167
            else if (!leftFileItem->isHidden() && rightFileItem->isHidden()) {
                return false;
            }
168

169
170
171
172
173
            // On our priority, folders go above regular files
            if (leftFileItem->isDir() && !rightFileItem->isDir()) {
                return true;
            }
            else if (!leftFileItem->isDir() && rightFileItem->isDir()) {
174
175
                return false;
            }
176
177

            // So we are in the same priority, what counts now is their names
178
179
180
181
182
            QString leftStr = leftData.toString();
            QString rightStr = rightData.toString();

            leftStr = leftStr.at(0) == '.' ? leftStr.mid(1) : leftStr;
            rightStr = rightStr.at(0) == '.' ? rightStr.mid(1) : rightStr;
183
184
185
186
187
188

            return sortCaseSensitivity() ? (naturalCompare(leftStr, rightStr) < 0) :
                   (naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
        }
    }
    else if (sortRole() == DolphinView::SortBySize) { // If we are sorting by size
189
190
        // If an item is hidden (doesn't matter if file or folder) will have
        // higher preference than a non-hidden item
191
192
        if (leftFileItem->isHidden() && !rightFileItem->isHidden()) {
            return true;
193
        }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        else if (!leftFileItem->isHidden() && rightFileItem->isHidden()) {
            return false;
        }

        // On our priority, folders go above regular files
        if (leftFileItem->isDir() && !rightFileItem->isDir()) {
            return true;
        }
        else if (!leftFileItem->isDir() && rightFileItem->isDir()) {
            return false;
        }

        // If we have two folders, what we have to measure is the number of
        // items that contains each other
        if (leftFileItem->isDir() && rightFileItem->isDir()) {
            const QVariant leftValue = dirModel->data(left, KDirModel::ChildCountRole);
            const int leftCount = leftValue.type() == QVariant::Int ? leftValue.toInt() : KDirModel::ChildCountUnknown;

            const QVariant rightValue = dirModel->data(right, KDirModel::ChildCountRole);
            const int rightCount = rightValue.type() == QVariant::Int ? rightValue.toInt() : KDirModel::ChildCountUnknown;
214

215
216
217
218
            // In the case they two have the same child items, we sort them by
            // their names. So we have always everything ordered. We also check
            // if we are taking in count their cases
            if (leftCount == rightCount) {
219
220
                const QString leftStr = dirModel->data(left,  DolphinView::SortByName).toString();
                const QString rightStr = dirModel->data(right,  DolphinView::SortByName).toString();
221

222
223
224
225
226
227
228
229
230
231
232
233
                return sortCaseSensitivity() ? (naturalCompare(leftStr, rightStr) < 0) :
                       (naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
            }

            // If they had different number of items, we sort them depending
            // on how many items had each other
            return leftCount < rightCount;
        }

        // If what we are measuring is two files and they have the same size,
        // sort them by their file names
        if (leftFileItem->size() == rightFileItem->size()) {
234
235
            const QString leftStr = dirModel->data(left,  DolphinView::SortByName).toString();
            const QString rightStr = dirModel->data(right,  DolphinView::SortByName).toString();
236
237
238
239
240
241
242

            return sortCaseSensitivity() ? (naturalCompare(leftStr, rightStr) < 0) :
                   (naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
        }

        // If their sizes are different, sort them by their sizes, as expected
        return leftFileItem->size() < rightFileItem->size();
243
244
245
246
    }

    // We have set a SortRole and trust the ProxyModel to do
    // the right thing for now.
247

248
    return QSortFilterProxyModel::lessThan(left, right);
249
250
}

251
int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
252
                                                const QString& b)
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{
    // 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
284
285
286
        const QString subA(begSeqA, currA - begSeqA);
        const QString subB(begSeqB, currB - begSeqB);
        const int cmp = QString::localeAwareCompare(subA, subB);
287
288
289
290
291
292
293
294
295
296
297
        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
298
            // do left aligned comparison (numbers are considered left aligned)
299
            while (1) {
300
301
                if (!currA->isDigit() && !currB->isDigit()) {
                    break;
302
                } else if (!currA->isDigit()) {
303
                    return -1;
304
305
306
                } else if (!currB->isDigit()) {
                    return + 1;
                } else if (*currA < *currB) {
307
                    return -1;
308
309
                } else if (*currA > *currB) {
                    return + 1;
310
311
312
313
                }
                ++currA;
                ++currB;
            }
314
        } else {
315
316
317
318
            // 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
319
            // value wins, but we can't know that it will until we've scanned
320
            // both numbers to know that they have the same magnitude.
321

322
            int weight = 0;
323
324
            while (1) {
                if (!currA->isDigit() && !currB->isDigit()) {
325
326
                    if (weight != 0) {
                        return weight;
327
328
                    }
                    break;
329
                } else if (!currA->isDigit()) {
330
                    return -1;
331
332
333
                } else if (!currB->isDigit()) {
                    return + 1;
                } else if ((*currA < *currB) && (weight == 0)) {
334
                    weight = -1;
335
336
                } else if ((*currA > *currB) && (weight == 0)) {
                    weight = + 1;
337
338
339
340
341
342
343
344
345
346
347
348
349
350
                }
                ++currA;
                ++currB;
            }
        }

        begSeqA = currA;
        begSeqB = currB;
    }

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

351
    return currA->isNull() ? -1 : + 1;
352
353
}

354
#include "dolphinsortfilterproxymodel.moc"