2018-06-06 11:05:20 +00:00
|
|
|
#include "StackWidget.h"
|
|
|
|
#include "ui_StackWidget.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/JsonModel.h"
|
|
|
|
#include "common/Helpers.h"
|
2018-07-24 23:15:38 +00:00
|
|
|
#include "dialogs/EditInstructionDialog.h"
|
2018-06-06 11:05:20 +00:00
|
|
|
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/MainWindow.h"
|
2018-06-06 11:05:20 +00:00
|
|
|
#include "QHeaderView"
|
2018-07-24 23:15:38 +00:00
|
|
|
#include "QMenu"
|
2018-06-06 11:05:20 +00:00
|
|
|
|
2019-09-25 14:18:30 +00:00
|
|
|
enum ColumnIndex {
|
|
|
|
COLUMN_OFFSET = 0,
|
|
|
|
COLUMN_VALUE,
|
|
|
|
COLUMN_DESCRIPTION
|
|
|
|
};
|
|
|
|
|
2018-06-06 11:05:20 +00:00
|
|
|
StackWidget::StackWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
2019-09-25 14:18:30 +00:00
|
|
|
ui(new Ui::StackWidget),
|
|
|
|
addressableItemContextMenu(this, main)
|
2018-06-06 11:05:20 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2018-11-05 21:51:27 +00:00
|
|
|
// Setup stack model
|
2019-09-25 14:18:30 +00:00
|
|
|
modelStack->setHorizontalHeaderItem(COLUMN_OFFSET, new QStandardItem(tr("Offset")));
|
|
|
|
modelStack->setHorizontalHeaderItem(COLUMN_VALUE, new QStandardItem(tr("Value")));
|
|
|
|
modelStack->setHorizontalHeaderItem(COLUMN_DESCRIPTION, new QStandardItem(tr("Reference")));
|
2018-07-18 10:15:10 +00:00
|
|
|
viewStack->setFont(Config()->getFont());
|
2018-06-06 11:05:20 +00:00
|
|
|
viewStack->setModel(modelStack);
|
|
|
|
viewStack->verticalHeader()->hide();
|
|
|
|
viewStack->setSortingEnabled(true);
|
2018-07-23 23:14:54 +00:00
|
|
|
viewStack->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
|
2018-06-06 11:05:20 +00:00
|
|
|
ui->verticalLayout->addWidget(viewStack);
|
2019-09-25 14:18:30 +00:00
|
|
|
viewStack->setEditTriggers(viewStack->editTriggers() &
|
|
|
|
~(QAbstractItemView::DoubleClicked | QAbstractItemView::AnyKeyPressed));
|
2018-06-06 11:05:20 +00:00
|
|
|
|
2019-05-26 10:12:23 +00:00
|
|
|
editAction = new QAction(tr("Edit stack value..."), this);
|
2018-07-24 23:15:38 +00:00
|
|
|
viewStack->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2019-01-13 18:11:59 +00:00
|
|
|
refreshDeferrer = createRefreshDeferrer([this]() {
|
|
|
|
updateContents();
|
|
|
|
});
|
|
|
|
|
2018-06-06 11:05:20 +00:00
|
|
|
connect(Core(), &CutterCore::refreshAll, this, &StackWidget::updateContents);
|
|
|
|
connect(Core(), &CutterCore::seekChanged, this, &StackWidget::updateContents);
|
2018-07-24 23:15:38 +00:00
|
|
|
connect(Core(), &CutterCore::stackChanged, this, &StackWidget::updateContents);
|
2018-07-18 10:15:10 +00:00
|
|
|
connect(Config(), &Configuration::fontsUpdated, this, &StackWidget::fontsUpdatedSlot);
|
2018-07-24 23:15:38 +00:00
|
|
|
connect(viewStack, SIGNAL(doubleClicked(const QModelIndex &)), this,
|
|
|
|
SLOT(onDoubleClicked(const QModelIndex &)));
|
|
|
|
connect(viewStack, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested(QPoint)));
|
|
|
|
connect(editAction, &QAction::triggered, this, &StackWidget::editStack);
|
2019-09-25 14:18:30 +00:00
|
|
|
connect(viewStack->selectionModel(), &QItemSelectionModel::currentChanged,
|
|
|
|
this, &StackWidget::onCurrentChanged);
|
|
|
|
connect(modelStack, &QStandardItemModel::itemChanged, this, &StackWidget::onItemChanged);
|
|
|
|
|
|
|
|
addressableItemContextMenu.addAction(editAction);
|
|
|
|
addActions(addressableItemContextMenu.actions());
|
|
|
|
|
|
|
|
menuText.setSeparator(true);
|
|
|
|
qhelpers::prependQAction(&menuText, &addressableItemContextMenu);
|
2018-06-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 18:11:59 +00:00
|
|
|
StackWidget::~StackWidget() = default;
|
2018-06-06 11:05:20 +00:00
|
|
|
|
|
|
|
void StackWidget::updateContents()
|
|
|
|
{
|
2019-01-13 18:11:59 +00:00
|
|
|
if (!refreshDeferrer->attemptRefresh(nullptr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-06 11:05:20 +00:00
|
|
|
setStackGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackWidget::setStackGrid()
|
|
|
|
{
|
2019-09-25 14:18:30 +00:00
|
|
|
updatingData = true;
|
2018-06-06 11:05:20 +00:00
|
|
|
QJsonArray stackValues = Core()->getStack().array();
|
|
|
|
int i = 0;
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const QJsonValue &value : stackValues) {
|
2018-06-06 11:05:20 +00:00
|
|
|
QJsonObject stackItem = value.toObject();
|
|
|
|
QString addr = RAddressString(stackItem["addr"].toVariant().toULongLong());
|
|
|
|
QString valueStack = RAddressString(stackItem["value"].toVariant().toULongLong());
|
|
|
|
QStandardItem *rowOffset = new QStandardItem(addr);
|
2018-07-24 23:15:38 +00:00
|
|
|
rowOffset->setEditable(false);
|
2018-06-06 11:05:20 +00:00
|
|
|
QStandardItem *rowValue = new QStandardItem(valueStack);
|
2019-09-25 14:18:30 +00:00
|
|
|
modelStack->setItem(i, COLUMN_OFFSET, rowOffset);
|
|
|
|
modelStack->setItem(i, COLUMN_VALUE, rowValue);
|
2018-06-06 11:05:20 +00:00
|
|
|
QJsonValue refObject = stackItem["ref"];
|
|
|
|
if (!refObject.isUndefined()) { // check that the key exists
|
|
|
|
QString ref = refObject.toString();
|
2018-07-24 23:15:38 +00:00
|
|
|
if (ref.contains("ascii") && ref.count("-->") == 1) {
|
|
|
|
ref = Core()->cmd("psz @ [" + addr + "]");
|
|
|
|
}
|
2018-06-06 11:05:20 +00:00
|
|
|
QStandardItem *rowRef = new QStandardItem(ref);
|
2019-09-25 14:18:30 +00:00
|
|
|
rowRef->setEditable(false);
|
|
|
|
QModelIndex cell = modelStack->index(i, COLUMN_DESCRIPTION);
|
|
|
|
modelStack->setItem(i, COLUMN_DESCRIPTION, rowRef);
|
2018-07-24 23:15:38 +00:00
|
|
|
if (refObject.toString().contains("ascii") && refObject.toString().count("-->") == 1) {
|
2019-09-25 14:18:30 +00:00
|
|
|
modelStack->setData(cell, QVariant(QColor(243, 156, 17)),
|
2018-09-30 20:00:53 +00:00
|
|
|
Qt::ForegroundRole); // orange
|
2018-07-24 23:15:38 +00:00
|
|
|
} else if (ref.contains("program R X") && ref.count("-->") == 0) {
|
2019-09-25 14:18:30 +00:00
|
|
|
modelStack->setData(cell, QVariant(QColor(Qt::red)),
|
2018-09-30 20:00:53 +00:00
|
|
|
Qt::ForegroundRole);
|
2018-07-24 23:15:38 +00:00
|
|
|
} else if (ref.contains("stack") && ref.count("-->") == 0) {
|
2019-09-25 14:18:30 +00:00
|
|
|
modelStack->setData(cell, QVariant(QColor(Qt::cyan)),
|
2018-09-30 20:00:53 +00:00
|
|
|
Qt::ForegroundRole);
|
2018-07-24 23:15:38 +00:00
|
|
|
} else if (ref.contains("library") && ref.count("-->") == 0) {
|
2019-09-25 14:18:30 +00:00
|
|
|
modelStack->setData(cell, QVariant(QColor(Qt::green)),
|
2018-09-30 20:00:53 +00:00
|
|
|
Qt::ForegroundRole);
|
2018-07-24 23:15:38 +00:00
|
|
|
}
|
2018-06-06 11:05:20 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
viewStack->setModel(modelStack);
|
2019-09-25 14:18:30 +00:00
|
|
|
viewStack->resizeColumnsToContents();
|
|
|
|
updatingData = false;
|
2018-06-06 11:05:20 +00:00
|
|
|
}
|
2018-07-18 10:15:10 +00:00
|
|
|
|
|
|
|
void StackWidget::fontsUpdatedSlot()
|
|
|
|
{
|
|
|
|
viewStack->setFont(Config()->getFont());
|
2018-07-24 23:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackWidget::onDoubleClicked(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2018-11-05 21:51:27 +00:00
|
|
|
// Check if we are clicking on the offset or value columns and seek if it is the case
|
2019-09-25 14:18:30 +00:00
|
|
|
int column = index.column();
|
|
|
|
if (column <= COLUMN_VALUE) {
|
2018-07-24 23:15:38 +00:00
|
|
|
QString item = index.data().toString();
|
2019-09-25 14:18:30 +00:00
|
|
|
Core()->seek(item);
|
|
|
|
if (column == COLUMN_OFFSET) {
|
|
|
|
mainWindow->showMemoryWidget(MemoryWidgetType::Hexdump);
|
|
|
|
} else {
|
|
|
|
Core()->showMemoryWidget();
|
|
|
|
}
|
2018-07-24 23:15:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackWidget::customMenuRequested(QPoint pos)
|
|
|
|
{
|
2019-09-25 14:18:30 +00:00
|
|
|
addressableItemContextMenu.exec(viewStack->viewport()->mapToGlobal(pos));
|
2018-07-24 23:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackWidget::editStack()
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
int row = viewStack->selectionModel()->currentIndex().row();
|
2019-09-25 14:18:30 +00:00
|
|
|
auto model = viewStack->model();
|
|
|
|
QString offset = model->index(row, COLUMN_OFFSET).data().toString();
|
2019-03-23 06:32:31 +00:00
|
|
|
EditInstructionDialog e(EDIT_NONE, this);
|
|
|
|
e.setWindowTitle(tr("Edit stack at %1").arg(offset));
|
2018-07-24 23:15:38 +00:00
|
|
|
|
2019-09-25 14:18:30 +00:00
|
|
|
QString oldBytes = model->index(row, COLUMN_VALUE).data().toString();
|
2019-03-23 06:32:31 +00:00
|
|
|
e.setInstruction(oldBytes);
|
2018-07-24 23:15:38 +00:00
|
|
|
|
2019-03-23 06:32:31 +00:00
|
|
|
if (e.exec()) {
|
|
|
|
QString bytes = e.getInstruction();
|
2018-07-24 23:15:38 +00:00
|
|
|
if (bytes != oldBytes) {
|
2018-11-05 21:51:27 +00:00
|
|
|
Core()->editBytesEndian(offset.toULongLong(&ok, 16), bytes);
|
2018-07-24 23:15:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 14:18:30 +00:00
|
|
|
|
|
|
|
void StackWidget::onCurrentChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
|
|
|
{
|
|
|
|
Q_UNUSED(current)
|
|
|
|
Q_UNUSED(previous)
|
|
|
|
auto currentIndex = viewStack->selectionModel()->currentIndex();
|
|
|
|
QString offsetString;
|
|
|
|
if (currentIndex.column() != COLUMN_DESCRIPTION) {
|
|
|
|
offsetString = currentIndex.data().toString();
|
|
|
|
} else {
|
|
|
|
offsetString = currentIndex.sibling(currentIndex.row(), COLUMN_VALUE).data().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
RVA offset = Core()->math(offsetString);
|
|
|
|
addressableItemContextMenu.setTarget(offset);
|
|
|
|
if (currentIndex.column() == COLUMN_OFFSET) {
|
|
|
|
menuText.setText(tr("Stack position"));
|
|
|
|
} else {
|
|
|
|
menuText.setText(tr("Pointed memory"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackWidget::onItemChanged(QStandardItem *item)
|
|
|
|
{
|
|
|
|
if (updatingData || item->column() != COLUMN_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QModelIndex index = item->index();
|
|
|
|
int row = item->row();
|
|
|
|
QString text = item->text();
|
|
|
|
// Queue the update instead of performing immediately. Editing will trigger reload.
|
|
|
|
// Performing reload while itemChanged signal is on stack would result
|
|
|
|
// in itemView getting stuck in EditingState and preventing further edits.
|
|
|
|
QMetaObject::invokeMethod(this, [this, index, row, text]() {
|
|
|
|
QString offsetString = index.sibling(row, COLUMN_OFFSET).data().toString();
|
|
|
|
bool ok = false;
|
|
|
|
auto offset = offsetString.toULongLong(&ok, 16);
|
|
|
|
if (ok) {
|
|
|
|
Core()->editBytesEndian(offset, text);
|
|
|
|
}
|
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
}
|