-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirTreeModel.h
More file actions
63 lines (50 loc) · 1.83 KB
/
DirTreeModel.h
File metadata and controls
63 lines (50 loc) · 1.83 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
#ifndef DIRTREEMODEL_H
#define DIRTREEMODEL_H
#include <QAbstractItemModel>
#include <QDir>
#include <QFileInfoList>
#include <QFileIconProvider>
#include <QThread>
#include <QObject>
#include <QCoreApplication>
#include <QtConcurrent>
#include <QFuture>
class DirTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit DirTreeModel(QObject* parent = nullptr);
~DirTreeModel() override;
void setRootToDrives();
QModelIndex expandToPath(const QString& path);
void loadNode(const QModelIndex& index);
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& index) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override { return 1; };
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
private:
struct Node {
QString path;
QFileInfoList dirList;
Node* parent;
QList<Node*> children;
QIcon icon;
bool populated = false;
bool isLoading = false;
int hasChildren = -1; // -1 - неизвестно, 0 - нет, 1 - да
int fileCount;
Node(const QString& path, Node* parent = nullptr) : path(path), parent(parent) {}
~Node() { qDeleteAll(children); }
};
Node* root = nullptr;
mutable QHash<QString, Node*> nodeCache;
void populateNode(Node* node);
Node* nodeFromIndex(const QModelIndex& index) const;
QModelIndex indexForNode(Node* node) const;
QFileIconProvider iconProvider;
private slots:
void onPopulateFinished(Node* node, const QFileInfoList& dirList);
};
#endif // DIRTREEMODEL_H