-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathgcodetablemodel.cpp
118 lines (98 loc) · 3.25 KB
/
gcodetablemodel.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
// This file is a part of "grblControl" application.
// Copyright 2015 Hayrullin Denis Ravilevich
#include "gcodetablemodel.h"
//GCodeItem::GCodeItem()
//{
// this->command = "";
// this->state = "";
// this->status = "";
// this->line = 0;
//}
GCodeTableModel::GCodeTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
m_headers << tr("#") << tr("Command") << tr("State") << tr("Response") << tr("Line") << tr("Args");
}
QVariant GCodeTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= m_data.size()) return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column())
{
case 0: return index.row() == this->rowCount() - 1 ? "" : QString::number(index.row() + 1);
case 1: return m_data.at(index.row())->command;
case 2: return m_data.at(index.row())->state;
case 3: return m_data.at(index.row())->status;
case 4: return m_data.at(index.row())->line;
case 5: return QVariant(m_data.at(index.row())->args);
}
}
if (role == Qt::TextAlignmentRole) {
switch (index.column()) {
case 0: return Qt::AlignCenter;
default: return Qt::AlignVCenter;
}
}
return QVariant();
}
bool GCodeTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
switch (index.column())
{
case 0: return false;
case 1: m_data.at(index.row())->command = value.toString(); break;
case 2: m_data.at(index.row())->state = value.toString(); break;
case 3: m_data.at(index.row())->status = value.toString(); break;
case 4: m_data.at(index.row())->line = value.toInt(); break;
case 5: m_data.at(index.row())->args = value.toStringList(); break;
}
emit dataChanged(index, index);
return true;
}
return false;
}
bool GCodeTableModel::insertRow(int row, const QModelIndex &parent)
{
if (row > rowCount()) return false;
beginInsertRows(parent, row, row);
m_data.insert(row, new GCodeItem());
endInsertRows();
return true;
}
bool GCodeTableModel::removeRow(int row, const QModelIndex &parent)
{
//if (!index(row, 0).isValid()) return false;
beginRemoveRows(parent, row, row);
m_data.removeAt(row);
endRemoveRows();
return true;
}
void GCodeTableModel::clear()
{
beginResetModel();
foreach (GCodeItem* item, m_data) delete item;
m_data.clear();
endResetModel();
}
int GCodeTableModel::rowCount(const QModelIndex &parent) const
{
return m_data.size();
}
int GCodeTableModel::columnCount(const QModelIndex &parent) const
{
return 6;
}
QVariant GCodeTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Horizontal) return m_headers.at(section);
else return QString::number(section + 1);
}
Qt::ItemFlags GCodeTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) return NULL;
if (index.column() == 1) return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
else return QAbstractTableModel::flags(index);
}