From 7a0be296fadb49eedac3828b1473a52f0bbe0b0e Mon Sep 17 00:00:00 2001 From: harry Date: Wed, 17 Jan 2024 07:14:10 -0500 Subject: [PATCH] Added logic to realtime update js global variable viewer. --- src/drivers/Qt/QtScriptManager.cpp | 82 ++++++++++++++++++++++++++---- src/drivers/Qt/QtScriptManager.h | 20 +++++++- 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/src/drivers/Qt/QtScriptManager.cpp b/src/drivers/Qt/QtScriptManager.cpp index 15917cb1..8757a523 100644 --- a/src/drivers/Qt/QtScriptManager.cpp +++ b/src/drivers/Qt/QtScriptManager.cpp @@ -667,7 +667,7 @@ QScriptDialog_t::QScriptDialog_t(QWidget *parent) tabWidget->addTab(jsOutput, tr("Output Console")); - propTree = new QTreeWidget(); + propTree = new JsPropertyTree(); propTree->setColumnCount(3); propTree->setSelectionMode( QAbstractItemView::SingleSelection ); @@ -742,6 +742,12 @@ void QScriptDialog_t::closeWindow(void) deleteLater(); } //---------------------------------------------------- +void QScriptDialog_t::clearPropertyTree() +{ + propTree->childMap.clear(); + propTree->clear(); +} +//---------------------------------------------------- void QScriptDialog_t::loadPropertyTree(QJSValue& object, JsPropertyItem* parentItem) { QJSValueIterator it(object); @@ -755,9 +761,35 @@ void QScriptDialog_t::loadPropertyTree(QJSValue& object, JsPropertyItem* parentI if (!isPrototype) { - JsPropertyItem* item = new JsPropertyItem(); + JsPropertyItem* item = nullptr; + QString name = it.name(); QString value; const char *type = "unknown"; + bool itemIsNew = false; + + if (parentItem == nullptr) + { + auto it = propTree->childMap.find(name); + + if (it != propTree->childMap.end()) + { + item = it.value(); + } + } + else + { + auto it = parentItem->childMap.find(name); + + if (it != parentItem->childMap.end()) + { + item = it.value(); + } + } + if (item == nullptr) + { + item = new JsPropertyItem(); + itemIsNew = true; + } if (child.isArray()) { @@ -830,18 +862,36 @@ void QScriptDialog_t::loadPropertyTree(QJSValue& object, JsPropertyItem* parentI value = child.toString(); } - item->setText(0, it.name()); - item->setText(1, type); - item->setText(2, value); - item->jsValue = child; - if (parentItem == nullptr) + if (itemIsNew) { - propTree->addTopLevelItem(item); + item->setText(0, name); + item->setText(1, type); + item->setText(2, value); } else { - parentItem->addChild(item); + bool itemHasChanged = !item->jsValue.strictlyEquals(child); + + if (itemHasChanged) + { + item->setText(2, value); + } + } + item->jsValue = child; + + if (itemIsNew) + { + if (parentItem == nullptr) + { + propTree->addTopLevelItem(item); + propTree->childMap[name] = item; + } + else + { + parentItem->addChild(item); + parentItem->childMap[name] = item; + } } if (child.isObject()) @@ -870,6 +920,18 @@ void QScriptDialog_t::updatePeriodic(void) } emuThreadText.clear(); } + + if (scriptInstance != nullptr) + { + auto* engine = scriptInstance->getEngine(); + + if (engine) + { + QJSValue globals = engine->globalObject(); + + loadPropertyTree(globals); + } + } refreshState(); FCEU_WRAPPER_UNLOCK(); } @@ -1035,6 +1097,7 @@ void QScriptDialog_t::openScriptFile(void) void QScriptDialog_t::startScript(void) { FCEU_WRAPPER_LOCK(); + clearPropertyTree(); scriptInstance->resetEngine(); if (scriptInstance->loadScriptFile(scriptPath->text())) { @@ -1056,7 +1119,6 @@ void QScriptDialog_t::startScript(void) QJSValue globals = scriptInstance->getEngine()->globalObject(); - propTree->clear(); loadPropertyTree(globals); FCEU_WRAPPER_UNLOCK(); diff --git a/src/drivers/Qt/QtScriptManager.h b/src/drivers/Qt/QtScriptManager.h index 4914e748..1474897c 100644 --- a/src/drivers/Qt/QtScriptManager.h +++ b/src/drivers/Qt/QtScriptManager.h @@ -155,6 +155,23 @@ public: QMap childMap; }; +class JsPropertyTree : public QTreeWidget +{ + Q_OBJECT + +public: + JsPropertyTree(QWidget *parent = nullptr) + : QTreeWidget(parent) + { + } + + virtual ~JsPropertyTree() override + { + } + + QMap childMap; +}; + class QScriptDialog_t : public QDialog { Q_OBJECT @@ -169,6 +186,7 @@ public: protected: void closeEvent(QCloseEvent *bar); void openJSKillMessageBox(void); + void clearPropertyTree(); void loadPropertyTree(QJSValue& val, JsPropertyItem* parentItem = nullptr); QTimer *periodicTimer; @@ -180,7 +198,7 @@ protected: QPushButton *clearButton; QTabWidget *tabWidget; QTextEdit *jsOutput; - QTreeWidget *propTree; + JsPropertyTree *propTree; QtScriptInstance *scriptInstance; QString emuThreadText;