diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 2d7b085b..7211bbf7 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -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 diff --git a/src/app/qt/view/QtGraphView.cpp b/src/app/qt/view/QtGraphView.cpp index a12a8812..7c286104 100644 --- a/src/app/qt/view/QtGraphView.cpp +++ b/src/app/qt/view/QtGraphView.cpp @@ -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 QtGraphView::addNode(const Vec2i& position, const std::string& name) +{ + 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(); + } +} + +void QtGraphView::addEdge(const std::weak_ptr& owner, const std::weak_ptr& target) +{ + 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"); + } +} + +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(""); - - 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(""); } diff --git a/src/app/qt/view/QtGraphView.h b/src/app/qt/view/QtGraphView.h index 891cfe1a..acbdd2bd 100644 --- a/src/app/qt/view/QtGraphView.h +++ b/src/app/qt/view/QtGraphView.h @@ -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 addNode(const Vec2i& position, const std::string& name); + virtual void addEdge(const std::weak_ptr& owner, const std::weak_ptr& target); + +private: + QGraphicsView* getView(); }; #endif // QT_GRAPH_VIEW_H diff --git a/src/app/qt/view/QtMainView.cpp b/src/app/qt/view/QtMainView.cpp index 4c0c7c83..e828c21c 100644 --- a/src/app/qt/view/QtMainView.cpp +++ b/src/app/qt/view/QtMainView.cpp @@ -5,7 +5,7 @@ QtMainView::QtMainView() { - m_window = new QtMainWindow(); + m_window = std::make_shared(); m_window->show(); } diff --git a/src/app/qt/view/QtMainView.h b/src/app/qt/view/QtMainView.h index d536eeeb..2a28750e 100644 --- a/src/app/qt/view/QtMainView.h +++ b/src/app/qt/view/QtMainView.h @@ -20,7 +20,7 @@ public: void removeView(View* view); private: - QtMainWindow* m_window; + std::shared_ptr m_window; std::vector m_views; }; diff --git a/src/app/qt/view/graphElements/QtGraphEdge.cpp b/src/app/qt/view/graphElements/QtGraphEdge.cpp new file mode 100644 index 00000000..ddf88915 --- /dev/null +++ b/src/app/qt/view/graphElements/QtGraphEdge.cpp @@ -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& owner, const std::weak_ptr& target) + : m_owner(owner) + , m_target(target) +{ + std::shared_ptr o = owner.lock(); + std::shared_ptr 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 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 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."); + } +} diff --git a/src/app/qt/view/graphElements/QtGraphEdge.h b/src/app/qt/view/graphElements/QtGraphEdge.h new file mode 100644 index 00000000..4437b577 --- /dev/null +++ b/src/app/qt/view/graphElements/QtGraphEdge.h @@ -0,0 +1,28 @@ +#ifndef QT_GRAPH_EDGE_H +#define QT_GRAPH_EDGE_H + +#include + +#include "qgraphicsitem.h" + +#include "component/view/graphElements/GraphEdge.h" + +class GraphNode; + +class QtGraphEdge: + public GraphEdge, + public QGraphicsLineItem +{ +public: + QtGraphEdge(const std::weak_ptr& owner, const std::weak_ptr& target); + virtual ~QtGraphEdge(); + + virtual void ownerMoved(); + virtual void targetMoved(); + +private: + std::weak_ptr m_owner; + std::weak_ptr m_target; +}; + +#endif // QT_GRAPH_EDGE_H diff --git a/src/app/qt/view/graphElements/QtGraphNode.cpp b/src/app/qt/view/graphElements/QtGraphNode.cpp index 38037b67..b5a071f7 100644 --- a/src/app/qt/view/graphElements/QtGraphNode.cpp +++ b/src/app/qt/view/graphElements/QtGraphNode.cpp @@ -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() { -} \ No newline at end of file +} + +Vec2i QtGraphNode::getPosition() +{ + return Vec2i(this->scenePos().x(), this->scenePos().y()); +} + +void QtGraphNode::addOutEdge(const std::shared_ptr& edge) +{ + m_outEdges.push_back(edge); +} + +void QtGraphNode::addInEdge(const std::weak_ptr& edge) +{ + m_inEdges.push_back(edge); +} + +void QtGraphNode::removeOutEdge(const std::shared_ptr& edge) +{ + std::list >::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 >::iterator it = m_outEdges.begin(); + for(it; it != m_outEdges.end(); it++) + { + (*it)->ownerMoved(); + } + + std::list >::iterator it2 = m_inEdges.begin(); + for(it2; it2 != m_inEdges.end(); it2++) + { + std::shared_ptr edge = it2->lock(); + if(edge.get() != NULL) + { + edge->targetMoved(); + } + else + { + m_inEdges.erase(it2, it2); + } + } +} diff --git a/src/app/qt/view/graphElements/QtGraphNode.h b/src/app/qt/view/graphElements/QtGraphNode.h index 7ff1d8ce..1f14f56c 100644 --- a/src/app/qt/view/graphElements/QtGraphNode.h +++ b/src/app/qt/view/graphElements/QtGraphNode.h @@ -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& edge); + virtual void addInEdge(const std::weak_ptr& edge); + + virtual void removeOutEdge(const std::shared_ptr& edge); + + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + private: QGraphicsTextItem* m_text; + + std::list > m_outEdges; + std::list > m_inEdges; + + Vec2i m_mouseOffset; }; #endif // QT_GRAPH_NODE_H diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index c86d3b2d..b7a24136 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -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 diff --git a/src/lib/component/ComponentManager.cpp b/src/lib/component/ComponentManager.cpp index eada6b4d..3241def4 100644 --- a/src/lib/component/ComponentManager.cpp +++ b/src/lib/component/ComponentManager.cpp @@ -25,9 +25,12 @@ ComponentManager::~ComponentManager() void ComponentManager::setup() { std::shared_ptr graphComponent = m_componentFactory->createGraphComponent(); + GraphView* graphView = graphComponent->getView(); - graphView->addNode(Vec2i(-100, -100), "foo"); - graphView->addNode(Vec2i(50, 50), "bar"); + 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")); + m_components.push_back(graphComponent); std::shared_ptr codeComponent = m_componentFactory->createCodeComponent(); diff --git a/src/lib/component/view/GraphView.h b/src/lib/component/view/GraphView.h index 716503c2..67d0e0bd 100644 --- a/src/lib/component/view/GraphView.h +++ b/src/lib/component/view/GraphView.h @@ -1,8 +1,14 @@ #ifndef GRAPH_VIEW_H #define GRAPH_VIEW_H +#include +#include + #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 addNode(const Vec2i& position, const std::string& name) = 0; + virtual void addEdge(const std::weak_ptr& owner, const std::weak_ptr& target) = 0; + +protected: + std::list > m_nodes; + std::list > m_edges; }; #endif // GRAPH_VIEW_H \ No newline at end of file diff --git a/src/lib/component/view/graphElements/GraphEdge.cpp b/src/lib/component/view/graphElements/GraphEdge.cpp new file mode 100644 index 00000000..aee08fc1 --- /dev/null +++ b/src/lib/component/view/graphElements/GraphEdge.cpp @@ -0,0 +1,9 @@ +#include "component/view/graphElements/GraphEdge.h" + +GraphEdge::GraphEdge() +{ +} + +GraphEdge::~GraphEdge() +{ +} \ No newline at end of file diff --git a/src/lib/component/view/graphElements/GraphEdge.h b/src/lib/component/view/graphElements/GraphEdge.h new file mode 100644 index 00000000..f5fd19f6 --- /dev/null +++ b/src/lib/component/view/graphElements/GraphEdge.h @@ -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 \ No newline at end of file diff --git a/src/lib/component/view/graphElements/GraphNode.h b/src/lib/component/view/graphElements/GraphNode.h index 0f4b42c7..b55d2125 100644 --- a/src/lib/component/view/graphElements/GraphNode.h +++ b/src/lib/component/view/graphElements/GraphNode.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& edge) = 0; + virtual void addInEdge(const std::weak_ptr& edge) = 0; + + virtual void removeOutEdge(const std::shared_ptr& edge) = 0; }; #endif // GRAPH_NODE_H