From 114e22c48ff0c96b989d3d259a079a8551e0a675 Mon Sep 17 00:00:00 2001 From: Matthew Budd Date: Wed, 26 Aug 2020 22:44:11 -0400 Subject: [PATCH] Added hex editor bookmark functionality. Still TODO save bookmarks to file based on ROM name. --- src/drivers/Qt/HexEditor.cpp | 188 +++++++++++++++++++++++++++++++++-- src/drivers/Qt/HexEditor.h | 61 +++++++++++- 2 files changed, 240 insertions(+), 9 deletions(-) diff --git a/src/drivers/Qt/HexEditor.cpp b/src/drivers/Qt/HexEditor.cpp index aefea213..18aaa3df 100644 --- a/src/drivers/Qt/HexEditor.cpp +++ b/src/drivers/Qt/HexEditor.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "../../types.h" #include "../../fceu.h" @@ -35,6 +36,7 @@ #include "Qt/HexEditor.h" #include "Qt/ConsoleUtilities.h" +static HexBookMarkManager_t hbm; //---------------------------------------------------------------------------- static int getRAM( unsigned int i ) { @@ -279,6 +281,114 @@ void memBlock_t::init(void) } } //---------------------------------------------------------------------------- +HexBookMark::HexBookMark(void) +{ + addr = 0; + mode = 0; + desc[0] = 0; +} +//---------------------------------------------------------------------------- +HexBookMark::~HexBookMark(void) +{ + +} +//---------------------------------------------------------------------------- +HexBookMarkManager_t::HexBookMarkManager_t(void) +{ + +} +//---------------------------------------------------------------------------- +HexBookMarkManager_t::~HexBookMarkManager_t(void) +{ + removeAll(); +} +//---------------------------------------------------------------------------- +void HexBookMarkManager_t::removeAll(void) +{ + HexBookMark *b; + + while ( !ls.empty() ) + { + b = ls.front(); + + delete b; + + ls.pop_front(); + } +} +//---------------------------------------------------------------------------- +int HexBookMarkManager_t::addBookMark( int addr, int mode, const char *desc ) +{ + HexBookMark *b; + + b = new HexBookMark(); + b->addr = addr; + b->mode = mode; + + if ( desc ) + { + strncpy( b->desc, desc, 63 ); + } + + b->desc[63] = 0; + + ls.push_back(b); + + updateVector(); + + return 0; +} +//---------------------------------------------------------------------------- +void HexBookMarkManager_t::updateVector(void) +{ + std::list ::iterator it; + + v.clear(); + + for (it=ls.begin(); it!=ls.end(); it++) + { + v.push_back( *it ); + } + + return; +} +//---------------------------------------------------------------------------- +int HexBookMarkManager_t::size(void) +{ + return ls.size(); +} +//---------------------------------------------------------------------------- +HexBookMark *HexBookMarkManager_t::getBookMark( int index ) +{ + if ( index < 0 ) + { + return NULL; + } + else if ( index >= (int)v.size() ) + { + return NULL; + } + return v[index]; +} +//---------------------------------------------------------------------------- +HexBookMarkMenuAction::HexBookMarkMenuAction(QString desc, QWidget *parent) + : QAction( desc, parent ) +{ + bm = NULL; qedit = NULL; +} +//---------------------------------------------------------------------------- +HexBookMarkMenuAction::~HexBookMarkMenuAction(void) +{ + +} +//---------------------------------------------------------------------------- +void HexBookMarkMenuAction::activateCB(void) +{ + //printf("Activate Bookmark: %p \n", bm ); + qedit->setMode( bm->mode ); + qedit->setAddr( bm->addr ); +} +//---------------------------------------------------------------------------- HexEditorDialog_t::HexEditorDialog_t(QWidget *parent) : QDialog( parent ) { @@ -472,9 +582,17 @@ HexEditorDialog_t::~HexEditorDialog_t(void) periodicTimer->stop(); } //---------------------------------------------------------------------------- +void HexEditorDialog_t::removeAllBookmarks(void) +{ + hbm.removeAll(); + + populateBookmarkMenu(); +} +//---------------------------------------------------------------------------- void HexEditorDialog_t::populateBookmarkMenu(void) { QAction *act; + HexBookMarkMenuAction *hAct; bookmarkMenu->clear(); @@ -482,10 +600,23 @@ void HexEditorDialog_t::populateBookmarkMenu(void) act = new QAction(tr("Remove All Bookmarks"), this); //act->setShortcuts(QKeySequence::Open); act->setStatusTip(tr("Remove All Bookmarks")); - //connect(act, SIGNAL(triggered(void)), this, SLOT(pickBackGroundColor(void)) ); + connect(act, SIGNAL(triggered(void)), this, SLOT(removeAllBookmarks(void)) ); bookmarkMenu->addAction(act); bookmarkMenu->addSeparator(); + + for (int i=0; idesc), this); + bookmarkMenu->addAction(hAct); + hAct->bm = b; hAct->qedit = editor; + connect(hAct, SIGNAL(triggered(void)), hAct, SLOT(activateCB(void)) ); + } + } } //---------------------------------------------------------------------------- void HexEditorDialog_t::closeEvent(QCloseEvent *event) @@ -654,6 +785,7 @@ QHexEdit::QHexEdit(QWidget *parent) { QPalette pal; + this->parent = (HexEditorDialog_t*)parent; this->setFocusPolicy(Qt::StrongFocus); font.setFamily("Courier New"); @@ -821,6 +953,8 @@ void QHexEdit::setAddr( int newAddr ) cursorPosX = 2*((newAddr - addr)%16); cursorPosY = (newAddr - addr)/16; + + vbar->setValue( lineOffset ); } //---------------------------------------------------------------------------- void QHexEdit::setHorzScroll( int value ) @@ -1205,7 +1339,7 @@ void QHexEdit::contextMenuEvent(QContextMenuEvent *event) int addr; char stmp[128]; - addr = convPixToAddr( event->pos() ); + ctxAddr = addr = convPixToAddr( event->pos() ); //printf("contextMenuEvent\n"); switch ( viewMode ) @@ -1241,26 +1375,30 @@ void QHexEdit::contextMenuEvent(QContextMenuEvent *event) } } - act = new QAction(tr("TODO Add Bookmark"), this); + act = new QAction(tr("Add Bookmark"), this); menu.addAction(act); + connect( act, SIGNAL(triggered(void)), this, SLOT(addBookMarkCB(void)) ); } break; case MODE_NES_PPU: { - act = new QAction(tr("TODO Add Bookmark"), this); + act = new QAction(tr("Add Bookmark"), this); menu.addAction(act); + connect( act, SIGNAL(triggered(void)), this, SLOT(addBookMarkCB(void)) ); } break; case MODE_NES_OAM: { - act = new QAction(tr("TODO Add Bookmark"), this); + act = new QAction(tr("Add Bookmark"), this); menu.addAction(act); + connect( act, SIGNAL(triggered(void)), this, SLOT(addBookMarkCB(void)) ); } break; case MODE_NES_ROM: { - act = new QAction(tr("TODO Add Bookmark"), this); + act = new QAction(tr("Add Bookmark"), this); menu.addAction(act); + connect( act, SIGNAL(triggered(void)), this, SLOT(addBookMarkCB(void)) ); } break; } @@ -1269,6 +1407,44 @@ void QHexEdit::contextMenuEvent(QContextMenuEvent *event) } //---------------------------------------------------------------------------- +void QHexEdit::addBookMarkCB(void) +{ + int ret; + char stmp[64]; + QInputDialog dialog(this); + + switch ( viewMode ) + { + default: + case MODE_NES_RAM: + sprintf( stmp, "RAM %04X", ctxAddr ); + break; + case MODE_NES_PPU: + sprintf( stmp, "PPU %04X", ctxAddr ); + break; + case MODE_NES_OAM: + sprintf( stmp, "OAM %04X", ctxAddr ); + break; + case MODE_NES_ROM: + sprintf( stmp, "ROM %04X", ctxAddr ); + break; + } + + dialog.setWindowTitle( tr("Add Bookmark") ); + dialog.setLabelText( tr("Specify New Bookmark Description") ); + dialog.setOkButtonText( tr("Add") ); + dialog.setTextValue( tr(stmp) ); + + dialog.show(); + ret = dialog.exec(); + + if ( QDialog::Accepted == ret ) + { + hbm.addBookMark( ctxAddr, viewMode, dialog.textValue().toStdString().c_str() ); + parent->populateBookmarkMenu(); + } +} +//---------------------------------------------------------------------------- void QHexEdit::jumpToROM(void) { setMode( MODE_NES_ROM ); diff --git a/src/drivers/Qt/HexEditor.h b/src/drivers/Qt/HexEditor.h index a2f7079a..de3a714c 100644 --- a/src/drivers/Qt/HexEditor.h +++ b/src/drivers/Qt/HexEditor.h @@ -3,9 +3,13 @@ #pragma once +#include +#include + #include #include #include +#include #include #include #include @@ -42,6 +46,52 @@ struct memBlock_t int (*memAccessFunc)( unsigned int offset); }; +class HexBookMark +{ + public: + HexBookMark(void); + ~HexBookMark(void); + + int addr; + int mode; + char desc[64]; +}; + +class HexBookMarkManager_t +{ + public: + HexBookMarkManager_t(void); + ~HexBookMarkManager_t(void); + + void removeAll(void); + int addBookMark( int addr, int mode, const char *desc ); + int size(void); + HexBookMark *getBookMark( int index ); + private: + void updateVector(void); + std::list ls; + std::vector v; +}; + +class QHexEdit; + +class HexBookMarkMenuAction : public QAction +{ + Q_OBJECT + + public: + HexBookMarkMenuAction( QString desc, QWidget *parent = 0); + ~HexBookMarkMenuAction(void); + + QHexEdit *qedit; + HexBookMark *bm; + public slots: + void activateCB(void); + +}; + +class HexEditorDialog_t; + class QHexEdit : public QWidget { Q_OBJECT @@ -93,6 +143,8 @@ class QHexEdit : public QWidget QColor highLightColor[ HIGHLIGHT_ACTIVITY_NUM_COLORS ]; QColor rvActvTextColor[ HIGHLIGHT_ACTIVITY_NUM_COLORS ]; + HexEditorDialog_t *parent; + uint64_t total_instructions_lp; int viewMode; @@ -119,6 +171,7 @@ class QHexEdit : public QWidget int editValue; int editMask; int jumpToRomValue; + int ctxAddr; bool cursorBlink; bool reverseVideo; @@ -126,6 +179,7 @@ class QHexEdit : public QWidget private slots: void jumpToROM(void); + void addBookMarkCB(void); }; @@ -137,11 +191,11 @@ class HexEditorDialog_t : public QDialog HexEditorDialog_t(QWidget *parent = 0); ~HexEditorDialog_t(void); - protected: - void closeEvent(QCloseEvent *bar); - void gotoAddress(int newAddr); void populateBookmarkMenu(void); + protected: + + void closeEvent(QCloseEvent *bar); QScrollBar *vbar; QScrollBar *hbar; @@ -168,4 +222,5 @@ class HexEditorDialog_t : public QDialog void actvHighlightRVCB(bool value); void pickForeGroundColor(void); void pickBackGroundColor(void); + void removeAllBookmarks(void); };