small cleanups, itemAt fix with Map

for/release
dmatetelki 11 years ago
parent de42d1f3ae
commit 228ee6505d

@ -8,12 +8,13 @@
static const double Pi = 3.14159265358979323846264338327950288419717; static const double Pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi; static double TwoPi = 2.0 * Pi;
Edge::Edge(Node *sourceNode, Node *destNode) Edge::Edge(Node* s, Node* d, Edge::ArrowStyle arrowStyle )
: arrowSize(10) : m_arrowStyle(arrowStyle)
, arrowSize(10)
{ {
setAcceptedMouseButtons(0); setAcceptedMouseButtons(0);
source = sourceNode; source = s;
dest = destNode; dest = d;
source->addEdge(this); source->addEdge(this);
dest->addEdge(this); dest->addEdge(this);
adjust(); adjust();
@ -67,7 +68,7 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
if (!source || !dest) if (!source || !dest)
return; return;
QLineF line(sourcePoint, destPoint); const QLineF line(sourcePoint, destPoint);
if (qFuzzyCompare(line.length(), qreal(0.))) if (qFuzzyCompare(line.length(), qreal(0.)))
return; return;
@ -76,20 +77,24 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
painter->drawLine(line); painter->drawLine(line);
// Draw the arrows // Draw the arrows
if (m_arrowStyle == NoArrow)
return;
double angle = ::acos(line.dx() / line.length()); double angle = ::acos(line.dx() / line.length());
if (line.dy() >= 0) if (line.dy() >= 0)
angle = TwoPi - angle; 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->setBrush(Qt::black);
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); 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); painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
} }
}

@ -8,7 +8,15 @@ class Node;
class Edge : public QGraphicsItem class Edge : public QGraphicsItem
{ {
public: public:
Edge(Node *sourceNode, Node *destNode);
enum ArrowStyle {
NoArrow,
ArrowToDestination,
ArrowToSource,
ArrowToBothSides
};
Edge(Node *source, Node *destination, ArrowStyle arrowStyle = NoArrow );
Node *sourceNode() const; Node *sourceNode() const;
Node *destNode() const; Node *destNode() const;
@ -24,6 +32,7 @@ protected:
private: private:
Node *source, *dest; Node *source, *dest;
ArrowStyle m_arrowStyle;
QPointF sourcePoint; QPointF sourcePoint;
QPointF destPoint; QPointF destPoint;

@ -12,10 +12,6 @@
#include <QtGui/QApplication> #include <QtGui/QApplication>
#include <QtCore/QtGlobal>
#include <math.h>
namespace std { namespace std {
template <> template <>
struct hash<float2> struct hash<float2>
@ -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<float2>* graph, QWidget *p) GraphWidget::GraphWidget(Graph<float2>* graph, QWidget *p)
: QGraphicsView(p) : QGraphicsView(p)
@ -56,25 +57,19 @@ void GraphWidget::itemMoved(const QPointF oldPos, const QPointF newPos)
void GraphWidget::updateFromGraph() void GraphWidget::updateFromGraph()
{ {
// for (const auto cit : m_graph) { // scene()->itemAt returns the topmost only which can be an Edge
QMap<float2, Node*> node_map;
for (Graph<float2>::iterator cit = m_graph->begin(); cit != m_graph->end(); ++cit) { for (Graph<float2>::iterator cit = m_graph->begin(); cit != m_graph->end(); ++cit) {
Node *node = new Node(this); Node *node = new Node(this);
scene()->addItem(node); scene()->addItem(node);
node->setPos(cit->x, cit->y); node->setPos(cit->x, cit->y);
node_map.insert(*cit, node);
} }
// for (const auto cit : g) {
for (Graph<float2>::iterator cit = m_graph->begin(); cit != m_graph->end(); ++cit) { for (Graph<float2>::iterator cit = m_graph->begin(); cit != m_graph->end(); ++cit) {
for (const auto cit2 : m_graph->neighboursOf(*cit)) { for (const auto cit2 : m_graph->neighboursOf(*cit)) {
Node* node1 = node_map.find(*cit).value();
float2 v = *cit; Node* node2 = node_map.find(cit2).value();
Node* node1 = dynamic_cast<Node*>(scene()->itemAt(v.x, v.y)); /// @bug itemAt sometimes doesn't work
Q_CHECK_PTR(node1);
float2 v2 = cit2;
Node* node2 = dynamic_cast<Node*>(scene()->itemAt(v2.x, v2.y)); /// @bug itemAt sometimes doesn't work
Q_CHECK_PTR(node2);
scene()->addItem(new Edge(node1, node2)); scene()->addItem(new Edge(node1, node2));
} }
} }

@ -27,19 +27,19 @@ namespace std {
}; };
template <typename T> template <typename T>
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; std::ostringstream out;
out << std::fixed << std::setprecision(n) << a_value; out << std::fixed << std::setprecision(n) << a_value;
return out.str(); return out.str();
} }
}
// inline std::ostream& operator<< (std::ostream& os, const float2& f2) inline std::ostream& operator<< (std::ostream& os, const float2& f2)
// { {
// os << f2.x << "," << f2.y; os << std::to_string_with_precision(f2);
// return os; return os;
// } }
}
float2 float2creator(const std::string& line) float2 float2creator(const std::string& line)
{ {
@ -61,7 +61,8 @@ int main(int argc, char **argv)
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
Graph<float2> g = readGraphFromXML<float2>("graph_example.xml", float2creator); const std::string xml_file = "graph_example.xml";
Graph<float2> g = readGraphFromXML<float2>(xml_file, float2creator);
GraphWidget *widget = new GraphWidget(&g); GraphWidget *widget = new GraphWidget(&g);
widget->updateFromGraph(); widget->updateFromGraph();
@ -77,6 +78,6 @@ int main(int argc, char **argv)
mainWindow.show(); mainWindow.show();
const int app_retval = app.exec(); const int app_retval = app.exec();
writeGraphToXML<float2>(g, "graph_example.xml", float2serializer); writeGraphToXML<float2>(g, xml_file, float2serializer);
return app_retval; return app_retval;
} }

Loading…
Cancel
Save