From 228ee6505de34669080f11cf0c4a700e36cd3004 Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Tue, 5 Aug 2014 12:39:16 +0200 Subject: [PATCH] small cleanups, itemAt fix with Map --- lib/qtgraph/edge.cpp | 37 +++++++++++++++++++++---------------- lib/qtgraph/edge.hpp | 11 ++++++++++- lib/qtgraph/graphwidget.cpp | 25 ++++++++++--------------- lib/qtgraph/main.cpp | 19 ++++++++++--------- 4 files changed, 51 insertions(+), 41 deletions(-) diff --git a/lib/qtgraph/edge.cpp b/lib/qtgraph/edge.cpp index bb5afa8..cb12fd8 100644 --- a/lib/qtgraph/edge.cpp +++ b/lib/qtgraph/edge.cpp @@ -8,12 +8,13 @@ static const double Pi = 3.14159265358979323846264338327950288419717; static double TwoPi = 2.0 * Pi; -Edge::Edge(Node *sourceNode, Node *destNode) - : arrowSize(10) +Edge::Edge(Node* s, Node* d, Edge::ArrowStyle arrowStyle ) + : m_arrowStyle(arrowStyle) + , arrowSize(10) { setAcceptedMouseButtons(0); - source = sourceNode; - dest = destNode; + source = s; + dest = d; source->addEdge(this); dest->addEdge(this); adjust(); @@ -67,7 +68,7 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) if (!source || !dest) return; - QLineF line(sourcePoint, destPoint); + const QLineF line(sourcePoint, destPoint); if (qFuzzyCompare(line.length(), qreal(0.))) return; @@ -76,20 +77,24 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) painter->drawLine(line); // Draw the arrows + if (m_arrowStyle == NoArrow) + return; + double angle = ::acos(line.dx() / line.length()); if (line.dy() >= 0) angle = TwoPi - angle; - QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize, - cos(angle + Pi / 3) * arrowSize); - QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, - cos(angle + Pi - Pi / 3) * arrowSize); - QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, - cos(angle - Pi / 3) * arrowSize); - QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, - cos(angle - Pi + Pi / 3) * arrowSize); - painter->setBrush(Qt::black); - painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); - painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); + + if (m_arrowStyle == ArrowToSource || m_arrowStyle == ArrowToBothSides) { + const QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize, cos(angle + Pi / 3) * arrowSize); + const QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, cos(angle + Pi - Pi / 3) * arrowSize); + painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); + } + + if (m_arrowStyle == ArrowToDestination || m_arrowStyle == ArrowToBothSides) { + const QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, cos(angle - Pi / 3) * arrowSize); + const QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, cos(angle - Pi + Pi / 3) * arrowSize); + painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); + } } \ No newline at end of file diff --git a/lib/qtgraph/edge.hpp b/lib/qtgraph/edge.hpp index a6b13ab..2a5968a 100644 --- a/lib/qtgraph/edge.hpp +++ b/lib/qtgraph/edge.hpp @@ -8,7 +8,15 @@ class Node; class Edge : public QGraphicsItem { public: - Edge(Node *sourceNode, Node *destNode); + + enum ArrowStyle { + NoArrow, + ArrowToDestination, + ArrowToSource, + ArrowToBothSides + }; + + Edge(Node *source, Node *destination, ArrowStyle arrowStyle = NoArrow ); Node *sourceNode() const; Node *destNode() const; @@ -24,6 +32,7 @@ protected: private: Node *source, *dest; + ArrowStyle m_arrowStyle; QPointF sourcePoint; QPointF destPoint; diff --git a/lib/qtgraph/graphwidget.cpp b/lib/qtgraph/graphwidget.cpp index 16c2495..d69f458 100644 --- a/lib/qtgraph/graphwidget.cpp +++ b/lib/qtgraph/graphwidget.cpp @@ -12,10 +12,6 @@ #include -#include - -#include - namespace std { template <> struct hash @@ -28,6 +24,11 @@ namespace std { }; } +// for the map +bool operator< (const float2& v1, const float2& v2) { + return length(v1) < length(v2); +} + GraphWidget::GraphWidget(Graph* graph, QWidget *p) : QGraphicsView(p) @@ -56,25 +57,19 @@ void GraphWidget::itemMoved(const QPointF oldPos, const QPointF newPos) void GraphWidget::updateFromGraph() { -// for (const auto cit : m_graph) { + // scene()->itemAt returns the topmost only which can be an Edge + QMap node_map; for (Graph::iterator cit = m_graph->begin(); cit != m_graph->end(); ++cit) { Node *node = new Node(this); scene()->addItem(node); node->setPos(cit->x, cit->y); + node_map.insert(*cit, node); } -// for (const auto cit : g) { for (Graph::iterator cit = m_graph->begin(); cit != m_graph->end(); ++cit) { for (const auto cit2 : m_graph->neighboursOf(*cit)) { - - float2 v = *cit; - Node* node1 = dynamic_cast(scene()->itemAt(v.x, v.y)); /// @bug itemAt sometimes doesn't work - Q_CHECK_PTR(node1); - - float2 v2 = cit2; - Node* node2 = dynamic_cast(scene()->itemAt(v2.x, v2.y)); /// @bug itemAt sometimes doesn't work - Q_CHECK_PTR(node2); - + Node* node1 = node_map.find(*cit).value(); + Node* node2 = node_map.find(cit2).value(); scene()->addItem(new Edge(node1, node2)); } } diff --git a/lib/qtgraph/main.cpp b/lib/qtgraph/main.cpp index 107bdcf..df76d42 100644 --- a/lib/qtgraph/main.cpp +++ b/lib/qtgraph/main.cpp @@ -27,19 +27,19 @@ namespace std { }; template - std::string to_string_with_precision(const T a_value, const int n = 6) + inline std::string to_string_with_precision(const T a_value, const int n = 6) { std::ostringstream out; out << std::fixed << std::setprecision(n) << a_value; return out.str(); } -} -// inline std::ostream& operator<< (std::ostream& os, const float2& f2) -// { -// os << f2.x << "," << f2.y; -// return os; -// } + inline std::ostream& operator<< (std::ostream& os, const float2& f2) + { + os << std::to_string_with_precision(f2); + return os; + } +} float2 float2creator(const std::string& line) { @@ -61,7 +61,8 @@ int main(int argc, char **argv) qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - Graph g = readGraphFromXML("graph_example.xml", float2creator); + const std::string xml_file = "graph_example.xml"; + Graph g = readGraphFromXML(xml_file, float2creator); GraphWidget *widget = new GraphWidget(&g); widget->updateFromGraph(); @@ -77,6 +78,6 @@ int main(int argc, char **argv) mainWindow.show(); const int app_retval = app.exec(); - writeGraphToXML(g, "graph_example.xml", float2serializer); + writeGraphToXML(g, xml_file, float2serializer); return app_retval; }