From bb326e2b505abfd7fc2f18237ce3e176fcff877a Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Mon, 23 Mar 2020 15:27:58 +1100 Subject: [PATCH] nodeview/core: moved node copy/paste functions to core so they can be used elsewhere --- app/core.cpp | 101 +++++++++++++++++++++++++++++++ app/core.h | 4 ++ app/widget/nodeview/nodeview.cpp | 94 +--------------------------- 3 files changed, 107 insertions(+), 92 deletions(-) diff --git a/app/core.cpp b/app/core.cpp index b61e3d3fd..b4755c3f7 100644 --- a/app/core.cpp +++ b/app/core.cpp @@ -21,6 +21,7 @@ #include "core.h" #include +#include #include #include #include @@ -30,6 +31,7 @@ #include #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 &nodes) +{ + QString copy_str; + + QXmlStreamWriter writer(©_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 Core::PasteNodesFromClipboard(Sequence *graph) +{ + QString clipboard = QGuiApplication::clipboard()->text(); + + if (clipboard.isEmpty()) { + return QList(); + } + + QXmlStreamReader reader(clipboard); + + QList pasted_nodes; + QHash output_ptrs; + QList desired_connections; + QList 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 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(con.footage); + + bool found = false; + + foreach (ItemPtr item, footage) { + const QList& streams = std::static_pointer_cast(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(); diff --git a/app/core.h b/app/core.h index 1d0988485..702bc10ce 100644 --- a/app/core.h +++ b/app/core.h @@ -151,6 +151,10 @@ public: */ void SetAutorecoveryInterval(int minutes); + void CopyNodesToClipboard(const QList& nodes); + + QList PasteNodesFromClipboard(Sequence *graph); + /** * @brief Return a list of supported frame rates in rational form * diff --git a/app/widget/nodeview/nodeview.cpp b/app/widget/nodeview/nodeview.cpp index 9a1ce07cc..a55057678 100644 --- a/app/widget/nodeview/nodeview.cpp +++ b/app/widget/nodeview/nodeview.cpp @@ -20,14 +20,11 @@ #include "nodeview.h" -#include #include -#include #include "core.h" #include "nodeviewundo.h" #include "node/factory.h" -#include "common/xmlutils.h" NodeView::NodeView(QWidget *parent) : QGraphicsView(parent), @@ -145,26 +142,11 @@ void NodeView::CopySelected(bool cut) return; } - QString copy_str; - - QXmlStreamWriter writer(©_str); - writer.setAutoFormatting(true); - - writer.writeStartDocument(); - writer.writeStartElement(QStringLiteral("olive")); - - foreach (Node* n, selected) { - n->Save(&writer); - } - - writer.writeEndElement(); // clipboard - writer.writeEndDocument(); + Core::instance()->CopyNodesToClipboard(selected); if (cut) { DeleteSelected(); } - - QGuiApplication::clipboard()->setText(copy_str); } void NodeView::Paste() @@ -173,79 +155,7 @@ void NodeView::Paste() return; } - QString clipboard = QGuiApplication::clipboard()->text(); - - if (clipboard.isEmpty()) { - return; - } - - QXmlStreamReader reader(clipboard); - - QList pasted_nodes; - QHash output_ptrs; - QList desired_connections; - QList 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 footage = static_cast(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(con.footage); - - bool found = false; - - foreach (ItemPtr item, footage) { - const QList& streams = std::static_pointer_cast(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; - } - } - } - } - } - } + QList pasted_nodes = Core::instance()->PasteNodesFromClipboard(static_cast(graph_)); if (!pasted_nodes.isEmpty()) { // FIXME: Attach to cursor so user can drop in place