logic: don't use shared_ptr for QtGraphNode and QtGraphEdge anymore

* fixes crashes on events to deleted Qt elements
This commit is contained in:
Eberhard Graether
2017-11-17 22:30:48 +01:00
parent 634601b4b6
commit b887a4a89e
15 changed files with 159 additions and 179 deletions
@@ -24,8 +24,8 @@ QtGraphEdge* QtGraphEdge::s_focusedEdge = nullptr;
QtGraphEdge* QtGraphEdge::s_focusedBezierEdge = nullptr;
QtGraphEdge::QtGraphEdge(
const std::weak_ptr<QtGraphNode>& owner,
const std::weak_ptr<QtGraphNode>& target,
QtGraphNode* owner,
QtGraphNode* target,
const Edge* data,
size_t weight,
bool isActive,
@@ -49,11 +49,13 @@ QtGraphEdge::QtGraphEdge(
{
if (m_direction == TokenComponentAggregation::DIRECTION_BACKWARD)
{
m_owner.swap(m_target);
QtGraphNode* temp = m_owner;
m_owner = m_target;
m_target = temp;
}
m_fromActive = m_owner.lock()->getIsActive();
m_toActive = m_target.lock()->getIsActive();
m_fromActive = m_owner->getIsActive();
m_toActive = m_target->getIsActive();
s_focusedEdge = nullptr;
s_focusedBezierEdge = nullptr;
@@ -68,26 +70,20 @@ const Edge* QtGraphEdge::getData() const
return m_data;
}
std::weak_ptr<QtGraphNode> QtGraphEdge::getOwner()
QtGraphNode* QtGraphEdge::getOwner()
{
return m_owner;
}
std::weak_ptr<QtGraphNode> QtGraphEdge::getTarget()
QtGraphNode* QtGraphEdge::getTarget()
{
return m_target;
}
void QtGraphEdge::updateLine()
{
std::shared_ptr<QtGraphNode> owner = m_owner.lock();
std::shared_ptr<QtGraphNode> target = m_target.lock();
if (owner == NULL || target == NULL)
{
LOG_WARNING("Either the owner or the target node is null.");
return;
}
QtGraphNode* owner = m_owner;
QtGraphNode* target = m_target;
Edge::EdgeType type = (getData() ? getData()->getType() : Edge::EDGE_AGGREGATION);
GraphViewStyle::EdgeStyle style = GraphViewStyle::getStyleForEdgeType(type, m_isActive | m_isFocused, false, m_isTrailEdge);
@@ -176,8 +172,7 @@ void QtGraphEdge::updateLine()
}
if (type != Edge::EDGE_INHERITANCE &&
(type != Edge::EDGE_AGGREGATION ||
owner.get() != owner->getLastParent() || target.get() != target->getLastParent()))
(type != Edge::EDGE_AGGREGATION || owner != owner->getLastParent() || target != target->getLastParent()))
{
child->setRoute(QtLineItemBase::ROUTE_HORIZONTAL);
}
@@ -246,9 +241,8 @@ void QtGraphEdge::onClick()
if (!getData())
{
std::weak_ptr<QtGraphNode> node =
(m_direction == TokenComponentAggregation::DIRECTION_BACKWARD ? m_owner : m_target);
MessageGraphNodeBundleSplit(node.lock()->getTokenId()).dispatch();
QtGraphNode* node = (m_direction == TokenComponentAggregation::DIRECTION_BACKWARD ? m_owner : m_target);
MessageGraphNodeBundleSplit(node->getTokenId()).dispatch();
}
else
{
@@ -22,8 +22,8 @@ class QtGraphEdge
public:
QtGraphEdge(
const std::weak_ptr<QtGraphNode>& owner,
const std::weak_ptr<QtGraphNode>& target,
QtGraphNode* owner,
QtGraphNode* target,
const Edge* data,
size_t weight,
bool isActive,
@@ -32,8 +32,8 @@ public:
const Edge* getData() const;
std::weak_ptr<QtGraphNode> getOwner();
std::weak_ptr<QtGraphNode> getTarget();
QtGraphNode* getOwner();
QtGraphNode* getTarget();
void updateLine();
@@ -72,8 +72,8 @@ private:
const Edge* m_data;
std::weak_ptr<QtGraphNode> m_owner;
std::weak_ptr<QtGraphNode> m_target;
QtGraphNode* m_owner;
QtGraphNode* m_target;
QGraphicsItem* m_child;
@@ -57,7 +57,7 @@ QtGraphNode::~QtGraphNode()
QtGraphNode* QtGraphNode::getParent() const
{
return m_parentNode.lock().get();
return m_parentNode;
}
QtGraphNode* QtGraphNode::getLastParent() const
@@ -75,18 +75,17 @@ QtGraphNode* QtGraphNode::getLastParent() const
return node;
}
void QtGraphNode::setParent(std::weak_ptr<QtGraphNode> parentNode)
void QtGraphNode::setParent(QtGraphNode* parentNode)
{
m_parentNode = parentNode;
std::shared_ptr<QtGraphNode> parent = parentNode.lock();
if (parent != NULL)
if (m_parentNode != nullptr)
{
QGraphicsRectItem::setParentItem(parent.get());
QGraphicsRectItem::setParentItem(m_parentNode);
}
}
std::list<std::shared_ptr<QtGraphNode>> QtGraphNode::getSubNodes() const
std::list<QtGraphNode*> QtGraphNode::getSubNodes() const
{
return m_subNodes;
}
@@ -147,12 +146,12 @@ Vec4i QtGraphNode::getParentBoundingRect() const
return getLastParent()->getBoundingRect();
}
void QtGraphNode::addOutEdge(const std::shared_ptr<QtGraphEdge>& edge)
void QtGraphNode::addOutEdge(QtGraphEdge* edge)
{
m_outEdges.push_back(edge);
}
void QtGraphNode::addInEdge(const std::weak_ptr<QtGraphEdge>& edge)
void QtGraphNode::addInEdge(QtGraphEdge* edge)
{
m_inEdges.push_back(edge);
}
@@ -317,7 +316,7 @@ Id QtGraphNode::getTokenId() const
return 0;
}
void QtGraphNode::addSubNode(const std::shared_ptr<QtGraphNode>& node)
void QtGraphNode::addSubNode(QtGraphNode* node)
{
m_subNodes.push_back(node);
@@ -415,18 +414,14 @@ void QtGraphNode::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
void QtGraphNode::forEachEdge(std::function<void(QtGraphEdge*)> func)
{
for (const std::shared_ptr<QtGraphEdge>& edge : m_outEdges)
for (QtGraphEdge* edge : m_outEdges)
{
func(edge.get());
func(edge);
}
for (const std::weak_ptr<QtGraphEdge>& e : m_inEdges)
for (QtGraphEdge* edge : m_inEdges)
{
std::shared_ptr<QtGraphEdge> edge = e.lock();
if (edge)
{
func(edge.get());
}
func(edge);
}
}
@@ -439,7 +434,7 @@ void QtGraphNode::notifyEdgesAfterMove()
}
);
for (const std::shared_ptr<QtGraphNode>& node : m_subNodes)
for (QtGraphNode* node : m_subNodes)
{
node->notifyEdgesAfterMove();
}
@@ -38,9 +38,9 @@ public:
QtGraphNode* getParent() const;
QtGraphNode* getLastParent() const;
void setParent(std::weak_ptr<QtGraphNode> parentNode);
void setParent(QtGraphNode* parentNode);
std::list<std::shared_ptr<QtGraphNode>> getSubNodes() const;
std::list<QtGraphNode*> getSubNodes() const;
Vec2i getPosition() const;
virtual bool setPosition(const Vec2i& position);
@@ -54,8 +54,8 @@ public:
Vec4i getBoundingRect() const;
Vec4i getParentBoundingRect() const;
void addOutEdge(const std::shared_ptr<QtGraphEdge>& edge);
void addInEdge(const std::weak_ptr<QtGraphEdge>& edge);
void addOutEdge(QtGraphEdge* edge);
void addInEdge(QtGraphEdge* edge);
size_t getOutEdgeCount() const;
size_t getInEdgeCount() const;
@@ -89,7 +89,7 @@ public:
virtual Id getTokenId() const;
virtual void addSubNode(const std::shared_ptr<QtGraphNode>& node);
virtual void addSubNode(QtGraphNode* node);
virtual void onClick();
virtual void moved(const Vec2i& oldPosition);
@@ -109,11 +109,11 @@ protected:
void setStyle(const GraphViewStyle::NodeStyle& style);
std::list<std::shared_ptr<QtGraphEdge>> m_outEdges;
std::list<std::weak_ptr<QtGraphEdge>> m_inEdges;
std::list<QtGraphEdge*> m_outEdges;
std::list<QtGraphEdge*> m_inEdges;
std::weak_ptr<QtGraphNode> m_parentNode;
std::list<std::shared_ptr<QtGraphNode>> m_subNodes;
QtGraphNode* m_parentNode = nullptr;
std::list<QtGraphNode*> m_subNodes;
QGraphicsSimpleTextItem* m_text = nullptr;
QtRoundedRectItem* m_rect = nullptr;
@@ -70,7 +70,7 @@ bool QtGraphNodeAccess::isAccessNode() const
return true;
}
void QtGraphNodeAccess::addSubNode(const std::shared_ptr<QtGraphNode>& node)
void QtGraphNodeAccess::addSubNode(QtGraphNode* node)
{
QtGraphNode::addSubNode(node);
m_text->show();
@@ -16,15 +16,15 @@ public:
AccessKind getAccessKind() const;
// QtGraphNode implementation
virtual bool isAccessNode() const;
virtual bool isAccessNode() const override;
virtual void addSubNode(const std::shared_ptr<QtGraphNode>& node);
virtual void updateStyle();
virtual void addSubNode(QtGraphNode* node) override;
virtual void updateStyle() override;
void hideLabel();
protected:
virtual void matchName(const std::string& query, std::vector<QtGraphNode*>* matchedNodes) {}
virtual void matchName(const std::string& query, std::vector<QtGraphNode*>* matchedNodes) override {}
private:
AccessKind m_accessKind;
@@ -154,6 +154,6 @@ void QtGraphNodeQualifier::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
m_rightArrow->hide();
m_rightArrowSmall->show();
this->setParentItem(m_parentNode.lock().get());
this->setParentItem(m_parentNode);
this->setPos(QPointF(0, 0));
}
@@ -1,6 +1,6 @@
#include "QtGraphNodeComponent.h"
QtGraphNodeComponent::QtGraphNodeComponent(const std::weak_ptr<QtGraphNode>& graphNode)
QtGraphNodeComponent::QtGraphNodeComponent(QtGraphNode* graphNode)
: m_graphNode(graphNode)
{
}
@@ -10,7 +10,7 @@ class QtGraphNode;
class QtGraphNodeComponent
{
public:
QtGraphNodeComponent(const std::weak_ptr<QtGraphNode>& graphNode);
QtGraphNodeComponent(QtGraphNode* graphNode);
virtual ~QtGraphNodeComponent();
virtual void nodeMousePressEvent(QGraphicsSceneMouseEvent* event);
@@ -18,7 +18,7 @@ public:
virtual void nodeMouseReleaseEvent(QGraphicsSceneMouseEvent* event);
protected:
std::weak_ptr<QtGraphNode> m_graphNode;
QtGraphNode* m_graphNode;
};
#endif // QT_GRAPH_NODE_COMPONENT_H
@@ -4,7 +4,7 @@
#include "qt/view/graphElements/QtGraphNode.h"
QtGraphNodeComponentClickable::QtGraphNodeComponentClickable(const std::weak_ptr<QtGraphNode>& graphNode)
QtGraphNodeComponentClickable::QtGraphNodeComponentClickable(QtGraphNode* graphNode)
: QtGraphNodeComponent(graphNode)
, m_mousePos(0.0f, 0.0f)
, m_mouseMoved(false)
@@ -35,11 +35,7 @@ void QtGraphNodeComponentClickable::nodeMouseReleaseEvent(QGraphicsSceneMouseEve
{
if (!m_mouseMoved)
{
std::shared_ptr<QtGraphNode> node = m_graphNode.lock();
if (node != NULL)
{
node->onClick();
event->accept();
}
m_graphNode->onClick();
event->accept();
}
}
@@ -9,7 +9,7 @@ class QtGraphNodeComponentClickable
: public QtGraphNodeComponent
{
public:
QtGraphNodeComponentClickable(const std::weak_ptr<QtGraphNode>& graphNode);
QtGraphNodeComponentClickable(QtGraphNode* graphNode);
virtual ~QtGraphNodeComponentClickable();
virtual void nodeMousePressEvent(QGraphicsSceneMouseEvent* event);
@@ -4,7 +4,7 @@
#include "qt/view/graphElements/QtGraphNode.h"
QtGraphNodeComponentMoveable::QtGraphNodeComponentMoveable(const std::weak_ptr<QtGraphNode>& graphNode)
QtGraphNodeComponentMoveable::QtGraphNodeComponentMoveable(QtGraphNode* graphNode)
: QtGraphNodeComponent(graphNode)
, m_mouseOffset(0.0f, 0.0f)
{
@@ -16,25 +16,17 @@ QtGraphNodeComponentMoveable::~QtGraphNodeComponentMoveable()
void QtGraphNodeComponentMoveable::nodeMousePressEvent(QGraphicsSceneMouseEvent* event)
{
std::shared_ptr<QtGraphNode> node = m_graphNode.lock();
if (node != NULL)
{
m_oldPos = node->getPosition();
m_mouseOffset.x = event->scenePos().x() - m_oldPos.x;
m_mouseOffset.y = event->scenePos().y() - m_oldPos.y;
m_oldPos = m_graphNode->getPosition();
m_mouseOffset.x = event->scenePos().x() - m_oldPos.x;
m_mouseOffset.y = event->scenePos().y() - m_oldPos.y;
event->accept();
}
event->accept();
}
void QtGraphNodeComponentMoveable::nodeMouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
std::shared_ptr<QtGraphNode> node = m_graphNode.lock();
if (node != NULL)
{
node->setPosition(Vec2i(event->scenePos().x() - m_mouseOffset.x, event->scenePos().y() - m_mouseOffset.y));
event->accept();
}
m_graphNode->setPosition(Vec2i(event->scenePos().x() - m_mouseOffset.x, event->scenePos().y() - m_mouseOffset.y));
event->accept();
}
void QtGraphNodeComponentMoveable::nodeMouseReleaseEvent(QGraphicsSceneMouseEvent* event)
@@ -44,9 +36,5 @@ void QtGraphNodeComponentMoveable::nodeMouseReleaseEvent(QGraphicsSceneMouseEven
return;
}
std::shared_ptr<QtGraphNode> node = m_graphNode.lock();
if (node != NULL)
{
node->moved(m_oldPos);
}
m_graphNode->moved(m_oldPos);
}
@@ -9,7 +9,7 @@ class QtGraphNodeComponentMoveable
: public QtGraphNodeComponent
{
public:
QtGraphNodeComponentMoveable(const std::weak_ptr<QtGraphNode>& graphNode);
QtGraphNodeComponentMoveable(QtGraphNode* graphNode);
virtual ~QtGraphNodeComponentMoveable();
virtual void nodeMousePressEvent(QGraphicsSceneMouseEvent* event);