-
Notifications
You must be signed in to change notification settings - Fork 10
/
gcodeplayermodel.cpp
88 lines (74 loc) · 2.27 KB
/
gcodeplayermodel.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
#include <QQmlEngine>
#include "gcodeplayermodel.h"
GcodePlayerModel::GcodePlayerModel(QObject *parent)
{
}
void GcodePlayerModel::registerQmlTypes()
{
qmlRegisterType<GcodePlayerModel>("tech.vhrd", 1, 0, "GcodePlayerModel");
qmlRegisterUncreatableType<GcodePlayerItem>("tech.vhrd", 1, 0, "GcodePlayerItem", "enums only");
//qRegisterMetaType<GcodePlayerItem>("GcodePlayerItem");
}
int GcodePlayerModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_items.count();
}
QVariant GcodePlayerModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_items.count())
return QVariant();
const GcodePlayerItem& item = m_items[index.row()];
if (role == StatusRole)
return item.m_status;
else if (role == LineNumberRole)
return item.m_lineNumber;
else if (role == CodeRole)
return item.m_code;
else if (role == ResponseRole)
return item.m_response;
return QVariant();
}
QHash<int, QByteArray> GcodePlayerModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[StatusRole] = "status";
roles[LineNumberRole] = "lineNumber";
roles[CodeRole] = "code";
roles[ResponseRole] = "response";
return roles;
}
void GcodePlayerModel::addItem(const GcodePlayerItem &item)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_items << item;
endInsertRows();
}
GcodePlayerItem GcodePlayerModel::getItem(int index)
{
if (index < 0 || index >= m_items.length())
return GcodePlayerItem();
return m_items[index];
}
void GcodePlayerModel::replaceItem(int index, const GcodePlayerItem &item)
{
if (index < 0 || index > m_items.count())
return;
QModelIndex modelIndex = createIndex(index, 0);
m_items[index] = item;
emit dataChanged(modelIndex, modelIndex);
}
void GcodePlayerModel::changeAllStates(GcodePlayerItem::Status to)
{
for (int i = 0; i < m_items.length(); ++i)
m_items[i].m_status = to;
QModelIndex startIndex = createIndex(0, 0);
QModelIndex endIndex = createIndex(m_items.length() - 1, 0);
emit dataChanged(startIndex, endIndex);
}
void GcodePlayerModel::removeAll()
{
beginRemoveRows(QModelIndex(), 0, m_items.count());
m_items.clear();
endRemoveRows();
}