From e333ef9cb80e40637235198f9c113fa8cfe0fceb Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Mon, 13 Apr 2020 16:54:08 +0100 Subject: split path works --- src/gui/tikzscene.cpp | 20 +++++++++++++++++++- src/gui/undocommands.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/gui/undocommands.h | 15 +++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/gui/tikzscene.cpp b/src/gui/tikzscene.cpp index da52277..57ddb48 100644 --- a/src/gui/tikzscene.cpp +++ b/src/gui/tikzscene.cpp @@ -399,7 +399,25 @@ void TikzScene::makePath() void TikzScene::splitPath() { - // TODO: stub + QSet selNodes; + QSet edges; + getSelection(selNodes, edges); + + // if no edges are selected, try to infer edges from nodes + if (edges.isEmpty()) { + foreach(Edge *e, graph()->edges()) { + if (selNodes.contains(e->source()) && selNodes.contains(e->target())) + edges << e; + } + } + + QVector paths; + foreach (Edge *e, edges) { + Path *p = e->path(); + if (p && !paths.contains(p)) paths << p; + } + + _tikzDocument->undoStack()->push(new SplitPathCommand(this, paths)); } void TikzScene::refreshZIndices() diff --git a/src/gui/undocommands.cpp b/src/gui/undocommands.cpp index 87d5a13..9771f44 100644 --- a/src/gui/undocommands.cpp +++ b/src/gui/undocommands.cpp @@ -667,3 +667,47 @@ void MakePathCommand::redo() _scene->refreshZIndices(); GraphUpdateCommand::redo(); } + +SplitPathCommand::SplitPathCommand(TikzScene *scene, + const QVector &paths, + QUndoCommand *parent) : + GraphUpdateCommand(scene, parent), _paths(paths) +{ + foreach (Path *p, paths) _edgeLists << p->edges(); +} + +void SplitPathCommand::undo() +{ + for (int i = 0; i < _paths.length(); ++i) { + Path *p = _paths[i]; + foreach (Edge *e, _edgeLists[i]) { + p->addEdge(e); + } + + _scene->graph()->addPath(p); + + PathItem *pi = new PathItem(p); + _scene->addItem(pi); + _scene->pathItems().insert(p, pi); + pi->readPos(); + } + + _scene->refreshZIndices(); + GraphUpdateCommand::undo(); +} + +void SplitPathCommand::redo() +{ + foreach (Path *p, _paths) { + PathItem *pi = _scene->pathItems()[p]; + _scene->removeItem(pi); + _scene->pathItems().remove(p); + delete pi; + + p->removeEdges(); + _scene->graph()->removePath(p); + } + + _scene->refreshZIndices(); + GraphUpdateCommand::redo(); +} diff --git a/src/gui/undocommands.h b/src/gui/undocommands.h index a1daa07..00b9a40 100644 --- a/src/gui/undocommands.h +++ b/src/gui/undocommands.h @@ -271,4 +271,19 @@ private: QMap _oldEdgeData; }; +class SplitPathCommand : public GraphUpdateCommand +{ +public: + explicit SplitPathCommand(TikzScene *scene, + const QVector &paths, + QUndoCommand *parent = nullptr); + void undo() override; + void redo() override; +private: + QVector _paths; + + // keep a copy of the edge lists so they can be added back to each path in undo() + QVector> _edgeLists; +}; + #endif // UNDOCOMMANDS_H -- cgit v1.2.3