Added logic to realtime update js global variable viewer.

This commit is contained in:
harry 2024-01-17 07:14:10 -05:00
parent 1dde9e7e75
commit 7a0be296fa
2 changed files with 91 additions and 11 deletions

View File

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

View File

@ -155,6 +155,23 @@ public:
QMap<QString, JsPropertyItem*> childMap;
};
class JsPropertyTree : public QTreeWidget
{
Q_OBJECT
public:
JsPropertyTree(QWidget *parent = nullptr)
: QTreeWidget(parent)
{
}
virtual ~JsPropertyTree() override
{
}
QMap<QString, JsPropertyItem*> 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;