summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleks Kissinger <aleks0@gmail.com>2017-12-22 13:39:13 +0000
committerAleks Kissinger <aleks0@gmail.com>2017-12-22 13:39:13 +0000
commitd8db367f6a4e49776834c82b092700dbda56cecc (patch)
treef06b386f2a160a8f0b6e8c10e6f4353b3fed3d47
parent9d8317cd593d47911bb6b2e6fb8ef0077e24ae36 (diff)
edge bend undo works
-rw-r--r--tikzit/src/data/graph.cpp5
-rw-r--r--tikzit/src/gui/tikzscene.cpp20
-rw-r--r--tikzit/src/gui/tikzscene.h5
-rw-r--r--tikzit/src/gui/undocommands.cpp43
-rw-r--r--tikzit/src/gui/undocommands.h21
5 files changed, 90 insertions, 4 deletions
diff --git a/tikzit/src/data/graph.cpp b/tikzit/src/data/graph.cpp
index 1985f8a..de3eb08 100644
--- a/tikzit/src/data/graph.cpp
+++ b/tikzit/src/data/graph.cpp
@@ -4,16 +4,17 @@
Graph::Graph(QObject *parent) : QObject(parent)
{
- _data = new GraphElementData();
+ _data = new GraphElementData(this);
_bbox = QRectF(0,0,0,0);
}
Graph::~Graph()
{
- delete _data;
}
void Graph::removeNode(Node *n) {
+ // the node itself is not deleted, as it may still be referenced in an undo command. It will
+ // be deleted when graph is, via QObject memory management.
_nodes.removeAll(n);
inEdges.remove(n);
outEdges.remove(n);
diff --git a/tikzit/src/gui/tikzscene.cpp b/tikzit/src/gui/tikzscene.cpp
index b502f84..619d916 100644
--- a/tikzit/src/gui/tikzscene.cpp
+++ b/tikzit/src/gui/tikzscene.cpp
@@ -89,7 +89,13 @@ void TikzScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
if (_modifyEdgeItem != 0) {
// disable rubber band drag, which will clear the selection
views()[0]->setDragMode(QGraphicsView::NoDrag);
- qDebug() << "Got a control point";
+
+ // store for undo purposes
+ Edge *e = _modifyEdgeItem->edge();
+ _oldBend = e->bend();
+ _oldInAngle = e->inAngle();
+ _oldOutAngle = e->outAngle();
+ _oldWeight = e->weight();
} else {
// since we are not dragging a control point, process the click normally
views()[0]->setDragMode(QGraphicsView::RubberBandDrag);
@@ -124,6 +130,7 @@ void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
case ToolPalette::SELECT:
if (_modifyEdgeItem != 0) {
Edge *e = _modifyEdgeItem->edge();
+
// dragging a control point
QPointF src = toScreen(e->source()->point());
QPointF targ = toScreen(e->target()->point());
@@ -222,7 +229,16 @@ void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
case ToolPalette::SELECT:
if (_modifyEdgeItem != 0) {
// finished dragging a control point
- // TODO
+ Edge *e = _modifyEdgeItem->edge();
+
+ if (_oldWeight != e->weight() ||
+ _oldBend != e->bend() ||
+ _oldInAngle != e->inAngle() ||
+ _oldOutAngle != e->outAngle())
+ {
+ EdgeBendCommand *cmd = new EdgeBendCommand(this, e, _oldWeight, _oldBend, _oldInAngle, _oldOutAngle);
+ _tikzDocument->undoStack()->push(cmd);
+ }
_modifyEdgeItem = 0;
} else {
diff --git a/tikzit/src/gui/tikzscene.h b/tikzit/src/gui/tikzscene.h
index cc7f4e4..db95d88 100644
--- a/tikzit/src/gui/tikzscene.h
+++ b/tikzit/src/gui/tikzscene.h
@@ -45,7 +45,12 @@ private:
QVector<EdgeItem*> _edgeItems;
EdgeItem *_modifyEdgeItem;
bool _firstControlPoint;
+
QMap<Node*,QPointF> _oldNodePositions;
+ float _oldWeight;
+ int _oldBend;
+ int _oldInAngle;
+ int _oldOutAngle;
};
#endif // TIKZSCENE_H
diff --git a/tikzit/src/gui/undocommands.cpp b/tikzit/src/gui/undocommands.cpp
index c28611f..54741c8 100644
--- a/tikzit/src/gui/undocommands.cpp
+++ b/tikzit/src/gui/undocommands.cpp
@@ -35,3 +35,46 @@ void MoveCommand::redo()
_scene->refreshAdjacentEdges(_newNodePositions.keys());
}
+
+EdgeBendCommand::EdgeBendCommand(TikzScene *scene, Edge *edge,
+ float oldWeight, int oldBend,
+ int oldInAngle, int oldOutAngle) :
+ _scene(scene), _edge(edge),
+ _oldWeight(oldWeight), _oldBend(oldBend),
+ _oldInAngle(oldInAngle), _oldOutAngle(oldOutAngle)
+{
+ _newWeight = edge->weight();
+ _newBend = edge->bend();
+ _newInAngle = edge->inAngle();
+ _newOutAngle = edge->outAngle();
+}
+
+void EdgeBendCommand::undo()
+{
+ _edge->setWeight(_oldWeight);
+ _edge->setBend(_oldBend);
+ _edge->setInAngle(_oldInAngle);
+ _edge->setOutAngle(_oldOutAngle);
+
+ foreach(EdgeItem *ei, _scene->edgeItems()) {
+ if (ei->edge() == _edge) {
+ ei->readPos();
+ break;
+ }
+ }
+}
+
+void EdgeBendCommand::redo()
+{
+ _edge->setWeight(_newWeight);
+ _edge->setBend(_newBend);
+ _edge->setInAngle(_newInAngle);
+ _edge->setOutAngle(_newOutAngle);
+
+ foreach(EdgeItem *ei, _scene->edgeItems()) {
+ if (ei->edge() == _edge) {
+ ei->readPos();
+ break;
+ }
+ }
+}
diff --git a/tikzit/src/gui/undocommands.h b/tikzit/src/gui/undocommands.h
index bdf9ad2..bb6a8e9 100644
--- a/tikzit/src/gui/undocommands.h
+++ b/tikzit/src/gui/undocommands.h
@@ -24,4 +24,25 @@ private:
QMap<Node*,QPointF> _newNodePositions;
};
+class EdgeBendCommand : public QUndoCommand
+{
+public:
+ explicit EdgeBendCommand(TikzScene *scene, Edge *edge,
+ float oldWeight, int oldBend,
+ int oldInAngle, int oldOutAngle);
+ void undo() override;
+ void redo() override;
+private:
+ TikzScene *_scene;
+ Edge *_edge;
+ float _oldWeight;
+ int _oldBend;
+ int _oldInAngle;
+ int _oldOutAngle;
+ float _newWeight;
+ int _newBend;
+ int _newInAngle;
+ int _newOutAngle;
+};
+
#endif // UNDOCOMMANDS_H