AntiClipSettings/Application.cpp

202 lines
7.4 KiB
C++
Raw Normal View History

2024-08-13 20:06:10 +08:00
#include "Application.h"
#include "BoostLog.h"
#include "Configuration.h"
#include "H264Palyer.h"
#include "VideoFrameProvider.h"
2024-08-14 20:01:38 +08:00
#include <QFont>
2024-08-13 20:06:10 +08:00
#include <QGuiApplication>
#include <QQmlApplicationEngine>
Application::Application(int &argc, char **argv)
: m_app(std::make_shared<QGuiApplication>(argc, argv)), m_videoFrameProvider(new VideoFrameProvider()),
2024-08-16 16:24:15 +08:00
m_player(std::make_shared<H264Palyer>()), m_devices(new DeviceListModel(this)) {
2024-08-14 20:01:38 +08:00
QFont font;
font.setPointSize(16);
m_app->setFont(font);
2024-08-13 20:06:10 +08:00
m_app->setApplicationName(APPLICATION_NAME);
m_app->setApplicationVersion(QString("v%1_%2 build: %3 %4").arg(APP_VERSION, GIT_COMMIT_ID, __DATE__, __TIME__));
m_player->open();
}
2024-08-16 16:24:15 +08:00
DeviceConnection::AreaWay Application::currentOpenDoorAreaWay() const {
return m_currentOpenDoorAreaWay;
2024-08-14 20:01:38 +08:00
}
2024-08-16 16:24:15 +08:00
void Application::setCurrentOpenDoorAreaWay(DeviceConnection::AreaWay way) {
if (m_currentOpenDoorAreaWay == way) return;
m_currentOpenDoorAreaWay = way;
if (m_currentOpenDoorAreaWay == DeviceConnection::Quadrangle) {
if ((m_currentOpenDoorAreaPoints.size() < 4) || (m_currentOpenDoorAreaPoints == FullArea)) {
m_currentOpenDoorAreaPoints.clear();
m_currentOpenDoorAreaPoints << QPointF(68, 6) << QPointF(570, 6) << QPointF(570, 354) << QPointF(68, 354);
emit currentOpenDoorAreaPointsChanged();
2024-08-14 20:01:38 +08:00
}
2024-08-16 16:24:15 +08:00
} else if (m_currentOpenDoorAreaWay == DeviceConnection::FullArea) {
m_currentOpenDoorAreaPoints.clear();
m_currentOpenDoorAreaPoints = FullArea;
emit currentOpenDoorAreaPointsChanged();
}
emit currentOpenDoorAreaWayChanged();
if (m_device != nullptr) {
m_device->updateOpenDoorAreaPoints(m_currentOpenDoorAreaWay, m_currentOpenDoorAreaPoints);
2024-08-14 20:01:38 +08:00
}
}
2024-08-13 20:06:10 +08:00
QList<QPointF> Application::currentOpenDoorAreaPoints() const {
return m_currentOpenDoorAreaPoints;
}
void Application::setCurrentOpenDoorAreaPoints(const QList<QPointF> &points) {
if (m_currentOpenDoorAreaPoints != points) {
m_currentOpenDoorAreaPoints = points;
emit currentOpenDoorAreaPointsChanged();
}
}
2024-08-16 16:24:15 +08:00
NetworkInfomation Application::currentNetworkInfomation() const {
return m_currentNetworkInfomation;
}
2024-08-14 20:01:38 +08:00
bool Application::currentShieldedAreaEnabled() const {
return m_currentShieldedAreaEnabled;
}
void Application::setCurrentShieldedAreaEnabled(bool enabled) {
if (m_currentShieldedAreaEnabled != enabled) {
m_currentShieldedAreaEnabled = enabled;
emit currentShieldedAreaEnabledChanged();
2024-08-16 16:24:15 +08:00
if (m_currentShieldedAreaPoints.size() < 4) {
m_currentShieldedAreaPoints.clear();
m_currentShieldedAreaPoints << QPointF(6, 6) << QPointF(40, 60) << QPointF(590, 6) << QPointF(630, 60);
emit currentShieldedAreaPointsChanged();
}
2024-08-14 20:01:38 +08:00
if (m_device != nullptr) {
m_device->updateShieldedAreaPoints(m_currentShieldedAreaEnabled, m_currentShieldedAreaPoints);
}
}
}
QList<QPointF> Application::currentShieldedAreaPoints() const {
return m_currentShieldedAreaPoints;
}
void Application::setCurrentShieldedAreaPoints(const QList<QPointF> &points) {
if (m_currentShieldedAreaPoints != points) {
m_currentShieldedAreaPoints = points;
emit currentShieldedAreaPointsChanged();
}
}
bool Application::currentAntiClipAreaEnabled() const {
return m_currentAntiClipAreaEnabled;
}
void Application::setCurrentAntiClipAreaEnabled(bool enabled) {
if (m_currentAntiClipAreaEnabled != enabled) {
m_currentAntiClipAreaEnabled = enabled;
emit currentAntiClipAreaEnabledChanged();
if (m_device != nullptr) {
m_device->updateAntiClipAreaPoints(m_currentAntiClipAreaEnabled, m_currentAntiClipAreaPoints);
}
}
}
QList<QPointF> Application::currentAntiClipAreaPoints() const {
return m_currentAntiClipAreaPoints;
}
void Application::setCurrentAntiClipAreaPoints(const QList<QPointF> &points) {
if (m_currentAntiClipAreaPoints != points) {
m_currentAntiClipAreaPoints = points;
emit currentAntiClipAreaPointsChanged();
}
}
void Application::updateOpenDoorAreaPoints(const QList<QPointF> &points) {
2024-08-16 16:24:15 +08:00
if (m_device != nullptr) {
m_device->updateOpenDoorAreaPoints(m_currentOpenDoorAreaWay, points);
}
2024-08-14 20:01:38 +08:00
}
void Application::updateAntiClipAreaPoints(const QList<QPointF> &points) {
m_device->updateAntiClipAreaPoints(m_currentAntiClipAreaEnabled, points);
}
void Application::updateShieldedAreaPoints(const QList<QPointF> &points) {
m_device->updateShieldedAreaPoints(m_currentShieldedAreaEnabled, points);
}
2024-08-16 16:24:15 +08:00
void Application::updateNetworkInfomation(bool dhcp, const QString &ip, const QString &netmask,
const QString &gateway) {
LOG(info) << dhcp;
LOG(info) << ip.toStdString();
if (m_device != nullptr) {
m_device->updateNetworkInfomation(dhcp, ip, netmask, gateway);
}
}
void Application::connectToDevice(const QString &address) {
if (m_device != nullptr) {
m_device->deleteLater();
}
m_device = new DeviceConnection();
connect(m_device, &DeviceConnection::currentOpenDoorAreaChanged, this, &Application::onDeviceOpenDoorArea);
connect(m_device, &DeviceConnection::currentShieldedAreaChanged, this, &Application::onDeviceShieldedArea);
connect(m_device, &DeviceConnection::currentAntiClipAreaChanged, this, &Application::onDeviceAntiClipArea);
connect(m_device, &DeviceConnection::currentNetworkInfomationChanged, this,
&Application::onDeviceNetworkInfomation);
m_device->connect(address);
m_device->setH264FrameCallback([this](const char *data, uint32_t size) {
auto image = m_player->decode((const uint8_t *)data, size);
if (image) {
m_videoFrameProvider->setImage(*image);
emit newVideoFrame();
}
});
}
void Application::onDeviceOpenDoorArea(DeviceConnection::AreaWay way, const QList<QPointF> &points) {
setCurrentOpenDoorAreaWay(way);
2024-08-13 20:06:10 +08:00
setCurrentOpenDoorAreaPoints(points);
}
2024-08-14 20:01:38 +08:00
void Application::onDeviceShieldedArea(bool enabled, const QList<QPointF> &points) {
LOG(info) << "onDeviceShieldedArea: " << points.size();
setCurrentShieldedAreaEnabled(enabled);
setCurrentShieldedAreaPoints(points);
}
void Application::onDeviceAntiClipArea(bool enabled, const QList<QPointF> &points) {
setCurrentAntiClipAreaEnabled(enabled);
setCurrentAntiClipAreaPoints(points);
}
2024-08-16 16:24:15 +08:00
void Application::onDeviceNetworkInfomation(const NetworkInfomation &info) {
m_currentNetworkInfomation = info;
emit currentNetworkInfomationChanged();
}
2024-08-13 20:06:10 +08:00
int Application::exec() {
QQmlApplicationEngine engine;
engine.addImageProvider("videoframe", m_videoFrameProvider);
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreationFailed, this, []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("AntiClipSettings", "Main");
return m_app->exec();
}
Application *Application::create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) {
Application *ret = nullptr;
auto app = Amass::Singleton<Application>::instance();
if (app) {
ret = app.get();
QJSEngine::setObjectOwnership(ret, QJSEngine::CppOwnership);
}
return ret;
}