Implement navigation in address view
MODIFY
src/memorydisplaydelegate.cpp
src/memorydisplaydelegate.cpp
@@ -5,6 +5,14 @@ MemoryDisplayDelegate::MemoryDisplayDelegate(QWidget *parent)
QString MemoryDisplayDelegate::displayText(const QVariant &value,
const QLocale & /*locale*/) const {
+
+ // check for invalid memory - hacky solution. Check for values that can
+ // be converted to a qstring larger than any valid value could be
+ QString ret = qvariant_cast<QString>(value);
+ if (ret.size() > 4) {
+ return "---";
+ }
+
QString retVal;
switch (m_displayType) {
case displayTypeN::Hex:

MODIFY
src/memorymodel.cpp
src/memorymodel.cpp
@@ -4,10 +4,41 @@ MemoryModel::MemoryModel(memory *memoryPtr, QObject *parent)
: QStandardItemModel(parent) {
m_memoryPtr = memoryPtr;
- // Create initial view
- // i is a byte index
+ updateModel();
+
+ // Set headers
+ setHeaderData(0, Qt::Horizontal, "Address");
+ setHeaderData(1, Qt::Horizontal, "+0");
+ setHeaderData(2, Qt::Horizontal, "+1");
+ setHeaderData(3, Qt::Horizontal, "+2");
+ setHeaderData(4, Qt::Horizontal, "+3");
+}
+
+void MemoryModel::setInvalidAddresLine(int row) {
+ for (int i = 0; i < 5; i++) {
+ auto invalidItem = new QStandardItem();
+ invalidItem->setData("INVALID", Qt::DisplayRole);
+ invalidItem->setTextAlignment(Qt::AlignCenter);
+ setItem(row, i, invalidItem);
+ };
+}
+
+void MemoryModel::offsetCentralAddress(int byteOffset) {
+ // Changes the central address of the model by byteOffset, and updates the
+ // model
+ if (byteOffset % 4 == 0 && (m_centralAddress + byteOffset >= 0)) {
+ m_centralAddress += byteOffset;
+ updateModel();
+ }
+}
+
+void MemoryModel::updateModel() {
for (int i = 0; i < m_addressRadius * 2 + 1; i++) {
auto lineAddress = m_centralAddress + (m_addressRadius * 4) - i * 4;
+ if (lineAddress < 0) {
+ // Memory is not available on negative addresses, set invalid data
+ setInvalidAddresLine(i);
+ } else {
auto iter = m_memoryPtr->find(lineAddress);
if (iter != m_memoryPtr->end()) {
auto addr = new QStandardItem();
@@ -15,11 +46,25 @@ MemoryModel::MemoryModel(memory *memoryPtr, QObject *parent)
QString("0x%1").arg(
QString().setNum(iter->first, 16).rightJustified(8, '0')),
Qt::DisplayRole);
+ addr->setTextAlignment(Qt::AlignCenter);
auto b1 = new QStandardItem();
b1->setData(iter->second, Qt::DisplayRole);
b1->setTextAlignment(Qt::AlignCenter);
setItem(i, 0, addr);
setItem(i, 1, b1);
+ } else {
+ // No address in memory - create "fake" address in model
+ auto addr = new QStandardItem();
+ addr->setData(
+ QString("0x%1").arg(
+ QString().setNum(lineAddress, 16).rightJustified(8, '0')),
+ Qt::DisplayRole);
+ addr->setTextAlignment(Qt::AlignCenter);
+ auto b1 = new QStandardItem();
+ b1->setData(0, Qt::DisplayRole);
+ b1->setTextAlignment(Qt::AlignCenter);
+ setItem(i, 0, addr);
+ setItem(i, 1, b1);
}
for (int j = 1; j < 4; j++) {
// Locate the remaining 3 bytes in the word
@@ -36,11 +81,5 @@ MemoryModel::MemoryModel(memory *memoryPtr, QObject *parent)
}
}
}
-
- // Set headers
- setHeaderData(0, Qt::Horizontal, "Address");
- setHeaderData(1, Qt::Horizontal, "+0");
- setHeaderData(2, Qt::Horizontal, "+1");
- setHeaderData(3, Qt::Horizontal, "+2");
- setHeaderData(4, Qt::Horizontal, "+3");
+ }
}

MODIFY
src/memorymodel.h
src/memorymodel.h
@@ -16,17 +16,20 @@ public:
// Custom functionality
void setCentralAddress(uint32_t address);
void jumpToAddress(uint32_t address);
- void moveSelection(int dir);
+ void offsetCentralAddress(int byteOffset);
void setAddressCount(int count);
private:
+ void updateModel();
+ void setInvalidAddresLine(int row);
+
// instead of direct access to the memory, we should have a pointer
// to the runner, which can return requested values, or null, if memory
// doesnt exist
memory *m_memoryPtr;
- uint32_t m_centralAddress = 30; // Address at the center of the model
+ long long m_centralAddress = 4; // Address at the center of the model
int m_addressRadius = 5; // amount of addresses in each direction, from
// centralAddress
};

MODIFY
src/memorytab.cpp
src/memorytab.cpp
@@ -130,6 +130,14 @@ void MemoryTab::initializeMemoryView() {
&MemoryDisplayDelegate::setDisplayType);
connect(m_ui->memorydisplaytype, &QComboBox::currentTextChanged,
[=] { m_ui->memoryView->viewport()->repaint(); });
+ connect(m_ui->memoryUp, &QPushButton::clicked, [=] {
+ m_model->offsetCentralAddress(4);
+ m_ui->memoryView->viewport()->repaint();
+ });
+ connect(m_ui->memoryDown, &QPushButton::clicked, [=] {
+ m_model->offsetCentralAddress(-4);
+ m_ui->memoryView->viewport()->repaint();
+ });
}
MemoryTab::~MemoryTab() { delete m_ui; }

MODIFY
src/memorytab.ui
src/memorytab.ui
<rect>
<x>0</x>
<y>0</y>
- <width>231</width>
+ <width>240</width>
<height>335</height>
</rect>
</property>
<rect>
<x>0</x>
<y>0</y>
- <width>520</width>
+ <width>511</width>
<height>335</height>
</rect>
</property>
</widget>
</item>
<item>
- <widget class="QPushButton" name="pushButton">
+ <widget class="QPushButton" name="memoryUp">
<property name="text">
<string>Up</string>
</property>
</widget>
</item>
<item>
- <widget class="QPushButton" name="pushButton_2">
+ <widget class="QPushButton" name="memoryDown">
<property name="text">
<string>down</string>
</property>

Implement navigation in address view