mirror of
https://github.com/ysoftdevs/Testa.git
synced 2026-01-12 06:40:25 +01:00
Added action receiver, refactored controller, extracted parsing logic from controller
This commit is contained in:
@@ -1,60 +1,14 @@
|
||||
#include <QJsonDocument>
|
||||
#include "applicationcontroller.h"
|
||||
#include "notification.h"
|
||||
#include "progressbar.h"
|
||||
|
||||
ApplicationController::ApplicationController(WebEngineView* webEngineView, QObject *parent) :
|
||||
QObject(parent),
|
||||
webSocket(new QWebSocket()),
|
||||
webEngineView(webEngineView)
|
||||
ApplicationController::ApplicationController(WebEngineView* webEngineView, ActionReceiver *actionReceiver, QObject *parent) :
|
||||
QObject(parent),
|
||||
webEngineView(webEngineView),
|
||||
actionReceiver(actionReceiver)
|
||||
{
|
||||
connect(webSocket, &QWebSocket::connected, this, &ApplicationController::connected);
|
||||
connect(webSocket, &QWebSocket::textMessageReceived, this, &ApplicationController::messageReceived);
|
||||
connect(webSocket, static_cast<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), this, &ApplicationController::error);
|
||||
|
||||
webSocket->open(QUrl("ws://localhost:12345")); // TODO: extract to configuration
|
||||
connect(actionReceiver, &ActionReceiver::showNotificationReceived, Notification::show);
|
||||
connect(actionReceiver, &ActionReceiver::setProgressReceived, ProgressBar::show);
|
||||
connect(actionReceiver, &ActionReceiver::loadUrlReceived, webEngineView, &WebEngineView::loadUrl);
|
||||
}
|
||||
|
||||
void ApplicationController::connected()
|
||||
{
|
||||
qDebug() << "Connected";
|
||||
}
|
||||
|
||||
void ApplicationController::messageReceived(const QString &message)
|
||||
{
|
||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8());
|
||||
QJsonObject jsonObject = jsonDocument.object();
|
||||
|
||||
QString messageType = getMessageType(jsonObject);
|
||||
QJsonObject parameters = getParameters(jsonObject);
|
||||
|
||||
if (messageType == "loadUrl") {
|
||||
QUrl url(parameters["url"].toString());
|
||||
|
||||
webEngineView->loadUrl(url);
|
||||
} else if (messageType == "showNotification") {
|
||||
const QString title = parameters["title"].toString();
|
||||
const QString message = parameters["message"].toString();
|
||||
|
||||
Notification::show(title.toStdString(), message.toStdString());
|
||||
} else if (messageType == "setProgress") {
|
||||
float value = parameters["value"].toDouble();
|
||||
|
||||
ProgressBar::show(value);
|
||||
}
|
||||
}
|
||||
void ApplicationController::error(QAbstractSocket::SocketError error)
|
||||
{
|
||||
Q_UNUSED(error);
|
||||
qDebug() << webSocket->errorString();
|
||||
}
|
||||
|
||||
QString ApplicationController::getMessageType(const QJsonObject &jsonObject)
|
||||
{
|
||||
return jsonObject["action"].toString();
|
||||
}
|
||||
|
||||
QJsonObject ApplicationController::getParameters(const QJsonObject &jsonObject)
|
||||
{
|
||||
return jsonObject["parameters"].toObject();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user