summaryrefslogtreecommitdiff
path: root/src/data/graph.cpp
diff options
context:
space:
mode:
authorAleks Kissinger <aleks0@gmail.com>2018-01-04 16:00:52 +0100
committerAleks Kissinger <aleks0@gmail.com>2018-01-04 16:00:52 +0100
commit738ecbd5fad2b46836bfd6a94aeebf165ae2bbca (patch)
treedf04709807cc9ec8481a3ebc7d80ac25e5b2f457 /src/data/graph.cpp
parent0421a96749743868554d44585050b1b3d04864d2 (diff)
relocated source code to the root
Diffstat (limited to 'src/data/graph.cpp')
-rw-r--r--src/data/graph.cpp172
1 files changed, 172 insertions, 0 deletions
diff --git a/src/data/graph.cpp b/src/data/graph.cpp
new file mode 100644
index 0000000..ba9a4c6
--- /dev/null
+++ b/src/data/graph.cpp
@@ -0,0 +1,172 @@
+#include "graph.h"
+
+#include <QTextStream>
+#include <QSet>
+#include <QtAlgorithms>
+#include <QDebug>
+#include <algorithm>
+
+Graph::Graph(QObject *parent) : QObject(parent)
+{
+ _data = new GraphElementData(this);
+ _bbox = QRectF(0,0,0,0);
+}
+
+Graph::~Graph()
+{
+}
+
+// add a node. The graph claims ownership.
+void Graph::addNode(Node *n) {
+ n->setParent(this);
+ _nodes << n;
+}
+
+void Graph::addNode(Node *n, int index)
+{
+ n->setParent(this);
+ _nodes.insert(index, n);
+}
+
+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.removeOne(n);
+}
+
+
+void Graph::addEdge(Edge *e)
+{
+ e->setParent(this);
+ _edges << e;
+}
+
+void Graph::addEdge(Edge *e, int index)
+{
+ e->setParent(this);
+ _edges.insert(index, e);
+}
+
+void Graph::removeEdge(Edge *e)
+{
+ // the edge 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.
+ _edges.removeOne(e);
+}
+
+GraphElementData *Graph::data() const
+{
+ return _data;
+}
+
+void Graph::setData(GraphElementData *data)
+{
+ delete _data;
+ _data = data;
+}
+
+const QVector<Node*> &Graph::nodes()
+{
+ return _nodes;
+}
+
+const QVector<Edge*> &Graph::edges()
+{
+ return _edges;
+}
+
+QRectF Graph::bbox() const
+{
+ return _bbox;
+}
+
+bool Graph::hasBbox() {
+ return !(_bbox == QRectF(0,0,0,0));
+}
+
+void Graph::clearBbox() {
+ _bbox = QRectF(0,0,0,0);
+}
+
+QString Graph::tikz()
+{
+ QString str;
+ QTextStream code(&str);
+
+ code << "\\begin{tikzpicture}" << _data->tikz() << "\n";
+ if (hasBbox()) {
+ code << "\t\\path [use as bounding box] ("
+ << _bbox.topLeft().x() << "," << _bbox.topLeft().y()
+ << ") rectangle ("
+ << _bbox.bottomRight().x() << "," << _bbox.bottomRight().y()
+ << ");\n";
+ }
+
+ if (!_nodes.isEmpty())
+ code << "\t\\begin{pgfonlayer}{nodelayer}\n";
+
+ Node *n;
+ foreach (n, _nodes) {
+ code << "\t\t\\node ";
+
+ if (!n->data()->isEmpty())
+ code << n->data()->tikz() << " ";
+
+ code << "(" << n->name() << ") at ("
+ << n->point().x() << ", " << n->point().y()
+ << ") {" << n->label() << "};\n";
+ }
+
+ if (!_nodes.isEmpty())
+ code << "\t\\end{pgfonlayer}\n";
+
+ if (!_edges.isEmpty())
+ code << "\t\\begin{pgfonlayer}{edgelayer}\n";
+
+
+ Edge *e;
+ foreach (e, _edges) {
+ code << "\t\t\\draw ";
+
+ if (!e->data()->isEmpty())
+ code << e->data()->tikz() << " ";
+
+ code << "(" << e->source()->name();
+ if (e->sourceAnchor() != "")
+ code << "." << e->sourceAnchor();
+ code << ") to ";
+
+ if (e->hasEdgeNode()) {
+ code << "node ";
+ if (!e->edgeNode()->data()->isEmpty())
+ code << e->edgeNode()->data()->tikz() << " ";
+ code << "{" << e->edgeNode()->label() << "} ";
+ }
+
+ if (e->source() == e->target()) {
+ code << "()";
+ } else {
+ code << "(" << e->target()->name();
+ if (e->targetAnchor() != "")
+ code << "." << e->targetAnchor();
+ code << ")";
+ }
+
+ code << ";\n";
+ }
+
+ if (!_edges.isEmpty())
+ code << "\t\\end{pgfonlayer}\n";
+
+ code << "\\end{tikzpicture}\n";
+
+ code.flush();
+ return str;
+}
+
+void Graph::setBbox(const QRectF &bbox)
+{
+ _bbox = bbox;
+}
+
+