1 #ifndef DATA_SYNCTHINGSORTFILTERDIRECTORYMODEL_H
2 #define DATA_SYNCTHINGSORTFILTERDIRECTORYMODEL_H
3 
4 #include "./global.h"
5 
6 #include <QSortFilterProxyModel>
7 
8 namespace Data {
9 
10 enum class SyncthingSortBehavior {
11     KeepRawOrder,
12     Alphabetically,
13 };
14 
15 class LIB_SYNCTHING_MODEL_EXPORT SyncthingSortFilterModel : public QSortFilterProxyModel {
16     Q_OBJECT
17 public:
18     explicit SyncthingSortFilterModel(QAbstractItemModel *sourceModel = nullptr, QObject *parent = nullptr);
19 
20     SyncthingSortBehavior behavior() const;
21     void setBehavior(SyncthingSortBehavior behavior);
22 
23 protected:
24     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
25     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
26 
27 private:
28     SyncthingSortBehavior m_behavior;
29 };
30 
SyncthingSortFilterModel(QAbstractItemModel * sourceModel,QObject * parent)31 inline SyncthingSortFilterModel::SyncthingSortFilterModel(QAbstractItemModel *sourceModel, QObject *parent)
32     : QSortFilterProxyModel(parent)
33     , m_behavior(SyncthingSortBehavior::Alphabetically)
34 {
35     setSortCaseSensitivity(Qt::CaseInsensitive);
36     setFilterCaseSensitivity(Qt::CaseInsensitive);
37     setSourceModel(sourceModel);
38 }
39 
behavior()40 inline SyncthingSortBehavior SyncthingSortFilterModel::behavior() const
41 {
42     return m_behavior;
43 }
44 
setBehavior(SyncthingSortBehavior behavior)45 inline void SyncthingSortFilterModel::setBehavior(SyncthingSortBehavior behavior)
46 {
47     if (behavior != m_behavior) {
48         m_behavior = behavior;
49         invalidate();
50     }
51 }
52 
53 } // namespace Data
54 
55 #endif // DATA_SYNCTHINGSORTFILTERDIRECTORYMODEL_H
56