ui: GraphView can set active tokenId on node clicked

By now the graph displays the active node, without neighbours or anything, and clicking a node sets the active token id to the clicked nodes id.

fortune cookie message = Be prepared to accept an exciting opportunity in the future.
This commit is contained in:
Manuel Dobusch
2014-07-07 16:53:44 +02:00
parent e38914887d
commit 0f60a9a556
14 changed files with 215 additions and 62 deletions
+76 -46
View File
@@ -15,6 +15,8 @@
QtGraphView::QtGraphView(ViewLayout* viewLayout)
: GraphView(viewLayout)
, m_rebuildGraph(std::bind(&QtGraphView::doRebuildGraph, this, std::placeholders::_1, std::placeholders::_2))
, m_clear(std::bind(&QtGraphView::doClear, this))
{
}
@@ -45,56 +47,14 @@ void QtGraphView::initGui()
widget->layout()->addWidget(view);
}
std::weak_ptr<GraphNode> QtGraphView::addNode(const Vec2i& position, const std::string& name)
void QtGraphView::rebuildGraph(const std::vector<DummyNode>& nodes, const std::vector<DummyEdge>& edges)
{
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>();
}
m_rebuildGraph(nodes, edges);
}
void QtGraphView::addEdge(const std::weak_ptr<GraphNode>& owner, const std::weak_ptr<GraphNode>& target)
void QtGraphView::clear()
{
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");
}
m_clear();
}
QGraphicsView* QtGraphView::getView()
@@ -107,3 +67,73 @@ QGraphicsView* QtGraphView::getView()
return widget->findChild<QGraphicsView*>("");
}
void QtGraphView::doRebuildGraph(const std::vector<DummyNode>& nodes, const std::vector<DummyEdge>& edges)
{
doClear();
QGraphicsView* view = getView();
if (view != NULL)
{
for(unsigned int i = 0; i < nodes.size(); i++)
{
std::shared_ptr<GraphNode> newNode = findOrCreateNode(view, nodes[i]);
}
}
else
{
LOG_WARNING("Failed to get QGraphicsView");
}
}
void QtGraphView::doClear()
{
m_nodes.clear();
m_edges.clear();
}
std::shared_ptr<GraphNode> QtGraphView::findOrCreateNode(QGraphicsView* view, const DummyNode& node)
{
std::shared_ptr<GraphNode> result;
result = findNode(node);
if(result == NULL)
{
result = createNode(view, node);
}
return result;
}
std::shared_ptr<GraphNode> QtGraphView::findNode(const DummyNode& node)
{
std::list<std::shared_ptr<GraphNode>>::iterator it = m_nodes.begin();
for(it; it != m_nodes.end(); it++)
{
if((*it)->getTokenId() == node.tokenId)
{
return *it;
}
}
return NULL;
}
std::shared_ptr<GraphNode> QtGraphView::createNode(QGraphicsView* view, const DummyNode& node)
{
if(view != NULL)
{
std::shared_ptr<QtGraphNode> newNode = std::make_shared<QtGraphNode>(node.position, node.name, node.tokenId);
view->scene()->addItem(newNode.get());
m_nodes.push_back(newNode);
return newNode;
}
else
{
LOG_WARNING("Received pointer to QGraphicsView was NULL. No node created.");
}
}
+17 -2
View File
@@ -1,8 +1,12 @@
#ifndef QT_GRAPH_VIEW_H
#define QT_GRAPH_VIEW_H
#include "qt/utility/QtThreadedFunctor.h"
#include "component/view/GraphView.h"
struct DummyEdge;
struct DummyNode;
class QGraphicsView;
class QtGraphView : public GraphView
@@ -15,11 +19,22 @@ public:
virtual void createWidgetWrapper();
virtual void initGui();
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);
virtual void rebuildGraph(const std::vector<DummyNode>& nodes, const std::vector<DummyEdge>& edges);
virtual void clear();
private:
QGraphicsView* getView();
void doRebuildGraph(const std::vector<DummyNode>& nodes, const std::vector<DummyEdge>& edges);
void doClear();
std::shared_ptr<GraphNode> findOrCreateNode(QGraphicsView* view, const DummyNode& node);
std::shared_ptr<GraphNode> findNode(const DummyNode& node);
std::shared_ptr<GraphNode> createNode(QGraphicsView* view, const DummyNode& node);
QtThreadedFunctor<const std::vector<DummyNode>&, const std::vector<DummyEdge>&> m_rebuildGraph;
QtThreadedFunctor<void> m_clear;
};
#endif // QT_GRAPH_VIEW_H
+10 -1
View File
@@ -5,7 +5,8 @@
#include "qt/view/graphElements/QtGraphEdge.h"
QtGraphNode::QtGraphNode(const Vec2i& position, const std::string& name)
QtGraphNode::QtGraphNode(const Vec2i& position, const std::string& name, const Id tokenId)
: GraphNode(tokenId)
{
this->setRect(0, 0, 100, 100);
this->setPos(position.x, position.y);
@@ -21,6 +22,11 @@ QtGraphNode::~QtGraphNode()
{
}
std::string QtGraphNode::getName()
{
return m_text->toPlainText().toStdString();
}
Vec2i QtGraphNode::getPosition()
{
return Vec2i(this->scenePos().x(), this->scenePos().y());
@@ -53,6 +59,9 @@ void QtGraphNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
m_mouseOffset.x = event->pos().x();
m_mouseOffset.y = event->pos().y();
MessageActivateToken message(m_tokenId);
message.dispatch();
}
void QtGraphNode::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+3 -1
View File
@@ -3,6 +3,7 @@
#include "qgraphicsitem.h"
#include "utility/messaging/type/MessageActivateToken.h"
#include "utility/math/Vector2.h"
#include "component/view/graphElements/GraphNode.h"
@@ -14,9 +15,10 @@ class QtGraphNode:
public QGraphicsRectItem
{
public:
QtGraphNode(const Vec2i& position, const std::string& name);
QtGraphNode(const Vec2i& position, const std::string& name, const Id tokenId);
virtual ~QtGraphNode();
virtual std::string getName();
virtual Vec2i getPosition();
virtual void addOutEdge(const std::shared_ptr<GraphEdge>& edge);
+2 -2
View File
@@ -27,9 +27,9 @@ void ComponentManager::setup()
std::shared_ptr<Component> graphComponent = m_componentFactory->createGraphComponent();
GraphView* graphView = graphComponent->getView<GraphView>();
std::weak_ptr<GraphNode> node0 = graphView->addNode(Vec2i(-50, -50), "foo");
/*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"));
graphView->addEdge(node0, graphView->addNode(Vec2i(50, -50), "war production co-ordinating commitee"));*/
m_components.push_back(graphComponent);
@@ -1,11 +1,12 @@
#include "component/controller/GraphController.h"
#include "component/view/graphElements/GraphEdge.h"
#include "component/view/graphElements/GraphNode.h"
#include "component/view/GraphView.h"
#include "data/access/GraphAccess.h"
#include "utility/logging/logging.h"
GraphController::GraphController(std::shared_ptr<GraphAccess> graphAccess)
: m_graphAccess(graphAccess)
{
@@ -15,6 +16,24 @@ GraphController::~GraphController()
{
}
void GraphController::handleMessage(MessageActivateToken* message)
{
GraphView* view = getView();
if (view != NULL)
{
std::string name = m_graphAccess->getNameForNodeWithId(message->tokenId);
DummyNode node(name, message->tokenId, Vec2i(0,0));
std::vector<DummyNode> nodes;
nodes.push_back(node);
std::vector<DummyEdge> edges;
view->rebuildGraph(nodes, edges);
}
}
GraphView* GraphController::getView()
{
return Controller::getView<GraphView>();
@@ -1,18 +1,24 @@
#ifndef GRAPH_CONTROLLER_H
#define GRAPH_CONTROLLER_H
#include "utility/messaging/MessageListener.h"
#include "utility/messaging/type/MessageActivateToken.h"
#include "component/controller/Controller.h"
class GraphView;
class GraphAccess;
class GraphController: public Controller
class GraphController:
public Controller,
public MessageListener<MessageActivateToken>
{
public:
GraphController(std::shared_ptr<GraphAccess> graphAccess);
~GraphController();
private:
virtual void handleMessage(MessageActivateToken* message);
GraphView* getView();
std::shared_ptr<GraphAccess> m_graphAccess;
+5 -2
View File
@@ -6,6 +6,8 @@
#include "component/view/View.h"
struct DummyEdge;
struct DummyNode;
class GraphEdge;
class GraphNode;
@@ -17,8 +19,9 @@ public:
virtual std::string getName() const;
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;
virtual void rebuildGraph(const std::vector<DummyNode>& nodes, const std::vector<DummyEdge>& edges) = 0;
virtual void clear() = 0;
protected:
std::list<std::shared_ptr<GraphNode> > m_nodes;
@@ -1,6 +1,10 @@
#ifndef GRAPH_EDGE_H
#define GRAPH_EDGE_H
#include <memory>
class GraphNode;
class GraphEdge
{
public:
@@ -11,4 +15,18 @@ public:
virtual void targetMoved() = 0;
};
// temporary data structure for (visual) graph creation process
struct DummyEdge
{
public:
DummyEdge(const std::weak_ptr<GraphNode> o, const std::weak_ptr<GraphNode> t)
: owner(o)
, target(t)
{
}
std::weak_ptr<GraphNode> owner;
std::weak_ptr<GraphNode> target;
};
#endif // GRAPH_EDGE_H
@@ -1,9 +1,15 @@
#include "component/view/graphElements/GraphNode.h"
GraphNode::GraphNode()
GraphNode::GraphNode(const Id tokenId)
: m_tokenId(tokenId)
{
}
GraphNode::~GraphNode()
{
}
Id GraphNode::getTokenId()
{
return m_tokenId;
}
@@ -1,22 +1,47 @@
#ifndef GRAPH_NODE_H
#define GRAPH_NODE_H
#include <memory>
#include "utility/math/Vector2.h"
#include "utility/types.h"
class GraphEdge;
class GraphNode
{
public:
GraphNode();
GraphNode(const Id tokenId);
virtual ~GraphNode();
virtual std::string getName() = 0;
Id getTokenId();
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;
protected:
Id m_tokenId;
};
struct DummyNode
{
public:
DummyNode(const std::string& n, const Id tId, const Vec2i& p)
: name(n)
, tokenId(tId)
, position(p)
{
}
std::string name;
Id tokenId;
Vec2i position;
std::weak_ptr<GraphNode> actualNode;
};
#endif // GRAPH_NODE_H
+11 -2
View File
@@ -13,12 +13,13 @@ public:
Vector2();
Vector2(const T& x, const T& y);
Vector2(const VectorBase<T, 2>& vector);
Vector2(const Vector2<T>& vector);
virtual ~Vector2();
T getValue(const unsigned int index) const;
void setValue(const unsigned int index, const T& value);
T& operator[](const unsigned int index);
T operator[](const unsigned int index);
Property<T> x;
Property<T> y;
@@ -62,6 +63,14 @@ Vector2<T>::Vector2(const VectorBase<T, 2>& vector)
{
}
template<class T>
Vector2<T>::Vector2(const Vector2<T>& vector)
: VectorBase<T, 2>(vector)
, x(&VectorBase<T, 2>::m_values[m_xIndex])
, y(&VectorBase<T, 2>::m_values[m_yIndex])
{
}
template<class T>
Vector2<T>::~Vector2()
{
@@ -95,7 +104,7 @@ void Vector2<T>::setValue(const unsigned int index, const T& value)
}
template<class T>
T& Vector2<T>::operator[](const unsigned int index)
T Vector2<T>::operator[](const unsigned int index)
{
try
{
+11
View File
@@ -13,6 +13,7 @@ public:
Vector4();
Vector4(const T& x, const T& y, const T& z, const T& w);
Vector4(const VectorBase<T, 4>& vector);
Vector4(const Vector4<T>& vector);
virtual ~Vector4();
T getValue(const unsigned int index) const;
@@ -76,6 +77,16 @@ Vector4<T>::Vector4(const VectorBase<T, 4>& vector)
{
}
template<class T>
Vector4<T>::Vector4(const Vector4<T>& vector)
: VectorBase<T, 4>(vector)
, x(&VectorBase<T, 4>::m_values[m_xIndex])
, y(&VectorBase<T, 4>::m_values[m_yIndex])
, z(&VectorBase<T, 4>::m_values[m_zIndex])
, w(&VectorBase<T, 4>::m_values[m_wIndex])
{
}
template<class T>
Vector4<T>::~Vector4()
{
+2 -2
View File
@@ -54,7 +54,7 @@ public:
template<class U>
bool isSame(const VectorBase<U, N>& other) const;
T& operator[](const unsigned int index);
T operator[](const unsigned int index);
template<class U>
void operator=(VectorBase<U, N>& other);
@@ -326,7 +326,7 @@ bool VectorBase<T, N>::isSame(const VectorBase<U, N>& other) const
}
template<class T, unsigned int N>
T& VectorBase<T, N>::operator[](const unsigned int index)
T VectorBase<T, N>::operator[](const unsigned int index)
{
VECTOR_CHECK_INDEX(index);