-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemorycellgraphicsitem.cpp
107 lines (91 loc) · 3.6 KB
/
memorycellgraphicsitem.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
// File: memorycellgraphicsitem.cpp
/*
Pep8-1 is a virtual machine for writing machine language and assembly
language programs.
Copyright (C) 2009 J. Stanley Warford, Pepperdine University
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "memorycellgraphicsitem.h"
#include "pep.h"
#include "sim.h"
#include <QPainter>
// #include <QDebug>
const int MemoryCellGraphicsItem::boxHeight = 22;
const int MemoryCellGraphicsItem::boxWidth = 50;
const int MemoryCellGraphicsItem::addressWidth = 48;
const int MemoryCellGraphicsItem::symbolWidth = 96;
const int MemoryCellGraphicsItem::bufferWidth = 14;
MemoryCellGraphicsItem::MemoryCellGraphicsItem(int addr, QString sym, Enu::ESymbolFormat eSymFrmt, int xLoc, int yLoc)
{
x = xLoc;
y = yLoc;
address = addr;
symbol = sym;
eSymbolFormat = eSymFrmt;
boxColor = Qt::black;
boxBgColor = Qt::white;
textColor = Qt::black;
boxTextColor = Qt::black;
}
QRectF MemoryCellGraphicsItem::boundingRect() const
{
const int Margin = 4;
return QRectF(QPointF(x - addressWidth - Margin, y - Margin),
QSizeF(addressWidth + bufferWidth * 2 + boxWidth + symbolWidth + Margin * 2, boxHeight + Margin * 2));
}
void MemoryCellGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QPen pen(boxColor);
pen.setWidth(2);
painter->setPen(pen);
painter->setBrush(boxBgColor);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawRoundedRect(QRectF(x, y, boxWidth, boxHeight), 2, 2, Qt::RelativeSize);
painter->setPen(textColor);
painter->setRenderHint(QPainter::TextAntialiasing);
painter->setFont(QFont(Pep::codeFont, Pep::codeFontSize));
painter->drawText(QRectF(x - addressWidth - bufferWidth, y, addressWidth, boxHeight), Qt::AlignVCenter | Qt::AlignRight, QString("%1").arg(address, 4, 16, QLatin1Char('0')).toUpper());
painter->drawText(QRectF(x + bufferWidth + boxWidth, y, symbolWidth, boxHeight), Qt::AlignVCenter | Qt::AlignLeft, QString("%1").arg(symbol));
painter->setPen(boxTextColor);
painter->drawText(QRectF(x, y, boxWidth, boxHeight), Qt::AlignCenter, value);
}
void MemoryCellGraphicsItem::updateValue()
{
switch (eSymbolFormat) {
case Enu::F_1C:
value = QString(QChar(Sim::Mem[address]));
break;
case Enu::F_1D:
value = QString("%1").arg(Sim::Mem[address]);
break;
case Enu::F_2D:
value = QString("%1").arg(Sim::toSignedDecimal(Sim::Mem[address] * 256 + Sim::Mem[address +1]));
break;
case Enu::F_1H:
value = QString("%1").arg(Sim::Mem[address], 2, 16, QLatin1Char('0')).toUpper();
break;
case Enu::F_2H:
value = QString("%1").arg(Sim::Mem[address] * 256 + Sim::Mem[address + 1], 4, 16, QLatin1Char('0')).toUpper();
break;
default:
value = ""; // Should not occur
break;
}
}
int MemoryCellGraphicsItem::getAddress()
{
return address;
}
int MemoryCellGraphicsItem::getNumBytes()
{
return Sim::cellSize(eSymbolFormat);
}