diff --git a/src/app/qt/view/QtGraphView.cpp b/src/app/qt/view/QtGraphView.cpp index 7c286104..ea07d5a8 100644 --- a/src/app/qt/view/QtGraphView.cpp +++ b/src/app/qt/view/QtGraphView.cpp @@ -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 QtGraphView::addNode(const Vec2i& position, const std::string& name) +void QtGraphView::rebuildGraph(const std::vector& nodes, const std::vector& edges) { - QGraphicsView* view = getView(); - - if (view != NULL) - { - std::shared_ptr node = std::make_shared(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(); - } + m_rebuildGraph(nodes, edges); } -void QtGraphView::addEdge(const std::weak_ptr& owner, const std::weak_ptr& target) +void QtGraphView::clear() { - QGraphicsView* view = getView(); - - if (view != NULL) - { - std::shared_ptr o = owner.lock(); - std::shared_ptr t = target.lock(); - - if (o != NULL && t != NULL) - { - std::shared_ptr edge = std::make_shared(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(""); } + +void QtGraphView::doRebuildGraph(const std::vector& nodes, const std::vector& edges) +{ + doClear(); + + QGraphicsView* view = getView(); + + if (view != NULL) + { + for(unsigned int i = 0; i < nodes.size(); i++) + { + std::shared_ptr newNode = findOrCreateNode(view, nodes[i]); + } + } + else + { + LOG_WARNING("Failed to get QGraphicsView"); + } +} + +void QtGraphView::doClear() +{ + m_nodes.clear(); + m_edges.clear(); +} + +std::shared_ptr QtGraphView::findOrCreateNode(QGraphicsView* view, const DummyNode& node) +{ + std::shared_ptr result; + + result = findNode(node); + + if(result == NULL) + { + result = createNode(view, node); + } + + return result; +} + +std::shared_ptr QtGraphView::findNode(const DummyNode& node) +{ + std::list>::iterator it = m_nodes.begin(); + + for(it; it != m_nodes.end(); it++) + { + if((*it)->getTokenId() == node.tokenId) + { + return *it; + } + } + + return NULL; +} + +std::shared_ptr QtGraphView::createNode(QGraphicsView* view, const DummyNode& node) +{ + if(view != NULL) + { + std::shared_ptr newNode = std::make_shared(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."); + } +} diff --git a/src/app/qt/view/QtGraphView.h b/src/app/qt/view/QtGraphView.h index acbdd2bd..9c94f402 100644 --- a/src/app/qt/view/QtGraphView.h +++ b/src/app/qt/view/QtGraphView.h @@ -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 addNode(const Vec2i& position, const std::string& name); - virtual void addEdge(const std::weak_ptr& owner, const std::weak_ptr& target); + virtual void rebuildGraph(const std::vector& nodes, const std::vector& edges); + + virtual void clear(); private: QGraphicsView* getView(); + + void doRebuildGraph(const std::vector& nodes, const std::vector& edges); + void doClear(); + + std::shared_ptr findOrCreateNode(QGraphicsView* view, const DummyNode& node); + std::shared_ptr findNode(const DummyNode& node); + std::shared_ptr createNode(QGraphicsView* view, const DummyNode& node); + + QtThreadedFunctor&, const std::vector&> m_rebuildGraph; + QtThreadedFunctor m_clear; }; #endif // QT_GRAPH_VIEW_H diff --git a/src/app/qt/view/graphElements/QtGraphNode.cpp b/src/app/qt/view/graphElements/QtGraphNode.cpp index b5a071f7..4e070cb5 100644 --- a/src/app/qt/view/graphElements/QtGraphNode.cpp +++ b/src/app/qt/view/graphElements/QtGraphNode.cpp @@ -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) diff --git a/src/app/qt/view/graphElements/QtGraphNode.h b/src/app/qt/view/graphElements/QtGraphNode.h index 1f14f56c..a7e9a9f7 100644 --- a/src/app/qt/view/graphElements/QtGraphNode.h +++ b/src/app/qt/view/graphElements/QtGraphNode.h @@ -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& edge); diff --git a/src/lib/component/ComponentManager.cpp b/src/lib/component/ComponentManager.cpp index 3241def4..2996a3a1 100644 --- a/src/lib/component/ComponentManager.cpp +++ b/src/lib/component/ComponentManager.cpp @@ -27,9 +27,9 @@ void ComponentManager::setup() std::shared_ptr graphComponent = m_componentFactory->createGraphComponent(); GraphView* graphView = graphComponent->getView(); - std::weak_ptr node0 = graphView->addNode(Vec2i(-50, -50), "foo"); + /*std::weak_ptr 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); diff --git a/src/lib/component/controller/GraphController.cpp b/src/lib/component/controller/GraphController.cpp index 97af40c4..7a493bb9 100644 --- a/src/lib/component/controller/GraphController.cpp +++ b/src/lib/component/controller/GraphController.cpp @@ -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) : 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 nodes; + nodes.push_back(node); + + std::vector edges; + + view->rebuildGraph(nodes, edges); + } +} + GraphView* GraphController::getView() { return Controller::getView(); diff --git a/src/lib/component/controller/GraphController.h b/src/lib/component/controller/GraphController.h index 338c0b4f..a0c17644 100644 --- a/src/lib/component/controller/GraphController.h +++ b/src/lib/component/controller/GraphController.h @@ -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 { public: GraphController(std::shared_ptr graphAccess); ~GraphController(); private: + virtual void handleMessage(MessageActivateToken* message); GraphView* getView(); std::shared_ptr m_graphAccess; diff --git a/src/lib/component/view/GraphView.h b/src/lib/component/view/GraphView.h index 67d0e0bd..52e7a139 100644 --- a/src/lib/component/view/GraphView.h +++ b/src/lib/component/view/GraphView.h @@ -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 addNode(const Vec2i& position, const std::string& name) = 0; - virtual void addEdge(const std::weak_ptr& owner, const std::weak_ptr& target) = 0; + virtual void rebuildGraph(const std::vector& nodes, const std::vector& edges) = 0; + + virtual void clear() = 0; protected: std::list > m_nodes; diff --git a/src/lib/component/view/graphElements/GraphEdge.h b/src/lib/component/view/graphElements/GraphEdge.h index f5fd19f6..7f901125 100644 --- a/src/lib/component/view/graphElements/GraphEdge.h +++ b/src/lib/component/view/graphElements/GraphEdge.h @@ -1,6 +1,10 @@ #ifndef GRAPH_EDGE_H #define GRAPH_EDGE_H +#include + +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 o, const std::weak_ptr t) + : owner(o) + , target(t) + { + } + + std::weak_ptr owner; + std::weak_ptr target; +}; + #endif // GRAPH_EDGE_H \ No newline at end of file diff --git a/src/lib/component/view/graphElements/GraphNode.cpp b/src/lib/component/view/graphElements/GraphNode.cpp index d9c8fdea..bcc33ded 100644 --- a/src/lib/component/view/graphElements/GraphNode.cpp +++ b/src/lib/component/view/graphElements/GraphNode.cpp @@ -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; +} diff --git a/src/lib/component/view/graphElements/GraphNode.h b/src/lib/component/view/graphElements/GraphNode.h index b55d2125..50ea36d2 100644 --- a/src/lib/component/view/graphElements/GraphNode.h +++ b/src/lib/component/view/graphElements/GraphNode.h @@ -1,22 +1,47 @@ #ifndef GRAPH_NODE_H #define GRAPH_NODE_H +#include + #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& edge) = 0; virtual void addInEdge(const std::weak_ptr& edge) = 0; virtual void removeOutEdge(const std::shared_ptr& 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 actualNode; }; #endif // GRAPH_NODE_H diff --git a/src/lib/utility/math/Vector2.h b/src/lib/utility/math/Vector2.h index 852dbcb6..6d504564 100644 --- a/src/lib/utility/math/Vector2.h +++ b/src/lib/utility/math/Vector2.h @@ -13,12 +13,13 @@ public: Vector2(); Vector2(const T& x, const T& y); Vector2(const VectorBase& vector); + Vector2(const Vector2& 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 x; Property y; @@ -62,6 +63,14 @@ Vector2::Vector2(const VectorBase& vector) { } +template +Vector2::Vector2(const Vector2& vector) + : VectorBase(vector) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) +{ +} + template Vector2::~Vector2() { @@ -95,7 +104,7 @@ void Vector2::setValue(const unsigned int index, const T& value) } template -T& Vector2::operator[](const unsigned int index) +T Vector2::operator[](const unsigned int index) { try { diff --git a/src/lib/utility/math/Vector4.h b/src/lib/utility/math/Vector4.h index eb86e154..18f158ff 100644 --- a/src/lib/utility/math/Vector4.h +++ b/src/lib/utility/math/Vector4.h @@ -13,6 +13,7 @@ public: Vector4(); Vector4(const T& x, const T& y, const T& z, const T& w); Vector4(const VectorBase& vector); + Vector4(const Vector4& vector); virtual ~Vector4(); T getValue(const unsigned int index) const; @@ -76,6 +77,16 @@ Vector4::Vector4(const VectorBase& vector) { } +template +Vector4::Vector4(const Vector4& vector) + : VectorBase(vector) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) + , z(&VectorBase::m_values[m_zIndex]) + , w(&VectorBase::m_values[m_wIndex]) +{ +} + template Vector4::~Vector4() { diff --git a/src/lib/utility/math/VectorBase.h b/src/lib/utility/math/VectorBase.h index ac693460..9a8bf73e 100644 --- a/src/lib/utility/math/VectorBase.h +++ b/src/lib/utility/math/VectorBase.h @@ -54,7 +54,7 @@ public: template bool isSame(const VectorBase& other) const; - T& operator[](const unsigned int index); + T operator[](const unsigned int index); template void operator=(VectorBase& other); @@ -326,7 +326,7 @@ bool VectorBase::isSame(const VectorBase& other) const } template -T& VectorBase::operator[](const unsigned int index) +T VectorBase::operator[](const unsigned int index) { VECTOR_CHECK_INDEX(index);