diff options
author | Jesus Sanchez-Palencia <jesus.palencia@openbossa.org> | 2009-05-28 11:45:33 -0300 |
---|---|---|
committer | Eduardo M. Fleury <eduardo.fleury@openbossa.org> | 2009-07-22 15:02:39 -0300 |
commit | a3b7580944d22b4f3f59bc6a4828a7b01a26407f (patch) | |
tree | b2599b536723ac8dbde6dafc025f121f065dc699 /src/gui/graphicsview/qgraph_p.h | |
parent | 5be62fbd822206c625cab4892ab485923dc79a00 (diff) | |
download | qt4-tools-a3b7580944d22b4f3f59bc6a4828a7b01a26407f.tar.gz |
QGraph: Adding the graph structure
This file was originally included by Alexis Menard <alexis.menard@nokia.com>
but now it has been fully updated to the new QGraphicsAnchorLayout needs.
Signed-off-by: Anselmo Lacerda S. de Melo <anselmo.melo@openbossa.org>
Signed-off-by: Caio Marcelo de Oliveira Filho <caio.oliveira@openbossa.org>
Signed-off-by: Eduardo M. Fleury <eduardo.fleury@openbossa.org>
Signed-off-by: Jesus Sanchez-Palencia <jesus.palencia@openbossa.org>
Diffstat (limited to 'src/gui/graphicsview/qgraph_p.h')
-rw-r--r-- | src/gui/graphicsview/qgraph_p.h | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/gui/graphicsview/qgraph_p.h b/src/gui/graphicsview/qgraph_p.h new file mode 100644 index 0000000000..4a6bc8fd23 --- /dev/null +++ b/src/gui/graphicsview/qgraph_p.h @@ -0,0 +1,179 @@ +#include <QtCore/QHash> +#include <QtCore/QQueue> +#include <QtCore/QString> +#include <QtCore/QDebug> + +#include <float.h> + +template <typename Vertex, typename EdgeData> +class Graph +{ +public: + Graph() {} + + class iterator { + public: + iterator(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(); + } + } else { + row = g->m_graph.end(); + } + } + + inline Vertex *operator*() { + return column.key(); + } + + inline bool operator==(const iterator &o) const { return !(*this != o); } + inline bool operator!=(const iterator &o) const { + if (row == g->m_graph.end()) { + 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;} + + // prefix + iterator &operator++() { + if (row != g->m_graph.end()) { + ++column; + if (column == (*row)->end()) { + ++row; + if (row != g->m_graph.end()) { + column = (*row)->begin(); + } + } + } + return *this; + } + + private: + Graph *g; + Q_TYPENAME QHash<Vertex *, QHash<Vertex *, EdgeData *> * >::iterator row; + Q_TYPENAME QHash<Vertex *, EdgeData *>::iterator column; + }; + + iterator begin() { + return iterator(this,true); + } + + iterator end() { + return iterator(this,false); + } + + EdgeData *edgeData(Vertex *first, Vertex *second) { + return m_graph.value(first)->value(second); + } + + void createEdge(Vertex *first, Vertex *second, EdgeData *data) + { + // Creates a bidirectional edge + createDirectedEdge(first, second, data); + createDirectedEdge(second, first, data); + } + + void removeEdge(Vertex *first, Vertex *second) + { + // Creates a bidirectional edge + EdgeData *data = edgeData(first, second); + if (data) delete data; + removeDirectedEdge(first, second); + removeDirectedEdge(second, first); + } + + QList<Vertex *> adjacentVertices(Vertex *vertex) const + { + QHash<Vertex *, EdgeData *> *row = m_graph.value(vertex); + QList<Vertex *> l; + if (row) + l = row->keys(); + return l; + } + + void setRootVertex(Vertex *vertex) + { + userVertex = vertex; + } + + QString serializeToDot() { // traversal + QString vertices; + 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); + 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.... + } + } + } + return QString::fromAscii("digraph anchorlayout {\nnode [shape=\"rect\"]\n%1%2}").arg(vertices).arg(edges); + } + + Vertex *firstVertex(bool *ok) + { + 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; + } + +protected: + void createDirectedEdge(Vertex *from, Vertex *to, EdgeData *data) + { + QHash<Vertex *, EdgeData *> *adjacentToFirst = m_graph.value(from); + if (!adjacentToFirst) { + adjacentToFirst = new QHash<Vertex *, EdgeData *>(); + m_graph.insert(from, adjacentToFirst); + } + adjacentToFirst->insert(to, data); + } + + void removeDirectedEdge(Vertex *from, Vertex *to) + { + QHash<Vertex *, EdgeData *> *adjacentToFirst = m_graph.value(from); + adjacentToFirst->remove(to); + if (adjacentToFirst->isEmpty()) { + //nobody point to 'from' so we can remove it from the graph + QHash<Vertex *, EdgeData *> *adjacentToFirst = m_graph.take(from); + delete adjacentToFirst; + delete from; + } + } + +private: + Vertex *userVertex; + + QHash<Vertex *, QHash<Vertex *, EdgeData *> *> m_graph; +}; |