-
Notifications
You must be signed in to change notification settings - Fork 142
/
LVGLObjectModel.cpp
148 lines (126 loc) · 3.39 KB
/
LVGLObjectModel.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
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
#include "LVGLObjectModel.h"
#include <QFont>
#include <QDebug>
LVGLObjectModel::LVGLObjectModel(QObject *parent)
: QAbstractItemModel(parent)
, m_current(nullptr)
{
}
QVariant LVGLObjectModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole))
return QVariant();
if (section == 0)
return "Object";
else if (section == 1)
return "Class";
return QVariant();
}
QModelIndex LVGLObjectModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (parent.isValid()) {
const LVGLObject *p = static_cast<const LVGLObject*>(parent.internalPointer());
return createIndex(row, column, p->childs().at(row));
}
return createIndex(row, column, lvgl.topLevelObjects().at(row));
}
QModelIndex LVGLObjectModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
LVGLObject *o = static_cast<LVGLObject*>(index.internalPointer());
LVGLObject *p = o->parent();
if (p == nullptr)
return QModelIndex();
const int row = p->childs().indexOf(o);
return createIndex(row, 0, p);
}
int LVGLObjectModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
const LVGLObject *p = static_cast<const LVGLObject*>(parent.internalPointer());
const int size = p->childs().size();
//qDebug() << size;
return size;
}
auto top = lvgl.topLevelObjects();
const int size = top.size();
//qDebug() << size;
return size;
}
int LVGLObjectModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 2;
}
QVariant LVGLObjectModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
const LVGLObject *o = reinterpret_cast<const LVGLObject*>(index.internalPointer());
if (role == Qt::DisplayRole) {
if (index.column() == 0) {
return o->name();
} else if (index.column() == 1) {
return o->widgetClass()->name();
}
} else if (role == Qt::FontRole) {
QFont font;
font.setBold(m_current == o);
return font;
}
return QVariant();
}
void LVGLObjectModel::beginInsertObject(LVGLObject *obj)
{
int row = 0;
LVGLObject *p = obj->parent();
QModelIndex parent;
if (p) {
row = p->childs().indexOf(obj);
parent = objIndex(p, 0);
}
beginInsertRows(parent, row, row);
}
void LVGLObjectModel::endInsertObject()
{
endInsertRows();
}
void LVGLObjectModel::beginRemoveObject(LVGLObject *obj)
{
int row = 0;
LVGLObject *p = obj->parent();
QModelIndex parent;
if (p) {
row = p->childs().indexOf(obj);
parent = objIndex(p, 0);
}
beginRemoveRows(parent, row, row);
}
void LVGLObjectModel::endRemoveObject()
{
endRemoveRows();
}
LVGLObject *LVGLObjectModel::object(const QModelIndex &index) const
{
if (index.isValid())
return reinterpret_cast<LVGLObject*>(index.internalPointer());
return nullptr;
}
void LVGLObjectModel::setCurrentObject(LVGLObject *obj)
{
LVGLObject *old = m_current;
m_current = obj;
emit dataChanged(objIndex(obj, 0), objIndex(obj, 1), QVector<int>({Qt::FontRole}));
emit dataChanged(objIndex(old, 0), objIndex(old, 1), QVector<int>({Qt::FontRole}));
}
QModelIndex LVGLObjectModel::objIndex(LVGLObject *obj, int col) const
{
if (obj == nullptr)
return QModelIndex();
LVGLObject *p = obj->parent();
if (p) return createIndex(p->childs().indexOf(obj), col, obj);
return createIndex(lvgl.topLevelObjects().indexOf(obj), col, obj);
}