summaryrefslogtreecommitdiff
path: root/tikzit/src/data/graph.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tikzit/src/data/graph.cpp')
-rw-r--r--tikzit/src/data/graph.cpp151
1 files changed, 69 insertions, 82 deletions
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;
}