Skip to content

Commit

Permalink
Remove baseAddress field
Browse files Browse the repository at this point in the history
  • Loading branch information
allencch committed Apr 15, 2018
1 parent d95b176 commit bfbbb0b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
8 changes: 6 additions & 2 deletions include/ui/MemEditor.hpp
Expand Up @@ -23,14 +23,16 @@ class MemEditor : public QWidget {

QPlainTextEdit* memArea;
std::string memHex;
QLineEdit* baseAddress;

void writeToProcessMemory(int position, char ch);
void refresh();
void updateAddresses();

void setBaseAddress(Address addr);
Address getBaseAddress();

private slots:
void onBaseAddressEdited();
void onCurrAddressEdited();
void onMemAreaCursorPositionChanged();
void onRefreshButtonClicked();

Expand All @@ -39,6 +41,7 @@ private slots:
MedUi* mainUi;
QWidget* parent;
QWidget* mainChild;
Address baseAddress;

QLineEdit* currAddress;
QLineEdit* valueLine;
Expand All @@ -59,6 +62,7 @@ private slots:

string getHexString(int position);
Address getAddressByCursorPosition(int position);
void setCursorPositionByAddress();

static std::string memoryToHex(Byte* memory, size_t size);
static std::string memoryToString(Byte* memory, size_t size, EncodingManager* encodingManager);
Expand Down
63 changes: 41 additions & 22 deletions src/ui/MemEditor.cpp
Expand Up @@ -20,6 +20,7 @@ MemEditor::MemEditor(MedUi* mainUi) : QWidget(NULL, Qt::SubWindow) {
this->mainUi = mainUi;
this->med = mainUi->med;
rawMemory = NULL;
baseAddress = 0;

QUiLoader loader;
QFile file("./mem-editor.ui");
Expand All @@ -31,7 +32,6 @@ MemEditor::MemEditor(MedUi* mainUi) : QWidget(NULL, Qt::SubWindow) {
this->setLayout(layout);
this->resize(800, 500);

baseAddress = mainChild->findChild<QLineEdit*>("baseAddress");
currAddress= mainChild->findChild<QLineEdit*>("currAddress");
valueLine = mainChild->findChild<QLineEdit*>("value");
memArea = mainChild->findChild<QPlainTextEdit*>("memArea");
Expand Down Expand Up @@ -59,10 +59,10 @@ MemEditor::~MemEditor() {
}

void MemEditor::setupSignals() {
QObject::connect(baseAddress,
QObject::connect(currAddress,
SIGNAL(editingFinished()),
this,
SLOT(onBaseAddressEdited()));
SLOT(onCurrAddressEdited()));
QObject::connect(memArea,
SIGNAL(cursorPositionChanged()),
this,
Expand All @@ -74,8 +74,8 @@ void MemEditor::setupSignals() {
}


void MemEditor::onBaseAddressEdited() {
QString addr = baseAddress->text();
void MemEditor::onCurrAddressEdited() {
QString addr = currAddress->text();
if (addr.trimmed() == "") {
return;
}
Expand All @@ -86,32 +86,31 @@ void MemEditor::onBaseAddressEdited() {
}

try {
Address roundedAddr = addressRoundDown(hexToInt(addr.toStdString()));
baseAddress->setText(intToHex(roundedAddr).c_str());
loadMemory(roundedAddr);
loadAddresses(roundedAddr);

baseAddress = addressRoundDown(hexToInt(addr.toStdString()));
loadMemory(baseAddress);
loadAddresses(baseAddress);
currAddress->setText(addr);
setCursorPositionByAddress();
} catch(MedException &ex) {
cerr << ex.getMessage() << endl;
}
}

void MemEditor::refresh() {
QString addr = baseAddress->text();
if (addr.trimmed() == "") {
if (!baseAddress) {
return;
}
Address roundedAddr = addressRoundDown(hexToInt(addr.toStdString()));
baseAddress->setText(intToHex(roundedAddr).c_str());
loadMemory(roundedAddr);
QString addr = currAddress->text();
loadMemory(baseAddress);
currAddress->setText(addr);
setCursorPositionByAddress();
}

void MemEditor::updateAddresses() {
QString addr = baseAddress->text();
if (addr.trimmed() == "") {
if (!baseAddress) {
return;
}
loadAddresses(hexToInt(addr.toStdString()));
loadAddresses(baseAddress);
}

void MemEditor::loadMemory(Address address, size_t size) {
Expand Down Expand Up @@ -172,13 +171,25 @@ void MemEditor::onMemAreaCursorPositionChanged() {
}

Address MemEditor::getAddressByCursorPosition(int position) {
QString addr = baseAddress->text();
if (addr.trimmed() == "") {
if (!baseAddress) {
return 0;
}
int distance = position / 3;
Address base = hexToInt(addr.toStdString());
return base + distance;
return baseAddress + distance;
}

void MemEditor::setCursorPositionByAddress() {
QString addr = currAddress->text();
if (!baseAddress || addr.trimmed() == "") {
return;
}

int distance = hexToInt(addr.toStdString()) - baseAddress;
int position = distance * 3;

auto cursor = memArea->textCursor();
cursor.setPosition(position, QTextCursor::MoveAnchor);
memArea->setTextCursor(cursor);
}

void MemEditor::updateCurrAddress() {
Expand Down Expand Up @@ -228,3 +239,11 @@ string MemEditor::getHexString(int position) {
void MemEditor::onRefreshButtonClicked() {
refresh();
}

void MemEditor::setBaseAddress(Address addr) {
baseAddress = addr;
}

Address MemEditor::getBaseAddress() {
return baseAddress;
}
8 changes: 2 additions & 6 deletions src/ui/MemEditorEventListener.cpp
Expand Up @@ -111,9 +111,7 @@ void MemEditorEventListener::moveCursorUpward() {
return;
}

Address base = hexToInt(memEditor->baseAddress->text().toStdString());
base -= 16;
memEditor->baseAddress->setText(intToHex(base).c_str());
memEditor->setBaseAddress(memEditor->getBaseAddress() - 16);
memEditor->refresh();
memEditor->updateAddresses();
cursor.setPosition(position, QTextCursor::MoveAnchor);
Expand All @@ -127,9 +125,7 @@ void MemEditorEventListener::moveCursorDownward() {
return;
}

Address base = hexToInt(memEditor->baseAddress->text().toStdString());
base += 16;
memEditor->baseAddress->setText(intToHex(base).c_str());
memEditor->setBaseAddress(memEditor->getBaseAddress() + 16);
memEditor->refresh();
memEditor->updateAddresses();
cursor.setPosition(position, QTextCursor::MoveAnchor);
Expand Down
9 changes: 1 addition & 8 deletions ui/mem-editor.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<width>604</width>
<height>500</height>
</rect>
</property>
Expand All @@ -18,13 +18,6 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="baseAddress">
<property name="placeholderText">
<string>Base</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="currAddress">
<property name="placeholderText">
Expand Down

0 comments on commit bfbbb0b

Please sign in to comment.