From ad49babb96c0b2bf18e46860c10c6df2bfe41fcd Mon Sep 17 00:00:00 2001 From: zetaPRIME Date: Thu, 24 Jan 2019 05:38:25 -0500 Subject: [PATCH] functions for importing/exporting nodes from/to file --- notes | 5 +++-- save format | 2 +- xybrid/fileops.cpp | 54 ++++++++++++++++++++++++++++++++++++++++------ xybrid/fileops.h | 11 +++++++++- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/notes b/notes index d39288a..3a35e81 100644 --- a/notes +++ b/notes @@ -45,10 +45,11 @@ project data { TODO { immediate frontburner { - patchboard copy+paste! cbor array of node objects, position relative to center of viewport + patchboard copy+paste! cbor array of node objects + position: copy relative to centroid, paste relative to center of viewport also record connections between those particular nodes! - provide conversions to/from qcborvalue in node.cpp? - while we're here, add file import/export of any given node (still .xyg? .xyn?) + > while we're here, add file import/export of any given node (.xyn) probably exclude i/o ports from copy+paste lest things futz up on the parent } diff --git a/save format b/save format index 533ead8..4ba7433 100644 --- a/save format +++ b/save format @@ -4,7 +4,7 @@ probably have a field of raw data for (some) node saves, and keep it resident in extensions { .xyp - project - .xyg - graph + .xyn - node } diff --git a/xybrid/fileops.cpp b/xybrid/fileops.cpp index 3c9dbf9..92317f2 100644 --- a/xybrid/fileops.cpp +++ b/xybrid/fileops.cpp @@ -1,6 +1,7 @@ #include "fileops.h" #include "uisocket.h" +#include "data/project.h" #include "data/graph.h" #include @@ -15,6 +16,8 @@ using Xybrid::Data::Project; using Xybrid::Data::Pattern; +using Xybrid::Data::Graph; +using Xybrid::Data::Node; namespace { constexpr uint32_t packedVersion(uint8_t major = 0, uint8_t minor = 0, uint8_t revision = 0, uint8_t wip = 0) { @@ -123,13 +126,16 @@ bool Xybrid::FileOps::saveProject(std::shared_ptr project, QString file std::shared_ptr Xybrid::FileOps::loadProject(QString fileName) { - QFile file(fileName); - if (!file.open(QFile::ReadOnly)) return nullptr; - QCborStreamReader read(&file); + QCborArray root; + { + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) return nullptr; + QCborStreamReader read(&file); - // if it's not an array it'll just return an empty which will fail the header check anyway - QCborArray root = QCborValue::fromCbor(read).toArray(); - file.close(); // don't want to leave this hanging any longer than necessary + // if it's not an array it'll just return an empty which will fail the header check anyway + root = QCborValue::fromCbor(read).toArray(); + file.close(); // don't want to leave this hanging any longer than necessary + } // header and sanity checks if (root.at(0) != QString("xybrid:project")) return nullptr; // not a project @@ -209,3 +215,39 @@ std::shared_ptr Xybrid::FileOps::loadProject(QString file return project; } + +bool Xybrid::FileOps::saveNode(std::shared_ptr node, QString fileName) { + QFile file(fileName); + if (!file.open(QFile::WriteOnly)) return false; + + // we handle header and let the node handle the rest of its conversion + QCborArray root; + root << "xybrid:node" << XYBRID_VERSION << node->toCbor(); + + // write out + QCborStreamWriter w(&file); + root.toCborValue().toCbor(w); + file.close(); + + return true; +} + +std::shared_ptr Xybrid::FileOps::loadNode(QString fileName, std::shared_ptr parent) { + QCborArray root; + { + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) return nullptr; + QCborStreamReader read(&file); + + // if it's not an array it'll just return an empty which will fail the header check anyway + root = QCborValue::fromCbor(read).toArray(); + file.close(); // don't want to leave this hanging any longer than necessary + } + + // header and sanity checks + if (root.at(0) != QString("xybrid:node")) return nullptr; // not a node + if (auto v = root.at(1); !v.isInteger() || v.toInteger() > XYBRID_VERSION) return nullptr; // invalid version or too new + if (!root.at(2).isMap()) return nullptr; // so close, but... nope + + return Node::fromCbor(root.at(2), parent); // let Node handle the rest +} diff --git a/xybrid/fileops.h b/xybrid/fileops.h index 20f0f74..fd09e8f 100644 --- a/xybrid/fileops.h +++ b/xybrid/fileops.h @@ -1,12 +1,21 @@ #pragma once -#include "data/project.h" +#include #include +namespace Xybrid::Data { + class Project; + class Graph; + class Node; +} + namespace Xybrid::FileOps { // bool saveProject(std::shared_ptr project, QString fileName = ""); std::shared_ptr loadProject(QString fileName); + bool saveNode(std::shared_ptr node, QString fileName = ""); + std::shared_ptr loadNode(QString fileName, std::shared_ptr parent = nullptr); + }