Ejemplo n.º 1
0
void ImageMovementTool::mouseMoved(const QPointF &pos, Qt::KeyboardModifiers modifiers)
{
    AbstractImageTool::mouseMoved(pos, modifiers);

    if (mMousePressed) {
        if (ImageLayer *layer = currentImageLayer()) {
            QPointF diff = pos - mMouseStart;

            bool snapToGrid = Preferences::instance()->snapToGrid();
            bool snapToFineGrid = Preferences::instance()->snapToFineGrid();
            if (modifiers & Qt::ControlModifier) {
                snapToGrid = !snapToGrid;
                snapToFineGrid = false;
            }

            if (snapToGrid || snapToFineGrid) {
                MapRenderer *renderer = mapDocument()->renderer();
                int scale = snapToFineGrid ? Preferences::instance()->gridFine() : 1;
                const QPointF alignScreenPos =
                        renderer->pixelToScreenCoords(mLayerStart);
                const QPointF newAlignPixelPos = alignScreenPos + diff;

                // Snap the position to the grid
                QPointF newTileCoords =
                        (renderer->screenToTileCoords(newAlignPixelPos) * scale).toPoint();
                newTileCoords /= scale;
                diff = renderer->tileToScreenCoords(newTileCoords) - alignScreenPos;
            }

            layer->setPosition(mLayerStart + diff.toPoint());
            mapDocument()->emitImageLayerChanged(layer);
        }

    }
}
Ejemplo n.º 2
0
static QRectF cellRect(const MapRenderer &renderer,
                       const Cell &cell,
                       const QPointF &tileCoords)
{
    const Tile *tile = cell.tile();
    if (!tile)
        return QRectF();

    QPointF pixelCoords = renderer.tileToScreenCoords(tileCoords);
    QPointF offset = tile->offset();
    QSize size = tile->size();

    if (cell.flippedAntiDiagonally())
        std::swap(size.rwidth(), size.rheight());

    // This is a correction needed because tileToScreenCoords does not return
    // the bottom-left origin of the tile image, but rather the top-left
    // corner of the cell.
    pixelCoords.ry() += renderer.map()->tileHeight() - size.height();

    return QRectF(pixelCoords, size).translated(offset);
}