-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqcheckableproxymodel.cpp
More file actions
161 lines (134 loc) · 4.56 KB
/
qcheckableproxymodel.cpp
File metadata and controls
161 lines (134 loc) · 4.56 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
#include "qcheckableproxymodel.h"
QCheckableProxyModel::QCheckableProxyModel(QObject *parent) :
QIdentityProxyModel(parent),
m_selection(new QItemSelectionModel(this))
{
connect(m_selection, &QItemSelectionModel::selectionChanged, this, &QCheckableProxyModel::onSelectionChanged);
}
QVariant QCheckableProxyModel::data(const QModelIndex &index, int role) const
{
if(!sourceModel())
return QVariant();
if (role == Qt::CheckStateRole)
{
if (index.column() != 0)
{
return QVariant();
}
if (!m_selection)
{
return Qt::Unchecked;
}
QVariant ret = m_selection->selection().contains(mapToSource(index)) ? Qt::Checked : Qt::Unchecked;
return ret;
}
return QIdentityProxyModel::data(index, role);
}
bool QCheckableProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(!sourceModel())
return false;
if (role == Qt::CheckStateRole)
{
if (index.column() != 0)
{
return false;
}
if (!m_selection)
{
return false;
}
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
const QModelIndex srcIndex = mapToSource(index);
m_selection->select(QItemSelection(srcIndex, srcIndex), state == Qt::Checked ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
emit this->dataChanged(index, index);
return true;
}
return QIdentityProxyModel::setData(index, value, role);
}
Qt::ItemFlags QCheckableProxyModel::flags(const QModelIndex &index) const
{
if (!index.isValid() || index.column() != 0)
{
return QIdentityProxyModel::flags(index);
}
return QIdentityProxyModel::flags(index) | Qt::ItemIsUserCheckable;
}
QHash<int, QByteArray> QCheckableProxyModel::roleNames() const
{
return m_roleNames;
}
void QCheckableProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
{
if (sourceModel && (sourceModel->roleNames().isEmpty())) { // workaround for when a model has no roles and roles are added when the model is populated
// QTBUG-57971
connect(sourceModel, &QAbstractItemModel::rowsInserted, this, &QCheckableProxyModel::initRoles);
connect(sourceModel, &QAbstractItemModel::modelReset, this, &QCheckableProxyModel::initRoles);
}
QIdentityProxyModel::setSourceModel(sourceModel);
m_selection->setModel(sourceModel);
}
void QCheckableProxyModel::resetInternalData()
{
QIdentityProxyModel::resetInternalData();
updateRoleNames();
}
QList<int> QCheckableProxyModel::selectedRows() const
{
QModelIndexList indexList = m_selection->selection().indexes();
QList<int> ret;
for(int i=0;i<indexList.size();i++)
{
ret+=indexList.at(i).row();
}
return ret;
}
bool QCheckableProxyModel::isChecked(int index) const
{
return m_selection->isSelected(this->index(index,0));
}
void QCheckableProxyModel::clear()
{
m_selection->clear();
}
void QCheckableProxyModel::toggle(int index)
{
const QModelIndex srcIndex = mapToSource(this->index(index,0));
Qt::CheckState oldState = (Qt::CheckState)data(srcIndex, Qt::CheckStateRole).toInt();
setData(srcIndex, oldState == Qt::Checked ? QItemSelectionModel::Deselect : QItemSelectionModel::Select, Qt::CheckStateRole);
}
QItemSelectionModel *QCheckableProxyModel::getSelection() const
{
return m_selection;
}
void QCheckableProxyModel::onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
const auto lstSelected = mapSelectionFromSource(selected);
for (const QItemSelectionRange &range : lstSelected)
{
emit this->dataChanged(range.topLeft(), range.bottomRight());
}
const auto lstDeselected = mapSelectionFromSource(deselected);
for (const QItemSelectionRange &range : lstDeselected)
{
emit this->dataChanged(range.topLeft(), range.bottomRight());
}
}
void QCheckableProxyModel::updateRoleNames()
{
if(sourceModel() && !sourceModel()->roleNames().isEmpty())
{
QHash<int, QByteArray> roles = sourceModel()->roleNames();
roles[Qt::CheckStateRole] = QByteArrayLiteral("checkState");
m_roleNames = roles;
}
}
void QCheckableProxyModel::initRoles()
{
if(sourceModel() && !sourceModel()->roleNames().isEmpty())
{
disconnect(sourceModel(), &QAbstractItemModel::rowsInserted, this, &QCheckableProxyModel::initRoles);
disconnect(sourceModel(), &QAbstractItemModel::modelReset, this, &QCheckableProxyModel::initRoles);
resetInternalData();
}
}