Skip to content

Commit

Permalink
Clean and test MemoryViewModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpd002 committed Mar 19, 2024
1 parent 3444c3c commit c011132
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 46 deletions.
20 changes: 20 additions & 0 deletions Source/ui_qt/DebugSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ set(SRC_FILES
${SRC_ROOT}/VariablesView.h
)

set(TEST_SRC_FILES
${SRC_ROOT}/tests/main.cpp
${SRC_ROOT}/tests/QtMemoryViewModelTests.cpp
${SRC_ROOT}/tests/QtMemoryViewModelTests.h
)

set(QT_DEBUGGER_MOC_HEADERS
${SRC_ROOT}/AddressListViewWnd.h
${SRC_ROOT}/CommentsView.h
Expand Down Expand Up @@ -174,3 +180,17 @@ add_library(PlayDebugSupport STATIC ${SRC_FILES} ${QT_DEBUGGER_UI_HEADERS} ${QT_
target_link_libraries(PlayDebugSupport PlayCore ${PLAYDEBUGSUPPORT_LIBS})
target_compile_definitions(PlayDebugSupport PRIVATE NOMINMAX ${PLAYDEBUGSUPPORT_DEFS})
target_include_directories(PlayDebugSupport PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ../../${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

add_executable(PlayDebugSupportTests ${TEST_SRC_FILES})
target_link_libraries(PlayDebugSupportTests PlayDebugSupport)
add_test(PlayDebugSupportTests PlayDebugSupportTests)

if(TARGET_PLATFORM_WIN32)
find_program(WINDEPLOYQT_EXE windeployqt HINTS "${QT_BINARY_DIRECTORY}")
add_custom_command(TARGET PlayDebugSupportTests POST_BUILD
COMMAND "${WINDEPLOYQT_EXE}" $<IF:$<CONFIG:Debug>,--debug,--release>
--no-translations
--no-compiler-runtime
"$<TARGET_FILE:PlayDebugSupportTests>"
)
endif()
16 changes: 4 additions & 12 deletions Source/ui_qt/DebugSupport/MemoryViewTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void CMemoryViewTable::SetBytesPerLine(int bytesForLine)
m_bytesPerLine = bytesForLine;
if(bytesForLine)
{
m_model->SetColumnCount(bytesForLine);
m_model->SetBytesPerRow(bytesForLine);
m_model->Redraw();
}
else
Expand Down Expand Up @@ -170,7 +170,7 @@ void CMemoryViewTable::AutoColumn()
}
++i;
}
m_model->SetColumnCount(i * bytesPerUnit);
m_model->SetBytesPerRow(i * bytesPerUnit);
m_model->Redraw();
}

Expand All @@ -183,10 +183,7 @@ void CMemoryViewTable::SetActiveUnit(int index)

void CMemoryViewTable::SetSelectionStart(uint32 address)
{
auto column = (address % m_model->BytesForCurrentLine()) / m_model->GetBytesPerUnit();
auto row = (address - column) / m_model->BytesForCurrentLine();

auto index = m_model->index(row, column);
auto index = m_model->TranslateAddressToModelIndex(address);
selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
scrollTo(index, QAbstractItemView::PositionAtCenter);
}
Expand All @@ -197,12 +194,7 @@ void CMemoryViewTable::SelectionChanged()
if(!indexes.empty())
{
auto index = indexes.first();
int address = index.row() * (m_model->BytesForCurrentLine());
if(m_model->columnCount() - 1 != index.column())
{
address += index.column() * m_model->GetBytesPerUnit();
}
m_selected = address;
m_selected = m_model->TranslateModelIndexToAddress(index);
OnSelectionChange(m_selected);
}
}
68 changes: 44 additions & 24 deletions Source/ui_qt/DebugSupport/QtMemoryViewModel.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "QtMemoryViewModel.h"

#include "string_format.h"
#include "lexical_cast_ex.h"

#include <cmath>
#include "string_format.h"

// clang-format off
std::vector<CQtMemoryViewModel::UNITINFO> CQtMemoryViewModel::g_units =
Expand All @@ -14,22 +11,20 @@ std::vector<CQtMemoryViewModel::UNITINFO> CQtMemoryViewModel::g_units =
};
// clang-format on

CQtMemoryViewModel::CQtMemoryViewModel(QObject* parent, getByteProto getByte, int size)
CQtMemoryViewModel::CQtMemoryViewModel(QObject* parent)
: QAbstractTableModel(parent)
, m_activeUnit(0)
, m_getByte(getByte)
, m_size(size)
{
}

int CQtMemoryViewModel::rowCount(const QModelIndex& /*parent*/) const
{
return std::ceil((m_size * 1.f) / m_columnCount);
return std::ceil((m_size * 1.f) / m_bytesPerRow);
}

int CQtMemoryViewModel::columnCount(const QModelIndex& /*parent*/) const
{
return (m_columnCount / GetBytesPerUnit()) + 1;
assert(m_bytesPerRow % GetBytesPerUnit() == 0);
return (m_bytesPerRow / GetBytesPerUnit()) + 1;
}

QVariant CQtMemoryViewModel::data(const QModelIndex& index, int role) const
Expand All @@ -38,15 +33,14 @@ QVariant CQtMemoryViewModel::data(const QModelIndex& index, int role) const
{
if(index.column() < columnCount() - 1)
{
int offset = (index.column() * g_units[m_activeUnit].bytesPerUnit);
int address = index.row() * m_columnCount;
return (this->*(g_units[m_activeUnit].renderer))(address + offset).c_str();
uint32 address = TranslateModelIndexToAddress(index);
return (this->*(g_units[m_activeUnit].renderer))(address).c_str();
}
else
{
int address = index.row() * m_columnCount;
uint32 address = TranslateModelIndexToAddress(this->index(index.row(), 0));
std::string res;
for(auto j = 0; j < m_columnCount; j++)
for(auto j = 0; j < m_bytesPerRow; j++)
{
if((address + j) >= m_size)
{
Expand Down Expand Up @@ -82,8 +76,8 @@ QVariant CQtMemoryViewModel::headerData(int section, Qt::Orientation orientation
}
else
{
auto address = section * m_columnCount;
return ("0x" + lexical_cast_hex<std::string>(address, 8)).c_str();
auto address = section * m_bytesPerRow;
return QString::fromStdString(string_format("0x%08X", address));
}
}

Expand Down Expand Up @@ -151,27 +145,48 @@ std::string CQtMemoryViewModel::RenderSingleUnit(uint32 address) const
}
}

unsigned int CQtMemoryViewModel::BytesForCurrentLine() const
unsigned int CQtMemoryViewModel::GetBytesPerRow() const
{
return m_columnCount;
return m_bytesPerRow;
}

void CQtMemoryViewModel::SetColumnCount(unsigned int count)
void CQtMemoryViewModel::SetBytesPerRow(unsigned int bytesPerRow)
{
m_columnCount = count;
m_bytesPerRow = bytesPerRow;
}

unsigned int CQtMemoryViewModel::CharsPerUnit() const
uint32 CQtMemoryViewModel::TranslateModelIndexToAddress(const QModelIndex& index) const
{
return g_units[m_activeUnit].charsPerUnit;
uint32 address = index.row() * m_bytesPerRow;
if((columnCount() - 1) != index.column())
{
address += index.column() * GetBytesPerUnit();
}
return address;
}

QModelIndex CQtMemoryViewModel::TranslateAddressToModelIndex(uint32 address) const
{
if(static_cast<int32>(address) < 0)
{
return index(0, 0);
}
if(static_cast<int32>(address) >= m_size)
{
address = (m_size - 1);
}
uint32 row = address / m_bytesPerRow;
uint32 col = address - (row * m_bytesPerRow);
col /= GetBytesPerUnit();
return index(row, col);
}

void CQtMemoryViewModel::SetActiveUnit(int index)
{
m_activeUnit = index;
}

int CQtMemoryViewModel::GetActiveUnit()
int CQtMemoryViewModel::GetActiveUnit() const
{
return m_activeUnit;
}
Expand All @@ -181,6 +196,11 @@ unsigned int CQtMemoryViewModel::GetBytesPerUnit() const
return g_units[m_activeUnit].bytesPerUnit;
}

unsigned int CQtMemoryViewModel::CharsPerUnit() const
{
return g_units[m_activeUnit].charsPerUnit;
}

void CQtMemoryViewModel::SetData(getByteProto getByte, int size)
{
m_getByte = getByte;
Expand Down
22 changes: 12 additions & 10 deletions Source/ui_qt/DebugSupport/QtMemoryViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@ class CQtMemoryViewModel : public QAbstractTableModel
const char* description = nullptr;
};

CQtMemoryViewModel(QObject*, getByteProto = nullptr, int = 0);
CQtMemoryViewModel(QObject*);
~CQtMemoryViewModel() = default;

int rowCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
void DoubleClicked(const QModelIndex& index, QWidget* parent = nullptr);

void Redraw();

unsigned int BytesForCurrentLine() const;
void SetColumnCount(unsigned int);
unsigned int CharsPerUnit() const;
unsigned int GetBytesPerRow() const;
void SetBytesPerRow(unsigned int);

void SetActiveUnit(int);
int GetActiveUnit();
int GetActiveUnit() const;
unsigned int GetBytesPerUnit() const;
unsigned int CharsPerUnit() const;

void SetData(getByteProto, int);

uint32 TranslateModelIndexToAddress(const QModelIndex&) const;
QModelIndex TranslateAddressToModelIndex(uint32) const;

static std::vector<UNITINFO> g_units;

Expand All @@ -51,8 +53,8 @@ class CQtMemoryViewModel : public QAbstractTableModel
std::string RenderWordUnit(uint32) const;
std::string RenderSingleUnit(uint32) const;

getByteProto m_getByte = nullptr;
int m_activeUnit;
unsigned int m_size;
std::atomic<unsigned int> m_columnCount = 0x2;
getByteProto m_getByte;
int m_activeUnit = 0;
unsigned int m_size = 0;
unsigned int m_bytesPerRow = 0x2;
};
Loading

0 comments on commit c011132

Please sign in to comment.