void MapObjectItem::syncWithMapObject() { // Update the whole object when the name or type has changed if (mObject->name() != mName || mObject->type() != mType) { mName = mObject->name(); mType = mObject->type(); update(); mResizeHandle->update(); } QString toolTip = mName; if (!mType.isEmpty()) toolTip += QLatin1String(" (") + mType + QLatin1String(")"); setToolTip(toolTip); MapRenderer *renderer = mMapDocument->renderer(); const QPointF pixelPos = renderer->tileToPixelCoords(mObject->position()); QRectF bounds = renderer->boundingRect(mObject); bounds.translate(-pixelPos); mSyncing = true; setPos(pixelPos); if (mBoundingRect != bounds) { // Notify the graphics scene about the geometry change in advance prepareGeometryChange(); mBoundingRect = bounds; const QPointF bottomRight = mObject->bounds().bottomRight(); const QPointF handlePos = renderer->tileToPixelCoords(bottomRight); mResizeHandle->setPos(handlePos - pixelPos); } mSyncing = false; }
QVariant MapObjectItem::itemChange(GraphicsItemChange change, const QVariant &value) { if (!mSyncing) { MapRenderer *renderer = mMapDocument->renderer(); if (change == ItemPositionChange && (QApplication::keyboardModifiers() & Qt::ControlModifier)) { const QPointF pixelDiff = value.toPointF() - mOldItemPos; const QPointF newPixelPos = renderer->tileToPixelCoords(mOldObjectPos) + pixelDiff; // Snap the position to the grid const QPointF newTileCoords = renderer->pixelToTileCoords(newPixelPos).toPoint(); return renderer->tileToPixelCoords(newTileCoords); } else if (change == ItemPositionHasChanged) { // Update the position of the map object const QPointF pixelDiff = value.toPointF() - mOldItemPos; const QPointF newPixelPos = renderer->tileToPixelCoords(mOldObjectPos) + pixelDiff; mObject->setPosition(renderer->pixelToTileCoords(newPixelPos)); } } return QGraphicsItem::itemChange(change, value); }
void MapObjectItem::syncWithMapObject() { // Update the whole object when the name or polygon has changed if (mObject->name() != mName || mObject->polygon() != mPolygon) { mName = mObject->name(); mPolygon = mObject->polygon(); update(); } const QColor color = objectColor(mObject); if (mColor != color) { mColor = color; update(); mResizeHandle->update(); } QString toolTip = mName; const QString &type = mObject->type(); if (!type.isEmpty()) toolTip += QLatin1String(" (") + type + QLatin1String(")"); setToolTip(toolTip); MapRenderer *renderer = mMapDocument->renderer(); const QPointF pixelPos = renderer->tileToPixelCoords(mObject->position()); QRectF bounds = renderer->boundingRect(mObject); bounds.translate(-pixelPos); setPos(pixelPos); setZValue(pixelPos.y()); setRotation(mObject->rotation()); setTransformOriginPoint(objectCenter()); mSyncing = true; if (mBoundingRect != bounds) { // Notify the graphics scene about the geometry change in advance prepareGeometryChange(); mBoundingRect = bounds; const QPointF bottomRight = mObject->bounds().bottomRight(); const QPointF handlePos = renderer->tileToPixelCoords(bottomRight); mResizeHandle->setPos(handlePos - pixelPos); } mSyncing = false; setVisible(mObject->isVisible()); }
QVariant ResizeHandle::itemChange(GraphicsItemChange change, const QVariant &value) { if (!mMapObjectItem->mSyncing) { MapRenderer *renderer = mMapObjectItem->mapDocument()->renderer(); if (change == ItemPositionChange) { // Calculate the absolute pixel position const QPointF itemPos = mMapObjectItem->pos(); QPointF pixelPos = value.toPointF() + itemPos; // Calculate the new coordinates in tiles QPointF tileCoords = renderer->pixelToTileCoords(pixelPos); const QPointF objectPos = mMapObjectItem->mapObject()->position(); tileCoords -= objectPos; tileCoords.setX(qMax(tileCoords.x(), qreal(0))); tileCoords.setY(qMax(tileCoords.y(), qreal(0))); if (QApplication::keyboardModifiers() & Qt::ControlModifier) tileCoords = tileCoords.toPoint(); tileCoords += objectPos; return renderer->tileToPixelCoords(tileCoords) - itemPos; } else if (change == ItemPositionHasChanged) { // Update the size of the map object const QPointF newPos = value.toPointF() + mMapObjectItem->pos(); QPointF tileCoords = renderer->pixelToTileCoords(newPos); tileCoords -= mMapObjectItem->mapObject()->position(); mMapObjectItem->resize(QSizeF(tileCoords.x(), tileCoords.y())); } } return QGraphicsItem::itemChange(change, value); }