QGCMovableItem.qml 2.18 KB
Newer Older
1 2
import QtQuick 2.3
import QtQuick.Controls 1.2
3
import QGroundControl.ScreenTools 1.0
4 5 6 7 8 9 10 11

// This item can be dragged around within its parent.
// Double click issues a signal the parent can use to
// reset its default position.

Item {
    id: root
    property bool   allowDragging:  true
12 13
    property real   minimumWidth:   ScreenTools.defaultFontPixelHeight * (5)
    property real   minimumHeight:  ScreenTools.defaultFontPixelHeight * (5)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    property alias  tForm:          tform
    signal          resetRequested()
    transform: Scale {
        id: tform
    }
    MouseArea {
        property double factor: 25
        enabled:            root.allowDragging
        cursorShape:        Qt.OpenHandCursor
        anchors.fill:       parent
        drag.target:        parent
        drag.axis:          Drag.XAndYAxis
        drag.minimumX:      0
        drag.minimumY:      0
        drag.maximumX:      root.parent.width  - (root.width  * tform.xScale)
        drag.maximumY:      root.parent.height - (root.height * tform.yScale)
        drag.filterChildren: true
        onPressed: {
            root.anchors.left  = undefined
            root.anchors.right = undefined
        }
        onDoubleClicked: {
            root.resetRequested();
        }
        onWheel:
        {
            var zoomFactor = 1;
            if(wheel.angleDelta.y > 0)
                zoomFactor = 1 + (1/factor)
            else
                zoomFactor = 1 - (1/factor)
            var realX = wheel.x * tform.xScale
            var realY = wheel.y * tform.yScale
            var tx = root.x + (1-zoomFactor)*realX
            var ty = root.y + (1-zoomFactor)*realY
            if(tx < 0) tx = 0
            if(ty < 0) ty = 0
            var ts = tform.xScale * zoomFactor
            if(root.width * ts >= root.minimumWidth) {
                if(root.height * ts >= root.minimumHeight) {
                    if(((root.width * ts) + tx) < root.parent.width && ((root.height * ts) + ty) < root.parent.height) {
                        root.x = tx
                        root.y = ty
                        tform.xScale = ts
                        tform.yScale = ts
                    }
                }
            }
        }
    }
}