From b392859bb192a2e02aec09f2eacf5ecdf44fdfe4 Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Sun, 22 Jan 2017 17:59:44 +0100 Subject: tikz output done --- tikzit/src/data/edge.cpp | 5 ++ tikzit/src/data/edge.h | 1 + tikzit/src/data/graph.cpp | 151 ++++++++++++++++------------------- tikzit/src/data/graphelementdata.cpp | 5 ++ tikzit/src/data/graphelementdata.h | 1 + tikzit/src/test/testtikzoutput.cpp | 57 +++++++++++++ tikzit/src/test/testtikzoutput.h | 3 + 7 files changed, 141 insertions(+), 82 deletions(-) diff --git a/tikzit/src/data/edge.cpp b/tikzit/src/data/edge.cpp index 67e2061..9068a1c 100644 --- a/tikzit/src/data/edge.cpp +++ b/tikzit/src/data/edge.cpp @@ -67,4 +67,9 @@ void Edge::setEdgeNode(Node *edgeNode) _edgeNode = edgeNode; } +bool Edge::hasEdgeNode() +{ + return _edgeNode != 0; +} + diff --git a/tikzit/src/data/edge.h b/tikzit/src/data/edge.h index fba30a3..9655e98 100644 --- a/tikzit/src/data/edge.h +++ b/tikzit/src/data/edge.h @@ -27,6 +27,7 @@ public: Node *edgeNode() const; void setEdgeNode(Node *edgeNode); + bool hasEdgeNode(); signals: diff --git a/tikzit/src/data/graph.cpp b/tikzit/src/data/graph.cpp index 2480507..1985f8a 100644 --- a/tikzit/src/data/graph.cpp +++ b/tikzit/src/data/graph.cpp @@ -73,88 +73,75 @@ QString Graph::tikz() { QString str; QTextStream code(&str); -// [NSMutableString -// stringWithFormat:@"\\begin{tikzpicture}%@\n", -// [[self data] tikzList]]; - -// if ([self hasBoundingBox]) { -// [code appendFormat:@"\t\\path [use as bounding box] (%@,%@) rectangle (%@,%@);\n", -// [NSNumber numberWithFloat:boundingBox.origin.x], -// [NSNumber numberWithFloat:boundingBox.origin.y], -// [NSNumber numberWithFloat:boundingBox.origin.x + boundingBox.size.width], -// [NSNumber numberWithFloat:boundingBox.origin.y + boundingBox.size.height]]; -// } - -// // NSArray *sortedNodeList = [[nodes allObjects] -// // sortedArrayUsingSelector:@selector(compareTo:)]; -// //NSMutableDictionary *nodeNames = [NSMutableDictionary dictionary]; - -// if ([nodes count] > 0) [code appendFormat:@"\t\\begin{pgfonlayer}{nodelayer}\n"]; - -// int i = 0; -// for (Node *n in nodes) { -// [n updateData]; -// [n setName:[NSString stringWithFormat:@"%d", i]]; -// [code appendFormat:@"\t\t\\node %@ (%d) at (%@, %@) {%@};\n", -// [[n data] tikzList], -// i, -// formatFloat([n point].x, 4), -// formatFloat([n point].y, 4), -// [n label] -// ]; -// i++; -// } - -// if ([nodes count] > 0) [code appendFormat:@"\t\\end{pgfonlayer}\n"]; -// if ([edges count] > 0) [code appendFormat:@"\t\\begin{pgfonlayer}{edgelayer}\n"]; - -// NSString *nodeStr; -// for (Edge *e in edges) { -// [e updateData]; - -// if ([e hasEdgeNode]) { -// nodeStr = [NSString stringWithFormat:@"node%@{%@} ", -// [[[e edgeNode] data] tikzList], -// [[e edgeNode] label] -// ]; -// } else { -// nodeStr = @""; -// } - -// NSString *edata = [[e data] tikzList]; - -// NSString *srcAnchor; -// NSString *tgtAnchor; - -// if ([[e sourceAnchor] isEqual:@""]) { -// srcAnchor = @""; -// } else { -// srcAnchor = [NSString stringWithFormat:@".%@", [e sourceAnchor]]; -// } - -// if ([[e targetAnchor] isEqual:@""]) { -// tgtAnchor = @""; -// } else { -// tgtAnchor = [NSString stringWithFormat:@".%@", [e targetAnchor]]; -// } - -// [code appendFormat:@"\t\t\\draw%@ (%@%@) to %@(%@%@);\n", -// ([edata isEqual:@""]) ? @"" : [NSString stringWithFormat:@" %@", edata], -// [[e source] name], -// srcAnchor, -// nodeStr, -// ([e source] == [e target]) ? @"" : [[e target] name], -// tgtAnchor -// ]; -// } - -// if ([edges count] > 0) [code appendFormat:@"\t\\end{pgfonlayer}\n"]; - -// [code appendString:@"\\end{tikzpicture}"]; - -// [graphLock unlock]; - -// return code; + + 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; } diff --git a/tikzit/src/data/graphelementdata.cpp b/tikzit/src/data/graphelementdata.cpp index ef16be2..3ce72c7 100644 --- a/tikzit/src/data/graphelementdata.cpp +++ b/tikzit/src/data/graphelementdata.cpp @@ -163,3 +163,8 @@ QString GraphElementData::tikz() { code.flush(); return str; } + +bool GraphElementData::isEmpty() +{ + return _properties.isEmpty(); +} diff --git a/tikzit/src/data/graphelementdata.h b/tikzit/src/data/graphelementdata.h index 42f63ba..1139a00 100644 --- a/tikzit/src/data/graphelementdata.h +++ b/tikzit/src/data/graphelementdata.h @@ -53,6 +53,7 @@ public: void add(GraphElementProperty p); QString tikz(); + bool isEmpty(); signals: public slots: diff --git a/tikzit/src/test/testtikzoutput.cpp b/tikzit/src/test/testtikzoutput.cpp index 8b14bd3..f086786 100644 --- a/tikzit/src/test/testtikzoutput.cpp +++ b/tikzit/src/test/testtikzoutput.cpp @@ -1,8 +1,12 @@ #include "testtikzoutput.h" #include "graphelementproperty.h" #include "graphelementdata.h" +#include "graph.h" +#include "tikzgraphassembler.h" #include +#include +#include void TestTikzOutput::escape() { @@ -38,3 +42,56 @@ void TestTikzOutput::data() d.unsetAtom("bar"); QVERIFY(d.tikz() == ""); } + +void TestTikzOutput::graphEmpty() +{ + Graph *g = new Graph(); + + QString tikz = + "\\begin{tikzpicture}\n" + "\\end{tikzpicture}\n"; + QVERIFY(g->tikz() == tikz); + + delete g; +} + +void TestTikzOutput::graphFromTikz() +{ + Graph *g = new Graph(); + TikzGraphAssembler ga(g); + + QString tikz = + "\\begin{tikzpicture}\n" + "\t\\path [use as bounding box] (-1.5,-1.5) rectangle (1.5,1.5);\n" + "\t\\begin{pgfonlayer}{nodelayer}\n" + "\t\t\\node [style=white dot] (0) at (-1, -1) {};\n" + "\t\t\\node [style=white dot] (1) at (0, 1) {};\n" + "\t\t\\node [style=white dot] (2) at (1, -1) {};\n" + "\t\\end{pgfonlayer}\n" + "\t\\begin{pgfonlayer}{edgelayer}\n" + "\t\t\\draw [style=diredge] (1) to (2);\n" + "\t\t\\draw [style=diredge] (2.center) to (0);\n" + "\t\t\\draw [style=diredge] (0) to ();\n" + "\t\\end{pgfonlayer}\n" + "\\end{tikzpicture}\n"; + bool res = ga.parse(tikz); + QVERIFY2(res, "parsed successfully"); + QVERIFY2(g->tikz() == tikz, "produced matching tikz"); + + delete g; +} + +void TestTikzOutput::graphBbox() +{ + Graph *g = new Graph(); + g->setBbox(QRectF(QPointF(-0.75, -0.5), QPointF(0.25, 1))); + + QString tikz = + "\\begin{tikzpicture}\n" + "\t\\path [use as bounding box] (-0.75,-0.5) rectangle (0.25,1);\n" + "\\end{tikzpicture}\n"; + QVERIFY(g->tikz() == tikz); + + + delete g; +} diff --git a/tikzit/src/test/testtikzoutput.h b/tikzit/src/test/testtikzoutput.h index 24c594e..dff1db1 100644 --- a/tikzit/src/test/testtikzoutput.h +++ b/tikzit/src/test/testtikzoutput.h @@ -9,6 +9,9 @@ class TestTikzOutput : public QObject private slots: void escape(); void data(); + void graphBbox(); + void graphEmpty(); + void graphFromTikz(); }; #endif // TESTTIKZOUTPUT_H -- cgit v1.2.3