cutter/src/widgets/MemoryMapWidget.cpp

154 lines
4.5 KiB
C++
Raw Normal View History

2018-06-12 08:43:14 +00:00
#include "MemoryMapWidget.h"
#include "ui_ListDockWidget.h"
#include "core/MainWindow.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
2019-07-18 17:11:42 +00:00
#include <QShortcut>
2018-06-12 08:43:14 +00:00
MemoryMapModel::MemoryMapModel(QList<MemoryMapDescription> *memoryMaps, QObject *parent)
: AddressableItemModel<QAbstractListModel>(parent),
2018-06-12 08:43:14 +00:00
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();
}
}
RVA MemoryMapModel::address(const QModelIndex &index) const
{
const MemoryMapDescription &memoryMap = memoryMaps->at(index.row());
return memoryMap.addrStart;
}
2018-06-12 08:43:14 +00:00
MemoryProxyModel::MemoryProxyModel(MemoryMapModel *sourceModel, QObject *parent)
: AddressableFilterProxyModel(sourceModel, parent)
2018-06-12 08:43:14 +00:00
{
}
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) :
ListDockWidget(main, action, ListDockWidget::SearchBarPolicy::HideByDefault)
2018-06-12 08:43:14 +00:00
{
setWindowTitle(tr("Memory Map"));
setObjectName("MemoryMapWidget");
2018-06-12 08:43:14 +00:00
memoryModel = new MemoryMapModel(&memoryMaps, this);
memoryProxyModel = new MemoryProxyModel(memoryModel, this);
setModels(memoryProxyModel);
ui->treeView->sortByColumn(MemoryMapModel::AddrStartColumn, Qt::AscendingOrder);
2018-06-12 08:43:14 +00:00
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);
showCount(false);
2018-06-12 08:43:14 +00:00
}
MemoryMapWidget::~MemoryMapWidget() = default;
2018-06-12 08:43:14 +00:00
void MemoryMapWidget::refreshMemoryMap()
{
if (!refreshDeferrer->attemptRefresh(nullptr)) {
return;
}
if (Core()->currentlyEmulating) {
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->treeView->resizeColumnToContents(0);
ui->treeView->resizeColumnToContents(1);
ui->treeView->resizeColumnToContents(2);
2018-06-12 08:43:14 +00:00
}