void EditPolygonTool::startMoving() { // Move only the clicked handle, if it was not part of the selection if (!mSelectedHandles.contains(mClickedHandle)) setSelectedHandle(mClickedHandle); mMode = Moving; MapRenderer *renderer = mapDocument()->renderer(); // Remember the current object positions mOldHandlePositions.clear(); mOldPolygons.clear(); mAlignPosition = renderer->screenToPixelCoords((*mSelectedHandles.begin())->pos()); const auto &selectedHandles = mSelectedHandles; for (PointHandle *handle : selectedHandles) { const QPointF pos = renderer->screenToPixelCoords(handle->pos()); mOldHandlePositions.append(handle->pos()); if (pos.x() < mAlignPosition.x()) mAlignPosition.setX(pos.x()); if (pos.y() < mAlignPosition.y()) mAlignPosition.setY(pos.y()); MapObject *mapObject = handle->mapObject(); if (!mOldPolygons.contains(mapObject)) mOldPolygons.insert(mapObject, mapObject->polygon()); } }
void EditPolygonTool::updateMovingItems(const QPointF &pos, Qt::KeyboardModifiers modifiers) { MapRenderer *renderer = mapDocument()->renderer(); QPointF diff = pos - mStart; SnapHelper snapHelper(renderer, modifiers); if (snapHelper.snaps()) { const QPointF alignScreenPos = renderer->pixelToScreenCoords(mAlignPosition); const QPointF newAlignScreenPos = alignScreenPos + diff; QPointF newAlignPixelPos = renderer->screenToPixelCoords(newAlignScreenPos); snapHelper.snap(newAlignPixelPos); diff = renderer->pixelToScreenCoords(newAlignPixelPos) - alignScreenPos; } const auto &selectedHandles = mSelectedHandles; int i = 0; for (PointHandle *handle : selectedHandles) { // update handle position const QPointF newScreenPos = mOldHandlePositions.at(i) + diff; handle->setPos(newScreenPos); // calculate new pixel position of polygon node const MapObjectItem *item = handle->mapObjectItem(); const QPointF newInternalPos = item->mapFromScene(newScreenPos); const QPointF newScenePos = item->pos() + newInternalPos; const QPointF newPixelPos = renderer->screenToPixelCoords(newScenePos); // update the polygon MapObject *mapObject = item->mapObject(); QPolygonF polygon = mapObject->polygon(); polygon[handle->pointIndex()] = newPixelPos - mapObject->position(); mapDocument()->mapObjectModel()->setObjectPolygon(mapObject, polygon); ++i; } }