diff --git a/lib/qtgraph/edge.cpp b/lib/qtgraph/edge.cpp index dfecb99..399b769 100644 --- a/lib/qtgraph/edge.cpp +++ b/lib/qtgraph/edge.cpp @@ -14,6 +14,7 @@ Edge::Edge(Node* s, Node* d, Edge::ArrowStyle arrowStyle ) , m_isRoute(false) { setAcceptedMouseButtons(0); + setZValue(0); source = s; dest = d; source->addEdge(this); @@ -74,10 +75,13 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) return; // Draw the line itself - if (m_isRoute) + if (m_isRoute) { painter->setPen(QPen(Qt::red, 10, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); - else + setZValue(1); + } else { painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + setZValue(0); + } painter->drawLine(line); diff --git a/lib/qtgraph/first_map.png b/lib/qtgraph/first_map.png new file mode 100644 index 0000000..70b536d Binary files /dev/null and b/lib/qtgraph/first_map.png differ diff --git a/lib/qtgraph/graphwidget.cpp b/lib/qtgraph/graphwidget.cpp index ce6d0d0..6582f08 100644 --- a/lib/qtgraph/graphwidget.cpp +++ b/lib/qtgraph/graphwidget.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -57,10 +58,10 @@ QPointF inline QPointFFromfloat2(const float2& f) return QPointF(f.x, f.y); } -inline float dist(const float2& v1, const float2& v2) -{ - return sqrt(pow((v2.x - v1.x),2) + pow((v2.y - v1.y),2)); -} +// inline float dist(const float2& v1, const float2& v2) +// { +// return sqrt(pow((v2.x - v1.x),2) + pow((v2.y - v1.y),2)); +// } QList calculateShortestRoute(const QGraphicsScene* scene, const Graph* graph, @@ -94,22 +95,35 @@ QList calculateShortestRoute(const QGraphicsScene* scene, } // anonym namespace -GraphWidget::GraphWidget(Graph* graph, QWidget *p) +GraphWidget::GraphWidget(Graph* graph, const QString& png_file, QWidget *p) : QGraphicsView(p) , m_graph(graph) + , m_background(0) +{ + m_background = new QPixmap(png_file); + if (m_background->isNull()) + throw std::invalid_argument("Could not load image from png file."); + + const int h = m_background->height(); + const int w = m_background->width(); + + QGraphicsScene *s = new QGraphicsScene(this); + s->setItemIndexMethod(QGraphicsScene::NoIndex); + s->setSceneRect(0, 0, w, h); + setScene(s); + + setCacheMode(CacheBackground); + setViewportUpdateMode(BoundingRectViewportUpdate); + setRenderHint(QPainter::Antialiasing); + setTransformationAnchor(AnchorUnderMouse); + setMinimumSize(w, h); + setWindowTitle(tr("Graph")); +} + +GraphWidget::~GraphWidget() { - QGraphicsScene *s = new QGraphicsScene(this); - s->setItemIndexMethod(QGraphicsScene::NoIndex); - s->setSceneRect(-200, -200, 400, 400); - setScene(s); - - setCacheMode(CacheBackground); - setViewportUpdateMode(BoundingRectViewportUpdate); - setRenderHint(QPainter::Antialiasing); - setTransformationAnchor(AnchorUnderMouse); - scale(qreal(0.8), qreal(0.8)); - setMinimumSize(400, 400); - setWindowTitle(tr("Elastic Nodes")); + if (m_background != 0) + delete m_background; } void GraphWidget::itemMoved(const QPointF oldPos, const QPointF newPos) @@ -210,7 +224,7 @@ void GraphWidget::removeEdge(Node* selectedNode, Node* nodeUnderMouse) { const float2 source_pos = float2FromQPointF(selectedNode->pos()); const float2 destination_pos = float2FromQPointF(nodeUnderMouse->pos()); - if (!m_graph->connected(source_pos, destination_pos)) + if (!connected(*m_graph, source_pos, destination_pos)) return; m_graph->removeEdge(source_pos, destination_pos); @@ -243,7 +257,7 @@ void GraphWidget::insertEdge(Node* selectedNode, Node* nodeUnderMouse) { const float2 source_pos = float2FromQPointF(selectedNode->pos()); const float2 destination_pos = float2FromQPointF(nodeUnderMouse->pos()); - if (m_graph->connected(source_pos, destination_pos)) + if (connected(*m_graph, source_pos, destination_pos)) return; scene()->addItem(new Edge(selectedNode, nodeUnderMouse)); @@ -271,24 +285,11 @@ void GraphWidget::wheelEvent(QWheelEvent *e) scaleView(pow((double)2, -e->delta() / 240.0)); } -void GraphWidget::drawBackground(QPainter *painter, const QRectF &r) +void GraphWidget::drawBackground(QPainter* painter, const QRectF& /*r*/) { - // Shadow - QRectF scene_rect = this->sceneRect(); - QRectF rightShadow(scene_rect.right(), scene_rect.top() + 5, 5, scene_rect.height()); - QRectF bottomShadow(scene_rect.left() + 5, scene_rect.bottom(), scene_rect.width(), 5); - if (rightShadow.intersects(r) || rightShadow.contains(r)) - painter->fillRect(rightShadow, Qt::darkGray); - if (bottomShadow.intersects(r) || bottomShadow.contains(r)) - painter->fillRect(bottomShadow, Qt::darkGray); - - // Fill - QLinearGradient gradient(scene_rect.topLeft(), scene_rect.bottomRight()); - gradient.setColorAt(0, Qt::white); - gradient.setColorAt(1, Qt::lightGray); - painter->fillRect(r.intersect(scene_rect), gradient); - painter->setBrush(Qt::NoBrush); - painter->drawRect(scene_rect); + const int h = m_background->height(); + const int w = m_background->width(); + painter->drawPixmap(QRectF(0, 0, w, h), *m_background, QRectF(0, 0, w, h)); } void GraphWidget::scaleView(qreal scaleFactor) diff --git a/lib/qtgraph/graphwidget.hpp b/lib/qtgraph/graphwidget.hpp index b3c3f3d..3e25d50 100644 --- a/lib/qtgraph/graphwidget.hpp +++ b/lib/qtgraph/graphwidget.hpp @@ -7,13 +7,15 @@ class float2; template class Graph; class Node; class Edge; +// class QPixmap; class GraphWidget : public QGraphicsView { Q_OBJECT public: - GraphWidget(Graph* graph, QWidget *parent = 0); + GraphWidget(Graph* graph, const QString& png_file, QWidget *parent = 0); + ~GraphWidget(); void itemMoved(const QPointF oldPos, const QPointF newPos); @@ -38,6 +40,7 @@ private: void removeEdge(Node* selectedNode, Node* nodeUnderMouse); Graph* m_graph; + QPixmap* m_background; QList m_route; }; diff --git a/lib/qtgraph/main.cpp b/lib/qtgraph/main.cpp index 0339b75..68d021b 100644 --- a/lib/qtgraph/main.cpp +++ b/lib/qtgraph/main.cpp @@ -79,7 +79,9 @@ int main(int argc, char **argv) const std::string xml_file = "graph_example.xml"; Graph g = readGraphFromXML(xml_file, float2creator); - GraphWidget *widget = new GraphWidget(&g); + const std::string png_file = "first_map.png"; + + GraphWidget *widget = new GraphWidget(&g, QString::fromStdString(png_file)); widget->updateFromGraph(); QMainWindow mainWindow; diff --git a/lib/qtgraph/node.cpp b/lib/qtgraph/node.cpp index d20bcc3..167e985 100644 --- a/lib/qtgraph/node.cpp +++ b/lib/qtgraph/node.cpp @@ -16,7 +16,7 @@ Node::Node(GraphWidget *graphWidget) setFlag(ItemIsSelectable); setAcceptHoverEvents(true); setCacheMode(DeviceCoordinateCache); - setZValue(1); // higher than the edge + setZValue(10); // higher than the edge } void Node::addEdge(Edge *edge)