Display charts using OGDF and Qt

I need to implement an application that works with displaying a simple graph. One of them is a tree, one looks like an automaton.

I decided to use OGDF in addition to Qt because I need prototyping. But I did not quite understand this ... do I need to implement all the drawing and positioning capabilities (for example, selecting all nodes and the coordinates of the edges from GraphAttributes), or does OGDF provide a good interface for this? (as good as GraphAttributes :: writeGML ())

+6
source share
1 answer

I could not find any nice interface, so I just choose the coordinates myself, however this approach is not ideal, since the layout algorithm returns negative coordinates (relative to the center point of the graph, in my opinion, instead of normal origin). My code looks something like this:

int nodeWidth = 30, nodeHeight = 30, siblingDistance = nodeWidth + nodeHeight; ogdf::TreeLayout treeLayout; treeLayout.siblingDistance(siblingDistance); treeLayout.call(GA); int width = GA.boundingBox().width(), height = GA.boundingBox().height(); ui->graphView->scene()->setSceneRect(QRect(0, 0, width+nodeWidth, height+nodeHeight)); cout << "Scene dimensions: " << GA.boundingBox().width() << " x " << GA.boundingBox().height() << endl; GA.setAllWidth(nodeWidth); GA.setAllHeight(nodeHeight); ogdf::edge e; forall_edges(e,graph){ ogdf::node source = e->source(), target = e->target(); int x1 = GA.x(source), y1 = GA.y(source); int x2 = GA.x(target), y2 = GA.y(target); QPainterPath p; p.moveTo(x1 + nodeWidth/2, y1 + nodeHeight/2); p.lineTo(x2 + nodeWidth/2, y2 + nodeHeight/2); (void) ui->graphView->scene()->addPath(p, QPen(Qt::darkGray), QBrush(Qt::white)); } ogdf::node n; forall_nodes(n, graph) { double x = GA.x(n); double y = GA.y(n); double w = GA.width(n); double h = GA.height(n); QRectF boundingRect(x, y, w, h); cout << x << " : " << y << " : " << endl; QRadialGradient radialGradient(boundingRect.center(), boundingRect.width()); radialGradient.setColorAt(1.0, Qt::lightGray); radialGradient.setColorAt(0.7, QColor(230,230,240)); radialGradient.setColorAt(0.0, Qt::white); (void) ui->graphView->scene()->addEllipse(boundingRect, QPen(Qt::black), QBrush(QRadialGradient(radialGradient))); QGraphicsTextItem *text = ui->graphView->scene()->addText(QString(GA.labelNode(n).cstr())); text->setPos(x, y); } // clear the graph after it has been displayed graph.clear(); 
+7
source

Source: https://habr.com/ru/post/899048/


All Articles