Implements more methods on the widget

This commit is contained in:
Arnaud 2026-01-22 18:52:27 +04:00
parent 0615899689
commit 4d5e501f9a
No known key found for this signature in database
GPG Key ID: 20E40A5D3110766F
4 changed files with 205 additions and 178 deletions

View File

@ -1,23 +1,18 @@
#include "StorageWidget.h"
#include <QDebug>
#include <QDateTime>
#include <QMessageBox>
#include <iostream>
#include <csignal>
#include <QTimer>
#include "logos_api_client.h"
#include <QDateTime>
#include <QDebug>
#include <QMessageBox>
// #include <QQmlContext>
#include <QTimer>
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(); }

View File

@ -1,55 +1,68 @@
#pragma once
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QLineEdit>
#include <QLabel>
#include <string>
#include "logos_api.h"
#include "logos_api_client.h"
#include "logos_sdk.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
// #include <QQuickWidget>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QWidget>
#include <string>
using StringCallback = std::function<void(int, const std::string&)>;
using StringCallback = std::function<void(int, const std::string&)>;
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();
};

View File

@ -1,76 +1,75 @@
#include "StorageWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QDebug>
#include <QMessageBox>
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();
// }

View File

@ -1,27 +1,25 @@
#pragma once
#include "StorageWidget.h"
#include <QMainWindow>
#include <QVBoxLayout>
#include <QMenuBar>
#include <QStatusBar>
#include "StorageWidget.h"
#include <QVBoxLayout>
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;
};
};