From 072a9595a308df9b8e7a547c370ce54ab5af6ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Wed, 3 Jun 2009 12:58:07 +0200 Subject: 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. --- src/gui/graphicsview/qgraph_p.h | 110 ++++++++++------------- src/gui/graphicsview/qgraphicsanchorlayout.cpp | 6 +- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 6 +- 3 files changed, 52 insertions(+), 70 deletions(-) diff --git a/src/gui/graphicsview/qgraph_p.h b/src/gui/graphicsview/qgraph_p.h index e487eca..6f4d7bd 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 * >::iterator row; - Q_TYPENAME QHash::iterator column; + const Graph *g; + Q_TYPENAME QHash * >::const_iterator row; + Q_TYPENAME QHash::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 vertices() const { + QSet setOfVertices; + for (const_iterator it = constBegin(); it != constEnd(); ++it) { + setOfVertices.insert(*it); + } + return setOfVertices; + } + QString serializeToDot() { // traversal - QString vertices; + QString strVertices; QString edges; - QQueue queue; - QSet 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 vertices = adjacentVertices(v); - for (int i = 0; i < vertices.count(); ++i) { - Vertex *v1 = vertices.at(i); + + QSet setOfVertices = vertices(); + for (Q_TYPENAME QSet::const_iterator it = setOfVertices.begin(); it != setOfVertices.end(); ++it) { + Vertex *v = *it; + QList 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: diff --git a/src/gui/graphicsview/qgraphicsanchorlayout.cpp b/src/gui/graphicsview/qgraphicsanchorlayout.cpp index ba3ba2a..1a72ace 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout.cpp @@ -254,10 +254,10 @@ void QGraphicsAnchorLayout::dumpGraph() if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) qWarning("Could not write to %s", file.fileName().toLocal8Bit().constData()); + QString str = QString::fromAscii("digraph anchorlayout {\nnode [shape=\"rect\"]\n%1}"); QString dotContents = d->graph[0].serializeToDot(); - file.write(dotContents.toLocal8Bit()); - dotContents = d->graph[1].serializeToDot(); - file.write(dotContents.toLocal8Bit()); + dotContents += d->graph[1].serializeToDot(); + file.write(str.arg(dotContents).toLocal8Bit()); file.close(); } diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index 644ed6b..add2db3 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -681,8 +681,7 @@ void QGraphicsAnchorLayoutPrivate::findPaths(Orientation orientation) QSet visited; - bool ok; - AnchorVertex *root = graph[orientation].firstVertex(&ok); + AnchorVertex *root = graph[orientation].rootVertex(); graphPaths[orientation].insert(root, GraphPath()); @@ -889,8 +888,7 @@ void QGraphicsAnchorLayoutPrivate::calculateVertexPositions( QSet visited; // Get root vertex - bool ok; - AnchorVertex *root = graph[orientation].firstVertex(&ok); + AnchorVertex *root = graph[orientation].rootVertex(); qreal widgetMargin; qreal layoutMargin; -- cgit v0.12