| 1 |
/* |
| 2 |
* Copyright 2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.0 |
| 18 |
import Lomiri.Components 1.3 |
| 19 |
|
| 20 |
Item { |
| 21 |
id: root |
| 22 |
width: 500 |
| 23 |
height: width * (15.0 / 16.0) |
| 24 |
focus: true |
| 25 |
|
| 26 |
// Zoom properties. |
| 27 |
property variant translation: Qt.point(0.0, 0.0) |
| 28 |
property real scaleBase: 1.1 |
| 29 |
property real scaleExponent: 0.0 |
| 30 |
property real minScaleExponent: 0.0 |
| 31 |
property real maxScaleExponent: 48.317715856 // Logarithm of 100 to base 1.1 (scaleBase). |
| 32 |
property real scaleFactor: 1.0 |
| 33 |
property real scale: 1.0 |
| 34 |
|
| 35 |
// Overlay text properties. |
| 36 |
property string textOverlayString: |
| 37 |
"Zoom (scroll): x " + root.scaleFactor.toFixed(1) + "\n" + |
| 38 |
"Width (a): " + shape.width.toFixed(2) + "\n" + |
| 39 |
"Height (z): " + shape.height.toFixed(2) |
| 40 |
|
| 41 |
// Main scene. |
| 42 |
Item { |
| 43 |
id: scene |
| 44 |
anchors.fill: parent |
| 45 |
|
| 46 |
Rectangle { |
| 47 |
id: background |
| 48 |
anchors.fill: parent |
| 49 |
color: "white" |
| 50 |
} |
| 51 |
|
| 52 |
ProportionalShape { |
| 53 |
id: shape |
| 54 |
anchors.centerIn: parent |
| 55 |
width: 300 |
| 56 |
source: Image { source: "music-app.png" } |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Zoom support. |
| 61 |
ShaderEffectSource { |
| 62 |
id: shaderEffectSource |
| 63 |
anchors.fill: scene |
| 64 |
sourceItem: scene |
| 65 |
hideSource: true |
| 66 |
visible: false |
| 67 |
smooth: false |
| 68 |
} |
| 69 |
ShaderEffect { |
| 70 |
anchors.fill: scene |
| 71 |
property variant tex: shaderEffectSource |
| 72 |
property variant translation: root.translation |
| 73 |
property real scaleFactor: root.scale |
| 74 |
vertexShader: " |
| 75 |
uniform mat4 qt_Matrix; |
| 76 |
uniform float scaleFactor; |
| 77 |
uniform vec2 translation; |
| 78 |
attribute vec4 qt_Vertex; |
| 79 |
attribute vec2 qt_MultiTexCoord0; |
| 80 |
varying vec2 texCoord; |
| 81 |
void main() { |
| 82 |
texCoord = vec2(scaleFactor) * qt_MultiTexCoord0 + translation; |
| 83 |
gl_Position = qt_Matrix * qt_Vertex; |
| 84 |
}" |
| 85 |
fragmentShader: " |
| 86 |
uniform sampler2D tex; |
| 87 |
uniform float qt_Opacity; |
| 88 |
varying vec2 texCoord; |
| 89 |
void main() { |
| 90 |
gl_FragColor = texture2D(tex, texCoord) * qt_Opacity; |
| 91 |
}" |
| 92 |
} |
| 93 |
|
| 94 |
// Text overlay. |
| 95 |
Text { |
| 96 |
id: textOverlay |
| 97 |
width:200 |
| 98 |
anchors.top: parent.top |
| 99 |
anchors.topMargin: 10 |
| 100 |
anchors.left: parent.left |
| 101 |
anchors.leftMargin: 10 |
| 102 |
font.family: "Ubuntu Mono" |
| 103 |
font.pixelSize: 14 |
| 104 |
font.weight: Font.Bold |
| 105 |
color: "black" |
| 106 |
text: textOverlayString |
| 107 |
} |
| 108 |
|
| 109 |
// Mouse handling. |
| 110 |
MouseArea { |
| 111 |
id: mouseArea |
| 112 |
anchors.fill: parent |
| 113 |
acceptedButtons: Qt.LeftButton |
| 114 |
hoverEnabled: true |
| 115 |
|
| 116 |
property real lastX: 0.0 |
| 117 |
property real lastY: 0.0 |
| 118 |
|
| 119 |
onPressed: { |
| 120 |
if (pressedButtons & Qt.LeftButton) { |
| 121 |
lastX = mouseX; |
| 122 |
lastY = mouseY; |
| 123 |
} |
| 124 |
} |
| 125 |
onPositionChanged: { |
| 126 |
if (pressedButtons & Qt.LeftButton) { |
| 127 |
var tx = root.translation.x; |
| 128 |
var ty = root.translation.y; |
| 129 |
var sx = root.scale / root.width; |
| 130 |
var sy = root.scale / root.height; |
| 131 |
var x = mouseX - lastX; |
| 132 |
var y = mouseY - lastY; |
| 133 |
root.translation = Qt.point(Math.max(0.0, Math.min(1.0 - root.scale, tx - sx * x)), |
| 134 |
Math.max(0.0, Math.min(1.0 - root.scale, ty - sy * y))); |
| 135 |
lastX = mouseX; |
| 136 |
lastY = mouseY; |
| 137 |
} |
| 138 |
} |
| 139 |
onWheel: { |
| 140 |
root.scaleExponent = Math.max(minScaleExponent, Math.min(maxScaleExponent, |
| 141 |
root.scaleExponent + (wheel.angleDelta.y < 0.0 ? -1.0 : 1.0))); |
| 142 |
root.scaleFactor = Math.pow(root.scaleBase, root.scaleExponent); |
| 143 |
var oldScale = root.scale; |
| 144 |
root.scale = 1.0 / root.scaleFactor; |
| 145 |
var s = oldScale - root.scale; |
| 146 |
var tx = root.translation.x; |
| 147 |
var ty = root.translation.y; |
| 148 |
var x = mouseX / root.width; |
| 149 |
var y = mouseY / root.height; |
| 150 |
root.translation = Qt.point(Math.max(0.0, Math.min(1.0 - root.scale, tx + s * x)), |
| 151 |
Math.max(0.0, Math.min(1.0 - root.scale, ty + s * y))); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Keyboard handling. |
| 156 |
Keys.onPressed: { |
| 157 |
var shift = Qt.ShiftModifier; |
| 158 |
|
| 159 |
if (event.key == Qt.Key_A) { |
| 160 |
shape.width = Math.max(0.0, shape.width + ((event.modifiers & shift) ? 2.0 : -2.0)); |
| 161 |
} else if (event.key == Qt.Key_Z) { |
| 162 |
shape.height = Math.max(0.0, shape.height + ((event.modifiers & shift) ? 2.0 : -2.0)); |
| 163 |
} |
| 164 |
} |
| 165 |
} |