archivesortfiltermodel.cpp 2.26 KB
Newer Older
Ragnar Thomsen's avatar
Ragnar Thomsen committed
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
/*
 * ark -- archiver for the KDE project
 *
 * Copyright (c) 2016 Ragnar Thomsen <rthomsen6@gmail.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.
 *
 */

#include "archivesortfiltermodel.h"
#include "archiveentry.h"
#include "archivemodel.h"

using namespace Kerfuffle;

ArchiveSortFilterModel::ArchiveSortFilterModel(QObject *parent)
29
    : QSortFilterProxyModel(parent)
Ragnar Thomsen's avatar
Ragnar Thomsen committed
30
{
31
32
    // always enable recursive fitlering
    setRecursiveFilteringEnabled(true);
Ragnar Thomsen's avatar
Ragnar Thomsen committed
33
34
35
36
37
38
39
40
41
42
43
}

ArchiveSortFilterModel::~ArchiveSortFilterModel()
{
}

bool ArchiveSortFilterModel::lessThan(const QModelIndex &leftIndex,
                                      const QModelIndex &rightIndex) const
{
    ArchiveModel *srcModel = qobject_cast<ArchiveModel*>(sourceModel());
    const int col = srcModel->shownColumns().at(leftIndex.column());
Elvis Angelaccio's avatar
Elvis Angelaccio committed
44
    const QByteArray property = srcModel->propertiesMap().value(col);
Ragnar Thomsen's avatar
Ragnar Thomsen committed
45
46
47
48
49
50
51
52
53
54
55
56

    const Archive::Entry *left = srcModel->entryForIndex(leftIndex);
    const Archive::Entry *right = srcModel->entryForIndex(rightIndex);

    if (left->isDir() && !right->isDir()) {
        return true;
    } else if (!left->isDir() && right->isDir()) {
        return false;
    } else {
        switch (col) {
        case Size:
        case CompressedSize:
57
            if (left->property(property.constData()).toULongLong() < right->property(property.constData()).toULongLong()) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
58
59
60
61
                return true;
            }
            break;
        default:
62
            if (left->property(property.constData()).toString() < right->property(property.constData()).toString()) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
63
64
65
66
67
68
                return true;
            }
        }
    }
    return false;
}