forked from Ramblurr/PietCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugWidget.cpp
278 lines (256 loc) · 9.87 KB
/
DebugWidget.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "DebugWidget.h"
extern "C"
{
#include "npiet/npiet_utils.h"
}
#include "ImageModel.h"
#include "CommandImpl.h"
#include <QDebug>
DebugWidget::DebugWidget( ImageModel* model, QWidget* parent, Qt::WindowFlags f ): QWidget( parent, f ), mImageModel( model )
{
setupUi( this );
mFlowCompass = new FlowCompass( this->mCompassBox );
mCompassBox->layout()->addWidget( mFlowCompass );
}
DebugWidget::~DebugWidget()
{
}
void DebugWidget::slotDebugStopped()
{
mImageModel->setDebuggedPixel( -1, -1 );
changeCurrent( 1 );
mBeforeStack->clear();
mAfterStack->clear();
mValueLabel->setText( "" );
mActionLabel->setText( "" );
}
void DebugWidget::slotDebugStarted()
{
mActionLabel->setText( "" );
mValueLabel->setText( "" );
mCoordinate->setText( "Before first instruction" );
mFlowCompass->reset();
changeCurrent( 0 );
}
void DebugWidget::changeCurrent( int idx )
{
mStackedWidget->currentWidget()->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
mStackedWidget->setCurrentIndex( idx );
mStackedWidget->currentWidget()->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
adjustSize();
}
void DebugWidget::slotActionChanged( trace_action* action )
{
mActionLabel->setText( command( action->light_change, action->hue_change ).name );
mAfterStack->clear();
for ( int i = 0; i < action->after_num; i++ ) {
QListWidgetItem *item = new QListWidgetItem( mAfterStack );
int val = action->after_stack[action->after_num - i - 1];
QString character;
if ( val >= 32 && val <= 126 )
character = QString( "(char: '%1')" ).arg( ( char ) val );
QString value = QString( "%1 %2" ).arg( val ).arg( character );
item->setText( value );
mAfterStack->addItem( item );
}
mBeforeStack->clear();
for ( int i = 0; i < action->before_num; i++ ) {
QListWidgetItem *item = new QListWidgetItem( mBeforeStack );
int val = action->before_stack[action->before_num - i - 1];
QString character;
if ( val >= 32 && val <= 126 )
character = QString( "(char: '%1')" ).arg( ( char ) val );
QString value = QString( "%1 %2" ).arg( val ).arg( character );
item->setText( value );
mBeforeStack->addItem( item );
}
}
void DebugWidget::slotStepped( trace_step* step )
{
mCoordinate->setText( QString( "%1,%2" ).arg( step->n_xpos ).arg( step->n_ypos ) );
mImageModel->setDebuggedPixel( step->n_xpos, step->n_ypos );
quint64 connected = mImageModel->data( mImageModel->index( step->n_ypos, step->n_xpos ), ImageModel::ContiguousBlocksRole ).toInt();
QString character;
if ( connected >= 32 && connected <= 126 )
character = QString( "(char: '%1')" ).arg( ( char ) connected );
QString value = QString( "%1 %2" ).arg( connected ).arg( character );
mValueLabel->setText( value );
if( step->n_dp == 'l' )
mFlowCompass->setDPDirection( FlowCompass::Left );
else if( step->n_dp == 'r' )
mFlowCompass->setDPDirection( FlowCompass::Right );
else if( step->n_dp == 'u' )
mFlowCompass->setDPDirection( FlowCompass::Up );
else if( step->n_dp == 'd' )
mFlowCompass->setDPDirection( FlowCompass::Down);
if( step->n_cc == 'l' )
mFlowCompass->setCCDirection( FlowCompass::Left );
else if( step->n_cc == 'r' )
mFlowCompass->setCCDirection( FlowCompass::Right );
}
Command DebugWidget::command( int light_change, int hue_change )
{
switch ( hue_change ) {
case 0:
/* None push pop */
if ( light_change == 0 ) {
/*
* noop - nothing to do (should not happen)
*/
return PietCommand::Noop();
} else if ( light_change == 1 ) {
/*
push: Pushes the value of the colour block just exited on to the
stack. Note that values of colour blocks are not automatically
pushed on to the stack - this push operation must be explicitly
carried out.
*/
return PietCommand::Push();
} else if ( light_change == 2 ) {
/*
pop: Pops the top value off the stack and discards it.
*/
return PietCommand::Pop();
}
break;
case 1:
/* 1 Step add subtract multiply */
if ( light_change == 0 ) {
/*
add: Pops the top two values off the stack, adds them, and pushes
the result back on the stack.
*/
return PietCommand::Add();
} else if ( light_change == 1 ) {
/*
subtract: Pops the top two values off the stack, subtracts the top
value from the second top value, and pushes the result back on the
stack.
*/
return PietCommand::Subtract();
} else if ( light_change == 2 ) {
/*
multiply: Pops the top two values off the stack, multiplies them,
and pushes the result back on the stack.
*/
return PietCommand::Multiply();
}
break;
case 2:
/* 2 Steps divide mod not */
if ( light_change == 0 ) {
/*
divide: Pops the top two values off the stack, calculates the
integer division of the second top value by the top value, and
pushes the result back on the stack.
*/
return PietCommand::Divide();
} else if ( light_change == 1 ) {
/*
mod: Pops the top two values off the stack, calculates the second
top value modulo the top value, and pushes the result back on the
stack.
*/
return PietCommand::Mod();
} else if ( light_change == 2 ) {
/*
not: Replaces the top value of the stack with 0 if it is non-zero,
and 1 if it is zero.
*/
return PietCommand::Not();
}
break;
case 3:
/* 3 Steps greater pointer switch */
if ( light_change == 0 ) {
/*
greater: Pops the top two values off the stack, and pushes 1 on to
the stack if the second top value is greater than the top value,
and pushes 0 if it is not greater.
*/
return PietCommand::Greater();
} else if ( light_change == 1 ) {
/*
pointer: Pops the top value off the stack and rotates the DP
clockwise that many steps (anticlockwise if negative).
*/
return PietCommand::Pointer();
} else if ( light_change == 2 ) {
/*
switch: Pops the top value off the stack and toggles the CC that
many times.
*/
return PietCommand::Switch();
}
break;
case 4:
/* 4 Steps duplicate roll in(number) */
if ( light_change == 0 ) {
/*
duplicate: Pushes a copy of the top value on the stack on to the
stack.
*/
return PietCommand::Duplicate();
} else if ( light_change == 1 ) {
/*
roll: Pops the top two values off the stack and "rolls" the
remaining stack entries to a depth equal to the second value
popped, by a number of rolls equal to the first value popped. A
single roll to depth n is defined as burying the top value on the
stack n deep and bringing all values above it up by 1 place. A
negative number of rolls rolls in the opposite direction. A
negative depth is an error and the command is ignored.
*/
return PietCommand::Roll();
} else if ( light_change == 2 ) {
/*
in: Reads a value from STDIN as either a number or character,
depending on the particular incarnation of this command and pushes
it on to the stack.
*/
return PietCommand::In();
}
break;
case 5:
/* 5 Steps in(char) out(number) out(char) */
if ( light_change == 0 ) {
/*
in: Reads a value from STDIN as either a number or character,
depending on the particular incarnation of this command and pushes
it on to the stack.
*/
return PietCommand::In();
} else if ( light_change == 1 ) {
/*
out: Pops the top value off the stack and prints it to STDOUT as
either a number or character, depending on the particular
incarnation of this command.
*/
return PietCommand::Out();
} else if ( light_change == 2 ) {
/*
out: Pops the top value off the stack and prints it to STDOUT as
either a number or character, depending on the particular
incarnation of this command.
*/
return PietCommand::Out();
}
break;
}
}
#include "DebugWidget.moc"