start implementing serializers

This commit is contained in:
itsmattkc
2022-01-12 08:48:31 -08:00
parent d8ccc62d51
commit 4ecce20c35
38 changed files with 2984 additions and 1329 deletions
+1 -234
View File
@@ -32,12 +32,6 @@
namespace olive {
// This is the project version that Olive will save
const uint Project::kProjectVersion = 211228;
// This is the minimum project version that Olive can load
const uint Project::kProjectMinimumVersion = 210528;
Project::Project() :
is_modified_(false),
autorecovery_saved_(true)
@@ -50,6 +44,7 @@ Project::Project() :
root_->setParent(this);
root_->SetLabel(tr("Root"));
root_->SetCanBeDeleted(false);
AddDefaultNode(root_);
// Adds a color manager "node" to this project so that it synchronizes
color_manager_ = new ColorManager();
@@ -67,194 +62,6 @@ Project::Project() :
this, &Project::ColorManagerValueChanged);
}
void Project::Load(QXmlStreamReader *reader, MainWindowLayoutInfo* layout, uint version, const QAtomicInt* cancelled)
{
XMLNodeData xml_node_data;
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("root")) {
root_->Load(reader, xml_node_data, version, cancelled);
} else if (reader->name() == QStringLiteral("layout")) {
// Since the main window's functions have to occur in the GUI thread (and we're likely
// loading in a secondary thread), we load all necessary data into a separate struct so we
// can continue loading and queue it with the main window so it can handle the data
// appropriately in its own thread.
*layout = MainWindowLayoutInfo::fromXml(reader, xml_node_data);
} else if (reader->name() == QStringLiteral("uuid")) {
uuid_ = QUuid::fromString(reader->readElementText());
} else if (reader->name() == QStringLiteral("nodes")) {
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("node")) {
bool is_root = false;
bool is_cm = false;
bool is_settings = false;
QString id;
{
XMLAttributeLoop(reader, attr) {
if (attr.name() == QStringLiteral("id")) {
id = attr.value().toString();
} else if (attr.name() == QStringLiteral("root") && attr.value() == QStringLiteral("1")) {
is_root = true;
} else if (attr.name() == QStringLiteral("cm") && attr.value() == QStringLiteral("1")) {
is_cm = true;
} else if (attr.name() == QStringLiteral("settings") && attr.value() == QStringLiteral("1")) {
is_settings = true;
}
}
}
if (id.isEmpty()) {
qWarning() << "Failed to load node with empty ID";
reader->skipCurrentElement();
} else {
Node* node;
if (is_root) {
node = root_;
} else if (is_cm) {
node = color_manager_;
} else if (is_settings) {
node = settings_;
} else {
node = NodeFactory::CreateFromID(id);
}
if (!node) {
qWarning() << "Failed to find node with ID" << id;
reader->skipCurrentElement();
} else {
node->Load(reader, xml_node_data, version, cancelled);
node->setParent(this);
}
}
} else {
reader->skipCurrentElement();
}
}
} else if (reader->name() == QStringLiteral("positions")) {
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("context")) {
quintptr context_ptr = 0;
XMLAttributeLoop(reader, attr) {
if (attr.name() == QStringLiteral("ptr")) {
context_ptr = attr.value().toULongLong();
break;
}
}
Node *context = xml_node_data.node_ptrs.value(context_ptr);
if (!context) {
qWarning() << "Failed to find pointer for context";
reader->skipCurrentElement();
} else {
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("node")) {
quintptr node_ptr;
Node::Position node_pos;
if (LoadPosition(reader, &node_ptr, &node_pos)) {
Node *node = xml_node_data.node_ptrs.value(node_ptr);
if (node) {
context->SetNodePositionInContext(node, node_pos);
} else {
qWarning() << "Failed to find pointer for node position";
reader->skipCurrentElement();
}
}
} else {
reader->skipCurrentElement();
}
}
}
} else {
reader->skipCurrentElement();
}
}
} else {
// Skip this
reader->skipCurrentElement();
}
}
// Make connections
xml_node_data.PostConnect(version);
}
void Project::Save(QXmlStreamWriter *writer) const
{
writer->writeTextElement(QStringLiteral("uuid"), uuid_.toString());
writer->writeStartElement(QStringLiteral("nodes"));
foreach (Node* node, nodes()) {
writer->writeStartElement(QStringLiteral("node"));
if (node == root_) {
writer->writeAttribute(QStringLiteral("root"), QStringLiteral("1"));
} else if (node == color_manager_) {
writer->writeAttribute(QStringLiteral("cm"), QStringLiteral("1"));
} else if (node == settings_) {
writer->writeAttribute(QStringLiteral("settings"), QStringLiteral("1"));
}
writer->writeAttribute(QStringLiteral("id"), node->id());
node->Save(writer);
writer->writeEndElement(); // node
}
writer->writeEndElement(); // nodes
writer->writeStartElement(QStringLiteral("positions"));
foreach (Node* context, nodes()) {
const Node::PositionMap &map = context->GetContextPositions();
if (!map.isEmpty()) {
writer->writeStartElement(QStringLiteral("context"));
writer->writeAttribute(QStringLiteral("ptr"), QString::number(reinterpret_cast<quintptr>(context)));
for (auto jt=map.cbegin(); jt!=map.cend(); jt++) {
writer->writeStartElement(QStringLiteral("node"));
SavePosition(writer, jt.key(), jt.value());
writer->writeEndElement(); // node
}
writer->writeEndElement(); // context
}
}
writer->writeEndElement(); // positions
// Save main window project layout
MainWindowLayoutInfo main_window_info = Core::instance()->main_window()->SaveLayout();
main_window_info.toXml(writer);
}
Folder *Project::root()
{
return root_;
@@ -353,46 +160,6 @@ void Project::RegenerateUuid()
uuid_ = QUuid::createUuid();
}
bool Project::LoadPosition(QXmlStreamReader *reader, quintptr *node_ptr, Node::Position *pos)
{
bool got_node_ptr = false;
bool got_pos_x = false;
bool got_pos_y = false;
XMLAttributeLoop(reader, attr) {
if (attr.name() == QStringLiteral("ptr")) {
*node_ptr = attr.value().toULongLong();
got_node_ptr = true;
break;
}
}
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("x")) {
pos->position.setX(reader->readElementText().toDouble());
got_pos_x = true;
} else if (reader->name() == QStringLiteral("y")) {
pos->position.setY(reader->readElementText().toDouble());
got_pos_y = true;
} else if (reader->name() == QStringLiteral("expanded")) {
pos->expanded = reader->readElementText().toInt();
} else {
reader->skipCurrentElement();
}
}
return got_node_ptr && got_pos_x && got_pos_y;
}
void Project::SavePosition(QXmlStreamWriter *writer, Node *node, const Node::Position &pos)
{
writer->writeAttribute(QStringLiteral("ptr"), QString::number(reinterpret_cast<quintptr>(node)));
writer->writeTextElement(QStringLiteral("x"), QString::number(pos.position.x()));
writer->writeTextElement(QStringLiteral("y"), QString::number(pos.position.y()));
writer->writeTextElement(QStringLiteral("expanded"), QString::number(pos.expanded));
}
void Project::ColorManagerValueChanged(const NodeInput &input, const TimeRange &range)
{
Q_UNUSED(input)