work towards finishing nodeview copy/paste refactor

This commit is contained in:
itsmattkc
2021-12-29 18:44:14 -08:00
parent a4934f1d47
commit 8daa83b682
7 changed files with 103 additions and 60 deletions
+6 -7
View File
@@ -53,16 +53,15 @@ void XMLNodeData::PostConnect(uint version, MultiUndoCommand *command) const
if (command) {
command->add_child(new NodeEdgeAddCommand(out, con.input));
if (version < 210907) {
/// Deprecated: backwards compatibility only
command->add_child(new NodeSetValueHintCommand(con.input, hint));
}
} else {
Node::ConnectEdge(out, con.input);
}
if (version < 210907) {
/// Deprecated: backwards compatibility only
if (version < 210907) {
/// Deprecated: backwards compatibility only
if (command) {
command->add_child(new NodeSetValueHintCommand(con.input, hint));
} else {
con.input.node()->SetValueHintForInput(con.input.input(), hint, con.input.element());
}
}
+10 -2
View File
@@ -201,7 +201,11 @@ QVector<Node *> NodeCopyPasteService::PasteNodesFromClipboard(NodeGraph *graph,
// Add all nodes to graph
foreach (Node* n, pasted_nodes) {
command->add_child(new NodeAddCommand(graph, n));
if (command) {
command->add_child(new NodeAddCommand(graph, n));
} else {
n->setParent(graph);
}
}
// Make connections
@@ -215,7 +219,11 @@ QVector<Node *> NodeCopyPasteService::PasteNodesFromClipboard(NodeGraph *graph,
for (auto jt=map.cbegin(); jt!=map.cend(); jt++) {
Node *subnode = xml_node_data.node_ptrs.value(jt.key());
if (subnode) {
command->add_child(new NodeSetPositionCommand(subnode, context, jt.value()));
if (command) {
command->add_child(new NodeSetPositionCommand(subnode, context, jt.value()));
} else {
context->SetNodePositionInContext(subnode, jt.value());
}
}
}
}
+66 -49
View File
@@ -181,13 +181,10 @@ void NodeView::Select(const QVector<Node *> &nodes, bool center_view_on_item)
context->Select(nodes);
}
/*
// Center on something
Node *first_item = nodes.isEmpty() ? nullptr : nodes.first();
if (center_view_on_item && first_item) {
centerOn(first_item);
if (center_view_on_item && !nodes.isEmpty()) {
QMetaObject::invokeMethod(this, "CenterOnNode", Qt::QueuedConnection, OLIVE_NS_ARG(Node*, nodes.first()));
}
*/
ConnectSelectionChangedSignal();
@@ -849,6 +846,16 @@ void NodeView::CenterOnItemsBoundingRect()
centerOn(scene_.itemsBoundingRect().center());
}
void NodeView::CenterOnNode(Node *n)
{
foreach (NodeViewContext *ctx, scene_.context_map()) {
if (NodeViewItem* item = ctx->GetItemFromMap(n)) {
centerOn(item);
break;
}
}
}
void NodeView::RepositionMiniMap()
{
if (minimap_->isVisible()) {
@@ -980,34 +987,32 @@ bool NodeView::eventFilter(QObject *object, QEvent *event)
void NodeView::CopyNodesToClipboardInternal(QXmlStreamWriter *writer, const QVector<Node *> &nodes, void *userdata)
{
qDebug() << "STUB!";
/*writer->writeStartElement(QStringLiteral("pos"));
writer->writeStartElement(QStringLiteral("pos"));
for (Node *n : nodes) {
NodeViewItem *item = scene_.item_map().value(n);
QPointF pos = item->GetNodePosition();
Node::Position pos = GetAssumedPositionForSelectedNode(n);
writer->writeStartElement(QStringLiteral("node"));
writer->writeAttribute(QStringLiteral("ptr"), QString::number(reinterpret_cast<quintptr>(n)));
writer->writeTextElement(QStringLiteral("x"), QString::number(pos.x()));
writer->writeTextElement(QStringLiteral("y"), QString::number(pos.y()));
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));
writer->writeEndElement(); // node
}
writer->writeEndElement(); // pos*/
writer->writeEndElement(); // pos
}
void NodeView::PasteNodesFromClipboardInternal(QXmlStreamReader *reader, XMLNodeData &xml_node_data, void *userdata)
{
qDebug() << "STUB!";
/*NodeGraph::PositionMap *map = static_cast<NodeGraph::PositionMap *>(userdata);
Node::PositionMap *map = static_cast<Node::PositionMap *>(userdata);
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("pos")) {
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("node")) {
Node *n = nullptr;
QPointF pos;
Node::Position pos;
XMLAttributeLoop(reader, attr) {
if (attr.name() == QStringLiteral("ptr")) {
@@ -1018,9 +1023,11 @@ void NodeView::PasteNodesFromClipboardInternal(QXmlStreamReader *reader, XMLNode
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("x")) {
pos.setX(reader->readElementText().toDouble());
pos.position.setX(reader->readElementText().toDouble());
} else if (reader->name() == QStringLiteral("y")) {
pos.setY(reader->readElementText().toDouble());
pos.position.setY(reader->readElementText().toDouble());
} else if (reader->name() == QStringLiteral("expanded")) {
pos.expanded = reader->readElementText().toInt();
} else {
reader->skipCurrentElement();
}
@@ -1036,7 +1043,7 @@ void NodeView::PasteNodesFromClipboardInternal(QXmlStreamReader *reader, XMLNode
} else {
reader->skipCurrentElement();
}
}*/
}
}
void NodeView::changeEvent(QEvent *e)
@@ -1070,6 +1077,21 @@ QPointF NodeView::GetEstimatedPositionForContext(NodeViewItem *item, Node *conte
return item->GetNodePosition() - context_offsets_.value(context);
}
Node::Position NodeView::GetAssumedPositionForSelectedNode(Node *node)
{
// Try to find corresponding selected item
foreach (NodeViewContext *ctx, scene_.context_map()) {
NodeViewItem *item = ctx->GetItemFromMap(node);
if (item && item->isSelected()) {
// Good enough
return Node::Position(item->GetNodePosition(), item->IsExpanded());
}
}
// Fallback
return Node::Position();
}
Menu *NodeView::CreateAddMenu(Menu *parent)
{
Menu* add_menu = NodeFactory::CreateMenu(parent);
@@ -1277,7 +1299,7 @@ void NodeView::ShowNodeProperties()
Node *first_node = selected_nodes_.first();
if (NodeGroup *group = dynamic_cast<NodeGroup*>(first_node)) {
qDebug() << "STUB!";
emit NodeGroupOpenRequested(group);
} else {
LabelSelectedNodes();
}
@@ -1290,51 +1312,46 @@ void NodeView::LabelSelectedNodes()
void NodeView::PasteNodesInternal(const QVector<Node *> &duplicate_nodes)
{
/*
// If no graph, do nothing
if (contexts_.isEmpty()) {
return;
}
paste_command_ = new MultiUndoCommand();
// If duplicating nodes, duplicate, otherwise paste
QVector<Node*> new_nodes;
NodeGraph::PositionMap map;
Node::PositionMap map;
if (duplicate_nodes.isEmpty()) {
new_nodes = PasteNodesFromClipboard(graph_, paste_command_, &map);
for (auto it=new_nodes.cbegin(); it!=new_nodes.cend(); it++) {
for (Node *context : qAsConst(contexts_)) {
paste_command_->add_child(new NodeSetPositionCommand(*it, context, map.value(*it), false));
}
}
new_nodes = PasteNodesFromClipboard(nullptr, nullptr, &map);
} else {
new_nodes = Node::CopyDependencyGraph(duplicate_nodes, paste_command_);
new_nodes.resize(selected_nodes_.size());
for (int i=0; i<duplicate_nodes.size(); i++) {
Node *src = duplicate_nodes.at(i);
Node *copy = new_nodes.at(i);
for (Node *context : qAsConst(contexts_)) {
QPointF p = scene_.item_map().value(src)->GetNodePosition();
paste_command_->add_child(new NodeSetPositionCommand(copy, context, p, false));
}
for (int i=0; i<new_nodes.size(); i++) {
Node *og = selected_nodes_.at(i);
Node *copy = og->copy();
Node::CopyInputs(og, copy, false);
map.insert(copy, GetAssumedPositionForSelectedNode(og));
new_nodes[i] = copy;
}
Node::CopyDependencyGraph(selected_nodes_, new_nodes, nullptr);
}
// If no nodes were retrieved, do nothing
if (new_nodes.isEmpty()) {
delete paste_command_;
paste_command_ = nullptr;
return;
if (!new_nodes.isEmpty()) {
QVector<NodeViewItem*> items(new_nodes.size());
for (int i=0; i<new_nodes.size(); i++) {
Node *node = new_nodes.at(i);
NodeViewItem *new_item = new NodeViewItem(node, nullptr);
new_item->SetFlowDirection(scene_.GetFlowDirection());
new_item->SetNodePosition(map.value(node));
scene_.addItem(new_item);
items[i] = new_item;
}
// Attach nodes to cursor
AttachItemsToCursor(items);
}
// Attach nodes to cursor
paste_command_->add_child(new NodeViewAttachNodesToCursor(this, new_nodes));
paste_command_->redo_now();
*/
}
void NodeView::AddContext(Node *n)
+11
View File
@@ -52,6 +52,11 @@ public:
void SetContexts(const QVector<Node *> &nodes);
const QVector<Node*> &GetContexts() const
{
return contexts_;
}
void CloseContextsBelongingToProject(Project *project);
void ClearGraph();
@@ -97,11 +102,15 @@ public slots:
void CenterOnItemsBoundingRect();
void CenterOnNode(olive::Node *n);
signals:
void NodesSelected(const QVector<Node*>& nodes);
void NodesDeselected(const QVector<Node*>& nodes);
void NodeGroupOpenRequested(NodeGroup *group);
protected:
virtual void keyPressEvent(QKeyEvent *event) override;
@@ -141,6 +150,8 @@ private:
QPointF GetEstimatedPositionForContext(NodeViewItem *item, Node *context) const;
Node::Position GetAssumedPositionForSelectedNode(Node *node);
Menu *CreateAddMenu(Menu *parent);
void PositionNewEdge(const QPoint &pos);
+2
View File
@@ -189,6 +189,8 @@ void NodeViewContext::DeleteSelected(NodeViewDeleteCommand *command)
command->AddNode(node->GetNode(), context_);
}
}
UpdateRect();
}
void NodeViewContext::Select(const QVector<Node *> &nodes)
+7 -2
View File
@@ -79,8 +79,7 @@ NodeViewItem::NodeViewItem(Node *node, const QString &input, int element, Node *
setFlag(QGraphicsItem::ItemIsSelectable);
if (context_) {
SetNodePosition(context_->GetNodePositionInContext(node_));
SetExpanded(context_->IsNodeExpandedInContext(node_));
SetNodePosition(context_->GetNodePositionDataInContext(node_));
}
} else {
output_connector_->setVisible(false);
@@ -110,6 +109,12 @@ void NodeViewItem::SetNodePosition(const QPointF &pos)
UpdateNodePosition();
}
void NodeViewItem::SetNodePosition(const Node::Position &pos)
{
SetNodePosition(pos.position);
SetExpanded(pos.expanded);
}
QVector<NodeViewEdge *> NodeViewItem::GetAllEdgesRecursively() const
{
QVector<NodeViewEdge *> list = edges_;
+1
View File
@@ -56,6 +56,7 @@ public:
QPointF GetNodePosition() const;
void SetNodePosition(const QPointF& pos);
void SetNodePosition(const Node::Position& pos);
QVector<NodeViewEdge*> GetAllEdgesRecursively() const;