-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemModel.h
More file actions
98 lines (82 loc) · 3.4 KB
/
FilesystemModel.h
File metadata and controls
98 lines (82 loc) · 3.4 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once
#include <QAbstractListModel>
#include <unordered_set>
#include <filesystem>
#include <deque>
#include <QString>
#include <QStringList>
namespace std
{
template<>
struct hash<std::filesystem::path>
{
std::size_t operator()(const std::filesystem::path& path)const
{
return std::hash<std::string>()(path.u8string());
}
};
}
namespace Hix
{
namespace QML
{
class FilesystemModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString folder READ folder WRITE setFolder NOTIFY folderChanged)
Q_PROPERTY(QString parentFolder READ parentFolder NOTIFY folderChanged)
Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters)
Q_PROPERTY(bool showFiles READ showFiles WRITE setShowFiles REVISION 1)
Q_PROPERTY(bool showDirs READ showDirs WRITE setShowDirs)
Q_PROPERTY(bool showDirsFirst READ showDirsFirst WRITE setShowDirsFirst)
Q_PROPERTY(bool sortReversed READ sortReversed WRITE setSortReversed)
Q_PROPERTY(SortField sortField READ sortField WRITE setSortField)
public:
explicit FilesystemModel(QObject* parent = nullptr);
int rowCount(const QModelIndex& parent) const override;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
enum class SortField { Unsorted, Name, Time, Size, Type };
Q_ENUM(SortField)
SortField sortField() const;
void setSortField(SortField field);
QString folder() const;
void setFolder(const QString& folder);
QString parentFolder() const;
bool showFiles() const;
void setShowFiles(bool showFiles);
bool showDirs() const;
void setShowDirs(bool showDirs);
bool showDirsFirst() const;
void setShowDirsFirst(bool showDirsFirst);
QStringList nameFilters() const;
void setNameFilters(const QStringList& filters);
bool sortReversed() const;
void setSortReversed(bool rev);
Q_INVOKABLE bool isFolder(int index) const;
Q_INVOKABLE QVariant get(int idx, const QString& property) const;
Q_INVOKABLE int indexOf(const QString& file) const;
Q_INVOKABLE QString getUSB() const;
Q_INVOKABLE bool fileExists(const QString& target) const;
signals:
void folderChanged();
void rowCountChanged() const;
void folderChangeError();
private:
std::filesystem::path _current;
QHash<int, QByteArray> _roleNames;
SortField _sortField = SortField::Name;
std::unordered_set<std::filesystem::path> _nameFilters;
bool _sortReversed = false;
bool _showFiles = true;
bool _showDirs = true;
bool _showDirsFirst = false;
bool _caseSensitive = true;
bool _sortCaseSensitive = true;
std::deque<std::filesystem::path> _data;
void updateSorting();
void applyNameFilter();
};
}
}