ui: graph view edges

Added simple edges to the graph view. Also fixed a memory leak source in QtMainWindow.

fortune cookie message = Good health will be with you.
This commit is contained in:
Manuel Dobusch
2014-07-04 14:11:09 +02:00
parent b1023ba4ac
commit 610aa2eb09
15 changed files with 290 additions and 23 deletions
+2
View File
@@ -18,6 +18,8 @@ add_files(
qt/utility/utilityQt.cpp
qt/utility/utilityQt.h
qt/view/graphElements/QtGraphEdge.cpp
qt/view/graphElements/QtGraphEdge.h
qt/view/graphElements/QtGraphNode.cpp
qt/view/graphElements/QtGraphNode.h
+55 -13
View File
@@ -10,6 +10,7 @@
#include "qt/utility/utilityQt.h"
#include "qt/QtWidgetWrapper.h"
#include "qt/view/graphElements/QtGraphEdge.h"
#include "qt/view/graphElements/QtGraphNode.h"
QtGraphView::QtGraphView(ViewLayout* viewLayout)
@@ -44,7 +45,59 @@ void QtGraphView::initGui()
widget->layout()->addWidget(view);
}
void QtGraphView::addNode(const Vec2i& position, const std::string& name)
std::weak_ptr<GraphNode> QtGraphView::addNode(const Vec2i& position, const std::string& name)
{
QGraphicsView* view = getView();
if (view != NULL)
{
std::shared_ptr<QtGraphNode> node = std::make_shared<QtGraphNode>(position, name);
view->scene()->addItem(node.get());
node->setFlag(QGraphicsItem::ItemIsMovable);
m_nodes.push_back(node);
return node;
}
else
{
LOG_WARNING("Unable to retrieve view from widget");
return std::weak_ptr<QtGraphNode>();
}
}
void QtGraphView::addEdge(const std::weak_ptr<GraphNode>& owner, const std::weak_ptr<GraphNode>& target)
{
QGraphicsView* view = getView();
if (view != NULL)
{
std::shared_ptr<GraphNode> o = owner.lock();
std::shared_ptr<GraphNode> t = target.lock();
if (o != NULL && t != NULL)
{
std::shared_ptr<QtGraphEdge> edge = std::make_shared<QtGraphEdge>(owner, target);
view->scene()->addItem(edge.get());
m_edges.push_back(edge);
o->addOutEdge(edge);
t->addInEdge(edge);
}
else
{
LOG_WARNING("Either the owner or target node could not be locked, make sure they still exist!");
}
}
else
{
LOG_WARNING("Unable to retrieve view from widget");
}
}
QGraphicsView* QtGraphView::getView()
{
QWidget* widget = QtWidgetWrapper::getWidgetOfView(this);
@@ -52,16 +105,5 @@ void QtGraphView::addNode(const Vec2i& position, const std::string& name)
QObjectList children = widget->children();
QGraphicsView* view = widget->findChild<QGraphicsView*>("");
if(view != NULL)
{
QtGraphNode* node = new QtGraphNode(position, name);
view->scene()->addItem(node);
node->setFlag(QGraphicsItem::ItemIsMovable);
}
else
{
LOG_WARNING("Unable to retrieve view from widget");
}
return widget->findChild<QGraphicsView*>("");
}
+7 -1
View File
@@ -3,6 +3,8 @@
#include "component/view/GraphView.h"
class QGraphicsView;
class QtGraphView : public GraphView
{
public:
@@ -13,7 +15,11 @@ public:
virtual void createWidgetWrapper();
virtual void initGui();
virtual void addNode(const Vec2i& position, const std::string& name);
virtual std::weak_ptr<GraphNode> addNode(const Vec2i& position, const std::string& name);
virtual void addEdge(const std::weak_ptr<GraphNode>& owner, const std::weak_ptr<GraphNode>& target);
private:
QGraphicsView* getView();
};
#endif // QT_GRAPH_VIEW_H
+1 -1
View File
@@ -5,7 +5,7 @@
QtMainView::QtMainView()
{
m_window = new QtMainWindow();
m_window = std::make_shared<QtMainWindow>();
m_window->show();
}
+1 -1
View File
@@ -20,7 +20,7 @@ public:
void removeView(View* view);
private:
QtMainWindow* m_window;
std::shared_ptr<QtMainWindow> m_window;
std::vector<View*> m_views;
};
@@ -0,0 +1,59 @@
#include "qt/view/graphElements/QtGraphEdge.h"
#include "qpen.h"
#include "component/view/graphElements/GraphNode.h"
QtGraphEdge::QtGraphEdge(const std::weak_ptr<GraphNode>& owner, const std::weak_ptr<GraphNode>& target)
: m_owner(owner)
, m_target(target)
{
std::shared_ptr<GraphNode> o = owner.lock();
std::shared_ptr<GraphNode> t = target.lock();
if (o != NULL && t != NULL)
{
this->setLine(o->getPosition().x, o->getPosition().y,t->getPosition().x, t->getPosition().y);
}
else
{
LOG_WARNING("Either the owner or the target node could is null.");
}
QPen blackPen(Qt::black);
blackPen.setWidth(3);
this->setPen(blackPen);
}
QtGraphEdge::~QtGraphEdge()
{
}
void QtGraphEdge::ownerMoved()
{
std::shared_ptr<GraphNode> node = m_owner.lock();
if (node != NULL)
{
this->setLine(node->getPosition().x, node->getPosition().y, this->line().x2(), this->line().y2());
}
else
{
LOG_WARNING("Owner node is null.");
}
}
void QtGraphEdge::targetMoved()
{
std::shared_ptr<GraphNode> node = m_target.lock();
if (node != NULL)
{
this->setLine(this->line().x1(), this->line().y1(), node->getPosition().x, node->getPosition().y);
}
else
{
LOG_WARNING("Target node is null.");
}
}
@@ -0,0 +1,28 @@
#ifndef QT_GRAPH_EDGE_H
#define QT_GRAPH_EDGE_H
#include <memory>
#include "qgraphicsitem.h"
#include "component/view/graphElements/GraphEdge.h"
class GraphNode;
class QtGraphEdge:
public GraphEdge,
public QGraphicsLineItem
{
public:
QtGraphEdge(const std::weak_ptr<GraphNode>& owner, const std::weak_ptr<GraphNode>& target);
virtual ~QtGraphEdge();
virtual void ownerMoved();
virtual void targetMoved();
private:
std::weak_ptr<GraphNode> m_owner;
std::weak_ptr<GraphNode> m_target;
};
#endif // QT_GRAPH_EDGE_H
+67 -4
View File
@@ -1,20 +1,83 @@
#include "qt/view/graphElements/QtGraphNode.h"
#include "qgraphicsscene.h"
#include "qgraphicssceneevent.h"
#include "qt/view/graphElements/QtGraphEdge.h"
QtGraphNode::QtGraphNode(const Vec2i& position, const std::string& name)
{
this->setRect(position.x, position.y, 100, 100);
this->setRect(0, 0, 100, 100);
this->setPos(position.x, position.y);
QBrush brush(Qt::lightGray);
this->setBrush(brush);
m_text = new QGraphicsTextItem(this);
m_text->setPos(position.x, position.y);
m_text->setPos(0, 0);
m_text->setPlainText(QString(name.c_str()));
}
QtGraphNode::~QtGraphNode()
{
}
}
Vec2i QtGraphNode::getPosition()
{
return Vec2i(this->scenePos().x(), this->scenePos().y());
}
void QtGraphNode::addOutEdge(const std::shared_ptr<GraphEdge>& edge)
{
m_outEdges.push_back(edge);
}
void QtGraphNode::addInEdge(const std::weak_ptr<GraphEdge>& edge)
{
m_inEdges.push_back(edge);
}
void QtGraphNode::removeOutEdge(const std::shared_ptr<GraphEdge>& edge)
{
std::list<std::shared_ptr<GraphEdge> >::iterator it = m_outEdges.begin();
for(it; it != m_outEdges.end(); it++)
{
if(*it == edge)
{
m_outEdges.erase(it, it);
break;
}
}
}
void QtGraphNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
m_mouseOffset.x = event->pos().x();
m_mouseOffset.y = event->pos().y();
}
void QtGraphNode::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
this->setPos(event->scenePos().x() - m_mouseOffset.x, event->scenePos().y() - m_mouseOffset.y);
Vec2i p(event->scenePos().x(), event->scenePos().y());
std::list<std::shared_ptr<GraphEdge> >::iterator it = m_outEdges.begin();
for(it; it != m_outEdges.end(); it++)
{
(*it)->ownerMoved();
}
std::list<std::weak_ptr<GraphEdge> >::iterator it2 = m_inEdges.begin();
for(it2; it2 != m_inEdges.end(); it2++)
{
std::shared_ptr<GraphEdge> edge = it2->lock();
if(edge.get() != NULL)
{
edge->targetMoved();
}
else
{
m_inEdges.erase(it2, it2);
}
}
}
@@ -7,6 +7,8 @@
#include "component/view/graphElements/GraphNode.h"
class QtGraphEdge;
class QtGraphNode:
public GraphNode,
public QGraphicsRectItem
@@ -15,8 +17,23 @@ public:
QtGraphNode(const Vec2i& position, const std::string& name);
virtual ~QtGraphNode();
virtual Vec2i getPosition();
virtual void addOutEdge(const std::shared_ptr<GraphEdge>& edge);
virtual void addInEdge(const std::weak_ptr<GraphEdge>& edge);
virtual void removeOutEdge(const std::shared_ptr<GraphEdge>& edge);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
private:
QGraphicsTextItem* m_text;
std::list<std::shared_ptr<GraphEdge> > m_outEdges;
std::list<std::weak_ptr<GraphEdge> > m_inEdges;
Vec2i m_mouseOffset;
};
#endif // QT_GRAPH_NODE_H
+2
View File
@@ -25,6 +25,8 @@ add_files(
component/controller/SearchController.cpp
component/controller/SearchController.h
component/view/graphElements/GraphEdge.cpp
component/view/graphElements/GraphEdge.h
component/view/graphElements/GraphNode.cpp
component/view/graphElements/GraphNode.h
+5 -2
View File
@@ -25,9 +25,12 @@ ComponentManager::~ComponentManager()
void ComponentManager::setup()
{
std::shared_ptr<Component> graphComponent = m_componentFactory->createGraphComponent();
GraphView* graphView = graphComponent->getView<GraphView>();
graphView->addNode(Vec2i(-100, -100), "foo");
graphView->addNode(Vec2i(50, 50), "bar");
std::weak_ptr<GraphNode> node0 = graphView->addNode(Vec2i(-50, -50), "foo");
graphView->addEdge(node0, graphView->addNode(Vec2i(100, 100), "bar"));
graphView->addEdge(node0, graphView->addNode(Vec2i(50, -50), "war production co-ordinating commitee"));
m_components.push_back(graphComponent);
std::shared_ptr<Component> codeComponent = m_componentFactory->createCodeComponent();
+12 -1
View File
@@ -1,8 +1,14 @@
#ifndef GRAPH_VIEW_H
#define GRAPH_VIEW_H
#include <list>
#include <memory>
#include "component/view/View.h"
class GraphEdge;
class GraphNode;
class GraphView : public View
{
public:
@@ -11,7 +17,12 @@ public:
virtual std::string getName() const;
virtual void addNode(const Vec2i& position, const std::string& name) = 0;
virtual std::weak_ptr<GraphNode> addNode(const Vec2i& position, const std::string& name) = 0;
virtual void addEdge(const std::weak_ptr<GraphNode>& owner, const std::weak_ptr<GraphNode>& target) = 0;
protected:
std::list<std::shared_ptr<GraphNode> > m_nodes;
std::list<std::weak_ptr<GraphEdge> > m_edges;
};
#endif // GRAPH_VIEW_H
@@ -0,0 +1,9 @@
#include "component/view/graphElements/GraphEdge.h"
GraphEdge::GraphEdge()
{
}
GraphEdge::~GraphEdge()
{
}
@@ -0,0 +1,14 @@
#ifndef GRAPH_EDGE_H
#define GRAPH_EDGE_H
class GraphEdge
{
public:
GraphEdge();
virtual ~GraphEdge();
virtual void ownerMoved() = 0;
virtual void targetMoved() = 0;
};
#endif // GRAPH_EDGE_H
@@ -1,11 +1,22 @@
#ifndef GRAPH_NODE_H
#define GRAPH_NODE_H
#include "utility/math/Vector2.h"
class GraphEdge;
class GraphNode
{
public:
GraphNode();
virtual ~GraphNode();
virtual Vec2i getPosition() = 0;
virtual void addOutEdge(const std::shared_ptr<GraphEdge>& edge) = 0;
virtual void addInEdge(const std::weak_ptr<GraphEdge>& edge) = 0;
virtual void removeOutEdge(const std::shared_ptr<GraphEdge>& edge) = 0;
};
#endif // GRAPH_NODE_H