From 8ac7248513189d82fe5bdf90c0d7fc15f2e718ce Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Tue, 17 Apr 2018 11:03:24 +0300 Subject: fixed bounding box bug --- src/gui/nodeitem.cpp | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'src/gui/nodeitem.cpp') diff --git a/src/gui/nodeitem.cpp b/src/gui/nodeitem.cpp index 06b46ff..b0b7ea6 100644 --- a/src/gui/nodeitem.cpp +++ b/src/gui/nodeitem.cpp @@ -18,6 +18,7 @@ NodeItem::NodeItem(Node *node) //setFlag(QGraphicsItem::ItemIsMovable); //setFlag(QGraphicsItem::ItemSendsGeometryChanges); readPos(); + updateBounds(); } void NodeItem::readPos() @@ -98,27 +99,42 @@ QPainterPath NodeItem::shape() const return path; } +// TODO: nodeitem should sync boundingRect()-relevant stuff (label etc) explicitly, +// to allow prepareGeometryChange() QRectF NodeItem::boundingRect() const { - QRectF r = labelRect(); - return r.united(shape().boundingRect()).adjusted(-4,-4,4,4); + return _boundingRect; } -Node *NodeItem::node() const +void NodeItem::updateBounds() { - return _node; + prepareGeometryChange(); + QString label = _node->label(); + if (label != "") { + QFontMetrics fm(Tikzit::LABEL_FONT); + QRectF labelRect = fm.boundingRect(label); + labelRect.moveCenter(QPointF(0, 0)); + _boundingRect = labelRect.united(shape().boundingRect()).adjusted(-4, -4, 4, 4); + } else { + _boundingRect = shape().boundingRect().adjusted(-4, -4, 4, 4); + } } -QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &value) +Node *NodeItem::node() const { - if (change == ItemPositionChange) { - QPointF newPos = value.toPointF(); - int gridSize = GLOBAL_SCALE / 8; - QPointF gridPos(round(newPos.x()/gridSize)*gridSize, round(newPos.y()/gridSize)*gridSize); - _node->setPoint(fromScreen(gridPos)); - - return gridPos; - } else { - return QGraphicsItem::itemChange(change, value); - } + return _node; } + +//QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &value) +//{ +// if (change == ItemPositionChange) { +// QPointF newPos = value.toPointF(); +// int gridSize = GLOBAL_SCALE / 8; +// QPointF gridPos(round(newPos.x()/gridSize)*gridSize, round(newPos.y()/gridSize)*gridSize); +// _node->setPoint(fromScreen(gridPos)); +// +// return gridPos; +// } else { +// return QGraphicsItem::itemChange(change, value); +// } +//} -- cgit v1.2.3