Применение абсолютного масштабирования к QGraphicsPixmapItems

В настоящее время я использую QTransform для масштабирования элементов, но так как мои элементы изменяются с помощью мыши, результаты выглядят неловко, когда несколько элементов выбираются, а затем изменяются размеры. Поэтому я хотел масштабировать элементы с абсолютным масштабированием, но, похоже, нет способа применить абсолютное масштабирование для всех элементов, хотя для некоторых элементов, таких как QGraphicsRectItem, это можно сделать, установив углы.

Вот мой код для реализации масштабирования, хотя мне еще предстоит прикоснуться к поворотам.

void Transform::move(QGraphicsSceneMouseEvent *event)
{
    _curPos = event->scenePos();
    qreal dx = _curPos.x() - _prevPos.x();
    qreal dy = _curPos.y() - _prevPos.y();

    if (_mouseButton == Qt::LeftButton){
        switch(_handle){

        case TransformTool::BoundingRectItem::HotSpot::Move:{
            foreach(QGraphicsItem *itm,_scene->selectedItems()) {
                itm->moveBy(dx,dy);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::ScaleBottomRightCorner:{
            _scalex += dx*0.002;
            _scaley += dy*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::ScaleTopLeftCorner:{
            _scalex -= dx*0.002;
            _scaley -= dy*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
                _curItems[i]->moveBy(dx,dy);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::ScaleTopRightCorner:{
            _scalex += dx*0.002;
            _scaley -= dy*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
                _curItems[i]->moveBy(dx,dy);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::ScaleBottomLeftCorner:{
            _scalex += dx*0.002;
            _scaley -= dy*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
                _curItems[i]->moveBy(dx,dy);
            }
            break;
        }
        case TransformTool::BoundingRectItem::HotSpot::ScaleTopBoundary:{
            _scaley -= dy*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
                _curItems[i]->moveBy(0,dy);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::ScaleBottomBoundary:{
            _scaley += dy*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::ScaleLeftBoundary:{
            _scalex -= dx*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
                _curItems[i]->moveBy(dx,0);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::ScaleRightBoundary:{
            _scalex += dx*0.002;
            for(int i=0;i<_curItems.length();i++){
                _curState[i].scale(_scalex,_scaley);
                _curItems[i]->setTransform(_curState[i],false);
            }
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::RotateBottomLeftCorner:{
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::RotateBottomRightCorner:{
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::RotateTopLeftCorner:{
            break;
        }

        case TransformTool::BoundingRectItem::HotSpot::RotateTopRightCorner:{
            break;
        }
        }
    }

    _scalex = 1;
    _scaley = 1;
    _prevPos = _curPos;
    updateBounds();
}

Вот _handle это одна из 8 точек вокруг ограничивающего прямоугольника (аналогично тому, что есть в фотошопе), _curItems это список выбранных элементов, _curState список соответствующих преобразований Вы можете найти полный код программы здесь

0 ответов

Другие вопросы по тегам