File indexing completed on 2024-04-28 05:47:43

0001 /*
0002     SPDX-FileCopyrightText: 2016 Ragnar Thomsen <rthomsen6@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "archivesortfiltermodel.h"
0008 #include "archiveentry.h"
0009 #include "archivemodel.h"
0010 
0011 using namespace Kerfuffle;
0012 
0013 ArchiveSortFilterModel::ArchiveSortFilterModel(QObject *parent)
0014     : QSortFilterProxyModel(parent)
0015 {
0016     // always enable recursive fitlering
0017     setRecursiveFilteringEnabled(true);
0018 }
0019 
0020 ArchiveSortFilterModel::~ArchiveSortFilterModel()
0021 {
0022 }
0023 
0024 bool ArchiveSortFilterModel::lessThan(const QModelIndex &leftIndex, const QModelIndex &rightIndex) const
0025 {
0026     ArchiveModel *srcModel = qobject_cast<ArchiveModel *>(sourceModel());
0027     const int col = srcModel->shownColumns().at(leftIndex.column());
0028     const QByteArray property = srcModel->propertiesMap().value(col);
0029 
0030     const Archive::Entry *left = srcModel->entryForIndex(leftIndex);
0031     const Archive::Entry *right = srcModel->entryForIndex(rightIndex);
0032 
0033     if (left->isDir() && !right->isDir()) {
0034         return true;
0035     } else if (!left->isDir() && right->isDir()) {
0036         return false;
0037     } else {
0038         switch (col) {
0039         case Size:
0040         case CompressedSize:
0041             if (left->property(property.constData()).toULongLong() < right->property(property.constData()).toULongLong()) {
0042                 return true;
0043             }
0044             break;
0045         default:
0046             if (left->property(property.constData()).toString() < right->property(property.constData()).toString()) {
0047                 return true;
0048             }
0049         }
0050     }
0051     return false;
0052 }
0053 
0054 #include "moc_archivesortfiltermodel.cpp"