From 4d5e501f9a48678fc655d9610700eef886d28d8e Mon Sep 17 00:00:00 2001 From: Arnaud Date: Thu, 22 Jan 2026 18:52:27 +0400 Subject: [PATCH] Implements more methods on the widget --- src/StorageWidget.cpp | 204 ++++++++++++++++++++++-------------------- src/StorageWidget.h | 78 +++++++++------- src/StorageWindow.cpp | 85 +++++++++--------- src/StorageWindow.h | 16 ++-- 4 files changed, 205 insertions(+), 178 deletions(-) diff --git a/src/StorageWidget.cpp b/src/StorageWidget.cpp index ce5f82f..aae757f 100644 --- a/src/StorageWidget.cpp +++ b/src/StorageWidget.cpp @@ -1,23 +1,18 @@ #include "StorageWidget.h" -#include -#include -#include -#include -#include -#include #include "logos_api_client.h" +#include +#include +#include +// #include +#include int RET_OK = 0; // Static pointer to the active StorageWidget for callbacks -static StorageWidget *activeWidget = nullptr; +static StorageWidget* activeWidget = nullptr; -StorageWidget::StorageWidget(QWidget *parent) - : QWidget(parent), - isStorageInitialized(false), - m_isStorageRunning(false), - m_logosAPI(nullptr) -{ +StorageWidget::StorageWidget(QWidget* parent) + : QWidget(parent), isStorageInitialized(false), m_isStorageRunning(false), m_logosAPI(nullptr) { // Set as the active widget activeWidget = this; @@ -25,7 +20,13 @@ StorageWidget::StorageWidget(QWidget *parent) m_logosAPI = new LogosAPI("core", this); logos = new LogosModules(m_logosAPI); - // Main vertical layout + // mainLayout = new QVBoxLayout(this); + // quickWidget = new QQuickWidget(this); + // quickWidget->rootContext()->setContextProperty("storage", this); + // quickWidget->setSource(QUrl("qrc:StorageWidget.qml")); + // mainLayout->addWidget(quickWidget); + + // // Main vertical layout mainLayout = new QVBoxLayout(this); // Create status label @@ -35,12 +36,13 @@ StorageWidget::StorageWidget(QWidget *parent) statusLabel->setLineWidth(1); statusLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); statusLabel->setMinimumHeight(30); - - startButton = new QPushButton("Start", this); - statusLayout->addWidget(statusLabel, 4); + + startButton = new QPushButton("Start", this); statusLayout->addWidget(startButton, 1); + debugButton = new QPushButton("Debug", this); + statusLayout->addWidget(debugButton, 1); // Create display // storageDisplay = new QTextEdit(this); // storageDisplay->setReadOnly(true); @@ -66,75 +68,75 @@ StorageWidget::StorageWidget(QWidget *parent) // Connect signals to slots connect(startButton, &QPushButton::clicked, this, &StorageWidget::onStartButtonClicked); + connect(debugButton, &QPushButton::clicked, this, &StorageWidget::onDebugButtonClicked); + // connect(messageInput, &QLineEdit::returnPressed, this, &StorageWidget::onStartButtonClicked); // Disable UI components until Storage is initialized // messageInput->setEnabled(false); // sendButton->setEnabled(false); startButton->setEnabled(false); + debugButton->setEnabled(false); // Auto-initialize Storage initStorage(); } -StorageWidget::~StorageWidget() -{ +StorageWidget::~StorageWidget() { // Reset the active widget if it's this instance - if (activeWidget == this) - { + if (activeWidget == this) { activeWidget = nullptr; } } -void StorageWidget::initStorage() -{ +void StorageWidget::initStorage() { updateStatus("Initializing Storage..."); - + emit storageRunningChanged(); response = logos->storage_module.init("{}"); - + qDebug() << "StorageWidget: Storage module init response:" << response; isStorageInitialized = true; m_isStorageRunning = false; - + updateStatus("Storage initialized."); if (!logos->storage_module.on("storageStart", [this](const QVariantList& data) { - int code = data[0].toInt(); + int code = data[0].toInt(); - if (code != RET_OK) { - updateStatus("Error starting Storage."); - } else { - updateStatus("Storage started successfully."); - m_isStorageRunning = true; - startButton->setText("Stop"); + if (code != RET_OK) { + updateStatus("Error starting Storage."); + } else { + updateStatus("Storage started successfully."); + m_isStorageRunning = true; + startButton->setText("Stop"); + // messageInput->setEnabled(true); + // sendButton->setEnabled(true); + } - // messageInput->setEnabled(true); - // sendButton->setEnabled(true); - } - - startButton->setEnabled(true); - })) { + startButton->setEnabled(true); + debugButton->setEnabled(true); + })) { qWarning() << "StorageWidget: failed to subscribe to storageStart events"; } if (!logos->storage_module.on("storageStop", [this](const QVariantList& data) { - int code = data[0].toInt(); + int code = data[0].toInt(); - if (code != RET_OK) { - updateStatus("Error stopping Storage."); - } else { - updateStatus("Storage stopped successfully."); - m_isStorageRunning = false; - startButton->setText("Start"); - // messageInput->setEnabled(false); - // sendButton->setEnabled(false); - } + if (code != RET_OK) { + updateStatus("Error stopping Storage."); + } else { + updateStatus("Storage stopped successfully."); + m_isStorageRunning = false; + startButton->setText("Start"); + // messageInput->setEnabled(false); + // sendButton->setEnabled(false); + } - startButton->setEnabled(true); + startButton->setEnabled(true); - emit storageStop(); - })) { + emit storageStop(); + })) { qWarning() << "StorageWidget: failed to subscribe to storageStop events"; } @@ -144,35 +146,45 @@ void StorageWidget::initStorage() // qDebug() << "Storage Version:" << QString::fromStdString(version); // })) { // qWarning() << "ChatWidget: failed to subscribe to historyMessage events"; - // } + // } // if (!storageModule.on("storageVersion", [this] // std::string version = data[1].toString().toStdString();(const QVariantList& data) { // qDebug() << "Storage Version:" << QString::fromStdString(version); // })) { // qWarning() << "ChatWidget: failed to subscribe to historyMessage events"; - // } + // } } -bool StorageWidget::isStorageRunning() const -{ - return m_isStorageRunning; +QString StorageWidget::storageVersion() const { + if (!isStorageInitialized) { + qDebug() << "StorageWidget: Storage not initialized, cannot get version."; + return ""; + } + + return logos->storage_module.version(); } -void StorageWidget::startStorage() -{ +QString StorageWidget::storageDebug() const { + if (!isStorageInitialized) { + qDebug() << "StorageWidget: Storage not initialized, cannot get debug info."; + return ""; + } + + return logos->storage_module.debug(); +} + +void StorageWidget::startStorage() { updateStatus("Starting Storage..."); - if (!isStorageInitialized) - { + if (!isStorageInitialized) { qDebug() << "StorageWidget: Storage not initialized, nothing to start."; return; } - if (m_isStorageRunning) - { + if (m_isStorageRunning) { qDebug() << "StorageWidget: Storage already started."; return; - } + } if (!logos->storage_module.start()) { qWarning() << "StorageWidget: Failed to send start command to Storage."; @@ -181,23 +193,20 @@ void StorageWidget::startStorage() } } -void StorageWidget::stopStorage() -{ - updateStatus("Stopping Storage..."); - - if (!isStorageInitialized) - { +void StorageWidget::stopStorage() { + if (!isStorageInitialized) { qDebug() << "StorageWidget: Storage not initialized, nothing to stop."; emit storageStop(); return; } - if (!m_isStorageRunning) - { + if (!m_isStorageRunning) { qDebug() << "StorageWidget: Storage already stopped."; emit storageStop(); return; - } + } + + updateStatus("Stopping Storage..."); if (!logos->storage_module.stop()) { qWarning() << "StorageWidget: Failed to send stop command to Storage."; @@ -206,12 +215,10 @@ void StorageWidget::stopStorage() } } -void StorageWidget::destroy() -{ +void StorageWidget::destroy() { qDebug() << "StorageWidget: destroy function called..."; - if (!isStorageInitialized) - { + if (!isStorageInitialized) { qDebug() << "StorageWidget: Storage not initialized, nothing to stop."; return; } @@ -227,39 +234,43 @@ void StorageWidget::destroy() emit storageCleanup(); } -void StorageWidget::onStartButtonClicked() -{ +void StorageWidget::onStartButtonClicked() { qDebug() << "Starting Storage from button"; startButton->setEnabled(false); - if (m_isStorageRunning) - { + if (m_isStorageRunning) { stopStorage(); - } - else - { + } else { startStorage(); } } -void StorageWidget::onSendButtonClicked() -{ +void StorageWidget::onDebugButtonClicked() { + qDebug() << "version" << logos->storage_module.version(); + qDebug() << "spr" << logos->storage_module.spr(); + qDebug() << "peerId" << logos->storage_module.peerId(); + + logos->storage_module.updateLogLevel("TRACE"); + + qDebug() << "Debug button clicked"; + emit debug(); +} + +void StorageWidget::onSendButtonClicked() { QString message = messageInput->text().trimmed(); - if (message.isEmpty()) - { + if (message.isEmpty()) { QMessageBox::warning(this, "Storage Error", "Message is empty."); return; } // Check if Storage is running - if (!m_isStorageRunning) - { + if (!m_isStorageRunning) { QMessageBox::warning(this, "Storage Error", "Storage is not running. Please initialize Storage first."); return; } - logos->storage_module.version(); + // if (m_logosAPI && m_logosAPI->getClient("storage")->isConnected()) // { @@ -276,15 +287,18 @@ void StorageWidget::onSendButtonClicked() messageInput->clear(); } -void StorageWidget::updateStatus(const QString &message) -{ +void StorageWidget::updateStatus(const QString& message) { statusLabel->setText(message); qDebug() << "StorageWidget Status:" << message; } -void StorageWidget::displayMessage(const QString &sender, const QString &message) -{ +void StorageWidget::displayMessage(const QString& sender, const QString& message) { QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); QString formattedMessage = QString("[%1] %2: %3").arg(timestamp, sender, message); storageDisplay->append(formattedMessage); } + +QString StorageWidget::statusText() const { return m_statusText; } +bool StorageWidget::storageRunning() const { return m_isStorageRunning; } + +void StorageWidget::requestDebug() { emit debug(); } diff --git a/src/StorageWidget.h b/src/StorageWidget.h index 24a0ef0..0beb8f2 100644 --- a/src/StorageWidget.h +++ b/src/StorageWidget.h @@ -1,55 +1,68 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include -#include #include "logos_api.h" #include "logos_api_client.h" #include "logos_sdk.h" +#include +#include +#include +#include +// #include +#include +#include +#include +#include -using StringCallback = std::function; +using StringCallback = std::function; -class StorageWidget : public QWidget -{ +class StorageWidget : public QWidget { Q_OBJECT + Q_PROPERTY(QString statusText READ statusText NOTIFY statusTextChanged) + Q_PROPERTY(bool storageRunning READ storageRunning NOTIFY storageRunningChanged) -public: - explicit StorageWidget(QWidget *parent = nullptr); + public: + explicit StorageWidget(QWidget* parent = nullptr); ~StorageWidget(); + Q_INVOKABLE void requestDebug(); + + QString statusText() const; + bool storageRunning() const; + // Storage operations Q_INVOKABLE void initStorage(); Q_INVOKABLE void startStorage(); Q_INVOKABLE void stopStorage(); + Q_INVOKABLE QString storageVersion() const; + Q_INVOKABLE QString storageDebug() const; Q_INVOKABLE void destroy(); - Q_INVOKABLE bool isStorageRunning() const; -private slots: + private slots: void onSendButtonClicked(); void onStartButtonClicked(); + void onDebugButtonClicked(); -private: + private: // UI elements - QVBoxLayout *mainLayout; - QHBoxLayout *inputLayout; - QHBoxLayout *statusLayout; - QHBoxLayout *channelLayout; + // QQuickWidget* quickWidget; + QString m_statusText; - QTextEdit *storageDisplay; - QLineEdit *messageInput; - QLineEdit *channelInput; - QPushButton *sendButton; - QPushButton *startButton; - QLabel *statusLabel; + QVBoxLayout* mainLayout; + QHBoxLayout* inputLayout; + QHBoxLayout* statusLayout; + QHBoxLayout* channelLayout; + + QTextEdit* storageDisplay; + QLineEdit* messageInput; + QLineEdit* channelInput; + QPushButton* sendButton; + QPushButton* startButton; + QPushButton* debugButton; + QLabel* statusLabel; // LogosAPI instance for remote method calls - LogosAPI *m_logosAPI; - LogosModules *logos; + LogosAPI* m_logosAPI; + LogosModules* logos; // Connection status bool isStorageInitialized; @@ -59,11 +72,14 @@ private: bool response; // Helper methods - void updateStatus(const QString &message); - void displayMessage(const QString &sender, const QString &message); + void updateStatus(const QString& message); + void displayMessage(const QString& sender, const QString& message); void emitEvent(const QString& eventName, const QVariantList& data); -signals: + signals: void storageCleanup(); void storageStop(); + void debug(); + void statusTextChanged(); + void storageRunningChanged(); }; diff --git a/src/StorageWindow.cpp b/src/StorageWindow.cpp index 7c8b576..df94486 100644 --- a/src/StorageWindow.cpp +++ b/src/StorageWindow.cpp @@ -1,76 +1,75 @@ #include "StorageWindow.h" -#include -#include #include -#include #include +#include -StorageWindow::StorageWindow(QWidget *parent) : QMainWindow(parent) -{ +StorageWindow::StorageWindow(QWidget* parent) : QMainWindow(parent) { // Create central widget storageWidget = new StorageWidget(this); setCentralWidget(storageWidget); // Setup the UI - setupMenu(); + // setupMenu(); setupStatusBar(); // Set window properties setMinimumSize(640, 480); + + // connect(stor, &StorageWindow::onDebugButtonClicked, this, [this] { + // debugDock->show(); + // debugDock->raise(); + // debugDock->setFocus(); + // }); } -StorageWindow::~StorageWindow() -{ +StorageWindow::~StorageWindow() { // StorageWidget will be deleted automatically as it's a child of this window } -void StorageWindow::setupMenu() -{ - // Create main menu - QMenu *fileMenu = menuBar()->addMenu("&File"); - QMenu *storageMenu = menuBar()->addMenu("&Storage"); - QMenu *helpMenu = menuBar()->addMenu("&Help"); - - // File menu actions - QAction *exitAction = fileMenu->addAction("E&xit"); - connect(exitAction, &QAction::triggered, this, &QMainWindow::close); - - // Storage menu actions - QAction *initStorageAction = storageMenu->addAction("&Initialize Storage"); - connect(initStorageAction, &QAction::triggered, this, &StorageWindow::onInitStorage); - - QAction *stopStorageAction = storageMenu->addAction("&Stop Storage"); - connect(stopStorageAction, &QAction::triggered, this, &StorageWindow::onStopStorage); - - // Help menu actions - QAction *aboutAction = helpMenu->addAction("&About"); - connect(aboutAction, &QAction::triggered, this, &StorageWindow::onAboutAction); -} - -void StorageWindow::setupStatusBar() -{ +void StorageWindow::setupStatusBar() { statusBar = new QStatusBar(this); setStatusBar(statusBar); statusBar->showMessage("Ready"); } -void StorageWindow::onAboutAction() -{ +void StorageWindow::onAboutAction() { QMessageBox::about(this, "About Logos Storage", "Logos Storage Application\n\n" "A sample Qt application demonstrating Storage integration."); } -void StorageWindow::onInitStorage() -{ +void StorageWindow::onInitStorage() { qDebug() << "Initializing Storage from menu"; storageWidget->initStorage(); statusBar->showMessage("Storage initialization requested"); } -void StorageWindow::onStopStorage() -{ - qDebug() << "Stopping Storage from menu"; - storageWidget->stopStorage(); - statusBar->showMessage("Storage stop requested"); -} +// void StorageWindow::onStopStorage() { +// qDebug() << "Stopping Storage from menu"; +// // storageWidget->stopStorage(); +// statusBar->showMessage("Storage stop requested"); +// } + +// void StorageWindow::showAbout() { +// QString version; + +// qDebug() << "StorageWindow: Retrieving Storage version for About Dialog..."; + +// QMetaObject::invokeMethod(storageWidget, "storageVersion", Qt::DirectConnection, Q_RETURN_ARG(QString, version)); + +// qDebug() << "StorageWindow: Showing About Dialog, version:" << version; +// AboutDialog dlg(this, version); +// dlg.exec(); +// } + +// void MainWindow::showDebug() { +// QString debug; + +// qDebug() << "MainWindow: Retrieving Storage debug for Debug Dialog..."; + +// QMetaObject::invokeMethod(storageWidget, "storageDebug", Qt::DirectConnection, Q_RETURN_ARG(QString, debug)); + +// qDebug() << "MainWindow: Showing Debug Dialog, debug:" << debug; +// DebugDialog dlg(this, debug); +// dlg.exec(); +// } diff --git a/src/StorageWindow.h b/src/StorageWindow.h index 605efe1..9c68b3a 100644 --- a/src/StorageWindow.h +++ b/src/StorageWindow.h @@ -1,27 +1,25 @@ #pragma once +#include "StorageWidget.h" #include -#include #include #include -#include "StorageWidget.h" +#include class StorageWindow : public QMainWindow { Q_OBJECT -public: + public: explicit StorageWindow(QWidget* parent = nullptr); ~StorageWindow(); -private slots: + private slots: void onAboutAction(); void onInitStorage(); - void onStopStorage(); -private: - void setupMenu(); + private: void setupStatusBar(); - + StorageWidget* storageWidget; QStatusBar* statusBar; -}; \ No newline at end of file +};