-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrootfilesysproxymodel.cpp
More file actions
194 lines (149 loc) · 5.05 KB
/
rootfilesysproxymodel.cpp
File metadata and controls
194 lines (149 loc) · 5.05 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
#include "rootfilesysproxymodel.h"
RootFileSysProxyModel::RootFileSysProxyModel(QFileSystemModel* model, const QString& rootAbsPath, QObject* parent):
QSortFilterProxyModel(parent),
fsm(model)
{
setSourceModel(fsm);
if(!rootAbsPath.isEmpty()) fsm->setRootPath(rootAbsPath);
}
QModelIndex RootFileSysProxyModel::mapFromSource(const QModelIndex& sourceIndex) const
{
if(!sourceIndex.isValid() || sourceIndex.row() < 0) return QModelIndex();
return ptrToProxyIdx[sourceIndex.internalPointer()];
}
QModelIndex RootFileSysProxyModel::mapToSource(const QModelIndex& proxyIndex) const
{
if(!proxyIndex.isValid()) return badSrcIndex();
if(!ptrToSrcIdx.contains(proxyIndex.internalPointer())) {return badSrcIndex();}
return ptrToSrcIdx[proxyIndex.internalPointer()];
}
QModelIndex RootFileSysProxyModel::buddy(const QModelIndex& index) const
{
if(!index.isValid()) return QModelIndex();
return mapFromSource(fsModel()->buddy(mapToSource(index)));
}
QModelIndex RootFileSysProxyModel::index(int row, int column, const QModelIndex& parent) const
{
if(column < 0 || column > 1) return QModelIndex();
QModelIndex sourceIndex;
if(!parent.isValid()) {sourceIndex = rootIndex(0);}
else {sourceIndex = fsModel()->index(row, column, mapToSource(parent));}
void* const ptr = sourceIndex.internalPointer();
ptrToSrcIdx[ptr] = sourceIndex;
ptrToProxyIdx[ptr] = createIndex(row, column, ptr);
return ptrToProxyIdx[ptr];
}
QModelIndex RootFileSysProxyModel::parent(const QModelIndex& idx) const
{
if(!idx.isValid()) return QModelIndex();
if(idx.internalPointer() == rootIndex(idx.column()).internalPointer())
{
return QModelIndex();
}
return mapFromSource(fsModel()->parent(mapToSource(idx)));
}
bool RootFileSysProxyModel::hasChildren(const QModelIndex& parent) const
{
if(!parent.isValid()) return true;
return fsModel()->hasChildren(mapToSource(parent));
}
int RootFileSysProxyModel::columnCount(const QModelIndex& parent) const
{
if(!parent.isValid()) return 1;
return fsModel()->columnCount(mapToSource(parent));
}
int RootFileSysProxyModel::rowCount(const QModelIndex& parent) const
{
if(!parent.isValid()) return 1;
if(fsModel()->fileInfo(mapToSource(parent)).isDir() && fsModel()->rowCount(mapToSource(parent)) == 0)
{
if(canFetchMore(parent)) const_cast<RootFileSysProxyModel*>(this)->fetchMore(parent);;
}
return fsModel()->rowCount(mapToSource(parent));
}
QVariant RootFileSysProxyModel::data(const QModelIndex& index, int role) const
{
if(role == Qt::CheckStateRole)
{
if(ptrToChecked[index.internalPointer()] ) return Qt::Checked;
else return Qt::Unchecked;
}
return fsModel()->data(mapToSource(index), role);
}
bool RootFileSysProxyModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (role == Qt::CheckStateRole && index.column() == 0)
{
ptrToChecked[index.internalPointer()] = static_cast<Qt::CheckState>(value.toInt()); //!ptrToChecked[index.internalPointer()];
emit dataChanged(index, index);
return true;
}
return fsModel()->setData(mapToSource(index), value, role);
}
Qt::ItemFlags RootFileSysProxyModel::flags(const QModelIndex& index) const
{
return fsModel()->flags(mapToSource(index)) | Qt::ItemIsUserCheckable;
}
QMimeData* RootFileSysProxyModel::mimeData(const QModelIndexList& indexes) const
{
if(indexes.isEmpty()) return fsModel()->mimeData(indexes);
QModelIndexList srcIndexes;
for(int i = 0; i < indexes.count(); ++i)
{
srcIndexes.append(mapToSource(indexes[i]));
}
return fsModel()->mimeData(srcIndexes);
}
QStringList RootFileSysProxyModel::mimeTypes() const
{
return fsModel()->mimeTypes();
}
QVariant RootFileSysProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return fsModel()->headerData(section, orientation, role);
}
bool RootFileSysProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role)
{
return fsModel()->setHeaderData(section, orientation, value, role);
}
QList<QFileInfo> RootFileSysProxyModel::checkedData(bool checked, int role) const
{
Q_UNUSED(role);
QMap<void*, bool>::const_iterator it = ptrToChecked.begin();
QList<QFileInfo> dataList;
for(; it != ptrToChecked.end(); ++it)
{
if(it.value() != checked) continue;
dataList.append(fsModel()->fileInfo(ptrToSrcIdx[it.key()]));
}
return dataList;
}
void RootFileSysProxyModel::fetchMore(const QModelIndex& parent)
{
fsModel()->fetchMore(mapToSource(parent));
}
bool RootFileSysProxyModel::canFetchMore(const QModelIndex& parent) const
{
return fsModel()->canFetchMore(mapToSource(parent));
}
QModelIndex RootFileSysProxyModel::sibling(int row, int column, const QModelIndex& index) const
{
if(!index.isValid()) return QModelIndex();
return fsModel()->sibling(row, column, mapToSource(index));;
}
const QFileSystemModel* RootFileSysProxyModel::fsModel() const
{
return fsm;
}
QFileSystemModel* RootFileSysProxyModel::fsModel()
{
return fsm;
}
QModelIndex RootFileSysProxyModel::rootIndex(int column) const
{
return fsModel()->index(fsModel()->rootPath(), column);
}
QModelIndex RootFileSysProxyModel::badSrcIndex() const
{
return fsModel()->index(-1, -1);
}