From f6c6d4e9dc50578b0ee06bb393d34038006b1535 Mon Sep 17 00:00:00 2001 From: Jon ESA Date: Wed, 1 Apr 2026 19:40:20 +0100 Subject: [PATCH] Added lazy loading --- WebSocketController.cpp | 69 +++++++++++++++++++++++++++++++++-------- WebSocketController.h | 21 +++++++++---- main.cpp | 63 +++++++++++++++++++++++-------------- 3 files changed, 111 insertions(+), 42 deletions(-) diff --git a/WebSocketController.cpp b/WebSocketController.cpp index 68217f3..493a801 100644 --- a/WebSocketController.cpp +++ b/WebSocketController.cpp @@ -15,7 +15,6 @@ WebSocketController::WebSocketController(QLineEdit *urlEdit, {} QWebSocket *WebSocketController::socket() { return &m_socket; } - void WebSocketController::addLogView(QTextEdit *log) { m_logs.append(log); } void WebSocketController::setSettingsTree(SettingsTree *tree) @@ -25,7 +24,7 @@ void WebSocketController::setSettingsTree(SettingsTree *tree) this, &WebSocketController::onValueEdited); } -void WebSocketController::setGamesPanel(GamesPanel *panel) { m_gamesPanel = panel; } +void WebSocketController::setGamesPanel(GamesPanel *panel) { m_gamesPanel = panel; } void WebSocketController::setVersionsPanel(VersionsPanel *panel) { m_versionsPanel = panel; } bool WebSocketController::isConnected() const @@ -33,6 +32,43 @@ bool WebSocketController::isConnected() const return m_socket.state() == QAbstractSocket::ConnectedState; } +// ---------------------------------------------------------------- +// Lazy loaders — called ONLY when user clicks a tab for first time +// ---------------------------------------------------------------- + +void WebSocketController::requestGamesData() +{ + if (!isConnected() || m_gamesRequested) return; + m_gamesRequested = true; + broadcast("-- Loading games tab --"); + sendCommand(QStringLiteral("GAM list")); +} + +void WebSocketController::requestVersionsData() +{ + if (!isConnected() || m_versionsRequested) return; + m_versionsRequested = true; + broadcast("-- Loading versions tab --"); + sendCommand(QStringLiteral("NAM")); + sendCommand(QStringLiteral("VER")); + sendCommand(QStringLiteral("UID")); + sendCommand(QStringLiteral("RNP")); + // Slave VER/UID loop fires in handleProtocol when RNP reply arrives +} + +void WebSocketController::requestSettingsData() +{ + if (!isConnected() || m_settingsRequested) return; + m_settingsRequested = true; + broadcast("-- Loading settings tab --"); + sendCommand(QStringLiteral("GBL List")); + // Individual GBL loop fires in handleProtocol when List reply arrives +} + +// ---------------------------------------------------------------- +// Connection +// ---------------------------------------------------------------- + void WebSocketController::startConnection() { const QUrl url(m_urlEdit->text().trimmed()); @@ -46,7 +82,10 @@ void WebSocketController::closeConnection() { broadcast("Closing connection"); m_settingsKeys.clear(); - m_rnpCount = -1; + m_rnpCount = -1; + m_gamesRequested = false; + m_versionsRequested = false; + m_settingsRequested = false; if (m_versionsPanel) m_versionsPanel->reset(); m_socket.close(); } @@ -60,16 +99,13 @@ void WebSocketController::sendCommand(const QString &cmd) void WebSocketController::onConnected() { + // Send NOTHING here — all data is lazy-loaded on tab click broadcast("Connected"); m_statusLabel->setText("Connected"); - m_rnpCount = -1; - - sendCommand(QStringLiteral("GBL List")); - sendCommand(QStringLiteral("GAM list")); - sendCommand(QStringLiteral("NAM")); - sendCommand(QStringLiteral("VER")); - sendCommand(QStringLiteral("UID")); - sendCommand(QStringLiteral("RNP")); + m_rnpCount = -1; + m_gamesRequested = false; + m_versionsRequested = false; + m_settingsRequested = false; } void WebSocketController::onDisconnected() @@ -77,7 +113,10 @@ void WebSocketController::onDisconnected() broadcast("Disconnected"); m_statusLabel->setText("Disconnected"); m_settingsKeys.clear(); - m_rnpCount = -1; + m_rnpCount = -1; + m_gamesRequested = false; + m_versionsRequested = false; + m_settingsRequested = false; } void WebSocketController::onTextMessageReceived(const QString &msg) @@ -98,6 +137,10 @@ void WebSocketController::onValueEdited(const QString &key, const QString &newVa sendCommand(QString("GBL %1").arg(key)); } +// ---------------------------------------------------------------- +// Protocol handler +// ---------------------------------------------------------------- + void WebSocketController::handleProtocol(const QString &msg) { const QStringList tokens = msg.split(' ', Qt::SkipEmptyParts); @@ -124,7 +167,7 @@ void WebSocketController::handleProtocol(const QString &msg) return; } - // RNP + // RNP — triggers the slave loop if (cmd == "RNP" && tokens.size() >= 2) { bool ok = false; const int count = tokens[1].toInt(&ok); diff --git a/WebSocketController.h b/WebSocketController.h index 047be98..bc0dab0 100644 --- a/WebSocketController.h +++ b/WebSocketController.h @@ -25,6 +25,11 @@ public: void setVersionsPanel(VersionsPanel *panel); bool isConnected() const; + // Called by main when user selects a tab for the first time + void requestGamesData(); + void requestVersionsData(); + void requestSettingsData(); + public slots: void startConnection(); void closeConnection(); @@ -40,13 +45,17 @@ private: void handleProtocol(const QString &msg); void broadcast(const QString &line); - QLineEdit *m_urlEdit = nullptr; - QLabel *m_statusLabel = nullptr; + QLineEdit *m_urlEdit = nullptr; + QLabel *m_statusLabel = nullptr; QWebSocket m_socket; QList m_logs; - SettingsTree *m_settingsTree = nullptr; - GamesPanel *m_gamesPanel = nullptr; - VersionsPanel *m_versionsPanel = nullptr; + SettingsTree *m_settingsTree = nullptr; + GamesPanel *m_gamesPanel = nullptr; + VersionsPanel *m_versionsPanel = nullptr; QStringList m_settingsKeys; - int m_rnpCount = -1; + int m_rnpCount = -1; + + bool m_gamesRequested = false; + bool m_versionsRequested = false; + bool m_settingsRequested = false; }; diff --git a/main.cpp b/main.cpp index 51ae8da..5919865 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,14 @@ #include "VersionsPanel.h" #include "WebSocketController.h" +// Must match tabs->addTab order below +static constexpr int TAB_GAMES = 0; +static constexpr int TAB_VERSIONS = 1; +static constexpr int TAB_MANUAL = 2; +static constexpr int TAB_SETTINGS = 3; +static constexpr int TAB_POWER = 4; +static constexpr int TAB_PANELS = 5; + static QWidget *makeGamesTab(WebSocketController *ctrl, QWidget *parent) { auto *panel = new GamesPanel(parent); @@ -35,7 +43,7 @@ static QWidget *makeSettingsTab(WebSocketController *ctrl, QWidget *parent) layout->setContentsMargins(2, 2, 2, 2); auto *tree = new SettingsTree(page); auto *ph = new QTreeWidgetItem(tree); - ph->setText(0, "Connect to load settings..."); + ph->setText(0, "Click the settings tab to load..."); layout->addWidget(tree, 1); ctrl->setSettingsTree(tree); return page; @@ -99,15 +107,14 @@ int main(int argc, char *argv[]) mainLayout->setContentsMargins(6, 6, 6, 6); mainLayout->setSpacing(4); - // --- Header bar --- - auto *headerRow = new QHBoxLayout(); - auto *urlEdit = new QLineEdit(&window); + auto *headerRow = new QHBoxLayout(); + auto *urlEdit = new QLineEdit(&window); urlEdit->setPlaceholderText("ws://127.0.0.1:3491/"); urlEdit->setText("ws://127.0.0.1:3491/"); - auto *connectBtn = new QPushButton("Connect", &window); - auto *disconnectBtn= new QPushButton("Disconnect", &window); + auto *connectBtn = new QPushButton("Connect", &window); + auto *disconnectBtn = new QPushButton("Disconnect", &window); disconnectBtn->setEnabled(false); - auto *statusLabel = new QLabel("Disconnected", &window); + auto *statusLabel = new QLabel("Disconnected", &window); headerRow->addWidget(new QLabel("WebSocket URL:", &window)); headerRow->addWidget(urlEdit, 1); @@ -116,33 +123,43 @@ int main(int argc, char *argv[]) headerRow->addWidget(statusLabel); mainLayout->addLayout(headerRow); - // --- Controller --- auto *ctrl = new WebSocketController(urlEdit, statusLabel, &window); - // --- Tabs --- auto *tabs = new QTabWidget(&window); - tabs->addTab(makeGamesTab (ctrl, &window), "games"); - tabs->addTab(makeVersionsTab(ctrl, &window), "versions"); - tabs->addTab(makeManualTab (ctrl, &window), "manual"); - tabs->addTab(makeSettingsTab(ctrl, &window), "settings"); - tabs->addTab(makePlaceholder("Power", &window), "power"); - tabs->addTab(makePanelsTab (ctrl, &window), "panels"); + tabs->addTab(makeGamesTab (ctrl, &window), "games"); // 0 + tabs->addTab(makeVersionsTab(ctrl, &window), "versions"); // 1 + tabs->addTab(makeManualTab (ctrl, &window), "manual"); // 2 + tabs->addTab(makeSettingsTab(ctrl, &window), "settings"); // 3 + tabs->addTab(makePlaceholder("Power content here", &window), "power"); // 4 + tabs->addTab(makePanelsTab (ctrl, &window), "panels"); // 5 mainLayout->addWidget(tabs, 1); - // --- Wire up buttons --- + // Lazy load: each tab fires its own data request on FIRST click only + QObject::connect(tabs, &QTabWidget::currentChanged, &window, + [ctrl](int index) { + switch (index) { + case TAB_GAMES: ctrl->requestGamesData(); break; + case TAB_VERSIONS: ctrl->requestVersionsData(); break; + case TAB_SETTINGS: ctrl->requestSettingsData(); break; + default: break; + } + }); + QObject::connect(connectBtn, &QPushButton::clicked, ctrl, &WebSocketController::startConnection); QObject::connect(disconnectBtn, &QPushButton::clicked, ctrl, &WebSocketController::closeConnection); - QObject::connect(ctrl->socket(), &QWebSocket::connected, ctrl, &WebSocketController::onConnected); - QObject::connect(ctrl->socket(), &QWebSocket::disconnected, ctrl, &WebSocketController::onDisconnected); - QObject::connect(ctrl->socket(), &QWebSocket::textMessageReceived, ctrl, &WebSocketController::onTextMessageReceived); - QObject::connect(ctrl->socket(), &QWebSocket::errorOccurred, ctrl, &WebSocketController::onErrorOccurred); + QObject::connect(ctrl->socket(), &QWebSocket::connected, ctrl, &WebSocketController::onConnected); + QObject::connect(ctrl->socket(), &QWebSocket::disconnected, ctrl, &WebSocketController::onDisconnected); + QObject::connect(ctrl->socket(), &QWebSocket::textMessageReceived, ctrl, &WebSocketController::onTextMessageReceived); + QObject::connect(ctrl->socket(), &QWebSocket::errorOccurred, ctrl, &WebSocketController::onErrorOccurred); - QObject::connect(ctrl->socket(), &QWebSocket::connected, &window, [connectBtn, disconnectBtn]() { - connectBtn->setEnabled(false); disconnectBtn->setEnabled(true); + QObject::connect(ctrl->socket(), &QWebSocket::connected, &window, [connectBtn, disconnectBtn]() { + connectBtn->setEnabled(false); + disconnectBtn->setEnabled(true); }); QObject::connect(ctrl->socket(), &QWebSocket::disconnected, &window, [connectBtn, disconnectBtn]() { - connectBtn->setEnabled(true); disconnectBtn->setEnabled(false); + connectBtn->setEnabled(true); + disconnectBtn->setEnabled(false); }); window.resize(1050, 620);