-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqfilesystemmodelimpl.cpp
executable file
·123 lines (107 loc) · 3.36 KB
/
qfilesystemmodelimpl.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
#include "qfilesystemmodelimpl.h"
QFileSystemModelImpl::QFileSystemModelImpl(QTreeView *treeView)
: QFileSystemModel(),
m_treeView(treeView)
{
}
Qt::ItemFlags QFileSystemModelImpl::flags(const QModelIndex &index) const
{
return Qt::ItemIsUserCheckable|QFileSystemModel::flags(index);
}
QVariant QFileSystemModelImpl::data(const QModelIndex &index, int role) const
{
if(!index.isValid()){
return QVariant();
}
if (index.column() == 0 && role == Qt::CheckStateRole)
{
if(isDir(index)) {
return QVariant();
}
if (m_indexMap.contains(index))
{
return m_indexMap[index] ? Qt::Checked : Qt::Unchecked;
}
else
{
int iChecked = Qt::Unchecked;
QModelIndex _parentIndex = index.parent();
//check if this node's parent is checked
while(_parentIndex.isValid())
{
if (m_indexMap[_parentIndex])
{
iChecked = Qt::Checked;
break;
}
else
{
_parentIndex = _parentIndex.parent();
}
}
if (iChecked == Qt::Checked)
{
m_indexMap[index] = true;
m_pathMap[filePath((index))] = true;
if(hasChildren(index) && rowCount(index) == 0){
m_treeView->expand(index);
}
}
else
{
m_indexMap[index] = false;
m_pathMap[filePath((index))] = false;
}
return iChecked;
}
}
return QFileSystemModel::data(index, role);
}
bool QFileSystemModelImpl::setData( const QModelIndex &index, const QVariant &value, int role /*= Qt::EditRole*/ )
{
if(hasChildren(index) && rowCount(index) == 0){
m_treeView->expand(index);
}
if (role == Qt::CheckStateRole && index.column() == 0)
{
if (value == Qt::Unchecked)
{
m_indexMap[index] = false;
m_pathMap[filePath((index))] = false;
//refresh it's child node
emit dataChanged(index, index);
}
else if (value == Qt::Checked)
{
m_indexMap[index] = true;
m_pathMap[filePath((index))] = true;
//refresh it's child node
emit dataChanged(index, index);
}
if (hasChildren(index))
{
QString strFilePath = filePath(index);
int iChildCount = rowCount(index);
if (iChildCount > 0)
{
for (int i = 0; i < iChildCount; i++)
{
QModelIndex _child = this->index(i, 0, index);
setData(_child, value,Qt::CheckStateRole);
}
}
}
}
return QFileSystemModel::setData(index, value, role);
}
QSet<QString> QFileSystemModelImpl::getSelectedFiles() const
{
QSet<QString> ret;
QMap<QString,bool>::const_iterator it = m_pathMap.constBegin();
for(;it!=m_pathMap.constEnd(); ++it) {
if (it.value()) {
ret.insert(it.key());
}
}
return ret;
}