~ubuntu-branches/debian/jessie/acoustid-fingerprinter/jessie

« back to all changes in this revision

Viewing changes to checkabledirmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2011-05-12 19:54:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110512195452-bj6g545s1bx7edvc
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QDebug>
 
2
#include "checkabledirmodel.h"
 
3
 
 
4
static const int CHECKABLE_COLUMN = 0;
 
5
 
 
6
Qt::ItemFlags CheckableDirModel::flags(const QModelIndex& index) const
 
7
{
 
8
        Qt::ItemFlags flags = QFileSystemModel::flags(index);
 
9
        if (index.column() == CHECKABLE_COLUMN) {
 
10
                flags |= Qt::ItemIsUserCheckable;
 
11
        }
 
12
        return flags;
 
13
}
 
14
 
 
15
Qt::CheckState CheckableDirModel::findParentState(const QModelIndex &index) const
 
16
{
 
17
        if (!index.isValid()) {
 
18
                return Qt::Unchecked;
 
19
        }
 
20
        if (hasIndexState(index)) {
 
21
                return getIndexState(index);
 
22
        }
 
23
        //qDebug() << "Looking up parent state for " << filePath(index);
 
24
        Qt::CheckState state = findParentState(index.parent());
 
25
        setIndexState(index, state);
 
26
        return state;
 
27
}
 
28
 
 
29
QVariant CheckableDirModel::data(const QModelIndex& index, int role) const
 
30
{
 
31
        if (index.isValid() && index.column() == CHECKABLE_COLUMN && role == Qt::CheckStateRole) {
 
32
                return findParentState(index);
 
33
        }
 
34
        return QFileSystemModel::data(index, role);
 
35
}
 
36
 
 
37
void CheckableDirModel::updateParents(const QModelIndex &parent)
 
38
{
 
39
        if (!parent.isValid()) {
 
40
                return;
 
41
        }
 
42
 
 
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)));
 
47
        }
 
48
 
 
49
        if (childStates.size() > 1) {
 
50
                setIndexState(parent, Qt::PartiallyChecked);
 
51
                emit dataChanged(parent, parent);
 
52
        }
 
53
        else if (childStates.size() == 1) {
 
54
                setIndexState(parent, *childStates.begin());
 
55
                emit dataChanged(parent, parent);
 
56
        }
 
57
 
 
58
        updateParents(parent.parent());
 
59
}
 
60
 
 
61
void CheckableDirModel::updateVisibleChildren(const QModelIndex &parent, Qt::CheckState state)
 
62
{
 
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);
 
68
                }
 
69
                setIndexState(child, state);
 
70
                emit dataChanged(child, child);
 
71
        }
 
72
}
 
73
 
 
74
bool CheckableDirModel::setData(const QModelIndex& index, const QVariant& value, int role)
 
75
{
 
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);
 
82
                return true;
 
83
        }
 
84
        return QFileSystemModel::setData(index, value, role);
 
85
}
 
86
 
 
87
QList<QString> CheckableDirModel::selectedDirectories()
 
88
{
 
89
        return m_state.keys(Qt::Checked);
 
90
}
 
91