GraphWidget has background png

master
dmatetelki 10 years ago
parent 9c85a9459f
commit 91972ea943

@ -14,6 +14,7 @@ Edge::Edge(Node* s, Node* d, Edge::ArrowStyle arrowStyle )
, m_isRoute(false) , m_isRoute(false)
{ {
setAcceptedMouseButtons(0); setAcceptedMouseButtons(0);
setZValue(0);
source = s; source = s;
dest = d; dest = d;
source->addEdge(this); source->addEdge(this);
@ -74,10 +75,13 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
return; return;
// Draw the line itself // Draw the line itself
if (m_isRoute) if (m_isRoute) {
painter->setPen(QPen(Qt::red, 10, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); 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)); painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
setZValue(0);
}
painter->drawLine(line); painter->drawLine(line);

Binary file not shown.

After

Width:  |  Height:  |  Size: 954 B

@ -10,6 +10,7 @@
#include <QtGui/QKeyEvent> #include <QtGui/QKeyEvent>
#include <QtGui/QWheelEvent> #include <QtGui/QWheelEvent>
#include <QtGui/QGraphicsScene> #include <QtGui/QGraphicsScene>
#include <QtGui/QPainter>
#include <QtGui/QApplication> #include <QtGui/QApplication>
@ -57,10 +58,10 @@ QPointF inline QPointFFromfloat2(const float2& f)
return QPointF(f.x, f.y); return QPointF(f.x, f.y);
} }
inline float dist(const float2& v1, const float2& v2) // inline float dist(const float2& v1, const float2& v2)
{ // {
return sqrt(pow((v2.x - v1.x),2) + pow((v2.y - v1.y),2)); // return sqrt(pow((v2.x - v1.x),2) + pow((v2.y - v1.y),2));
} // }
QList<Edge*> calculateShortestRoute(const QGraphicsScene* scene, QList<Edge*> calculateShortestRoute(const QGraphicsScene* scene,
const Graph<float2>* graph, const Graph<float2>* graph,
@ -94,22 +95,35 @@ QList<Edge*> calculateShortestRoute(const QGraphicsScene* scene,
} // anonym namespace } // anonym namespace
GraphWidget::GraphWidget(Graph<float2>* graph, QWidget *p) GraphWidget::GraphWidget(Graph<float2>* graph, const QString& png_file, QWidget *p)
: QGraphicsView(p) : QGraphicsView(p)
, m_graph(graph) , 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); if (m_background != 0)
s->setItemIndexMethod(QGraphicsScene::NoIndex); delete m_background;
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"));
} }
void GraphWidget::itemMoved(const QPointF oldPos, const QPointF newPos) 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 source_pos = float2FromQPointF(selectedNode->pos());
const float2 destination_pos = float2FromQPointF(nodeUnderMouse->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; return;
m_graph->removeEdge(source_pos, destination_pos); 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 source_pos = float2FromQPointF(selectedNode->pos());
const float2 destination_pos = float2FromQPointF(nodeUnderMouse->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; return;
scene()->addItem(new Edge(selectedNode, nodeUnderMouse)); scene()->addItem(new Edge(selectedNode, nodeUnderMouse));
@ -271,24 +285,11 @@ void GraphWidget::wheelEvent(QWheelEvent *e)
scaleView(pow((double)2, -e->delta() / 240.0)); 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 const int h = m_background->height();
QRectF scene_rect = this->sceneRect(); const int w = m_background->width();
QRectF rightShadow(scene_rect.right(), scene_rect.top() + 5, 5, scene_rect.height()); painter->drawPixmap(QRectF(0, 0, w, h), *m_background, QRectF(0, 0, w, h));
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);
} }
void GraphWidget::scaleView(qreal scaleFactor) void GraphWidget::scaleView(qreal scaleFactor)

@ -7,13 +7,15 @@ class float2;
template<class T> class Graph; template<class T> class Graph;
class Node; class Node;
class Edge; class Edge;
// class QPixmap;
class GraphWidget : public QGraphicsView class GraphWidget : public QGraphicsView
{ {
Q_OBJECT Q_OBJECT
public: public:
GraphWidget(Graph<float2>* graph, QWidget *parent = 0); GraphWidget(Graph<float2>* graph, const QString& png_file, QWidget *parent = 0);
~GraphWidget();
void itemMoved(const QPointF oldPos, const QPointF newPos); void itemMoved(const QPointF oldPos, const QPointF newPos);
@ -38,6 +40,7 @@ private:
void removeEdge(Node* selectedNode, Node* nodeUnderMouse); void removeEdge(Node* selectedNode, Node* nodeUnderMouse);
Graph<float2>* m_graph; Graph<float2>* m_graph;
QPixmap* m_background;
QList<Edge*> m_route; QList<Edge*> m_route;
}; };

@ -79,7 +79,9 @@ int main(int argc, char **argv)
const std::string xml_file = "graph_example.xml"; const std::string xml_file = "graph_example.xml";
Graph<float2> g = readGraphFromXML<float2>(xml_file, float2creator); Graph<float2> g = readGraphFromXML<float2>(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(); widget->updateFromGraph();
QMainWindow mainWindow; QMainWindow mainWindow;

@ -16,7 +16,7 @@ Node::Node(GraphWidget *graphWidget)
setFlag(ItemIsSelectable); setFlag(ItemIsSelectable);
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
setZValue(1); // higher than the edge setZValue(10); // higher than the edge
} }
void Node::addEdge(Edge *edge) void Node::addEdge(Edge *edge)

Loading…
Cancel
Save