From 1d3de98158b9409fa63e2e7cf8d3ca8b77f26285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Wed, 12 Aug 2009 09:06:38 +0200 Subject: Added Graph::connections(). This gives a list of all "edges" in the graph, but each edge is specified as a pair of vertices, since you cannot easily know the two AnchorVertex'es of an EdgeData. Added some debug stuff. --- src/gui/graphicsview/qgraph_p.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/gui/graphicsview/qgraph_p.h b/src/gui/graphicsview/qgraph_p.h index 053114b..3abbe3b 100644 --- a/src/gui/graphicsview/qgraph_p.h +++ b/src/gui/graphicsview/qgraph_p.h @@ -30,6 +30,14 @@ public: return column.key(); } + inline Vertex *from() const { + return row.key(); + } + + inline Vertex *to() const { + return column.key(); + } + 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()) { @@ -83,6 +91,15 @@ public: void createEdge(Vertex *first, Vertex *second, EdgeData *data) { // Creates a bidirectional edge +#if 0 + qDebug("Graph::createEdge(): %s", + qPrintable(QString::fromAscii("%1-%2") + .arg(first->toString()).arg(second->toString()))); +#endif + if (edgeData(first, second)) { + qWarning(qPrintable(QString::fromAscii("%1-%2 already has an edge") + .arg(first->toString()).arg(second->toString()))); + } createDirectedEdge(first, second, data); createDirectedEdge(second, first, data); } @@ -90,6 +107,11 @@ public: void removeEdge(Vertex *first, Vertex *second) { // Removes a bidirectional edge +#if 0 + qDebug("Graph::removeEdge(): %s", + qPrintable(QString::fromAscii("%1-%2") + .arg(first->toString()).arg(second->toString()))); +#endif EdgeData *data = edgeData(first, second); removeDirectedEdge(first, second); removeDirectedEdge(second, first); @@ -98,6 +120,11 @@ public: EdgeData *takeEdge(Vertex* first, Vertex* second) { +#if 0 + qDebug("Graph::takeEdge(): %s", + qPrintable(QString::fromAscii("%1-%2") + .arg(first->toString()).arg(second->toString()))); +#endif // Removes a bidirectional edge EdgeData *data = edgeData(first, second); if (data) { @@ -129,6 +156,19 @@ public: return setOfVertices; } + QList > connections() const { + QList > conns; + for (const_iterator it = constBegin(); it != constEnd(); ++it) { + Vertex *from = it.from(); + Vertex *to = it.to(); + // do not return (from,to) *and* (to,from) + if (from < to) { + conns.append(qMakePair(from, to)); + } + } + return conns; + } + QString serializeToDot() { // traversal QString strVertices; QString edges; -- cgit v0.12