QVariant GraphicsItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if ( change == QGraphicsItem::ItemSelectedHasChanged ) {
QGraphicsItemGroup *g = dynamic_cast<QGraphicsItemGroup*>(parentItem());
if (!g)
setState(value.toBool() ? SelectionHandleActive : SelectionHandleOff);
else{
setSelected(false);
return QVariant::fromValue<bool>(false);
}
}
else if (change == ItemPositionChange && scene()) {
// value is the new position.
QPointF newPos = value.toPointF();
QRectF rect = scene()->sceneRect();
if (!rect.contains(newPos)) {
// Keep the item inside the scene rect.
newPos.setX(qMin(rect.right()-boundingRect().width()/2, qMax(newPos.x(), rect.left()+boundingRect().width()/2)));
newPos.setY(qMin(rect.bottom()-boundingRect().height()/2, qMax(newPos.y(), rect.top()+boundingRect().height()/2)));
return newPos;
}
}
return QGraphicsItem::itemChange(change, value);
}