#include "qt/view/graphElements/QtGraphNode.h" #include #include "qgraphicsscene.h" #include "qgraphicssceneevent.h" #include "qt/view/graphElements/QtGraphEdge.h" 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); QBrush brush(Qt::lightGray); this->setBrush(brush); m_text = new QGraphicsTextItem(this); m_text->setPos(0, 0); std::stringstream text; text << m_tokenId << " -> " << name; m_text->setPlainText(QString(text.str().c_str())); } QtGraphNode::~QtGraphNode() { std::list >::iterator it = m_inEdges.begin(); for(it; it != m_inEdges.end(); it++) { std::shared_ptr edge = it->lock(); if(edge != NULL) { edge->removeEdgeFromScene(); } } } std::string QtGraphNode::getName() const { return m_text->toPlainText().toStdString(); } Vec2i QtGraphNode::getPosition() const { return Vec2i(this->scenePos().x(), this->scenePos().y()); } void QtGraphNode::setPosition(const Vec2i& position) { Vec2i currentPosition = getPosition(); Vec2i offset = position - currentPosition; if(offset.getLength() > 0.0f) { this->moveBy(offset.x, offset.y); notifyEdgesAfterMove(); } } bool QtGraphNode::addOutEdge(const std::shared_ptr& edge) { std::list >::iterator it = m_outEdges.begin(); for(it; it != m_outEdges.end(); it++) { if ((*it)->getOwner().lock() == edge->getOwner().lock() && (*it)->getTarget().lock() == edge->getTarget().lock()) { return false; } } m_outEdges.push_back(edge); return true; } bool QtGraphNode::addInEdge(const std::weak_ptr& edge) { std::list >::iterator it = m_inEdges.begin(); for(it; it != m_inEdges.end(); it++) { std::shared_ptr existingEdge = it->lock(); if(existingEdge != NULL) { if (existingEdge->getOwner().lock() == edge.lock()->getOwner().lock() && existingEdge->getTarget().lock() == edge.lock()->getTarget().lock()) { return false; } } } m_inEdges.push_back(edge); return true; } void QtGraphNode::removeOutEdge(GraphEdge* edge) { std::list >::iterator it = m_outEdges.begin(); while(it != m_outEdges.end()) { if((*it).get() == edge) { m_outEdges.erase(it); break; } ++it; } } std::list > QtGraphNode::getSubNodes() const { return m_subNodes; } void QtGraphNode::addSubNode(const std::shared_ptr& node) { m_subNodes.push_back(node); } void QtGraphNode::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) { MessageActivateToken message(m_tokenId); message.dispatch(); } void QtGraphNode::notifyParentMoved() { notifyEdgesAfterMove(); } void QtGraphNode::notifyEdgesAfterMove() { std::list >::iterator it = m_outEdges.begin(); for(it; it != m_outEdges.end(); it++) { (*it)->ownerMoved(); } std::list >::iterator it2 = m_inEdges.begin(); while(it2 != m_inEdges.end()) { std::shared_ptr edge = it2->lock(); if(edge.get() != NULL) { edge->targetMoved(); ++it2; } else { m_inEdges.erase(it2++); } } std::list >::iterator it3 = m_subNodes.begin(); for(it3; it3 != m_subNodes.end(); it3++) { (*it3)->notifyParentMoved(); } }