2
#include "checkabledirmodel.h"
4
static const int CHECKABLE_COLUMN = 0;
6
Qt::ItemFlags CheckableDirModel::flags(const QModelIndex& index) const
8
Qt::ItemFlags flags = QFileSystemModel::flags(index);
9
if (index.column() == CHECKABLE_COLUMN) {
10
flags |= Qt::ItemIsUserCheckable;
15
Qt::CheckState CheckableDirModel::findParentState(const QModelIndex &index) const
17
if (!index.isValid()) {
20
if (hasIndexState(index)) {
21
return getIndexState(index);
23
//qDebug() << "Looking up parent state for " << filePath(index);
24
Qt::CheckState state = findParentState(index.parent());
25
setIndexState(index, state);
29
QVariant CheckableDirModel::data(const QModelIndex& index, int role) const
31
if (index.isValid() && index.column() == CHECKABLE_COLUMN && role == Qt::CheckStateRole) {
32
return findParentState(index);
34
return QFileSystemModel::data(index, role);
37
void CheckableDirModel::updateParents(const QModelIndex &parent)
39
if (!parent.isValid()) {
43
int childRowCount = rowCount(parent);
44
QSet<Qt::CheckState> childStates;
45
for (int i = 0; i < childRowCount; i++) {
46
childStates.insert(getIndexState(index(i, CHECKABLE_COLUMN, parent)));
49
if (childStates.size() > 1) {
50
setIndexState(parent, Qt::PartiallyChecked);
51
emit dataChanged(parent, parent);
53
else if (childStates.size() == 1) {
54
setIndexState(parent, *childStates.begin());
55
emit dataChanged(parent, parent);
58
updateParents(parent.parent());
61
void CheckableDirModel::updateVisibleChildren(const QModelIndex &parent, Qt::CheckState state)
63
int childRowCount = rowCount(parent);
64
for (int i = 0; i < childRowCount; i++) {
65
const QModelIndex child = index(i, CHECKABLE_COLUMN, parent);
66
if (hasIndexState(child)) {
67
updateVisibleChildren(child, state);
69
setIndexState(child, state);
70
emit dataChanged(child, child);
74
bool CheckableDirModel::setData(const QModelIndex& index, const QVariant& value, int role)
76
if (index.isValid() && index.column() == CHECKABLE_COLUMN && role == Qt::CheckStateRole) {
77
Qt::CheckState state = Qt::CheckState(value.toInt());
78
setIndexState(index, state);
79
updateVisibleChildren(index, state);
80
updateParents(index.parent());
81
emit dataChanged(index, index);
84
return QFileSystemModel::setData(index, value, role);
87
QList<QString> CheckableDirModel::selectedDirectories()
89
return m_state.keys(Qt::Checked);