summaryrefslogtreecommitdiff
path: root/tikzit/src/data/graph.cpp
blob: ba9a4c6bc4ef8ed8e0907d597e87d0c1f6db56d0 (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>
#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;
}