1
0
Fork 0

Further work on snapshots feature

This commit is contained in:
James Turner 2017-11-05 19:42:57 +00:00
parent 6156cbbe1d
commit a22a17a995
3 changed files with 68 additions and 24 deletions

View file

@ -31,6 +31,7 @@
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <QDataStream>
#include "canvasconnection.h" #include "canvasconnection.h"
@ -117,7 +118,7 @@ void ApplicationController::saveSnapshot(QString snapshotName)
// convert spaces to underscores // convert spaces to underscores
QString filesystemCleanName = snapshotName.replace(QRegularExpression("[\\s-\\\"/]"), "_"); QString filesystemCleanName = snapshotName.replace(QRegularExpression("[\\s-\\\"/]"), "_");
QFile f(d.filePath(filesystemCleanName + ".json")); QFile f(d.filePath(filesystemCleanName + ".fgcanvassnapshot"));
if (f.exists()) { if (f.exists()) {
qWarning() << "not over-writing" << f.fileName(); qWarning() << "not over-writing" << f.fileName();
return; return;
@ -135,7 +136,29 @@ void ApplicationController::saveSnapshot(QString snapshotName)
void ApplicationController::restoreSnapshot(int index) void ApplicationController::restoreSnapshot(int index)
{ {
QString path = m_snapshots.at(index).toMap().value("path").toString();
QFile f(path);
if (!f.open(QIODevice::ReadOnly)) {
qWarning() << Q_FUNC_INFO << "failed to open the file";
return;
}
clearConnections();
{
QDataStream ds(&f);
int version, canvasCount;
QString name;
ds >> version >> name >> canvasCount;
for (int i=0; i < canvasCount; ++i) {
CanvasConnection* cc = new CanvasConnection(this);
cc->restoreSnapshot(ds);
m_activeCanvases.append(cc);
}
}
emit activeCanvasesChanged();
} }
void ApplicationController::rebuildSnapshotData() void ApplicationController::rebuildSnapshotData()
@ -148,18 +171,22 @@ void ApplicationController::rebuildSnapshotData()
return; return;
} }
// this requires parsing each snapshit in its entirety just to extract Q_FOREACH (auto entry, d.entryList(QStringList() << "*.fgcanvassnapshot")) {
// the name, which is horrible.
Q_FOREACH (auto entry, d.entryList(QStringList() << "*.json")) {
QFile f(d.filePath(entry)); QFile f(d.filePath(entry));
f.open(QIODevice::ReadOnly); f.open(QIODevice::ReadOnly);
QJsonDocument doc = QJsonDocument::fromJson(f.readAll()); {
QDataStream ds(&f);
int version;
QString name;
ds >> version;
QVariantMap m; QVariantMap m;
m["path"] = f.fileName(); m["path"] = f.fileName();
m["name"] = doc.object().value("snapshotName").toString(); ds >>name;
m["name"] = name;
m_snapshots.append(m); m_snapshots.append(m);
} }
}
emit snapshotListChanged(); emit snapshotListChanged();
} }
@ -405,19 +432,17 @@ void ApplicationController::clearConnections()
QByteArray ApplicationController::createSnapshot(QString name) const QByteArray ApplicationController::createSnapshot(QString name) const
{ {
QJsonObject json; QByteArray bytes;
json["snapshotName"] = name; const int version = 1;
#if 0 {
QJsonArray canvases; QDataStream ds(&bytes, QIODevice::WriteOnly);
Q_FOREACH (auto canvas, m_activeCanvases) { ds << version << name;
canvases.append(canvas->saveState());
ds << m_activeCanvases.size();
Q_FOREACH(auto c, m_activeCanvases) {
c->saveSnapshot(ds);
}
} }
json["canvases"] = canvases; return bytes;
// background color?
// window geometry and state?
#endif
QJsonDocument doc;
doc.setObject(json);
return doc.toJson();
} }

View file

@ -26,6 +26,7 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QDataStream>
#include "localprop.h" #include "localprop.h"
#include "fgqcanvasfontcache.h" #include "fgqcanvasfontcache.h"
@ -88,6 +89,20 @@ bool CanvasConnection::restoreState(QJsonObject state)
return true; return true;
} }
void CanvasConnection::saveSnapshot(QDataStream &ds) const
{
ds << m_webSocketUrl << m_rootPropertyPath << m_destRect;
m_localPropertyRoot->saveToStream(ds);
}
void CanvasConnection::restoreSnapshot(QDataStream &ds)
{
ds >> m_webSocketUrl >> m_rootPropertyPath >> m_destRect;
m_localPropertyRoot.reset(new LocalProp{nullptr, NameIndexTuple("")});
m_localPropertyRoot->restoreFromStream(ds, nullptr);
setStatus(Snapshot);
}
void CanvasConnection::reconnect() void CanvasConnection::reconnect()
{ {
m_webSocket.open(m_webSocketUrl); m_webSocket.open(m_webSocketUrl);

View file

@ -30,6 +30,7 @@ class LocalProp;
class QNetworkAccessManager; class QNetworkAccessManager;
class FGQCanvasImageLoader; class FGQCanvasImageLoader;
class FGQCanvasFontCache; class FGQCanvasFontCache;
class QDataStream;
class CanvasConnection : public QObject class CanvasConnection : public QObject
{ {
@ -59,7 +60,8 @@ public:
Connected, Connected,
Closed, Closed,
Reconnecting, Reconnecting,
Error Error,
Snapshot // offline mode, data from snapshot
}; };
Status status() const Status status() const
@ -70,6 +72,9 @@ public:
QJsonObject saveState() const; QJsonObject saveState() const;
bool restoreState(QJsonObject state); bool restoreState(QJsonObject state);
void saveSnapshot(QDataStream& ds) const;
void restoreSnapshot(QDataStream &ds);
void connectWebSocket(QByteArray hostName, int port); void connectWebSocket(QByteArray hostName, int port);
QPointF origin() const; QPointF origin() const;
@ -129,7 +134,6 @@ private:
std::unique_ptr<LocalProp> m_localPropertyRoot; std::unique_ptr<LocalProp> m_localPropertyRoot;
QHash<int, LocalProp*> idPropertyDict; QHash<int, LocalProp*> idPropertyDict;
Status m_status = NotConnected; Status m_status = NotConnected;
QString m_rootPath;
mutable FGQCanvasImageLoader* m_imageLoader = nullptr; mutable FGQCanvasImageLoader* m_imageLoader = nullptr;
mutable FGQCanvasFontCache* m_fontCache = nullptr; mutable FGQCanvasFontCache* m_fontCache = nullptr;