summaryrefslogtreecommitdiff
path: root/src/gui/graphicsview/qgraph_p.h
diff options
context:
space:
mode:
authorJan-Arve Sæther <jan-arve.saether@nokia.com>2009-06-03 12:58:07 +0200
committerEduardo M. Fleury <eduardo.fleury@openbossa.org>2009-07-22 15:04:23 -0300
commit072a9595a308df9b8e7a547c370ce54ab5af6ed4 (patch)
treebc95701fb6e200b1317cf8d6ea7ae490ecca71ce /src/gui/graphicsview/qgraph_p.h
parentb58d43e407029f19d313cdc2314cf114c572f692 (diff)
downloadqt4-tools-072a9595a308df9b8e7a547c370ce54ab5af6ed4.tar.gz
Some cleanup - improve dot dumper.
The previous dumper relied on traversal of the graph, which meant that it would not output the nodes that were not connected to the "main" graph. Due to this, we cannot rely on traversal, so instead we must iterate through all the vertices of the graph. As an added bonus the code gets simpler :-) There was also a problem with the previous dumper, since it dumped two "digraph" elements into one file. Graphwiz (win32) did not handle that. Instead just dump all the nodes and all the connections, both horizontal and vertical ones. The horizontal and vertical connections are never connected anyway, so the result will be two separate graphs when it is rendered by graphwiz. Also renamed firstVertex to rootVertex and simplified it.
Diffstat (limited to 'src/gui/graphicsview/qgraph_p.h')
-rw-r--r--src/gui/graphicsview/qgraph_p.h110
1 files changed, 47 insertions, 63 deletions
diff --git a/src/gui/graphicsview/qgraph_p.h b/src/gui/graphicsview/qgraph_p.h
index e487eca43d..6f4d7bd896 100644
--- a/src/gui/graphicsview/qgraph_p.h
+++ b/src/gui/graphicsview/qgraph_p.h
@@ -11,18 +11,18 @@ class Graph
public:
Graph() {}
- class iterator {
+ class const_iterator {
public:
- iterator(Graph *graph, bool begin) : g(graph){
+ const_iterator(const Graph *graph, bool begin) : g(graph){
if (begin) {
- row = g->m_graph.begin();
- //test if the graph is empty
- if (row != g->m_graph.end())
- {
- column = (*row)->begin();
- }
+ row = g->m_graph.constBegin();
+ //test if the graph is empty
+ if (row != g->m_graph.constEnd())
+ {
+ column = (*row)->constBegin();
+ }
} else {
- row = g->m_graph.end();
+ row = g->m_graph.constEnd();
}
}
@@ -30,24 +30,24 @@ public:
return column.key();
}
- inline bool operator==(const iterator &o) const { return !(*this != o); }
- inline bool operator!=(const iterator &o) const {
+ inline bool operator==(const const_iterator &o) const { return !(*this != o); }
+ inline bool operator!=(const const_iterator &o) const {
if (row == g->m_graph.end()) {
- return row != o.row;}
- else {
+ return row != o.row;
+ } else {
return row != o.row || column != o.column;
}
}
- inline iterator& operator=(const iterator &o) const { row = o.row; column = o.column; return *this;}
+ inline const_iterator& operator=(const const_iterator &o) const { row = o.row; column = o.column; return *this;}
// prefix
- iterator &operator++() {
- if (row != g->m_graph.end()) {
+ const_iterator &operator++() {
+ if (row != g->m_graph.constEnd()) {
++column;
- if (column == (*row)->end()) {
+ if (column == (*row)->constEnd()) {
++row;
- if (row != g->m_graph.end()) {
- column = (*row)->begin();
+ if (row != g->m_graph.constEnd()) {
+ column = (*row)->constBegin();
}
}
}
@@ -55,17 +55,17 @@ public:
}
private:
- Graph *g;
- Q_TYPENAME QHash<Vertex *, QHash<Vertex *, EdgeData *> * >::iterator row;
- Q_TYPENAME QHash<Vertex *, EdgeData *>::iterator column;
+ const Graph *g;
+ Q_TYPENAME QHash<Vertex *, QHash<Vertex *, EdgeData *> * >::const_iterator row;
+ Q_TYPENAME QHash<Vertex *, EdgeData *>::const_iterator column;
};
- iterator begin() {
- return iterator(this,true);
+ const_iterator constBegin() const {
+ return const_iterator(this,true);
}
- iterator end() {
- return iterator(this,false);
+ const_iterator constEnd() const {
+ return const_iterator(this,false);
}
EdgeData *edgeData(Vertex* first, Vertex* second) {
@@ -112,51 +112,35 @@ public:
userVertex = vertex;
}
+ QSet<Vertex*> vertices() const {
+ QSet<Vertex *> setOfVertices;
+ for (const_iterator it = constBegin(); it != constEnd(); ++it) {
+ setOfVertices.insert(*it);
+ }
+ return setOfVertices;
+ }
+
QString serializeToDot() { // traversal
- QString vertices;
+ QString strVertices;
QString edges;
- QQueue<Vertex *> queue;
- QSet<Vertex *> visited;
- bool ok;
- Vertex *v = firstVertex(&ok);
- if (ok) {
- queue.enqueue(v);
- }
- while (!queue.isEmpty()) {
- Vertex *v = queue.dequeue();
- vertices += QString::fromAscii("%1 [label=\"%2\"]\n").arg(v->toString()).arg(v->toString());
- visited.insert(v);
- // visit it here
- QList<Vertex *> vertices = adjacentVertices(v);
- for (int i = 0; i < vertices.count(); ++i) {
- Vertex *v1 = vertices.at(i);
+
+ QSet<Vertex *> setOfVertices = vertices();
+ for (Q_TYPENAME QSet<Vertex*>::const_iterator it = setOfVertices.begin(); it != setOfVertices.end(); ++it) {
+ Vertex *v = *it;
+ QList<Vertex*> adjacents = adjacentVertices(v);
+ for (int i = 0; i < adjacents.count(); ++i) {
+ Vertex *v1 = adjacents.at(i);
EdgeData *data = edgeData(v, v1);
- edges+=QString::fromAscii("%1->%2 [label=\"[%3,%4]\"]\n").arg(v->toString()).arg(v1->toString()).arg(data->minSize).arg(data->maxSize);
- if (!queue.contains(v1) && !visited.contains(v1)) {
- queue.enqueue(v1);
- } else {
- // a cycle....
- }
+ edges += QString::fromAscii("%1->%2 [label=\"[%3,%4]\"]\n").arg(v->toString()).arg(v1->toString()).arg(data->minSize).arg(data->maxSize);
}
+ strVertices += QString::fromAscii("%1 [label=\"%2\"]\n").arg(v->toString()).arg(v->toString());
}
- return QString::fromAscii("digraph anchorlayout {\nnode [shape=\"rect\"]\n%1%2}").arg(vertices).arg(edges);
+ return QString::fromAscii("%1\n%2\n").arg(strVertices).arg(edges);
}
- Vertex *firstVertex(bool *ok)
+ Vertex *rootVertex() const
{
- if (userVertex) {
- *ok = true;
- return userVertex;
- }
-
- Vertex *v = 0;
- *ok = false;
- Q_TYPENAME Graph::iterator it = Graph::begin();
- if (it != Graph::end()) {
- v = *it;
- *ok = true;
- }
- return v;
+ return userVertex;
}
protected: