keep focus when scrolling

This commit is contained in:
Yanis002 2025-07-30 18:35:52 +02:00
parent 07cbc786af
commit 597e68c8d0
2 changed files with 28 additions and 19 deletions

View File

@ -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<int>>("QVector<int>");
qRegisterMetaType<u32>("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()

View File

@ -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;