Skip to content

Commit

Permalink
Merge 939edf0 into bc75f94
Browse files Browse the repository at this point in the history
  • Loading branch information
Sedictious committed May 8, 2019
2 parents bc75f94 + 939edf0 commit 504e5fd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
46 changes: 44 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 @@ -240,12 +242,49 @@ void GraphView::drawEdges(QPainter& painter) const
for (const Star& star : m_cache) {
for (auto const& ep : star.edges) {
painter.drawLine(ep.second);
if (m_directed) {
drawArrow(painter, QPoint(ep.second.x1(), ep.second.y1()), QPoint(ep.second.x2(), ep.second.y2()));
}
}
}
}
painter.restore();
}

void GraphView::drawArrow(QPainter& painter, QPoint& p1, 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 - m_edgeScale * u0 / 1.5);
float y2 = (y1 - m_edgeScale * u1 / 1.5);

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

// rotate around the pivot point
float th = qDegreesToRadians(33.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 (Qt::darkGray));
}

void GraphView::drawSelectedStar(QPainter& painter, double nodeRadius) const
{
if (m_selectedStar.node.isNull()) {
Expand All @@ -269,7 +308,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);
painter.drawLine(m_selectedStar.xy.x(), m_selectedStar.xy.y(), ep.second.x2(), ep.second.y2());
if (m_directed) {
drawArrow(painter, QPoint(ep.second.x1(), ep.second.y1()), QPoint(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, QPoint& p1, QPoint& p2) const;
void drawNodes(QPainter& painter, double nodeRadius) const;
void drawSelectedStar(QPainter& painter, double nodeRadius) const;

Expand Down

0 comments on commit 504e5fd

Please sign in to comment.