-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirInfoModel.cpp
139 lines (117 loc) · 4.04 KB
/
DirInfoModel.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "DirInfoModel.hpp"
#include <QDirIterator>
DirInfoModel::DirInfoModel()
: TABLE_SECTIONS {
tr("Type"),
tr("Files count"),
tr("Total size"),
tr("Average size"),
} {
}
bool DirInfoModel::scanDirectory(const QString &dirPath, size_t &subDirsCount, QAtomicInt::QAtomicInteger &isTerminateScanningNeeded) {
QFlags<QDir::Filter> additionalFilters = QDir::NoDotAndDotDot | QDir::Hidden | QDir::System | QDir::Readable | QDir::Writable | QDir::Executable;
subDirsCount = QDir(dirPath).entryList(QDir::AllDirs | additionalFilters).size();
QMap<QString, FileGroupInfo> groupsData;
QDirIterator dirIt(dirPath, QDir::AllDirs | QDir::Files | additionalFilters, QDirIterator::Subdirectories);
while (true) {
if (isTerminateScanningNeeded) {
isTerminateScanningNeeded = false;
return false;
}
QFileInfo fileInfo = dirIt.fileInfo();
if (fileInfo.isFile() || fileInfo.isShortcut()) {
QString fileExtension = fileInfo.suffix().toLower();
FileGroupInfo &info = groupsData[fileExtension];
info.name = fileExtension;
info.filesCount++;
info.totalSize += fileInfo.size();
}
if (dirIt.hasNext()) {
dirIt.next();
}
else {
break;
}
}
FileGroupInfo totalInfo;
totalInfo.name = tr("All types");
for (auto &group : groupsData) {
if (group.filesCount > 0) {
group.avgSize = group.totalSize / group.filesCount;
}
totalInfo.filesCount += group.filesCount;
totalInfo.totalSize += group.totalSize;
}
if (totalInfo.filesCount > 0) {
totalInfo.avgSize = totalInfo.totalSize / totalInfo.filesCount;
}
this->beginResetModel();
mGroupsData = groupsData.values().toVector();
mTotalInfo = totalInfo;
this->endResetModel();
return true;
}
int DirInfoModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
if (mGroupsData.size() > 0) {
return mGroupsData.size() + 1;
}
else {
return 0;
}
}
int DirInfoModel::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return static_cast<int>(TABLE_SECTIONS.size());
}
QVariant DirInfoModel::data(const QModelIndex &index, int role) const {
if (role == Qt::DisplayRole) {
if (index.row() == 0) {
switch (index.column()) {
case 0: return QVariant(mTotalInfo.name); break;
case 1: return QVariant(mTotalInfo.filesCount); break;
case 2: return QVariant(mGetHumanReadableSize(mTotalInfo.totalSize)); break;
case 3: return QVariant(mGetHumanReadableSize(mTotalInfo.avgSize)); break;
}
}
else {
const FileGroupInfo &group = mGroupsData.at(index.row() - 1);
switch (index.column()) {
case 0: return QVariant(group.name); break;
case 1: return QVariant(group.filesCount); break;
case 2: return QVariant(mGetHumanReadableSize(group.totalSize)); break;
case 3: return QVariant(mGetHumanReadableSize(group.avgSize)); break;
}
}
}
return QVariant();
}
QVariant DirInfoModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal) {
return TABLE_SECTIONS[section];
}
else {
return QVariant(section + 1);
}
}
else {
return QVariant();
}
}
QString DirInfoModel::mGetHumanReadableSize(size_t size) {
static const std::array<QString, 5> unitsStr = {
tr("B"),
tr("KB"),
tr("MB"),
tr("GB"),
tr("TB"),
};
double new_size = size;
size_t unitOrder = 0;
while (new_size > 1024.0 && (unitOrder + 1) < unitsStr.size()) {
new_size /= 1024.0;
unitOrder++;
}
return QString("%1 %2").arg(new_size, 0, 'f', 2).arg(unitsStr[unitOrder]);
}