-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathSimpleTreeModel.cpp
78 lines (71 loc) · 1.91 KB
/
SimpleTreeModel.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
#include "SimpleTreeModel.h"
#include <QDebug>
SimpleTreeModel::SimpleTreeModel(QObject *parent)
: QStandardItemModel(parent)
{
reset();
}
QVariant SimpleTreeModel::data(const QModelIndex &index, int role) const
{
QStandardItem *item = itemFromIndex(index);
if (!item)
return QVariant();
if (role == Qt::DisplayRole) {
return item->data(role);
}
return QVariant();
}
void SimpleTreeModel::appendNode(const QModelIndex &index, const QString &text)
{
QStandardItem *item = itemFromIndex(index);
if (!item)
return;
QStandardItem *newitem = new QStandardItem(text);
newitem->setData(text, Qt::DisplayRole);
item->appendRow(newitem);
}
void SimpleTreeModel::removeNode(const QModelIndex &index)
{
QStandardItem *item = itemFromIndex(index);
if (!item)
return;
QStandardItem *parentitem = item->parent();
if (parentitem) {
parentitem->removeRow(item->row());
} else {
removeRow(item->row());
}
}
void SimpleTreeModel::reset()
{
beginResetModel();
{
const QSignalBlocker blocker(this); (void)blocker;
for (int i = 0; i < 10; i++)
{
QStandardItem *top = new QStandardItem(QString("Top %1").arg(i));
appendRow(top);
for (int j = 0; j < 3; j++)
{
QStandardItem *sub = new QStandardItem(QString("Sub %1 %2").arg(i).arg(j));
top->appendRow(sub);
for (int k = 0; k < 3; k++)
{
QStandardItem *inner = new QStandardItem(QString("In %1").arg(k));
sub->appendRow(inner);
}
}
}
}
endResetModel();
}
void SimpleTreeModel::clear()
{
beginResetModel();
{
const QSignalBlocker blocker(this); (void)blocker;
if (rowCount() > 0)
removeRows(0, rowCount());
}
endResetModel();
}