3
votes

I'm building a pinch and zoom feature in QML. For performance reasons, the pinchArea is a child of a flickable item.

The code below is set up where if the user double clicks on the image after manipulation, the image re-centers itself and the scale is reset to 1.

The unwanted behavior is : There is a feature of a flickable item where if the user taps somewhere in the screen after the image is zoomed/moved, the item re-centers itself. I don't want the image to recenter itself after the screen has been tapped, instead I want to control the behavior through a double click

One way I can semi-solve this is using a mouse area, but the mouse area moves when the flickable item moves, and clicking outside of the mouse area re-centers the item. Any ideas on how to modify the mouse area so that this action is prevented, or to turn off the behavior of tapping a flickable item and it centering itself after a pinch and zoom?

Thanks for the help / advice!

import QtQuick 2.0

Rectangle {
    width: 640
    height: 360
    color: "gray"

    Flickable {
        id: flick
        anchors.fill: parent
        contentWidth: 500
        contentHeight: 500

        Rectangle {
            width: flick.contentWidth
            height: flick.contentHeight
            id: imageFlick

            gradient: Gradient {
                GradientStop { position: 0.0; color: "red" }
                GradientStop { position: 1.0; color: "white" }
            }
        }

        PinchArea {
            width: Math.max(flick.contentWidth, flick.width)
            height: Math.max(flick.contentHeight, flick.height)

            pinch.minimumScale: 1
            pinch.maximumScale: 10
            pinch.dragAxis: Pinch.XAndYAxis
            pinch.target: imageFlick

            property real initialWidth
            property real initialHeight

            onPinchStarted: {
                initialWidth = flick.contentWidth
                initialHeight = flick.contentHeight
                flick.interactive = false
            }

            onPinchUpdated: {
                flick.contentX += pinch.previousCenter.x - pinch.center.x
                flick.contentY += pinch.previousCenter.y - pinch.center.y
            }

            onPinchFinished: {
                flick.interactive = true
            }

            MouseArea {
                anchors.fill: flick
                width: flick.width
                height: flick.height

                //Prevents behaivor of recentering on tap within mouse area
                //                onClicked: {
                //                    flick.cancelFlick()
                //                }

                //For debugging - if tap inside area, recentering doesn't happen. If you tap
                // outside of area, recentering happens
                //                Rectangle {
                //                    anchors.fill:parent
                //                    color: "blue"
                //                }

                onDoubleClicked: {
                    flick.contentX = 0
                    flick.contentY = 0
                    imageFlick.scale = 1
                }
            }
        }
    }
}
1

1 Answers

0
votes

I managed to answer the question myself. For onPinchUpdated, flick.contentX and flick.contentY needed to be changed to flick.x and flick.y. I also changed the ordering of some elements and how they were nested.

Now the unwanted behavior of the rectangle recentering on a click no longer happens.


        Rectangle {
            width: 900
            height: parent.height

            Rectangle { // pinch area
                id: test
                width: 800
                height: 500
                anchors.centerIn: parent
                color: "grey"
                clip: true

                Flickable {
                    id: flick
                    anchors.fill: parent
                    contentWidth: imageFlick.width
                    contentHeight: imageFlick.height

                    PinchArea {
                        id: pinchROI
                        width: Math.max(flick.contentWidth, flick.width)
                        height: Math.max(flick.contentHeight, flick.height)

                        pinch.minimumScale: 1
                        pinch.maximumScale: 10
                        pinch.dragAxis: Pinch.XAndYAxis
                        pinch.target: imageFlick

                        property real initialWidth
                        property real initialHeight

                        onPinchStarted: {
                            initialWidth = flick.contentWidth
                            initialHeight = flick.contentHeight
                            flick.interactive = false
                        }

                        onPinchUpdated: {
                            flick.x = pinch.previousCenter.x - pinch.center.x
                            flick.y = pinch.previousCenter.y - pinch.center.y
                        }

                        onPinchFinished: {
                            flick.interactive = true
                        }

                        MouseArea {
                            anchors.fill: parent

                            Rectangle {
                                id: imageFlick
                                width: 800
                                height: 500

                                gradient: Gradient {
                                    GradientStop { position: 0.0; color: "red" }
                                    GradientStop { position: 1.0; color: "yellow" }
                                }
                            }

                            onDoubleClicked: {
                                imageFlick.scale = 1
                                imageFlick.x = 0
                                imageFlick.y = 0
                            }
                        }
                    }
                }
            }
        }