diff options
Diffstat (limited to 'src/gui/graphicsview/qgraph_p.h')
-rw-r--r-- | src/gui/graphicsview/qgraph_p.h | 110 |
1 files changed, 47 insertions, 63 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<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: |