File indexing completed on 2024-05-12 17:22:02

0001 /*
0002     SPDX-FileCopyrightText: 2002 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2002 Rafi Yanai <yanai@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KRSORT_H
0010 #define KRSORT_H
0011 
0012 #include <sys/types.h>
0013 
0014 // QtCore
0015 #include <QString>
0016 #include <QVariant>
0017 #include <QVector>
0018 
0019 class FileItem;
0020 class KrViewProperties;
0021 
0022 /** Implements sorting for the panel list model. */
0023 namespace KrSort
0024 {
0025 
0026 class SortProps
0027 {
0028 public:
0029     SortProps()
0030     {
0031     }
0032     SortProps(const SortProps &other)
0033     {
0034         init(other.fileitem(), other.column(), other.properties(), other.isDummy(), other.isAscending(), other.originalIndex(), other.customData());
0035     }
0036     SortProps(FileItem *fileitem, int col, const KrViewProperties *props, bool isDummy, bool asc, int origNdx, QVariant customData)
0037     {
0038         init(fileitem, col, props, isDummy, asc, origNdx, customData);
0039     }
0040 
0041     inline int column() const
0042     {
0043         return _col;
0044     }
0045     inline const KrViewProperties *properties() const
0046     {
0047         return _prop;
0048     }
0049     inline bool isDummy() const
0050     {
0051         return _isdummy;
0052     }
0053     inline bool isAscending() const
0054     {
0055         return _ascending;
0056     }
0057     inline QString name() const
0058     {
0059         return _name;
0060     }
0061     inline QString extension() const
0062     {
0063         return _ext;
0064     }
0065     inline FileItem *fileitem() const
0066     {
0067         return _fileItem;
0068     }
0069     inline int originalIndex() const
0070     {
0071         return _index;
0072     }
0073     inline QString data() const
0074     {
0075         return _data;
0076     }
0077     inline const QVariant &customData() const
0078     {
0079         return _customData;
0080     }
0081 
0082 private:
0083     void init(FileItem *fileitem, int col, const KrViewProperties *props, bool isDummy, bool asc, int origNdx, QVariant customData);
0084 
0085     int _col;
0086     const KrViewProperties *_prop;
0087     bool _isdummy;
0088     FileItem *_fileItem;
0089     bool _ascending;
0090     QString _name;
0091     QString _ext;
0092     int _index;
0093     QString _data;
0094     QVariant _customData;
0095 };
0096 
0097 bool compareTexts(QString aS1, QString aS2, const KrViewProperties *_viewProperties, bool asc, bool isName);
0098 bool itemLessThan(SortProps *sp, SortProps *sp2);
0099 bool itemGreaterThan(SortProps *sp, SortProps *sp2);
0100 bool compareTime(time_t time1, time_t time2, SortProps *sp, SortProps *sp2);
0101 
0102 typedef bool (*LessThanFunc)(SortProps *, SortProps *);
0103 
0104 class Sorter
0105 {
0106 public:
0107     Sorter(int reserveItems, const KrViewProperties *viewProperties, LessThanFunc lessThanFunc, LessThanFunc greaterThanFunc);
0108     Sorter(const Sorter &other);
0109 
0110     const QVector<SortProps *> &items() const
0111     {
0112         return _items;
0113     }
0114     void sort();
0115     void addItem(FileItem *fileitem, bool isDummy, int idx, QVariant customData);
0116     int insertIndex(FileItem *fileitem, bool isDummy, QVariant customData);
0117 
0118 private:
0119     bool descending() const;
0120 
0121     const KrViewProperties *_viewProperties;
0122     QVector<SortProps *> _items;
0123     QVector<SortProps> _itemStore;
0124     LessThanFunc _lessThanFunc, _greaterThanFunc;
0125 };
0126 
0127 } // namespace KrSort
0128 
0129 #endif // KRSORT_H