Skip to content

Instantly share code, notes, and snippets.

@kovrov
Last active December 22, 2015 14:59
Show Gist options
  • Save kovrov/6489240 to your computer and use it in GitHub Desktop.
Save kovrov/6489240 to your computer and use it in GitHub Desktop.
Pincho the pinchinator for flickable.
import QtQuick 2.0
Item {
id: main
width: 1280
height: (800-87)
Flickable {
id: flickable
anchors.fill: parent
Pincho {
minScale: Math.min(flickable.width / img.paintedWidth,
flickable.height / img.paintedHeight)
Item {
id: target
implicitWidth: img.paintedWidth
implicitHeight: img.paintedHeight
Image {
id: img
source: "http://farm6.staticflickr.com/5464/9695445266_94ef4d42ff_h.jpg"
}
}
onMinScaleChanged: {
flickable.contentHeight = target.implicitHeight * minScale
flickable.contentWidth = target.implicitWidth * minScale
target.scale = minScale
centerImage()
}
onPinchFinished: centerImage()
function centerImage() {
// Center image if smaller than flickable.
var x_off = Math.max(0, (flickable.width - flickable.contentWidth) / 2)
var y_off = Math.max(0, (flickable.height - flickable.contentHeight) / 2)
flickable.contentX += (x_off - x)
flickable.contentY += (y_off - y)
x = x_off
y = y_off
flickable.returnToBounds()
}
}
}
}
var scale
var flickable
var target
import QtQuick 2.0
import "Pincho.js" as D
PinchArea {
property real maxScale: 3
property real minScale: 1
Component.onCompleted: {
D.target = children[0]
D.target.transformOrigin = Item.TopLeft
for (var item = parent; item; item = item.parent) {
if (item.flicking !== undefined) {
D.flickable = item
break
}
}
D.flickable.contentWidth = Qt.binding(function() { return D.target.implicitWidth })
D.flickable.contentHeight = Qt.binding(function() { return D.target.implicitHeight })
width = Qt.binding(function() { return D.flickable.contentWidth })
height = Qt.binding(function() { return D.flickable.contentHeight })
}
onPinchStarted: {
D.flickable.interactive = false
D.scale = D.target.scale
}
onPinchUpdated: {
if (pinch.pointCount < 2)
return
var scale = Math.max(minScale, Math.min(pinch.scale * D.scale, maxScale))
var pinch_scale = scale / D.scale
D.flickable.resizeContent(D.target.implicitWidth * scale,
D.target.implicitHeight * scale,
Qt.point(pinch.startCenter.x * pinch_scale,
pinch.startCenter.y * pinch_scale))
D.target.scale = scale
}
onPinchFinished: D.flickable.interactive = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment