cutter/src/widgets/MemoryMapWidget.cpp

157 lines
4.6 KiB
C++
Raw Normal View History

2018-06-12 08:43:14 +00:00
#include "MemoryMapWidget.h"
#include "ui_MemoryMapWidget.h"
#include "MainWindow.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
2018-06-12 08:43:14 +00:00
MemoryMapModel::MemoryMapModel(QList<MemoryMapDescription> *memoryMaps, QObject *parent)
: QAbstractListModel(parent),
memoryMaps(memoryMaps)
{
}
int MemoryMapModel::rowCount(const QModelIndex &) const
{
return memoryMaps->count();
}
int MemoryMapModel::columnCount(const QModelIndex &) const
{
return MemoryMapModel::ColumnCount;
}
QVariant MemoryMapModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= memoryMaps->count())
return QVariant();
const MemoryMapDescription &memoryMap = memoryMaps->at(index.row());
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case AddrStartColumn:
return RAddressString(memoryMap.addrStart);
case AddrEndColumn:
return RAddressString(memoryMap.addrEnd);
case NameColumn:
return memoryMap.name;
case PermColumn:
return memoryMap.permission;
default:
return QVariant();
}
case MemoryDescriptionRole:
return QVariant::fromValue(memoryMap);
default:
return QVariant();
}
}
QVariant MemoryMapModel::headerData(int section, Qt::Orientation, int role) const
{
switch (role) {
case Qt::DisplayRole:
switch (section) {
case AddrStartColumn:
return tr("Offset start");
case AddrEndColumn:
return tr("Offset end");
case NameColumn:
return tr("Name");
case PermColumn:
return tr("Permissions");
default:
return QVariant();
}
default:
return QVariant();
}
}
MemoryProxyModel::MemoryProxyModel(MemoryMapModel *sourceModel, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(sourceModel);
}
bool MemoryProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
2018-09-30 20:00:53 +00:00
MemoryMapDescription item = index.data(
MemoryMapModel::MemoryDescriptionRole).value<MemoryMapDescription>();
2018-06-12 08:43:14 +00:00
return item.name.contains(filterRegExp());
}
bool MemoryProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
2018-09-30 20:00:53 +00:00
MemoryMapDescription leftMemMap = left.data(
MemoryMapModel::MemoryDescriptionRole).value<MemoryMapDescription>();
MemoryMapDescription rightMemMap = right.data(
MemoryMapModel::MemoryDescriptionRole).value<MemoryMapDescription>();
2018-06-12 08:43:14 +00:00
switch (left.column()) {
case MemoryMapModel::AddrStartColumn:
return leftMemMap.addrStart < rightMemMap.addrStart;
case MemoryMapModel::AddrEndColumn:
return leftMemMap.addrEnd < rightMemMap.addrEnd;
case MemoryMapModel::NameColumn:
return leftMemMap.name < rightMemMap.name;
case MemoryMapModel::PermColumn:
return leftMemMap.permission < rightMemMap.permission;
default:
break;
}
return leftMemMap.addrStart < rightMemMap.addrStart;
}
MemoryMapWidget::MemoryMapWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::MemoryMapWidget)
{
ui->setupUi(this);
memoryModel = new MemoryMapModel(&memoryMaps, this);
memoryProxyModel = new MemoryProxyModel(memoryModel, this);
ui->memoryTreeView->setModel(memoryProxyModel);
ui->memoryTreeView->sortByColumn(MemoryMapModel::AddrStartColumn, Qt::AscendingOrder);
setScrollMode();
refreshDeferrer = createRefreshDeferrer([this]() {
refreshMemoryMap();
});
2018-06-12 08:43:14 +00:00
connect(Core(), &CutterCore::refreshAll, this, &MemoryMapWidget::refreshMemoryMap);
connect(Core(), &CutterCore::registersChanged, this, &MemoryMapWidget::refreshMemoryMap);
}
MemoryMapWidget::~MemoryMapWidget() = default;
2018-06-12 08:43:14 +00:00
void MemoryMapWidget::refreshMemoryMap()
{
if (!refreshDeferrer->attemptRefresh(nullptr)) {
return;
}
memoryModel->beginResetModel();
2018-06-12 08:43:14 +00:00
memoryMaps = Core()->getMemoryMap();
memoryModel->endResetModel();
2018-06-12 08:43:14 +00:00
ui->memoryTreeView->resizeColumnToContents(0);
ui->memoryTreeView->resizeColumnToContents(1);
ui->memoryTreeView->resizeColumnToContents(2);
}
void MemoryMapWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->memoryTreeView);
}
void MemoryMapWidget::on_memoryTreeView_doubleClicked(const QModelIndex &index)
{
2018-09-30 20:00:53 +00:00
MemoryMapDescription item = index.data(
MemoryMapModel::MemoryDescriptionRole).value<MemoryMapDescription>();
2018-06-12 08:43:14 +00:00
Core()->seek(item.addrStart);
}