functions for importing/exporting nodes from/to file

portability/boost
zetaPRIME 2019-01-24 05:38:25 -05:00
parent 9d3c06aef0
commit ad49babb96
4 changed files with 62 additions and 10 deletions

5
notes
View File

@ -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
}

View File

@ -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
}

View File

@ -1,6 +1,7 @@
#include "fileops.h"
#include "uisocket.h"
#include "data/project.h"
#include "data/graph.h"
#include <QDebug>
@ -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> project, QString file
std::shared_ptr<Xybrid::Data::Project> 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::Data::Project> Xybrid::FileOps::loadProject(QString file
return project;
}
bool Xybrid::FileOps::saveNode(std::shared_ptr<Xybrid::Data::Node> 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::Data::Node> Xybrid::FileOps::loadNode(QString fileName, std::shared_ptr<Graph> 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
}

View File

@ -1,12 +1,21 @@
#pragma once
#include "data/project.h"
#include <memory>
#include <QFileInfo>
namespace Xybrid::Data {
class Project;
class Graph;
class Node;
}
namespace Xybrid::FileOps {
//
bool saveProject(std::shared_ptr<Data::Project> project, QString fileName = "");
std::shared_ptr<Data::Project> loadProject(QString fileName);
bool saveNode(std::shared_ptr<Data::Node> node, QString fileName = "");
std::shared_ptr<Data::Node> loadNode(QString fileName, std::shared_ptr<Data::Graph> parent = nullptr);
}