diff --git a/utils/fgqcanvas/applicationcontroller.cpp b/utils/fgqcanvas/applicationcontroller.cpp index a899c961e..c97e65470 100644 --- a/utils/fgqcanvas/applicationcontroller.cpp +++ b/utils/fgqcanvas/applicationcontroller.cpp @@ -65,7 +65,7 @@ void ApplicationController::save(QString configName) // convert spaces to underscores QString filesystemCleanName = configName.replace(QRegularExpression("[\\s-\\\"/]"), "_"); - QFile f(d.filePath(configName + ".json")); + QFile f(d.filePath(filesystemCleanName + ".json")); if (f.exists()) { qWarning() << "not over-writing" << f.fileName(); return; @@ -107,6 +107,63 @@ void ApplicationController::rebuildConfigData() emit configListChanged(m_configs); } +void ApplicationController::saveSnapshot(QString snapshotName) +{ + QDir d(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); + d.cd("Snapshots"); + if (!d.exists()) { + d.mkpath("."); + } + + // convert spaces to underscores + QString filesystemCleanName = snapshotName.replace(QRegularExpression("[\\s-\\\"/]"), "_"); + QFile f(d.filePath(filesystemCleanName + ".json")); + if (f.exists()) { + qWarning() << "not over-writing" << f.fileName(); + return; + } + + f.open(QIODevice::WriteOnly | QIODevice::Truncate); + f.write(createSnapshot(snapshotName)); + + QVariantMap m; + m["path"] = f.fileName(); + m["name"] = snapshotName; + m_snapshots.append(m); + emit snapshotListChanged(); +} + +void ApplicationController::restoreSnapshot(int index) +{ + +} + +void ApplicationController::rebuildSnapshotData() +{ + m_snapshots.clear(); + QDir d(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); + d.cd("Snapshots"); + if (!d.exists()) { + emit snapshotListChanged(); + return; + } + + // this requires parsing each snapshit in its entirety just to extract + // the name, which is horrible. + Q_FOREACH (auto entry, d.entryList(QStringList() << "*.json")) { + QFile f(d.filePath(entry)); + f.open(QIODevice::ReadOnly); + QJsonDocument doc = QJsonDocument::fromJson(f.readAll()); + + QVariantMap m; + m["path"] = f.fileName(); + m["name"] = doc.object().value("snapshotName").toString(); + m_snapshots.append(m); + } + + emit snapshotListChanged(); +} + void ApplicationController::query() { if (m_query) { @@ -345,3 +402,22 @@ void ApplicationController::clearConnections() m_activeCanvases.clear(); emit activeCanvasesChanged(); } + +QByteArray ApplicationController::createSnapshot(QString name) const +{ + QJsonObject json; + json["snapshotName"] = name; +#if 0 + QJsonArray canvases; + Q_FOREACH (auto canvas, m_activeCanvases) { + canvases.append(canvas->saveState()); + } + + json["canvases"] = canvases; + // background color? + // window geometry and state? +#endif + QJsonDocument doc; + doc.setObject(json); + return doc.toJson(); +} diff --git a/utils/fgqcanvas/applicationcontroller.h b/utils/fgqcanvas/applicationcontroller.h index 45ec8db6f..ed931630a 100644 --- a/utils/fgqcanvas/applicationcontroller.h +++ b/utils/fgqcanvas/applicationcontroller.h @@ -36,6 +36,7 @@ class ApplicationController : public QObject Q_PROPERTY(QVariantList canvases READ canvases NOTIFY canvasListChanged) Q_PROPERTY(QVariantList configs READ configs NOTIFY configListChanged) + Q_PROPERTY(QVariantList snapshots READ snapshots NOTIFY snapshotListChanged) Q_PROPERTY(QQmlListProperty activeCanvases READ activeCanvases NOTIFY activeCanvasesChanged) @@ -47,18 +48,20 @@ public: explicit ApplicationController(QObject *parent = nullptr); ~ApplicationController(); - Q_INVOKABLE void save(QString configName); Q_INVOKABLE void query(); Q_INVOKABLE void cancelQuery(); Q_INVOKABLE void clearQuery(); + Q_INVOKABLE void save(QString configName); Q_INVOKABLE void restoreConfig(int index); Q_INVOKABLE void deleteConfig(int index); Q_INVOKABLE void saveConfigChanges(int index); Q_INVOKABLE void openCanvas(QString path); -// void restore(); + Q_INVOKABLE void saveSnapshot(QString snapshotName); + Q_INVOKABLE void restoreSnapshot(int index); + QString host() const; unsigned int port() const; @@ -86,6 +89,11 @@ public: return m_configs; } + QVariantList snapshots() const + { + return m_snapshots; + } + signals: void hostChanged(QString host); @@ -99,6 +107,8 @@ signals: void configListChanged(QVariantList configs); + void snapshotListChanged(); + public slots: void setHost(QString host); @@ -111,6 +121,7 @@ private: void setStatus(Status newStatus); void rebuildConfigData(); + void rebuildSnapshotData(); void clearConnections(); void doSaveToFile(QString path, QString configName); @@ -118,6 +129,8 @@ private: QByteArray saveState(QString name) const; void restoreState(QByteArray bytes); + QByteArray createSnapshot(QString name) const; + QString m_host; unsigned int m_port; QVariantList m_canvases; @@ -126,7 +139,7 @@ private: Status m_status; QVariantList m_configs; QNetworkReply* m_query = nullptr; - + QVariantList m_snapshots; }; #endif // APPLICATIONCONTROLLER_H