Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add directed graphs in viewport #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/gui/graphview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <QPainter>
#include <QtMath>

#include "core/trial.h"

Expand All @@ -35,7 +36,8 @@ GraphView::GraphView(ColorMapMgr* cMgr, ExperimentPtr exp, GraphWidget* parent)
m_edgeAttr(-1),
m_edgeCMap(nullptr),
m_edgePen(Qt::gray),
m_nodePen(Qt::black)
m_nodePen(Qt::black),
m_directed(true)
{
m_settingsDlg->init();
setNodeScale(m_settingsDlg->nodeScale());
Expand Down Expand Up @@ -232,20 +234,60 @@ void GraphView::drawEdges(QPainter& painter) const
const Value& value = ep.first.attr(m_edgeAttr);
pen.setColor(m_edgeCMap->colorFromValue(value));
painter.setPen(m_edgePen);
if (m_directed) {
drawArrow(painter, m_edgeCMap->colorFromValue(value), QPoint(ep.second.x1(), ep.second.y1()), QPoint(ep.second.x2(), ep.second.y2()));
}
painter.drawLine(ep.second);
}
}
} else {
painter.setPen(m_edgePen);
for (const Star& star : m_cache) {
for (auto const& ep : star.edges) {
if (m_directed) {
drawArrow(painter, Qt::gray, QPoint(ep.second.x1(), ep.second.y1()), QPoint(ep.second.x2(), ep.second.y2()));
}
painter.drawLine(ep.second);
}
}
}
painter.restore();
}

void GraphView::drawArrow(QPainter& painter, const QColor& col,const QPoint p1, const QPoint p2) const
{
float N = qSqrt(qPow(p2.x() - p1.x(), 2) + qPow(p2.y() - p1.y(), 2));

float u0 = (p2.x() - p1.x()) / N;
float u1 = (p2.y() - p1.y()) / N;

float x1 = p2.x() - m_nodeRadius * u0;
float y1 = p2.y() - m_nodeRadius * u1;

// Temporary helper point
float x2 = (x1 - qPow(1.25f, m_zoomLevel) * m_edgeScale * u0 / 5.0f);
float y2 = (y1 - qPow(1.25f, m_zoomLevel) * m_edgeScale * u1 / 5.0f);

float l1 = qSqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
float l2 = l1 / 2;

// rotate around the pivot point
float th = qDegreesToRadians(28.0f);
float x3 = x1 + (l2/l1) * (x2 - x1) * qCos(th) + (y2 - y1) * qSin(th);
float y3 = y1 + (l2/l1) * (y2 - y1) * qCos(th) - (x2 - x1) * qSin(th);

float x4 = x1 + (l2/l1) * (x2 - x1) * qCos(th) - (y2 - y1) * qSin(th);
float y4 = y1 + (l2/l1) * (y2 - y1) * qCos(th) + (x2 - x1) * qSin(th);

QPainterPath arr_head;
arr_head.moveTo(x1, y1);
arr_head.lineTo(x3, y3);
arr_head.lineTo(x4, y4);
arr_head.lineTo(x1, y1);

painter.fillPath (arr_head, QBrush (col));
}

void GraphView::drawSelectedStar(QPainter& painter, double nodeRadius) const
{
if (m_selectedStar.node.isNull()) {
Expand All @@ -269,7 +311,10 @@ void GraphView::drawSelectedStar(QPainter& painter, double nodeRadius) const
// highlight immediate edges
painter.setPen(QPen(Qt::darkGray, m_edgePen.width() + 3));
for (auto const& ep : m_selectedStar.edges) {
painter.drawLine(ep.second);
if (m_directed) {
drawArrow(painter, Qt::darkGray, QPoint(ep.second.x1(), ep.second.y1()), QPoint(ep.second.x2(), ep.second.y2()));
}
painter.drawLine(m_selectedStar.xy.x(), m_selectedStar.xy.y(), ep.second.x2(), ep.second.y2());
}

// draw selected node
Expand Down
3 changes: 3 additions & 0 deletions src/gui/graphview.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ private slots:
void setEdgeCMap(ColorMap* cmap);

private:
bool directed = true;
GraphSettings* m_settingsDlg;

int m_edgeAttr;
ColorMap* m_edgeCMap;
qreal m_edgeScale;

bool m_directed;
bool m_showNodes;
bool m_showEdges;

Expand All @@ -80,6 +82,7 @@ private slots:

void drawNode(QPainter& painter, const Star& s, double r) const;
void drawEdges(QPainter& painter) const;
void drawArrow(QPainter& painter, const QColor& col,const QPoint p1, const QPoint p2) const;
void drawNodes(QPainter& painter, double nodeRadius) const;
void drawSelectedStar(QPainter& painter, double nodeRadius) const;

Expand Down