From 597e68c8d034d9d3e379bdde78e210c85c911530 Mon Sep 17 00:00:00 2001 From: Yanis002 <35189056+Yanis002@users.noreply.github.com> Date: Wed, 30 Jul 2025 18:35:52 +0200 Subject: [PATCH] keep focus when scrolling --- src/frontend/qt_sdl/MemViewDialog.cpp | 41 ++++++++++++++++----------- src/frontend/qt_sdl/MemViewDialog.h | 6 ++-- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/frontend/qt_sdl/MemViewDialog.cpp b/src/frontend/qt_sdl/MemViewDialog.cpp index 12ffb9bd..67e5bbf1 100644 --- a/src/frontend/qt_sdl/MemViewDialog.cpp +++ b/src/frontend/qt_sdl/MemViewDialog.cpp @@ -181,13 +181,6 @@ void CustomGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event) if (this->ScrollBar != nullptr) { - QGraphicsItem* item = this->focusItem(); - - if (item) - { - item->clearFocus(); - } - QWheelEvent *pEvent = new QWheelEvent( event->pos(), event->screenPos(), @@ -205,21 +198,21 @@ void CustomGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event) void CustomGraphicsScene::onFocusItemChanged(QGraphicsItem *newFocus, QGraphicsItem *oldFocus, Qt::FocusReason reason) { - MemViewDialog* Dialog = (MemViewDialog*)this->parent(); + MemViewDialog* dialog = (MemViewDialog*)this->parent(); - if (Dialog != nullptr && newFocus != nullptr) + if (dialog != nullptr && newFocus != nullptr) { - uint32_t addr = Dialog->GetFocusAddress(newFocus); + uint32_t addr = dialog->GetFocusAddress(newFocus); if (addr != -1) { QString text; text.setNum(addr, 16); - Dialog->GetAddrLabel()->setText(text.toUpper().rightJustified(8, '0').prepend("0x")); + dialog->GetAddrLabel()->setText(text.toUpper().rightJustified(8, '0').prepend("0x")); - if (Dialog->GetFocusCheckbox()->isChecked()) + if (dialog->GetFocusCheckbox()->isChecked()) { - Dialog->GetValueAddrLineEdit()->setText(text.toUpper().rightJustified(8, '0').prepend("0x")); + dialog->GetValueAddrLineEdit()->setText(text.toUpper().rightJustified(8, '0').prepend("0x")); } } } @@ -238,7 +231,7 @@ MemViewDialog::MemViewDialog(QWidget* parent) : QDialog(parent) this->setWindowTitle("Memory Viewer - melonDS"); setAttribute(Qt::WA_DeleteOnClose); - QColor placeholderColor = QColor(127, 135, 140); + QColor placeholderColor = QColor(160, 160, 160); // create the widgets, maybe not necessary to keep a reference to everything but whatever this->GfxScene = new CustomGraphicsScene(this); @@ -269,7 +262,7 @@ MemViewDialog::MemViewDialog(QWidget* parent) : QDialog(parent) this->SearchLineEdit->setGeometry(8, 40, 144, 32); QPalette paletteSearch = this->SearchLineEdit->palette(); paletteSearch.setColor(QPalette::ColorRole::PlaceholderText, placeholderColor); - this->SetValAddr->setPalette(paletteSearch); + this->SearchLineEdit->setPalette(paletteSearch); this->SetValFocus->setText("Address on focus"); this->SetValFocus->setGeometry(4, 106, 131, 22); @@ -413,6 +406,7 @@ MemViewDialog::MemViewDialog(QWidget* parent) : QDialog(parent) connect(this->SearchLineEdit, &QLineEdit::textChanged, this, &MemViewDialog::onAddressTextChanged); connect(this->SetValBtn, &QPushButton::pressed, this, &MemViewDialog::onValueBtnSetPressed); connect(this->GfxScene, &QGraphicsScene::focusItemChanged, this->GfxScene, &CustomGraphicsScene::onFocusItemChanged); + connect(this->ScrollBar, &QScrollBar::valueChanged, this, &MemViewDialog::onScrollBarValueChanged); qRegisterMetaType>("QVector"); qRegisterMetaType("u32"); @@ -567,6 +561,10 @@ void MemViewDialog::UpdateText(int addrIndex, int index) } } } + + if (this->ForceTextUpdate) { + this->ForceTextUpdate = false; + } } void MemViewDialog::UpdateAddress(int index) @@ -777,12 +775,23 @@ void MemViewDialog::onSwitchFocus(FocusDirection eDirection) // so we need to force an update to make sure what we focus has the right value this->ForceTextUpdate = true; this->UpdateText(addrIndex, index); - this->ForceTextUpdate = false; item->SetTextSelection(true); } } } +void MemViewDialog::onScrollBarValueChanged(int value) { + CustomTextItem* item = (CustomTextItem*)this->GfxScene->focusItem(); + int packed = this->GetItemIndex(item); + + if (item != nullptr && packed != -1) + { + this->ForceTextUpdate = true; + this->UpdateText(packed >> 8, packed & 0xFF); + item->SetTextSelection(true); + } +} + // --- MemViewThread --- MemViewThread::~MemViewThread() diff --git a/src/frontend/qt_sdl/MemViewDialog.h b/src/frontend/qt_sdl/MemViewDialog.h index b34e7982..22c1aae2 100644 --- a/src/frontend/qt_sdl/MemViewDialog.h +++ b/src/frontend/qt_sdl/MemViewDialog.h @@ -169,7 +169,7 @@ public: } QGraphicsItem* GetItem(int addrIndex, int index) - { + { if (addrIndex < 16 && index < 16) { return this->RAMTextItems[addrIndex][index]; @@ -249,14 +249,14 @@ private slots: void onValueBtnSetPressed(); void onApplyEditToRAM(uint8_t value, QGraphicsItem *focus); void onSwitchFocus(FocusDirection eDirection); + void onScrollBarValueChanged(int value); public: uint32_t ARM9AddrStart; uint32_t ARM9AddrEnd; - -private: bool ForceTextUpdate; +private: QGraphicsView* GfxView; CustomGraphicsScene* GfxScene; MemViewThread* UpdateThread;