summaryrefslogtreecommitdiff
path: root/tikzit/src/data/graph.cpp
blob: 2480507e5b28b68ba7baa0762f8888c918c00f43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "graph.h"

#include <QTextStream>

Graph::Graph(QObject *parent) : QObject(parent)
{
    _data = new GraphElementData();
    _bbox = QRectF(0,0,0,0);
}

Graph::~Graph()
{
    delete _data;
}

void Graph::removeNode(Node *n) {
    _nodes.removeAll(n);
    inEdges.remove(n);
    outEdges.remove(n);
}

Edge *Graph::addEdge(Node *s, Node *t)
{
    Edge *e = new Edge(s, t, this);
    _edges << e;
    outEdges.insert(s, e);
    inEdges.insert(t, e);
    return e;
}

void Graph::removeEdge(Edge *e)
{
    _edges.removeAll(e);
    outEdges.remove(e->source(), e);
    inEdges.remove(e->target(), 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);
//    [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;
    return str;
}

void Graph::setBbox(const QRectF &bbox)
{
    _bbox = bbox;
}

Node *Graph::addNode() {
    Node *n = new Node(this);
    _nodes << n;
    return n;
}