-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfilesystemmodel.cpp
More file actions
271 lines (239 loc) · 7.14 KB
/
filesystemmodel.cpp
File metadata and controls
271 lines (239 loc) · 7.14 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "filesystemmodel.h"
#include <QFileInfo>
#include <QDir>
#include <algorithm>
#include <QFileIconProvider>
#include <QDateTime>
#include <QDebug>
FilesystemModel::FilesystemModel(QObject *parent) :
QAbstractItemModel(parent),
_metaProvider(new QFileIconProvider())
{
fetchRootDirectory();
}
struct FilesystemModel::NodeInfo
{
NodeInfo():
parent(0),
mapped(false)
{}
NodeInfo(const QFileInfo& fileInfo, NodeInfo* parent = 0):
fileInfo(fileInfo),
parent(parent),
mapped(false)
{}
bool operator ==(const NodeInfo& another) const
{
bool r = this->fileInfo == another.fileInfo;
Q_ASSERT(!r || this->parent == another.parent);
Q_ASSERT(!r || this->mapped == another.mapped);
Q_ASSERT(!r || this->children == another.children);
return r;
}
QFileInfo fileInfo;
QVector<NodeInfo> children;
NodeInfo* parent;
bool mapped;
};
FilesystemModel::~FilesystemModel()
{}
QModelIndex FilesystemModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
if (!parent.isValid()) {
Q_ASSERT(_nodes.size() > row);
return createIndex(row, column, const_cast<NodeInfo*>(&_nodes[row]));
}
NodeInfo* parentInfo = static_cast<NodeInfo*>(parent.internalPointer());
Q_ASSERT(parentInfo != 0);
Q_ASSERT(parentInfo->mapped);
Q_ASSERT(parentInfo->children.size() > row);
return createIndex(row, column, &parentInfo->children[row]);
}
QModelIndex FilesystemModel::parent(const QModelIndex &child) const
{
if (!child.isValid()) {
return QModelIndex();
}
NodeInfo* childInfo = static_cast<NodeInfo*>(child.internalPointer());
Q_ASSERT(childInfo != 0);
NodeInfo* parentInfo = childInfo->parent;
if (parentInfo != 0) {
return createIndex(findRow(parentInfo), RamificationColumn, parentInfo);
}
else {
return QModelIndex();
}
}
int FilesystemModel::findRow(const NodeInfo *nodeInfo) const
{
Q_ASSERT(nodeInfo != 0);
const NodeInfoList& parentInfoChildren = nodeInfo->parent != 0 ? nodeInfo->parent->children: _nodes;
NodeInfoList::const_iterator position = qFind(parentInfoChildren, *nodeInfo);
Q_ASSERT(position != parentInfoChildren.end());
return std::distance(parentInfoChildren.begin(), position);
}
int FilesystemModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return _nodes.size();
}
const NodeInfo* parentInfo = static_cast<const NodeInfo*>(parent.internalPointer());
Q_ASSERT(parentInfo != 0);
return parentInfo->children.size();
}
bool FilesystemModel::hasChildren(const QModelIndex &parent) const
{
if (parent.isValid()) {
const NodeInfo* parentInfo = static_cast<const NodeInfo*>(parent.internalPointer());
Q_ASSERT(parentInfo != 0);
if (!parentInfo->mapped) {
return true;//QDir(parentInfo->fileInfo.absoluteFilePath()).count() > 0;
}
}
return QAbstractItemModel::hasChildren(parent);
}
int FilesystemModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return ColumnCount;
}
QVariant FilesystemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
const NodeInfo* nodeInfo = static_cast<NodeInfo*>(index.internalPointer());
const QFileInfo& fileInfo = nodeInfo->fileInfo;
Q_ASSERT(nodeInfo != 0);
switch (index.column()) {
case NameColumn:
return nameData(fileInfo, role);
case ModificationDateColumn:
if (role == Qt::DisplayRole) {
return fileInfo.lastModified();
}
break;
case SizeColumn:
if (role == Qt::DisplayRole) {
return fileInfo.isDir()? QVariant(): fileInfo.size();
}
break;
case TypeColumn:
if (role == Qt::DisplayRole) {
return _metaProvider->type(fileInfo);
}
break;
default:
break;
}
return QVariant();
}
QVariant FilesystemModel::nameData(const QFileInfo &fileInfo, int role) const
{
switch (role) {
case Qt::EditRole:
return fileInfo.fileName();
case Qt::DisplayRole:
if (fileInfo.isRoot()) {
return fileInfo.absoluteFilePath();
}
else if (fileInfo.isDir()){
return fileInfo.fileName();
}
else {
return fileInfo.completeBaseName();
}
case Qt::DecorationRole:
return _metaProvider->icon(fileInfo);
default:
return QVariant();
}
Q_UNREACHABLE();
}
bool FilesystemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid()) {
return false;
}
if (role != Qt::EditRole) {
return false;
}
if (index.column() != NameColumn) {
return false;
}
QString newName = value.toString();
if (newName.contains('/') || newName.contains(QDir::separator())) {
return false;
}
NodeInfo* nodeInfo = static_cast<NodeInfo*>(index.internalPointer());
QString fullNewName = nodeInfo->fileInfo.absoluteDir().path() +"/" + newName;
QString fullOldName = nodeInfo->fileInfo.absoluteFilePath();
qDebug() << fullOldName << fullNewName;
bool renamed = QFile::rename(fullOldName, fullNewName);
qDebug() << renamed;
if (renamed) {
nodeInfo->fileInfo = QFileInfo(fullNewName);
emit dataChanged(index, index.sibling(index.row(), ColumnCount));
}
return renamed;
}
QVariant FilesystemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
const QStringList headers = {"Имя", "Дата изменения", "Размер", "Тип"};
if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section < headers.size()) {
return headers[section];
}
return QVariant();
}
bool FilesystemModel::canFetchMore(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return false;
}
const NodeInfo* parentInfo = static_cast<const NodeInfo*>(parent.internalPointer());
Q_ASSERT(parentInfo != 0);
return !parentInfo->mapped;
}
void FilesystemModel::fetchMore(const QModelIndex &parent)
{
Q_ASSERT(parent.isValid());
NodeInfo* parentInfo = static_cast<NodeInfo*>(parent.internalPointer());
Q_ASSERT(parentInfo != 0);
Q_ASSERT(!parentInfo->mapped);
const QFileInfo& fileInfo = parentInfo->fileInfo;
Q_ASSERT(fileInfo.isDir());
QDir dir = QDir(fileInfo.absoluteFilePath());
QFileInfoList children = dir.entryInfoList(QStringList(), QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name);
int insrtCnt = children.size() - 1;
if (insrtCnt < 0) {
insrtCnt = 0;
}
beginInsertRows(parent, 0, insrtCnt);
parentInfo->children.reserve(children.size());
for (const QFileInfo& entry: children) {
NodeInfo nodeInfo(entry, parentInfo);
nodeInfo.mapped = !entry.isDir();
parentInfo->children.push_back(std::move(nodeInfo));
}
parentInfo->mapped = true;
endInsertRows();
}
void FilesystemModel::fetchRootDirectory()
{
const QFileInfoList drives = QDir::drives();
qCopy(drives.begin(), drives.end(), std::back_inserter(_nodes));
}
Qt::ItemFlags FilesystemModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
if (index.isValid() && index.column() == NameColumn) {
const NodeInfo* nodeInfo = static_cast<const NodeInfo*>(index.internalPointer());
if (!nodeInfo->fileInfo.isRoot()) {
flags |= Qt::ItemIsEditable;
}
}
return flags;
}