Added lazy loading

This commit is contained in:
Jon ESA
2026-04-01 19:40:20 +01:00
parent e10e7127b7
commit f6c6d4e9dc
3 changed files with 111 additions and 42 deletions

View File

@@ -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)
@@ -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 <key> loop fires in handleProtocol when List reply arrives
}
// ----------------------------------------------------------------
// Connection
// ----------------------------------------------------------------
void WebSocketController::startConnection()
{
const QUrl url(m_urlEdit->text().trimmed());
@@ -47,6 +83,9 @@ void WebSocketController::closeConnection()
broadcast("Closing connection");
m_settingsKeys.clear();
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_gamesRequested = false;
m_versionsRequested = false;
m_settingsRequested = false;
}
void WebSocketController::onDisconnected()
@@ -78,6 +114,9 @@ void WebSocketController::onDisconnected()
m_statusLabel->setText("Disconnected");
m_settingsKeys.clear();
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 <count>
// RNP <count> — triggers the slave loop
if (cmd == "RNP" && tokens.size() >= 2) {
bool ok = false;
const int count = tokens[1].toInt(&ok);

View File

@@ -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();
@@ -49,4 +54,8 @@ private:
VersionsPanel *m_versionsPanel = nullptr;
QStringList m_settingsKeys;
int m_rnpCount = -1;
bool m_gamesRequested = false;
bool m_versionsRequested = false;
bool m_settingsRequested = false;
};

View File

@@ -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,7 +107,6 @@ 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);
urlEdit->setPlaceholderText("ws://127.0.0.1:3491/");
@@ -116,20 +123,28 @@ 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);
@@ -139,10 +154,12 @@ int main(int argc, char *argv[])
QObject::connect(ctrl->socket(), &QWebSocket::errorOccurred, ctrl, &WebSocketController::onErrorOccurred);
QObject::connect(ctrl->socket(), &QWebSocket::connected, &window, [connectBtn, disconnectBtn]() {
connectBtn->setEnabled(false); disconnectBtn->setEnabled(true);
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);