nodeview: implemented copying/pasting nodes

Re-uses XML-based project saving/loading functions to store nodes, their
parameters, and their connections in a text-based format in the clipboard.
This commit is contained in:
itsmattkc
2020-03-15 20:01:58 +11:00
parent 50061e4e55
commit 34f60e639a
24 changed files with 306 additions and 71 deletions
+2
View File
@@ -45,5 +45,7 @@ set(OLIVE_SOURCES
common/timelinecommon.h
common/timerange.h
common/timerange.cpp
common/xmlutils.h
common/xmlutils.cpp
PARENT_SCOPE
)
+37
View File
@@ -0,0 +1,37 @@
#include "xmlutils.h"
#include "node/factory.h"
Node* XMLLoadNode(QXmlStreamReader* reader) {
QString node_id;
XMLAttributeLoop(reader, attr) {
if (attr.name() == "id") {
node_id = attr.value().toString();
// Currently the only thing we need
break;
}
}
if (node_id.isEmpty()) {
qWarning() << "Found node with no ID";
return nullptr;
}
Node* node = NodeFactory::CreateFromID(node_id);
if (!node) {
qWarning() << "Failed to load" << node_id << "- no node with that ID is installed";
}
return node;
}
void XMLConnectNodes(const QHash<quintptr, NodeOutput*>& output_ptrs, const QList<NodeParam::SerializedConnection>& desired_connections)
{
foreach (const NodeParam::SerializedConnection& con, desired_connections) {
NodeParam::ConnectEdge(output_ptrs.value(con.output),
con.input);
}
}
@@ -1,6 +1,10 @@
#ifndef XMLREADLOOP_H
#define XMLREADLOOP_H
#include <QXmlStreamReader>
#include "node/node.h"
#define XMLReadLoop(reader, section) \
while (!reader->atEnd() && !(reader->name() == section && reader->isEndElement()) && reader->readNext())
@@ -8,4 +12,8 @@
QXmlStreamAttributes __attributes = reader->attributes(); \
foreach (const QXmlStreamAttribute& item, __attributes)
Node *XMLLoadNode(QXmlStreamReader* reader);
void XMLConnectNodes(const QHash<quintptr, NodeOutput *> &output_ptrs, const QList<NodeParam::SerializedConnection> &desired_connections);
#endif // XMLREADLOOP_H
+1 -1
View File
@@ -26,7 +26,7 @@
#include "common/bezier.h"
#include "common/lerp.h"
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "node.h"
#include "output.h"
#include "inputarray.h"
+1 -1
View File
@@ -2,7 +2,7 @@
#include <QApplication>
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "node.h"
NodeInputArray::NodeInputArray(const QString &id, const DataType &type, const QVariant &default_value) :
+1 -1
View File
@@ -24,7 +24,7 @@
#include <QDebug>
#include <QFile>
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
Node::Node() :
can_be_deleted_(true)
+1 -1
View File
@@ -20,7 +20,7 @@
#include "output.h"
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "node/node.h"
NodeOutput::NodeOutput(const QString &id) :
+15
View File
@@ -59,6 +59,21 @@ void NodePanel::DeleteSelected()
node_view_->DeleteSelected();
}
void NodePanel::CutSelected()
{
node_view_->CopySelected(true);
}
void NodePanel::CopySelected()
{
node_view_->CopySelected(false);
}
void NodePanel::Paste()
{
node_view_->Paste();
}
void NodePanel::Select(const QList<Node *> &nodes)
{
node_view_->Select(nodes);
+5
View File
@@ -40,6 +40,11 @@ public:
virtual void DeleteSelected() override;
virtual void CutSelected() override;
virtual void CopySelected() override;
virtual void Paste() override;
public slots:
void Select(const QList<Node*>& nodes);
void SelectWithDependencies(const QList<Node*>& nodes);
+1 -1
View File
@@ -20,7 +20,7 @@
#include "folder.h"
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "project/item/footage/footage.h"
#include "project/item/sequence/sequence.h"
#include "ui/icons/icons.h"
+1 -1
View File
@@ -24,7 +24,7 @@
#include "codec/decoder.h"
#include "common/timecodefunctions.h"
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "config/config.h"
#include "ui/icons/icons.h"
+1 -1
View File
@@ -20,7 +20,7 @@
#include "imagestream.h"
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "footage.h"
#include "project/project.h"
#include "render/colormanager.h"
+28 -3
View File
@@ -76,11 +76,19 @@ const QList<ItemPtr> &Item::children() const
return children_;
}
ItemPtr Item::shared_ptr_from_raw(Item *item)
ItemPtr Item::shared_ptr_from_raw(Item *item, bool traverse)
{
for (int i=0;i<children_.size();i++) {
if (children_.at(i).get() == item) {
return children_.at(i);
ItemPtr c = children_.at(i);
if (c.get() == item) {
return c;
} else if (traverse && c->CanHaveChildren()) {
ItemPtr grandchild = shared_ptr_from_raw(item);
if (grandchild) {
return grandchild;
}
}
}
@@ -147,6 +155,23 @@ void Item::set_project(Project *project)
project_ = project;
}
QList<ItemPtr> Item::get_children_of_type(Type type, bool recursive) const
{
QList<ItemPtr> list;
foreach (ItemPtr item, children_) {
if (item->type() == type) {
list.append(item);
}
if (recursive && item->CanHaveChildren()) {
list.append(item->get_children_of_type(type, recursive));
}
}
return list;
}
bool Item::CanHaveChildren() const
{
return false;
+3 -1
View File
@@ -77,7 +77,7 @@ public:
Item* child(int i) const;
const QList<ItemPtr>& children() const;
ItemPtr shared_ptr_from_raw(Item* item);
ItemPtr shared_ptr_from_raw(Item* item, bool traverse = false);
const QString& name() const;
void set_name(const QString& n);
@@ -97,6 +97,8 @@ public:
Project* project() const;
void set_project(Project* project);
QList<ItemPtr> get_children_of_type(Type type, bool recursive) const;
virtual bool CanHaveChildren() const;
bool ChildExistsWithName(const QString& name);
+3 -27
View File
@@ -25,7 +25,7 @@
#include "config/config.h"
#include "common/channellayout.h"
#include "common/timecodefunctions.h"
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "node/factory.h"
#include "panel/panelmanager.h"
#include "panel/node/node.h"
@@ -110,28 +110,7 @@ void Sequence::Load(QXmlStreamReader *reader, QHash<quintptr, StreamPtr> &, QLis
Node* node;
if (reader->name() == "node") {
QString node_id;
XMLAttributeLoop(reader, attr) {
if (attr.name() == "id") {
node_id = attr.value().toString();
// Currently the only thing we need
break;
}
}
if (node_id.isEmpty()) {
qDebug() << "Found node with no ID";
continue;
}
node = NodeFactory::CreateFromID(node_id);
if (!node) {
qDebug() << "Failed to load" << node_id << "- no node with that ID is installed";
continue;
}
node = XMLLoadNode(reader);
} else {
node = viewer_output_;
}
@@ -146,10 +125,7 @@ void Sequence::Load(QXmlStreamReader *reader, QHash<quintptr, StreamPtr> &, QLis
}
// Make connections
foreach (const NodeParam::SerializedConnection& con, desired_connections) {
NodeParam::ConnectEdge(output_ptrs.value(con.output),
con.input);
}
XMLConnectNodes(output_ptrs, desired_connections);
// Ensure this and all children are in the main thread
// (FIXME: Weird place for this? This should probably be in ProjectLoadManager somehow)
+6 -1
View File
@@ -23,7 +23,7 @@
#include <QDir>
#include <QFileInfo>
#include "common/xmlreadloop.h"
#include "common/xmlutils.h"
#include "core.h"
#include "dialog/progress/progress.h"
#include "window/mainwindow/mainwindow.h"
@@ -137,3 +137,8 @@ ColorManager *Project::color_manager()
{
return &color_manager_;
}
QList<ItemPtr> Project::get_items_of_type(Item::Type type) const
{
return root_.get_children_of_type(type, true);
}
+2
View File
@@ -63,6 +63,8 @@ public:
ColorManager* color_manager();
QList<ItemPtr> get_items_of_type(Item::Type type) const;
signals:
void NameChanged();
+24 -4
View File
@@ -34,10 +34,10 @@ MenuShared::MenuShared()
new_folder_item_ = Menu::CreateItem(this, "newfolder", Core::instance(), SLOT(CreateNewFolder()));
// "Edit" menu shared items
edit_cut_item_ = Menu::CreateItem(this, "cut", nullptr, nullptr, "Ctrl+X");
edit_copy_item_ = Menu::CreateItem(this, "copy", nullptr, nullptr, "Ctrl+C");
edit_paste_item_ = Menu::CreateItem(this, "paste", nullptr, nullptr, "Ctrl+V");
edit_paste_insert_item_ = Menu::CreateItem(this, "pasteinsert", nullptr, nullptr, "Ctrl+Shift+V");
edit_cut_item_ = Menu::CreateItem(this, "cut", this, SLOT(CutTriggered()), "Ctrl+X");
edit_copy_item_ = Menu::CreateItem(this, "copy", this, SLOT(CopyTriggered()), "Ctrl+C");
edit_paste_item_ = Menu::CreateItem(this, "paste", this, SLOT(PasteTriggered()), "Ctrl+V");
edit_paste_insert_item_ = Menu::CreateItem(this, "pasteinsert", this, SLOT(PasteInsertTriggered()), "Ctrl+Shift+V");
edit_duplicate_item_ = Menu::CreateItem(this, "duplicate", nullptr, nullptr, "Ctrl+D");
edit_delete_item_ = Menu::CreateItem(this, "delete", this, SLOT(DeleteSelectedTriggered()), "Del");
edit_ripple_delete_item_ = Menu::CreateItem(this, "rippledelete", this, SLOT(RippleDeleteTriggered()), "Shift+Del");
@@ -161,6 +161,26 @@ void MenuShared::ToggleLinksTriggered()
PanelManager::instance()->CurrentlyFocused()->ToggleLinks();
}
void MenuShared::CutTriggered()
{
PanelManager::instance()->CurrentlyFocused()->CutSelected();
}
void MenuShared::CopyTriggered()
{
PanelManager::instance()->CurrentlyFocused()->CopySelected();
}
void MenuShared::PasteTriggered()
{
PanelManager::instance()->CurrentlyFocused()->Paste();
}
void MenuShared::PasteInsertTriggered()
{
PanelManager::instance()->CurrentlyFocused()->PasteInsert();
}
void MenuShared::Retranslate()
{
// "New" menu shared items
+8
View File
@@ -93,6 +93,14 @@ private slots:
void ToggleLinksTriggered();
void CutTriggered();
void CopyTriggered();
void PasteTriggered();
void PasteInsertTriggered();
};
#endif // MENUSHARED_H
+116 -28
View File
@@ -20,11 +20,14 @@
#include "nodeview.h"
#include <QClipboard>
#include <QMouseEvent>
#include <QGuiApplication>
#include "core.h"
#include "nodeviewundo.h"
#include "node/factory.h"
#include "common/xmlutils.h"
NodeView::NodeView(QWidget *parent) :
QGraphicsView(parent),
@@ -90,16 +93,7 @@ void NodeView::DeleteSelected()
return;
}
QList<QGraphicsItem*> selected = scene_.selectedItems();
QList<Node*> selected_nodes;
foreach (QGraphicsItem* item, selected) {
NodeViewItem* node_item = dynamic_cast<NodeViewItem*>(item);
if (node_item) {
selected_nodes.append(node_item->node());
}
}
QList<Node*> selected_nodes = scene_.GetSelectedNodes();
if (selected_nodes.isEmpty()) {
return;
@@ -133,9 +127,6 @@ void NodeView::Select(const QList<Node *> &nodes)
foreach (Node* n, nodes) {
NodeViewItem* item = scene_.NodeToUIObject(n);
Q_ASSERT(n);
Q_ASSERT(item);
item->setSelected(true);
}
}
@@ -150,6 +141,117 @@ void NodeView::SelectWithDependencies(QList<Node *> nodes)
Select(nodes);
}
void NodeView::CopySelected(bool cut)
{
if (!graph_) {
return;
}
QList<Node*> selected = scene_.GetSelectedNodes();
if (selected.isEmpty()) {
return;
}
QString copy_str;
QXmlStreamWriter writer(&copy_str);
writer.setAutoFormatting(true);
writer.writeStartDocument();
writer.writeStartElement(QStringLiteral("olive"));
foreach (Node* n, selected) {
n->Save(&writer);
}
writer.writeEndElement(); // clipboard
writer.writeEndDocument();
if (cut) {
DeleteSelected();
}
QGuiApplication::clipboard()->setText(copy_str);
}
void NodeView::Paste()
{
if (!graph_) {
return;
}
QString clipboard = QGuiApplication::clipboard()->text();
if (clipboard.isEmpty()) {
return;
}
QXmlStreamReader reader(clipboard);
QList<Node*> pasted_nodes;
QHash<quintptr, NodeOutput*> output_ptrs;
QList<NodeParam::SerializedConnection> desired_connections;
QList<NodeInput::FootageConnection> footage_connections;
XMLReadLoop((&reader), QStringLiteral("olive")) {
if (reader.name() == QStringLiteral("node")) {
Node* node = XMLLoadNode(&reader);
if (node) {
node->Load(&reader, output_ptrs, desired_connections, footage_connections, nullptr, reader.name().toString());
graph_->AddNode(node);
pasted_nodes.append(node);
}
}
}
// 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 = static_cast<Sequence*>(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;
}
}
}
}
}
}
if (!pasted_nodes.isEmpty()) {
// FIXME: Attach to cursor so user can drop in place
}
}
void NodeView::ItemsChanged()
{
QHash<NodeEdge *, NodeViewEdge *>::const_iterator i;
@@ -263,21 +365,7 @@ void NodeView::mouseMoveEvent(QMouseEvent *event)
void NodeView::SceneSelectionChangedSlot()
{
// Get the scene's selected items and convert it into a list of selected nodes
QList<QGraphicsItem*> selected_items = scene_.selectedItems();
QList<Node*> selected_nodes;
for (int i=0;i<selected_items.size();i++) {
// Try to dynamically cast to a widget holding a Node
NodeViewItem* item = dynamic_cast<NodeViewItem*>(selected_items.at(i));
if (item != nullptr) {
selected_nodes.append(item->node());
}
}
emit SelectionChanged(selected_nodes);
emit SelectionChanged(scene_.GetSelectedNodes());
}
void NodeView::ShowContextMenu(const QPoint &pos)
+3
View File
@@ -57,6 +57,9 @@ public:
void Select(const QList<Node*>& nodes);
void SelectWithDependencies(QList<Node *> nodes);
void CopySelected(bool cut);
void Paste();
signals:
/**
* @brief Signal emitted when the selected nodes have changed
+28
View File
@@ -42,6 +42,34 @@ void NodeViewScene::SetGraph(NodeGraph *graph)
graph_ = graph;
}
QList<Node *> NodeViewScene::GetSelectedNodes() const
{
QHash<Node*, NodeViewItem*>::const_iterator iterator;
QList<Node *> selected;
for (iterator=item_map_.begin();iterator!=item_map_.end();iterator++) {
if (iterator.value()->isSelected()) {
selected.append(iterator.key());
}
}
return selected;
}
QList<NodeViewItem *> NodeViewScene::GetSelectedItems() const
{
QHash<Node*, NodeViewItem*>::const_iterator iterator;
QList<NodeViewItem *> selected;
for (iterator=item_map_.begin();iterator!=item_map_.end();iterator++) {
if (iterator.value()->isSelected()) {
selected.append(iterator.value());
}
}
return selected;
}
const QHash<Node *, NodeViewItem *> &NodeViewScene::item_map() const
{
return item_map_;
+3
View File
@@ -38,6 +38,9 @@ public:
void SetGraph(NodeGraph* graph);
QList<Node*> GetSelectedNodes() const;
QList<NodeViewItem*> GetSelectedItems() const;
const QHash<Node*, NodeViewItem*>& item_map() const;
const QHash<NodeEdge*, NodeViewEdge*>& edge_map() const;
+8
View File
@@ -132,6 +132,14 @@ public:
virtual void ToggleLinks(){}
virtual void CutSelected(){}
virtual void CopySelected(){}
virtual void Paste(){}
virtual void PasteInsert(){}
protected:
/**
* @brief paintEvent