From c10b56fe63620f9444e50fe746380ee75943228a Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Tue, 7 Jun 2011 14:49:14 +0200 Subject: [PATCH] node kept inside the scene done --- graphwidget.cpp | 1 + node.cpp | 62 +++++++++++++++++++--------------------------- node.h | 8 +++--- qtmindmap.pro.user | 2 +- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index 4c84a64..a76cfd1 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -59,6 +59,7 @@ GraphWidget::GraphWidget(QWidget *parent) : m_activeNode = node1; m_activeNode->setFocus(); + } QGraphicsScene *GraphWidget::getScene() diff --git a/node.cpp b/node.cpp index 4a568d8..6f1fa51 100644 --- a/node.cpp +++ b/node.cpp @@ -35,46 +35,36 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) qDebug() << __PRETTY_FUNCTION__; switch (change) { - case ItemPositionHasChanged: - foreach (Edge *edge, m_edgeList) edge->adjust(); - break; - default: - break; + + case ItemPositionChange: + + if (change == ItemPositionChange && scene()) { + // value is the new position. + QPointF newPos = value.toPointF(); + QRectF rect (scene()->sceneRect().topLeft(), + scene()->sceneRect().bottomRight()-boundingRect().bottomRight()); + + if (!rect.contains(newPos)) + { + // Keep the item inside the scene rect. + newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); + newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); + return newPos; + } + } + break; + + case ItemPositionHasChanged: + + foreach (Edge *edge, m_edgeList) edge->adjust(); + break; + default: + break; }; return QGraphicsItem::itemChange(change, value); } -void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) -{ - qDebug() << __PRETTY_FUNCTION__; - - prevpt = event->pos(); - - update(); - QGraphicsTextItem::mousePressEvent(event); -} - -void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) -{ - qDebug() << __PRETTY_FUNCTION__; - - if (!scene()->sceneRect().contains(sceneBoundingRect())) - { - setPos(prevpt); - } - - update(); - QGraphicsTextItem::mouseReleaseEvent(event); -} - -void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) -{ - qDebug() << __PRETTY_FUNCTION__; - - QGraphicsTextItem::mouseMoveEvent(event); -} - void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w) { qDebug() << __PRETTY_FUNCTION__; @@ -85,6 +75,4 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid painter->drawRect(QRect(boundingRect().topLeft().toPoint(), boundingRect().bottomRight().toPoint() - QPoint(1,1))); - - prevpt = pos(); } diff --git a/node.h b/node.h index 1925842..2dedcec 100644 --- a/node.h +++ b/node.h @@ -11,23 +11,21 @@ class GraphWidget; class Node : public QGraphicsTextItem { public: + Node(GraphWidget *graphWidget = 0); void addEdge(Edge *edge); QList edges() const; protected: + QVariant itemChange(GraphicsItemChange change, const QVariant &value); - void mousePressEvent(QGraphicsSceneMouseEvent *event); - void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - private: + QList m_edgeList; GraphWidget *m_graph; - QPointF prevpt; }; #endif // NODE_H diff --git a/qtmindmap.pro.user b/qtmindmap.pro.user index a2bd180..675c486 100644 --- a/qtmindmap.pro.user +++ b/qtmindmap.pro.user @@ -182,7 +182,7 @@ qtmindmap.pro false - true + false 3768