| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifndef CANVASCONNECTION_H |
| 19 |
#define CANVASCONNECTION_H |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
|
| 23 |
#include <QObject> |
| 24 |
#include <QtWebSockets/QWebSocket> |
| 25 |
#include <QJsonObject> |
| 26 |
#include <QUrl> |
| 27 |
#include <QRectF> |
| 28 |
#include <QPointer> |
| 29 |
#include <QTimer> |
| 30 |
|
| 31 |
class LocalProp; |
| 32 |
class QNetworkAccessManager; |
| 33 |
class FGQCanvasImageLoader; |
| 34 |
class FGQCanvasFontCache; |
| 35 |
class QDataStream; |
| 36 |
|
| 37 |
class CanvasConnection : public QObject |
| 38 |
{ |
| 39 |
Q_OBJECT |
| 40 |
|
| 41 |
Q_ENUMS(Status) |
| 42 |
|
| 43 |
Q_PROPERTY(Status status READ status NOTIFY statusChanged) |
| 44 |
|
| 45 |
|
| 46 |
Q_PROPERTY(QPointF origin READ origin WRITE setOrigin NOTIFY geometryChanged) |
| 47 |
Q_PROPERTY(QSizeF size READ size WRITE setSize NOTIFY geometryChanged) |
| 48 |
|
| 49 |
Q_PROPERTY(int windowIndex READ windowIndex WRITE setWindowIndex NOTIFY geometryChanged) |
| 50 |
|
| 51 |
Q_PROPERTY(QUrl webSocketUrl READ webSocketUrl NOTIFY webSocketUrlChanged) |
| 52 |
Q_PROPERTY(QString rootPath READ rootPath NOTIFY rootPathChanged) |
| 53 |
public: |
| 54 |
explicit CanvasConnection(QObject *parent = nullptr); |
| 55 |
~CanvasConnection(); |
| 56 |
|
| 57 |
void setNetworkAccess(QNetworkAccessManager *dl); |
| 58 |
void setRootPropertyPath(QByteArray path); |
| 59 |
|
| 60 |
void setAutoReconnect(); |
| 61 |
|
| 62 |
enum Status |
| 63 |
{ |
| 64 |
NotConnected, |
| 65 |
Connecting, |
| 66 |
Connected, |
| 67 |
Closed, |
| 68 |
Reconnecting, |
| 69 |
Error, |
| 70 |
Snapshot |
| 71 |
}; |
| 72 |
|
| 73 |
Status status() const |
| 74 |
{ |
| 75 |
return m_status; |
| 76 |
} |
| 77 |
|
| 78 |
QJsonObject saveState() const; |
| 79 |
bool restoreState(QJsonObject state); |
| 80 |
|
| 81 |
void saveSnapshot(QDataStream& ds) const; |
| 82 |
void restoreSnapshot(QDataStream &ds); |
| 83 |
|
| 84 |
void connectWebSocket(QByteArray hostName, int port); |
| 85 |
QPointF origin() const; |
| 86 |
|
| 87 |
QSizeF size() const; |
| 88 |
|
| 89 |
int windowIndex() const |
| 90 |
{ |
| 91 |
return m_windowIndex; |
| 92 |
} |
| 93 |
|
| 94 |
void setWindowIndex(int index); |
| 95 |
|
| 96 |
LocalProp* propertyRoot() const; |
| 97 |
|
| 98 |
QUrl webSocketUrl() const |
| 99 |
{ |
| 100 |
return m_webSocketUrl; |
| 101 |
} |
| 102 |
|
| 103 |
QString rootPath() const |
| 104 |
{ |
| 105 |
return QString::fromUtf8(m_rootPropertyPath); |
| 106 |
} |
| 107 |
|
| 108 |
FGQCanvasImageLoader* imageLoader() const; |
| 109 |
|
| 110 |
FGQCanvasFontCache* fontCache() const; |
| 111 |
|
| 112 |
public Q_SLOTS: |
| 113 |
void reconnect(); |
| 114 |
|
| 115 |
|
| 116 |
void showDebugTree(); |
| 117 |
|
| 118 |
void setOrigin(QPointF center); |
| 119 |
|
| 120 |
void setSize(QSizeF size); |
| 121 |
|
| 122 |
signals: |
| 123 |
void statusChanged(Status status); |
| 124 |
|
| 125 |
void geometryChanged(); |
| 126 |
|
| 127 |
void rootPathChanged(); |
| 128 |
|
| 129 |
void webSocketUrlChanged(); |
| 130 |
|
| 131 |
void updated(); |
| 132 |
private Q_SLOTS: |
| 133 |
void onWebSocketConnected(); |
| 134 |
void onTextMessageReceived(QString message); |
| 135 |
void onWebSocketClosed(); |
| 136 |
|
| 137 |
private: |
| 138 |
void setStatus(Status newStatus); |
| 139 |
LocalProp *propertyFromPath(QByteArray path) const; |
| 140 |
|
| 141 |
QUrl m_webSocketUrl; |
| 142 |
QByteArray m_rootPropertyPath; |
| 143 |
QRectF m_destRect; |
| 144 |
int m_windowIndex = 0; |
| 145 |
|
| 146 |
QWebSocket m_webSocket; |
| 147 |
QNetworkAccessManager* m_netAccess = nullptr; |
| 148 |
QTimer* m_reconnectTimer = nullptr; |
| 149 |
bool m_autoReconnect = false; |
| 150 |
|
| 151 |
std::unique_ptr<LocalProp> m_localPropertyRoot; |
| 152 |
QHash<int, QPointer<LocalProp>> idPropertyDict; |
| 153 |
Status m_status = NotConnected; |
| 154 |
|
| 155 |
mutable FGQCanvasImageLoader* m_imageLoader = nullptr; |
| 156 |
mutable FGQCanvasFontCache* m_fontCache = nullptr; |
| 157 |
}; |
| 158 |
|
| 159 |
#endif |