mirror of
https://github.com/ysoftdevs/Testa.git
synced 2026-01-11 22:30:26 +01:00
Added action receiver, refactored controller, extracted parsing logic from controller
This commit is contained in:
62
actionreceiver.cpp
Normal file
62
actionreceiver.cpp
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#include "actionreceiver.h"
|
||||||
|
|
||||||
|
ActionReceiver::ActionReceiver(const QUrl& address, QObject *parent)
|
||||||
|
: QObject(parent),
|
||||||
|
address(address),
|
||||||
|
webSocket(new QWebSocket())
|
||||||
|
{
|
||||||
|
connect(webSocket, &QWebSocket::connected, this, &ActionReceiver::socketConnected);
|
||||||
|
connect(webSocket, &QWebSocket::textMessageReceived, this, &ActionReceiver::socketReceivedStringMessage);
|
||||||
|
connect(webSocket, static_cast<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), this, &ActionReceiver::socketError);
|
||||||
|
|
||||||
|
webSocket->open(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
ActionReceiver::~ActionReceiver()
|
||||||
|
{
|
||||||
|
webSocket->close();
|
||||||
|
delete webSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActionReceiver::socketConnected()
|
||||||
|
{
|
||||||
|
qInfo() << "Socket connected to server";
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActionReceiver::socketReceivedStringMessage(const QString &message)
|
||||||
|
{
|
||||||
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8());
|
||||||
|
QJsonObject jsonObject = jsonDocument.object();
|
||||||
|
|
||||||
|
QString messageType = getActionType(jsonObject);
|
||||||
|
QJsonObject parameters = getParameters(jsonObject);
|
||||||
|
|
||||||
|
if (messageType == "loadUrl") {
|
||||||
|
emit loadUrlReceived(parameters["url"].toString());
|
||||||
|
} else if (messageType == "showNotification") {
|
||||||
|
const QString title = parameters["title"].toString();
|
||||||
|
const QString message = parameters["message"].toString();
|
||||||
|
emit showNotificationReceived(title, message);
|
||||||
|
} else if (messageType == "setProgress") {
|
||||||
|
float value = parameters["value"].toDouble();
|
||||||
|
emit setProgressReceived(value);
|
||||||
|
} else {
|
||||||
|
qWarning() << "Unsupported action type [" << messageType << "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActionReceiver::socketError(QAbstractSocket::SocketError socketError)
|
||||||
|
{
|
||||||
|
Q_UNUSED(socketError);
|
||||||
|
qWarning() << "There was an error:" << webSocket->errorString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ActionReceiver::getActionType(const QJsonObject &jsonObject)
|
||||||
|
{
|
||||||
|
return jsonObject["action"].toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject ActionReceiver::getParameters(const QJsonObject &jsonObject)
|
||||||
|
{
|
||||||
|
return jsonObject["parameters"].toObject();
|
||||||
|
}
|
||||||
29
actionreceiver.h
Normal file
29
actionreceiver.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef ACTIONRECEIVER_H
|
||||||
|
#define ACTIONRECEIVER_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QtWebSockets>
|
||||||
|
|
||||||
|
class ActionReceiver : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit ActionReceiver(const QUrl &address, QObject *parent = 0);
|
||||||
|
~ActionReceiver();
|
||||||
|
private slots:
|
||||||
|
void socketConnected();
|
||||||
|
void socketReceivedStringMessage(const QString &message);
|
||||||
|
void socketError(QAbstractSocket::SocketError socketError);
|
||||||
|
signals:
|
||||||
|
void loadUrlReceived(QString);
|
||||||
|
void setProgressReceived(float);
|
||||||
|
void showNotificationReceived(QString, QString);
|
||||||
|
private:
|
||||||
|
QUrl address;
|
||||||
|
QWebSocket* webSocket;
|
||||||
|
|
||||||
|
QString getActionType(const QJsonObject &jsonObject);
|
||||||
|
QJsonObject getParameters(const QJsonObject &jsonObject);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ACTIONRECEIVER_H
|
||||||
@@ -1,60 +1,14 @@
|
|||||||
#include <QJsonDocument>
|
|
||||||
#include "applicationcontroller.h"
|
#include "applicationcontroller.h"
|
||||||
#include "notification.h"
|
#include "notification.h"
|
||||||
#include "progressbar.h"
|
#include "progressbar.h"
|
||||||
|
|
||||||
ApplicationController::ApplicationController(WebEngineView* webEngineView, QObject *parent) :
|
ApplicationController::ApplicationController(WebEngineView* webEngineView, ActionReceiver *actionReceiver, QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
webSocket(new QWebSocket()),
|
webEngineView(webEngineView),
|
||||||
webEngineView(webEngineView)
|
actionReceiver(actionReceiver)
|
||||||
{
|
{
|
||||||
connect(webSocket, &QWebSocket::connected, this, &ApplicationController::connected);
|
connect(actionReceiver, &ActionReceiver::showNotificationReceived, Notification::show);
|
||||||
connect(webSocket, &QWebSocket::textMessageReceived, this, &ApplicationController::messageReceived);
|
connect(actionReceiver, &ActionReceiver::setProgressReceived, ProgressBar::show);
|
||||||
connect(webSocket, static_cast<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), this, &ApplicationController::error);
|
connect(actionReceiver, &ActionReceiver::loadUrlReceived, webEngineView, &WebEngineView::loadUrl);
|
||||||
|
|
||||||
webSocket->open(QUrl("ws://localhost:12345")); // TODO: extract to configuration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,27 +2,19 @@
|
|||||||
#define APPLICATIONCONTROLLER_H
|
#define APPLICATIONCONTROLLER_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QJsonObject>
|
|
||||||
#include <QAbstractSocket>
|
|
||||||
#include <QtWebSockets/QWebSocket>
|
|
||||||
#include "webengineview.h"
|
#include "webengineview.h"
|
||||||
|
#include "actionreceiver.h"
|
||||||
|
|
||||||
class ApplicationController : public QObject
|
class ApplicationController : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit ApplicationController(WebEngineView *webEngineView, QObject *parent = 0);
|
explicit ApplicationController(WebEngineView *webEngineView, ActionReceiver *actionReceiver, QObject *parent = 0);
|
||||||
void test();
|
void test();
|
||||||
private slots:
|
private:
|
||||||
void connected();
|
|
||||||
void messageReceived(const QString &message);
|
|
||||||
void error(QAbstractSocket::SocketError error);
|
|
||||||
private:
|
|
||||||
QWebSocket* webSocket;
|
|
||||||
WebEngineView* webEngineView;
|
WebEngineView* webEngineView;
|
||||||
|
ActionReceiver* actionReceiver;
|
||||||
QString getMessageType(const QJsonObject &jsonObject);
|
|
||||||
QJsonObject getParameters(const QJsonObject &jsonObject);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // APPLICATIONCONTROLLER_H
|
#endif // APPLICATIONCONTROLLER_H
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -10,8 +10,8 @@ int main(int argc, char *argv[])
|
|||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
|
|
||||||
WebEngineView view;
|
WebEngineView view;
|
||||||
|
ActionReceiver actionReceiver(QUrl("ws://localhost:12345"));
|
||||||
ApplicationController applicationController(&view);
|
ApplicationController applicationController(&view, &actionReceiver);
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
|
|
||||||
static QSystemTrayIcon* sharedSystemTrayIcon;
|
static QSystemTrayIcon* sharedSystemTrayIcon;
|
||||||
|
|
||||||
void Notification::show(const std::string& title, const std::string& message)
|
void Notification::show(const QString &title, const QString &message)
|
||||||
{
|
{
|
||||||
if (sharedSystemTrayIcon == nullptr)
|
if (sharedSystemTrayIcon == nullptr)
|
||||||
sharedSystemTrayIcon = new QSystemTrayIcon();
|
sharedSystemTrayIcon = new QSystemTrayIcon();
|
||||||
sharedSystemTrayIcon->show();
|
sharedSystemTrayIcon->show();
|
||||||
sharedSystemTrayIcon->showMessage(QString::fromStdString(title), QString::fromStdString(message));
|
sharedSystemTrayIcon->showMessage(title, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Notification::hide()
|
void Notification::hide()
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
#ifndef NOTIFICATION_H
|
#ifndef NOTIFICATION_H
|
||||||
#define NOTIFICATION_H
|
#define NOTIFICATION_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Notification
|
class Notification : public QObject
|
||||||
{
|
{
|
||||||
public:
|
Q_OBJECT
|
||||||
static void show(const std::string& title, const std::string& message);
|
public slots:
|
||||||
|
static void show(const QString &title, const QString &message);
|
||||||
static void hide();
|
static void hide();
|
||||||
private:
|
private:
|
||||||
Notification() { }
|
Notification() { }
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ SOURCES += main.cpp \
|
|||||||
mainwindow.cpp \
|
mainwindow.cpp \
|
||||||
webengineview.cpp \
|
webengineview.cpp \
|
||||||
notification.cpp \
|
notification.cpp \
|
||||||
applicationcontroller.cpp
|
applicationcontroller.cpp \
|
||||||
|
actionreceiver.cpp
|
||||||
|
|
||||||
macx {
|
macx {
|
||||||
OBJECTIVE_SOURCES += nativeprogressbar_mac.mm \
|
OBJECTIVE_SOURCES += nativeprogressbar_mac.mm \
|
||||||
@@ -40,4 +41,5 @@ HEADERS += mainwindow.h \
|
|||||||
webengineview.h \
|
webengineview.h \
|
||||||
progressbar.h \
|
progressbar.h \
|
||||||
notification.h \
|
notification.h \
|
||||||
applicationcontroller.h
|
applicationcontroller.h \
|
||||||
|
actionreceiver.h
|
||||||
|
|||||||
Reference in New Issue
Block a user