summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleks Kissinger <aleks0@gmail.com>2018-08-01 17:23:00 +0200
committerAleks Kissinger <aleks0@gmail.com>2018-08-01 17:23:00 +0200
commit74551d71e128efe0f105b52e84420555b70b9f6d (patch)
treea582fb097fa559bca18313a442440c5e744fe654 /src
parent7f074813bdb2bfca2f7a9933e7c922132197aaba (diff)
added rotation
Diffstat (limited to 'src')
-rw-r--r--src/data/graph.cpp38
-rw-r--r--src/data/graph.h8
-rw-r--r--src/gui/mainmenu.cpp11
-rw-r--r--src/gui/mainmenu.h2
-rw-r--r--src/gui/tikzscene.cpp10
-rw-r--r--src/gui/tikzscene.h1
-rw-r--r--src/gui/undocommands.cpp32
-rw-r--r--src/gui/undocommands.h14
8 files changed, 115 insertions, 1 deletions
diff --git a/src/data/graph.cpp b/src/data/graph.cpp
index 3123485..7d71ceb 100644
--- a/src/data/graph.cpp
+++ b/src/data/graph.cpp
@@ -327,6 +327,44 @@ void Graph::reflectNodes(QSet<Node*> nds, bool horizontal)
}
}
+void Graph::rotateNodes(QSet<Node*> nds, bool clockwise)
+{
+ QRectF bds = boundsForNodes(nds);
+ // QPointF ctr = bds.center();
+ // ctr.setX((float)floor(ctr.x() * 4.0f) / 4.0f);
+ // ctr.setY((float)floor(ctr.y() * 4.0f) / 4.0f);
+ float sign = (clockwise) ? 1.0f : -1.0f;
+
+ QPointF p;
+ // float dx, dy;
+ foreach(Node *n, nds) {
+ p = n->point();
+ // dx = p.x() - ctr.x();
+ // dy = p.y() - ctr.y();
+ n->setPoint(QPointF(sign * p.y(), -sign * p.x()));
+ }
+
+ int newIn, newOut;
+ foreach (Edge *e, _edges) {
+ if (nds.contains(e->source()) && nds.contains(e->target())) {
+ // update angles if necessary. Note that "basic" bends are computed based
+ // on node position, so they don't need to be updated.
+ if (!e->basicBendMode()) {
+ newIn = e->inAngle() - sign * 90;
+ newOut = e->outAngle() - sign * 90;
+
+ // normalise the angle to be within (-180,180]
+ if (newIn > 180) newIn -= 360;
+ else if (newIn <= -180) newIn += 360;
+ if (newOut > 180) newOut -= 360;
+ else if (newOut <= -180) newOut += 360;
+ e->setInAngle(newIn);
+ e->setOutAngle(newOut);
+ }
+ }
+ }
+}
+
void Graph::setBbox(const QRectF &bbox)
{
_bbox = bbox;
diff --git a/src/data/graph.h b/src/data/graph.h
index e9e2218..404fd8c 100644
--- a/src/data/graph.h
+++ b/src/data/graph.h
@@ -103,6 +103,14 @@ public:
* vertically
*/
void reflectNodes(QSet<Node*> nds, bool horizontal);
+
+ /*!
+ * \brief rotateNodes rotates the given set of nodes clockwise or counter-clockwise,
+ * depending on the value of the second parameter.
+ * \param nds a set of nodes to flip
+ * \param clockwose a boolean determining whether to rotate clockwise or counter-clockwise
+ */
+ void rotateNodes(QSet<Node*> nds, bool clockwise);
signals:
public slots:
diff --git a/src/gui/mainmenu.cpp b/src/gui/mainmenu.cpp
index e297625..6bbc95a 100644
--- a/src/gui/mainmenu.cpp
+++ b/src/gui/mainmenu.cpp
@@ -122,6 +122,17 @@ void MainMenu::on_actionReflectVertical_triggered()
tikzit->activeWindow()->tikzScene()->reflectNodes(false);
}
+void MainMenu::on_actionRotateCW_triggered() {
+ if (tikzit->activeWindow() != 0)
+ tikzit->activeWindow()->tikzScene()->rotateNodes(true);
+}
+
+void MainMenu::on_actionRotateCCW_triggered() {
+ if (tikzit->activeWindow() != 0)
+ tikzit->activeWindow()->tikzScene()->rotateNodes(false);
+}
+
+
// Tikz
void MainMenu::on_actionParse_triggered()
{
diff --git a/src/gui/mainmenu.h b/src/gui/mainmenu.h
index 79aafad..b561eab 100644
--- a/src/gui/mainmenu.h
+++ b/src/gui/mainmenu.h
@@ -52,6 +52,8 @@ public slots:
void on_actionDeselect_All_triggered();
void on_actionReflectHorizontal_triggered();
void on_actionReflectVertical_triggered();
+ void on_actionRotateCW_triggered();
+ void on_actionRotateCCW_triggered();
// Tikz
void on_actionParse_triggered();
diff --git a/src/gui/tikzscene.cpp b/src/gui/tikzscene.cpp
index 9eee2fc..8950325 100644
--- a/src/gui/tikzscene.cpp
+++ b/src/gui/tikzscene.cpp
@@ -686,11 +686,19 @@ bool TikzScene::parseTikz(QString tikz)
}
}
-void TikzScene::reflectNodes(bool horizontal) {
+void TikzScene::reflectNodes(bool horizontal)
+{
ReflectNodesCommand *cmd = new ReflectNodesCommand(this, getSelectedNodes(), horizontal);
tikzDocument()->undoStack()->push(cmd);
}
+void TikzScene::rotateNodes(bool clockwise)
+{
+ RotateNodesCommand *cmd = new RotateNodesCommand(this, getSelectedNodes(), clockwise);
+ tikzDocument()->undoStack()->push(cmd);
+}
+
+
void TikzScene::getSelection(QSet<Node *> &selNodes, QSet<Edge *> &selEdges)
{
foreach (QGraphicsItem *gi, selectedItems()) {
diff --git a/src/gui/tikzscene.h b/src/gui/tikzscene.h
index a1d7e26..91f606f 100644
--- a/src/gui/tikzscene.h
+++ b/src/gui/tikzscene.h
@@ -65,6 +65,7 @@ public:
void deselectAll();
bool parseTikz(QString tikz);
void reflectNodes(bool horizontal);
+ void rotateNodes(bool clockwise);
bool enabled() const;
void setEnabled(bool enabled);
int lineNumberForSelection();
diff --git a/src/gui/undocommands.cpp b/src/gui/undocommands.cpp
index 9c52bf3..0cfa0bd 100644
--- a/src/gui/undocommands.cpp
+++ b/src/gui/undocommands.cpp
@@ -465,3 +465,35 @@ void ReflectNodesCommand::redo()
_scene->refreshAdjacentEdges(_nodes.toList());
GraphUpdateCommand::redo();
}
+
+
+RotateNodesCommand::RotateNodesCommand(TikzScene *scene, QSet<Node*> nodes, bool clockwise, QUndoCommand *parent) :
+ GraphUpdateCommand(scene, parent), _nodes(nodes), _clockwise(clockwise)
+{
+}
+
+void RotateNodesCommand::undo()
+{
+ _scene->graph()->rotateNodes(_nodes, !_clockwise);
+ foreach (NodeItem *ni, _scene->nodeItems()) {
+ if (_nodes.contains(ni->node())) {
+ ni->readPos();
+ }
+ }
+
+ _scene->refreshAdjacentEdges(_nodes.toList());
+ GraphUpdateCommand::undo();
+}
+
+void RotateNodesCommand::redo()
+{
+ _scene->graph()->rotateNodes(_nodes, _clockwise);
+ foreach (NodeItem *ni, _scene->nodeItems()) {
+ if (_nodes.contains(ni->node())) {
+ ni->readPos();
+ }
+ }
+
+ _scene->refreshAdjacentEdges(_nodes.toList());
+ GraphUpdateCommand::redo();
+} \ No newline at end of file
diff --git a/src/gui/undocommands.h b/src/gui/undocommands.h
index 0e7a63d..688c2ba 100644
--- a/src/gui/undocommands.h
+++ b/src/gui/undocommands.h
@@ -205,4 +205,18 @@ private:
bool _horizontal;
};
+class RotateNodesCommand : public GraphUpdateCommand
+{
+public:
+ explicit RotateNodesCommand(TikzScene *scene,
+ QSet<Node*> nodes,
+ bool clockwise,
+ QUndoCommand *parent = 0);
+ void undo() override;
+ void redo() override;
+private:
+ QSet<Node*> _nodes;
+ bool _clockwise;
+};
+
#endif // UNDOCOMMANDS_H