-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilesystemmodel.cpp
121 lines (109 loc) · 3.97 KB
/
filesystemmodel.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "filesystemmodel.h"
#include <QDebug>
#include <QStorageInfo>
FileSystemModel::FileSystemModel(QObject *parent)
: QAbstractListModel(parent),
dir(QDir("/"))
{
updateDirInfo();
}
void FileSystemModel::updateDirInfo (void){
beginResetModel();
items.clear();
if(dir.isRoot()){
items.append(Item({cdItemName, "drives", Back}));
QFileInfoList currentDir = dir.entryInfoList(QStringList("*.log"),
QDir::NoDotAndDotDot | QDir::Dirs |
QDir::AllDirs | QDir::Files,
QDir::DirsFirst | QDir::Name);
for(int i = 0; i < currentDir.count(); i++){
items.append(Item({currentDir.at(i).fileName(),
currentDir.at(i).absoluteFilePath(),
currentDir.at(i).isFile() ? File : Dir}));
}
}
else {
QString currentPath = dir.absolutePath();
dir.cdUp();
items.append(Item({cdItemName, dir.absolutePath(), Back}));
dir.cd(currentPath);
QFileInfoList currentDir = dir.entryInfoList(QStringList("*.log"),
QDir::NoDotAndDotDot | QDir::Dirs |
QDir::AllDirs | QDir::Files,
QDir::DirsFirst | QDir::Name);
for(int i = 0; i < currentDir.count(); i++){
items.append(Item({currentDir.at(i).fileName(),
currentDir.at(i).absoluteFilePath(),
currentDir.at(i).isFile() ? File : Dir}));
}
}
endResetModel();
emit currentPathChanged(currentPath());
}
void FileSystemModel::updateDrivesInfo(void){ // This fills items list of drives names as normal files/folders
beginResetModel(); // but without "back" item
items.clear();
QFileInfoList drives = dir.drives();
for(int i = 0; i < drives.count(); i++){
QString driveName = QStorageInfo(drives.at(i).absoluteFilePath()).name();
driveName += " (" + drives.at(i).absoluteFilePath() + ")";
items.append(Item({driveName, drives.at(i).absoluteFilePath(), Drive}));
}
endResetModel();
emit currentPathChanged(currentPath());
}
QString FileSystemModel::currentPath(void) const{
return dir.absolutePath();
}
void FileSystemModel::setCurrentPath(QString){
}
int FileSystemModel::rowCount(const QModelIndex &parent) const{
Q_UNUSED(parent)
return items.count();
}
QHash<int, QByteArray> FileSystemModel::roleNames(void) const{
static QHash<int, QByteArray> roles = {
{NAME, "name"},
{DIR, "dir"},
{DIR_UP, "dir_up"},
{DRIVE, "drive"},
{FULL_PATH, "fullPath"}
};
return roles;
}
QVariant FileSystemModel::data(const QModelIndex &index, int role) const{
const int row = index.row();
if(items.count() < row)
return QVariant();
const auto& item = items.at(row);
switch (role) {
case NAME:
return item.name;
case DIR:
return (item.type == Dir ? true : false);
case DIR_UP:
return (item.type == Back ? true : false);
case DRIVE:
return (item.type == Drive ? true : false);
case FULL_PATH:
return item.fullPath;
default:
return QVariant();
}
}
void FileSystemModel::cdUp(void){
if(dir.isRoot()) // When user want to move up from current directory, but it's root directory
updateDrivesInfo(); // we introduce an abstraction to show drives as folders with special role
else {
dir.cdUp();
updateDirInfo();
}
}
void FileSystemModel::cd(const QString& path){
if(path == "drives") // path "drives" is abstraction level to show drives as folders
updateDrivesInfo();
else {
dir = QDir(path);
updateDirInfo();
}
}