Как правильно сдеать относительный zooming на QML

Рейтинг: 0Ответов: 0Опубликовано: 16.04.2015

Накидал вот такую кодину:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            MenuItem {
                text: qsTr("&Open")
                onTriggered: messageDialog.show(qsTr("Open action triggered"));
            }
            MenuItem {
                text: qsTr("E&xit")
                onTriggered: Qt.quit();
            }
        }
    }

    Rectangle {
        anchors.fill: parent
        color: '#454545'

        Component.onCompleted: {
            console.log(width/2.0, height/2.0)
        }

        Rectangle {
            id:scheme
            color: 'green'
            width: 500
            height: 500

            Rectangle {
                color: 'red'
                width: 100
                height: 100
                anchor.centrIn: parent
            }


            onXChanged: {
                var bound = parent.width/2

                if (x > bound)
                    x = bound

                if (x + width < bound)
                    x = bound - width


                console.log('parent.width/2:', parent.width/2)
                console.log(x, y)
            }

            onYChanged: {
                var bound = parent.height/2

                if (y > parent.height/2)
                    y = parent.height/2

                if (y + height < bound)
                    y = bound - height

                console.log('parent.height/2:', parent.height/2)
                console.log(x, y)
            }

                onScaleChanged: {

                }

            function scaleToPoint(value, originX, originY){
                schemeScale.origin = Qt.vector3d(originX, originY, 0)
                schemeScale.xScale += value
                schemeScale.yScale += value
            }

            transform: [Scale{
                    id: schemeScale
                    Behavior on xScale {
                        NumberAnimation{duration: 300}
                    }

                    Behavior on yScale {
                        NumberAnimation{duration: 300}
                    }

                    Behavior on origin {
                        Vector3dAnimation{duration: 300}
                    }
                }]

        }
    }

    MouseArea {
        anchors.fill: parent
        drag.target: scheme
        drag.axis: Drag.XAndYAxis

        onWheel: {
            var origin = mapToItem(scheme, wheel.x, wheel.y)
            scheme.scaleToPoint(wheel.angleDelta.y/1200, origin.x, origin.y)
            console.log('Wheel:', wheel.angleDelta.y/1200, '; Origin:', origin.x, origin.y)
        }
    }

    MessageDialog {
        id: messageDialog
        title: qsTr("May I have your attention, please?")

        function show(caption) {
            messageDialog.text = caption;
            messageDialog.open();
        }
    }
}

Так вот он отрабатывает вроде бы как надо, но подглюючивает. Т.е. уезжает точка относительно которой происходи зумирование. Помогите разобраться, почему он иногда неправильно считает?

Ответы

Ответов пока нет.