-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistoryitem.cpp
181 lines (147 loc) · 3.77 KB
/
historyitem.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "historyitem.h"
bool HistoryItem::iconsInitialized = false;
QIcon HistoryItem::s_iconRunning;
QIcon HistoryItem::s_iconOk;
QIcon HistoryItem::s_iconError;
QIcon HistoryItem::s_iconCancelled;
QIcon HistoryItem::s_iconAlarm;
QIcon HistoryItem::s_iconReset;
void HistoryItem::initializeIcons(){
s_iconRunning = QIcon(":/monitor-running");
s_iconOk = QIcon(":/monitor-ok");
s_iconError = QIcon(":/monitor-error");
s_iconCancelled = QIcon(":/monitor-cancelled");
s_iconAlarm = QIcon(":/monitor-alarm");
s_iconReset = QIcon(":/monitor-reset");
iconsInitialized = true;
}
HistoryItem::HistoryItem(const GrblInstruction &instruction, HistoryItem *parent):
m_status(Running),
m_parentItem(parent)
{
m_instruction = new GrblInstruction(instruction);
m_text = m_instruction->getStringWithLineNumber();
if(!iconsInitialized){
initializeIcons();
}
}
HistoryItem::HistoryItem(const QString &caption, HistoryItem* parent):
m_status(None),
m_instruction(nullptr),
m_text(caption),
m_parentItem(parent)
{
if(!iconsInitialized){
initializeIcons();
}
}
HistoryItem::ItemStatus HistoryItem::status() const {
return m_status;
}
GrblInstruction* HistoryItem::instruction() const {
return m_instruction;
}
void HistoryItem::setCancelled(){
if(m_status == Running){
m_status = Cancelled;
}
}
void HistoryItem::setOk() {
if(m_status != Alarm){
m_status = Ok;
}
}
void HistoryItem::setError() {
if(m_status != Alarm){
m_status = Error;
}
}
void HistoryItem::setReset(){
if(m_status == None){
m_status = Reset;
}
}
void HistoryItem::setAlarm(){
m_status = Alarm;
}
void HistoryItem::addChild(HistoryItem* child){
m_childItems.append(child);
}
void HistoryItem::deleteFirstChild(){
if(!m_childItems.isEmpty()){
delete m_childItems.takeFirst();
}
}
bool HistoryItem::hasInstruction(void){
return (m_instruction != nullptr);
}
QList<HistoryItem *> HistoryItem::childList(){
return m_childItems;
}
HistoryItem *HistoryItem::lastChild(){
if(m_childItems.isEmpty()){
return nullptr;
}
return m_childItems.last();
}
int HistoryItem::childCount() const{
return m_childItems.count();
}
HistoryItem* HistoryItem::searchItemMatchingInstruction(const GrblInstruction &instructionToLocate){
//Check is this item has the matching instruction
if(hasInstruction() && *m_instruction == instructionToLocate){
return this;
}
//Else check every child
foreach( HistoryItem* child, m_childItems){
HistoryItem* childResult = child->searchItemMatchingInstruction(instructionToLocate);
if(childResult != nullptr){
return childResult;
}
}
return nullptr;
}
int HistoryItem::columnCount() const{
return 1;
}
QVariant HistoryItem::data(int role) const{
switch(role){
case Qt::DisplayRole:
return m_text.simplified();
case Qt::DecorationRole:{
switch(m_status){
case Running:
return s_iconRunning;
case Ok:
return s_iconOk;
case Error:
return s_iconError;
case Cancelled:
return s_iconCancelled;
case Alarm:
return s_iconAlarm;
case Reset:
return s_iconReset;
default:
return QIcon();
}
}
default:
return QVariant();
}
}
HistoryItem *HistoryItem::parentItem(){
return m_parentItem;
}
int HistoryItem::row() const{
if (m_parentItem){
return m_parentItem->m_childItems.indexOf(const_cast<HistoryItem*>(this));
}
return 0;
}
HistoryItem::~HistoryItem(){
if(m_instruction != nullptr){
delete m_instruction;
}
qDeleteAll(m_childItems);
}