nodeview/core: moved node copy/paste functions to core so they can be used elsewhere

This commit is contained in:
itsmattkc
2020-03-23 15:27:58 +11:00
parent 52a8966abd
commit bb326e2b50
3 changed files with 107 additions and 92 deletions
+101
View File
@@ -21,6 +21,7 @@
#include "core.h"
#include <QApplication>
#include <QClipboard>
#include <QCommandLineParser>
#include <QDebug>
#include <QFileDialog>
@@ -30,6 +31,7 @@
#include <QStyleFactory>
#include "audio/audiomanager.h"
#include "common/xmlutils.h"
#include "config/config.h"
#include "dialog/about/about.h"
#include "dialog/export/export.h"
@@ -511,6 +513,105 @@ void Core::SetAutorecoveryInterval(int minutes)
autorecovery_timer_.setInterval(minutes * 60000);
}
void Core::CopyNodesToClipboard(const QList<Node *> &nodes)
{
QString copy_str;
QXmlStreamWriter writer(&copy_str);
writer.setAutoFormatting(true);
writer.writeStartDocument();
writer.writeStartElement(QStringLiteral("olive"));
foreach (Node* n, nodes) {
n->Save(&writer);
}
writer.writeEndElement(); // clipboard
writer.writeEndDocument();
QGuiApplication::clipboard()->setText(copy_str);
}
QList<Node*> Core::PasteNodesFromClipboard(Sequence *graph)
{
QString clipboard = QGuiApplication::clipboard()->text();
if (clipboard.isEmpty()) {
return QList<Node*>();
}
QXmlStreamReader reader(clipboard);
QList<Node*> pasted_nodes;
QHash<quintptr, NodeOutput*> output_ptrs;
QList<NodeParam::SerializedConnection> desired_connections;
QList<NodeInput::FootageConnection> footage_connections;
while (XMLReadNextStartElement(&reader)) {
if (reader.name() == QStringLiteral("olive")) {
while (XMLReadNextStartElement(&reader)) {
if (reader.name() == QStringLiteral("node")) {
Node* node = XMLLoadNode(&reader);
if (node) {
node->Load(&reader, output_ptrs, desired_connections, footage_connections, nullptr);
graph->AddNode(node);
pasted_nodes.append(node);
}
} else {
reader.skipCurrentElement();
}
}
} else {
reader.skipCurrentElement();
}
}
// Make connections
if (!desired_connections.isEmpty()) {
XMLConnectNodes(output_ptrs, desired_connections);
}
// Connect footage to existing footage if it exists
if (!footage_connections.isEmpty()) {
// Get list of all footage from project
// FIXME: Assumes sequence
QList<ItemPtr> footage = graph->project()->get_items_of_type(Item::kFootage);
if (!footage.isEmpty()) {
foreach (const NodeInput::FootageConnection& con, footage_connections) {
if (con.footage) {
// Assume this is a pointer to a Stream*
Stream* loaded_stream = reinterpret_cast<Stream*>(con.footage);
bool found = false;
foreach (ItemPtr item, footage) {
const QList<StreamPtr>& streams = std::static_pointer_cast<Footage>(item)->streams();
foreach (StreamPtr s, streams) {
if (s.get() == loaded_stream) {
con.input->set_standard_value(QVariant::fromValue(s));
found = true;
break;
}
}
if (found) {
break;
}
}
}
}
}
}
return pasted_nodes;
}
bool Core::SaveActiveProject()
{
Project* active_project = GetActiveProject();