node kept inside the scene done

master
Denes Matetelki 14 years ago
parent 28528963a8
commit c10b56fe63

@ -59,6 +59,7 @@ GraphWidget::GraphWidget(QWidget *parent) :
m_activeNode = node1; m_activeNode = node1;
m_activeNode->setFocus(); m_activeNode->setFocus();
} }
QGraphicsScene *GraphWidget::getScene() QGraphicsScene *GraphWidget::getScene()

@ -35,44 +35,34 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
switch (change) { switch (change) {
case ItemPositionHasChanged:
foreach (Edge *edge, m_edgeList) edge->adjust();
break;
default:
break;
};
return QGraphicsItem::itemChange(change, value); case ItemPositionChange:
}
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) if (change == ItemPositionChange && scene()) {
{ // value is the new position.
qDebug() << __PRETTY_FUNCTION__; QPointF newPos = value.toPointF();
QRectF rect (scene()->sceneRect().topLeft(),
scene()->sceneRect().bottomRight()-boundingRect().bottomRight());
prevpt = event->pos(); if (!rect.contains(newPos))
update();
QGraphicsTextItem::mousePressEvent(event);
}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
qDebug() << __PRETTY_FUNCTION__; // Keep the item inside the scene rect.
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
if (!scene()->sceneRect().contains(sceneBoundingRect())) newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
{ return newPos;
setPos(prevpt);
} }
update();
QGraphicsTextItem::mouseReleaseEvent(event);
} }
break;
void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) case ItemPositionHasChanged:
{
qDebug() << __PRETTY_FUNCTION__; foreach (Edge *edge, m_edgeList) edge->adjust();
break;
default:
break;
};
QGraphicsTextItem::mouseMoveEvent(event); return QGraphicsItem::itemChange(change, value);
} }
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w) void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w)
@ -85,6 +75,4 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
painter->drawRect(QRect(boundingRect().topLeft().toPoint(), painter->drawRect(QRect(boundingRect().topLeft().toPoint(),
boundingRect().bottomRight().toPoint() - boundingRect().bottomRight().toPoint() -
QPoint(1,1))); QPoint(1,1)));
prevpt = pos();
} }

@ -11,23 +11,21 @@ class GraphWidget;
class Node : public QGraphicsTextItem class Node : public QGraphicsTextItem
{ {
public: public:
Node(GraphWidget *graphWidget = 0); Node(GraphWidget *graphWidget = 0);
void addEdge(Edge *edge); void addEdge(Edge *edge);
QList<Edge *> edges() const; QList<Edge *> edges() const;
protected: protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value); 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); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private: private:
QList<Edge *> m_edgeList; QList<Edge *> m_edgeList;
GraphWidget *m_graph; GraphWidget *m_graph;
QPointF prevpt;
}; };
#endif // NODE_H #endif // NODE_H

@ -182,7 +182,7 @@
<value key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments" type="QString"></value> <value key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments" type="QString"></value>
<value key="Qt4ProjectManager.Qt4RunConfiguration.ProFile" type="QString">qtmindmap.pro</value> <value key="Qt4ProjectManager.Qt4RunConfiguration.ProFile" type="QString">qtmindmap.pro</value>
<value key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix" type="bool">false</value> <value key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix" type="bool">false</value>
<value key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal" type="bool">true</value> <value key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal" type="bool">false</value>
<valuelist key="Qt4ProjectManager.Qt4RunConfiguration.UserEnvironmentChanges" type="QVariantList"/> <valuelist key="Qt4ProjectManager.Qt4RunConfiguration.UserEnvironmentChanges" type="QVariantList"/>
<value key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory" type="QString"></value> <value key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory" type="QString"></value>
<value key="RunConfiguration.QmlDebugServerPort" type="uint">3768</value> <value key="RunConfiguration.QmlDebugServerPort" type="uint">3768</value>

Loading…
Cancel
Save