summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleks Kissinger <aleks0@gmail.com>2020-04-13 16:54:08 +0100
committerAleks Kissinger <aleks0@gmail.com>2020-04-13 16:54:08 +0100
commite333ef9cb80e40637235198f9c113fa8cfe0fceb (patch)
tree2ed815077bad29459a1e078c4381f802815604b6
parent732499eead71b23a7ba551fe788b47a4cf45124e (diff)
split path works
-rw-r--r--src/gui/tikzscene.cpp20
-rw-r--r--src/gui/undocommands.cpp44
-rw-r--r--src/gui/undocommands.h15
3 files changed, 78 insertions, 1 deletions
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<Node*> selNodes;
+ QSet<Edge*> 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<Path*> 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<Path *> &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<Edge*,GraphElementData*> _oldEdgeData;
};
+class SplitPathCommand : public GraphUpdateCommand
+{
+public:
+ explicit SplitPathCommand(TikzScene *scene,
+ const QVector<Path*> &paths,
+ QUndoCommand *parent = nullptr);
+ void undo() override;
+ void redo() override;
+private:
+ QVector<Path*> _paths;
+
+ // keep a copy of the edge lists so they can be added back to each path in undo()
+ QVector<QVector<Edge*>> _edgeLists;
+};
+
#endif // UNDOCOMMANDS_H