1
0
Fork 0
flightgear/utils/fgqcanvas/canvasconnection.h

144 lines
3.6 KiB
C
Raw Normal View History

//
// Copyright (C) 2017 James Turner zakalawe@mac.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef CANVASCONNECTION_H
#define CANVASCONNECTION_H
#include <memory>
#include <QObject>
#include <QtWebSockets/QWebSocket>
#include <QJsonObject>
#include <QUrl>
#include <QRectF>
2017-11-28 22:32:30 +00:00
#include <QPointer>
class LocalProp;
class QNetworkAccessManager;
class FGQCanvasImageLoader;
class FGQCanvasFontCache;
2017-11-05 19:42:57 +00:00
class QDataStream;
class CanvasConnection : public QObject
{
Q_OBJECT
Q_ENUMS(Status)
Q_PROPERTY(Status status READ status NOTIFY statusChanged)
// QML exposed versions of the destination rect
2017-11-03 15:31:57 +00:00
Q_PROPERTY(QPointF origin READ origin WRITE setOrigin NOTIFY geometryChanged)
Q_PROPERTY(QSizeF size READ size WRITE setSize NOTIFY geometryChanged)
Q_PROPERTY(QUrl webSocketUrl READ webSocketUrl NOTIFY webSocketUrlChanged)
Q_PROPERTY(QString rootPath READ rootPath NOTIFY rootPathChanged)
public:
explicit CanvasConnection(QObject *parent = nullptr);
~CanvasConnection();
void setNetworkAccess(QNetworkAccessManager *dl);
void setRootPropertyPath(QByteArray path);
enum Status
{
NotConnected,
Connecting,
Connected,
Closed,
Reconnecting,
2017-11-05 19:42:57 +00:00
Error,
Snapshot // offline mode, data from snapshot
};
Status status() const
{
return m_status;
}
QJsonObject saveState() const;
bool restoreState(QJsonObject state);
2017-11-05 19:42:57 +00:00
void saveSnapshot(QDataStream& ds) const;
void restoreSnapshot(QDataStream &ds);
void connectWebSocket(QByteArray hostName, int port);
2017-11-03 15:31:57 +00:00
QPointF origin() const;
QSizeF size() const;
LocalProp* propertyRoot() const;
QUrl webSocketUrl() const
{
return m_webSocketUrl;
}
QString rootPath() const
{
return QString::fromUtf8(m_rootPropertyPath);
}
FGQCanvasImageLoader* imageLoader() const;
FGQCanvasFontCache* fontCache() const;
public Q_SLOTS:
void reconnect();
// not on iOS / Android - requires widgets
void showDebugTree();
2017-11-03 15:31:57 +00:00
void setOrigin(QPointF center);
void setSize(QSizeF size);
signals:
void statusChanged(Status status);
2017-11-03 15:31:57 +00:00
void geometryChanged();
void rootPathChanged();
void webSocketUrlChanged();
void updated();
private Q_SLOTS:
void onWebSocketConnected();
void onTextMessageReceived(QString message);
void onWebSocketClosed();
private:
void setStatus(Status newStatus);
LocalProp *propertyFromPath(QByteArray path) const;
QUrl m_webSocketUrl;
QByteArray m_rootPropertyPath;
QRectF m_destRect;
QWebSocket m_webSocket;
QNetworkAccessManager* m_netAccess = nullptr;
std::unique_ptr<LocalProp> m_localPropertyRoot;
2017-11-28 22:32:30 +00:00
QHash<int, QPointer<LocalProp>> idPropertyDict;
Status m_status = NotConnected;
mutable FGQCanvasImageLoader* m_imageLoader = nullptr;
mutable FGQCanvasFontCache* m_fontCache = nullptr;
};
#endif // CANVASCONNECTION_H