| 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: 1000 |
| 23 |
height: 600 |
| 24 |
focus: true |
| 25 |
|
| 26 |
// Enum to string tables. |
| 27 |
property variant aspectTable: [ |
| 28 |
"Flat", "Inset", "DropShadow" |
| 29 |
] |
| 30 |
property variant backgroundModeTable: [ |
| 31 |
"SolidColor", "VerticalGradient" |
| 32 |
] |
| 33 |
property variant sourceHAlignmentTable: [ |
| 34 |
"AlignLeft", "AlignHCenter", "AlignRight" |
| 35 |
] |
| 36 |
property variant sourceVAlignmentTable: [ |
| 37 |
"AlignTop", "AlignVCenter", "AlignBottom" |
| 38 |
] |
| 39 |
property variant sourceFillModeTable: [ |
| 40 |
"Stretch", "PreserveAspectFit", "PreserveAspectCrop", "Pad" |
| 41 |
] |
| 42 |
property variant wrapModeTable: [ |
| 43 |
"Transparent", "Repeat" |
| 44 |
] |
| 45 |
property variant imageFillModeTable: [ |
| 46 |
"Stretch", "PreserveAspectFit", "PreserveAspectCrop", "Tile", "TileVertically", |
| 47 |
"TileHorizontally", "Pad" |
| 48 |
] |
| 49 |
|
| 50 |
// Zoom properties. |
| 51 |
property variant translation: Qt.point(0.0, 0.0) |
| 52 |
property real scaleBase: 1.1 |
| 53 |
property real scaleExponent: 0.0 |
| 54 |
property real minScaleExponent: 0.0 |
| 55 |
property real maxScaleExponent: 48.317715856 // Logarithm of 100 to base 1.1 (scaleBase). |
| 56 |
property real scaleFactor: 1.0 |
| 57 |
property real scale: 1.0 |
| 58 |
|
| 59 |
// Overlay text properties. French keymapping... :) |
| 60 |
property string textOverlayString: |
| 61 |
"Zoom (scroll): x " + root.scaleFactor.toFixed(1) + "\n\n" + |
| 62 |
"Background colors (a/z): " + shape.backgroundColor + ", " + shape.secondaryBackgroundColor + "\n" + |
| 63 |
"Background mode (e): " + root.backgroundModeTable[shape.backgroundMode] + "\n\n" + |
| 64 |
"Radius (r): " + "\"" + shape.radius + "\"\n" + |
| 65 |
"Aspect (t): " + root.aspectTable[shape.aspect] + "\n" + |
| 66 |
"relativeRadius (y): " + shape.relativeRadius.toFixed(2) + "\n\n" + |
| 67 |
"Source (o): " + shape.source + "\n" + |
| 68 |
"Source opacity (p): " + shape.sourceOpacity.toFixed(2) + "\n" + |
| 69 |
"Source fill (q): " + root.sourceFillModeTable[shape.sourceFillMode] + "\n" + |
| 70 |
"Source hwrap (s): " + root.wrapModeTable[shape.sourceHorizontalWrapMode] + "\n" + |
| 71 |
"Source vwrap (d): " + root.wrapModeTable[shape.sourceVerticalWrapMode] + "\n" + |
| 72 |
"Source halign (f): " + root.sourceHAlignmentTable[shape.sourceHorizontalAlignment] + "\n" + |
| 73 |
"Source valign (g): " + root.sourceVAlignmentTable[shape.sourceVerticalAlignment] + "\n" + |
| 74 |
"Source translation (h/j): " + shape.sourceTranslation.x.toFixed(2) + ", " + shape.sourceTranslation.y.toFixed(2) + "\n" + |
| 75 |
"Source scale (k/l): " + shape.sourceScale.x.toFixed(2) + ", " + shape.sourceScale.y.toFixed(2) + "\n\n" + |
| 76 |
"Image (deprecated) (m): " + shape.image + "\n" + |
| 77 |
"Image fill (w): " + root.imageFillModeTable[img1.fillMode] + "\n" + |
| 78 |
"Image halign (x): " + img1.horizontalAlignment + "\n" + |
| 79 |
"Image valign (c): " + img1.verticalAlignment + "\n\n" + |
| 80 |
"Border (deprecated) (b): " + "\"" + shape.borderSource + "\"\n\n" + |
| 81 |
"Colors (deprecated) (n/,): " + shape.color + ", " + shape.gradientColor + "\n\n" + |
| 82 |
"Overlay color (F1): " + shape.overlayColor + "\n" + |
| 83 |
"Overlay rect (F2/F3/F4/F5): " + shape.overlayRect.x.toFixed(2) + ", " + shape.overlayRect.y.toFixed(2) + ", " + shape.overlayRect.width.toFixed(2) + ", " + shape.overlayRect.height.toFixed(2) |
| 84 |
|
| 85 |
// Main scene. |
| 86 |
Item { |
| 87 |
id: scene |
| 88 |
anchors.fill: parent |
| 89 |
|
| 90 |
Image { |
| 91 |
id: background |
| 92 |
anchors.fill: parent |
| 93 |
source: "background.jpg" |
| 94 |
fillMode: Image.Tile |
| 95 |
} |
| 96 |
|
| 97 |
// Put the LomiriShape source image in the middle of a texture atlas. We use img1. |
| 98 |
Image { id: img1; visible: false; source: "img1.png"; } |
| 99 |
Image { id: img2; visible: false; source: "img2.png"; } |
| 100 |
Image { id: img3; visible: false; source: "img3.png"; } |
| 101 |
Image { id: img4; visible: false; source: "img4.png"; } |
| 102 |
|
| 103 |
LomiriShapeOverlay { |
| 104 |
id: shape |
| 105 |
anchors.fill: parent |
| 106 |
anchors.leftMargin: 400 |
| 107 |
anchors.rightMargin: 100 |
| 108 |
anchors.topMargin: 100 |
| 109 |
anchors.bottomMargin: 100 |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Zoom support. |
| 114 |
ShaderEffectSource { |
| 115 |
id: shaderEffectSource |
| 116 |
anchors.fill: scene |
| 117 |
sourceItem: scene |
| 118 |
hideSource: true |
| 119 |
visible: false |
| 120 |
smooth: false |
| 121 |
} |
| 122 |
ShaderEffect { |
| 123 |
anchors.fill: scene |
| 124 |
property variant tex: shaderEffectSource |
| 125 |
property variant translation: root.translation |
| 126 |
property real scaleFactor: root.scale |
| 127 |
vertexShader: " |
| 128 |
uniform mat4 qt_Matrix; |
| 129 |
uniform float scaleFactor; |
| 130 |
uniform vec2 translation; |
| 131 |
attribute vec4 qt_Vertex; |
| 132 |
attribute vec2 qt_MultiTexCoord0; |
| 133 |
varying vec2 texCoord; |
| 134 |
void main() { |
| 135 |
texCoord = vec2(scaleFactor) * qt_MultiTexCoord0 + translation; |
| 136 |
gl_Position = qt_Matrix * qt_Vertex; |
| 137 |
}" |
| 138 |
fragmentShader: " |
| 139 |
uniform sampler2D tex; |
| 140 |
uniform float qt_Opacity; |
| 141 |
varying vec2 texCoord; |
| 142 |
void main() { |
| 143 |
gl_FragColor = texture2D(tex, texCoord) * qt_Opacity; |
| 144 |
}" |
| 145 |
} |
| 146 |
|
| 147 |
// Text overlay. |
| 148 |
Text { |
| 149 |
id: textOverlay |
| 150 |
width:200 |
| 151 |
anchors.top: parent.top |
| 152 |
anchors.topMargin: 10 |
| 153 |
anchors.left: parent.left |
| 154 |
anchors.leftMargin: 10 |
| •155 |
font.family: "Noto Mono" |
| 156 |
font.pixelSize: 14 |
| 157 |
font.weight: Font.Bold |
| 158 |
color: "black" |
| 159 |
text: textOverlayString |
| 160 |
} |
| 161 |
|
| 162 |
// Mouse handling. |
| 163 |
MouseArea { |
| 164 |
id: mouseArea |
| 165 |
anchors.fill: parent |
| 166 |
acceptedButtons: Qt.LeftButton |
| 167 |
hoverEnabled: true |
| 168 |
|
| 169 |
property real lastX: 0.0 |
| 170 |
property real lastY: 0.0 |
| 171 |
|
| 172 |
onPressed: { |
| 173 |
if (pressedButtons & Qt.LeftButton) { |
| 174 |
lastX = mouseX; |
| 175 |
lastY = mouseY; |
| 176 |
} |
| 177 |
} |
| 178 |
onPositionChanged: { |
| 179 |
if (pressedButtons & Qt.LeftButton) { |
| 180 |
var tx = root.translation.x; |
| 181 |
var ty = root.translation.y; |
| 182 |
var sx = root.scale / root.width; |
| 183 |
var sy = root.scale / root.height; |
| 184 |
var x = mouseX - lastX; |
| 185 |
var y = mouseY - lastY; |
| 186 |
root.translation = Qt.point(Math.max(0.0, Math.min(1.0 - root.scale, tx - sx * x)), |
| 187 |
Math.max(0.0, Math.min(1.0 - root.scale, ty - sy * y))); |
| 188 |
lastX = mouseX; |
| 189 |
lastY = mouseY; |
| 190 |
} |
| 191 |
} |
| 192 |
onWheel: { |
| 193 |
root.scaleExponent = Math.max(minScaleExponent, Math.min(maxScaleExponent, |
| 194 |
root.scaleExponent + (wheel.angleDelta.y < 0.0 ? -1.0 : 1.0))); |
| 195 |
root.scaleFactor = Math.pow(root.scaleBase, root.scaleExponent); |
| 196 |
var oldScale = root.scale; |
| 197 |
root.scale = 1.0 / root.scaleFactor; |
| 198 |
var s = oldScale - root.scale; |
| 199 |
var tx = root.translation.x; |
| 200 |
var ty = root.translation.y; |
| 201 |
var x = mouseX / root.width; |
| 202 |
var y = mouseY / root.height; |
| 203 |
root.translation = Qt.point(Math.max(0.0, Math.min(1.0 - root.scale, tx + s * x)), |
| 204 |
Math.max(0.0, Math.min(1.0 - root.scale, ty + s * y))); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// Keyboard handling. |
| 209 |
Keys.onPressed: { |
| 210 |
var shift = Qt.ShiftModifier; |
| 211 |
var alt = Qt.AltModifier; |
| 212 |
|
| 213 |
// Background. |
| 214 |
if (event.key == Qt.Key_A) { |
| 215 |
shape.backgroundColor = Qt.rgba( |
| 216 |
Math.random(), Math.random(), Math.random(), Math.random()); |
| 217 |
} else if (event.key == Qt.Key_Z) { |
| 218 |
shape.secondaryBackgroundColor = Qt.rgba( |
| 219 |
Math.random(), Math.random(), Math.random(), Math.random()); |
| 220 |
} else if (event.key == Qt.Key_E) { |
| 221 |
shape.backgroundMode = (shape.backgroundMode + 1) % 3; |
| 222 |
|
| 223 |
// Styling. |
| 224 |
} else if (event.key == Qt.Key_R) { |
| 225 |
shape.radius = (shape.radius == "large") ? "small" : |
| 226 |
((shape.radius == "small") ? "medium" : "large"); |
| 227 |
} else if (event.key == Qt.Key_T) { |
| 228 |
shape.aspect = (shape.aspect + 1) % 3; |
| 229 |
} else if (event.key == Qt.Key_Y) { |
| 230 |
shape.relativeRadius = Math.max(0.0, Math.min( |
| 231 |
1.0, shape.relativeRadius + ((event.modifiers & shift) ? 0.01 : -0.01))); |
| 232 |
|
| 233 |
// Source. |
| 234 |
} else if (event.key == Qt.Key_O) { |
| 235 |
if (shape.source == null) { |
| 236 |
shape.source = img1; |
| 237 |
} else { |
| 238 |
shape.source = null; |
| 239 |
} |
| 240 |
} else if (event.key == Qt.Key_P) { |
| 241 |
shape.sourceOpacity = Math.max(0.0, Math.min( |
| 242 |
1.0, shape.sourceOpacity + ((event.modifiers & shift) ? 0.01 : -0.01))); |
| 243 |
} else if (event.key == Qt.Key_Q) { |
| 244 |
shape.sourceFillMode = (shape.sourceFillMode + 1) % 4; |
| 245 |
} else if (event.key == Qt.Key_S) { |
| 246 |
shape.sourceHorizontalWrapMode = (shape.sourceHorizontalWrapMode + 1) % 3; |
| 247 |
} else if (event.key == Qt.Key_D) { |
| 248 |
shape.sourceVerticalWrapMode = (shape.sourceVerticalWrapMode + 1) % 3; |
| 249 |
} else if (event.key == Qt.Key_F) { |
| 250 |
shape.sourceHorizontalAlignment = (shape.sourceHorizontalAlignment + 1) % 3; |
| 251 |
} else if (event.key == Qt.Key_G) { |
| 252 |
shape.sourceVerticalAlignment = (shape.sourceVerticalAlignment + 1) % 3; |
| 253 |
} else if (event.key == Qt.Key_H) { |
| 254 |
shape.sourceTranslation = Qt.vector2d( |
| 255 |
shape.sourceTranslation.x + ((event.modifiers & shift) ? 0.01 : -0.01), |
| 256 |
shape.sourceTranslation.y); |
| 257 |
} else if (event.key == Qt.Key_J) { |
| 258 |
shape.sourceTranslation = Qt.vector2d( |
| 259 |
shape.sourceTranslation.x, |
| 260 |
shape.sourceTranslation.y + ((event.modifiers & shift) ? 0.01 : -0.01)); |
| 261 |
} else if (event.key == Qt.Key_K) { |
| 262 |
shape.sourceScale = Qt.vector2d( |
| 263 |
shape.sourceScale.x + ((event.modifiers & shift) ? 0.02 : -0.02), |
| 264 |
shape.sourceScale.y); |
| 265 |
} else if (event.key == Qt.Key_L) { |
| 266 |
shape.sourceScale = Qt.vector2d( |
| 267 |
shape.sourceScale.x, |
| 268 |
shape.sourceScale.y + ((event.modifiers & shift) ? 0.02 : -0.02)); |
| 269 |
|
| 270 |
// Deprecated image. |
| 271 |
} else if (event.key == Qt.Key_M) { |
| 272 |
if (shape.image == null) { |
| 273 |
shape.image = img1; |
| 274 |
} else { |
| 275 |
shape.image = null; |
| 276 |
} |
| 277 |
} else if (event.key == Qt.Key_W) { |
| 278 |
img1.fillMode = (img1.fillMode + 1) % 7; |
| 279 |
} else if (event.key == Qt.Key_X) { |
| 280 |
if (img1.horizontalAlignment == Image.AlignLeft) { |
| 281 |
img1.horizontalAlignment = Image.AlignHCenter; |
| 282 |
} else if (img1.horizontalAlignment == Image.AlignHCenter) { |
| 283 |
img1.horizontalAlignment = Image.AlignRight; |
| 284 |
} else { |
| 285 |
img1.horizontalAlignment = Image.AlignLeft; |
| 286 |
} |
| 287 |
} else if (event.key == Qt.Key_C) { |
| 288 |
if (img1.verticalAlignment == Image.AlignTop) { |
| 289 |
img1.verticalAlignment = Image.AlignVCenter; |
| 290 |
} else if (img1.verticalAlignment == Image.AlignVCenter) { |
| 291 |
img1.verticalAlignment = Image.AlignBottom; |
| 292 |
} else { |
| 293 |
img1.verticalAlignment = Image.AlignTop; |
| 294 |
} |
| 295 |
|
| 296 |
// Deprecated styling. |
| 297 |
} else if (event.key == Qt.Key_B) { |
| 298 |
if (shape.borderSource == "radius_idle.sci") { |
| 299 |
shape.borderSource = "radius_pressed.sci"; |
| 300 |
} else if (shape.borderSource == "radius_pressed.sci") { |
| 301 |
shape.borderSource = ""; |
| 302 |
} else { |
| 303 |
shape.borderSource = "radius_idle.sci"; |
| 304 |
} |
| 305 |
} else if (event.key == Qt.Key_N) { |
| 306 |
shape.color = Qt.rgba( |
| 307 |
Math.random(), Math.random(), Math.random(), Math.random()); |
| 308 |
} else if (event.key == Qt.Key_Comma) { |
| 309 |
shape.gradientColor = Qt.rgba( |
| 310 |
Math.random(), Math.random(), Math.random(), Math.random()); |
| 311 |
|
| 312 |
// Overlay. |
| 313 |
} else if (event.key == Qt.Key_F1) { |
| 314 |
shape.overlayColor = Qt.rgba( |
| 315 |
Math.random(), Math.random(), Math.random(), Math.random()); |
| 316 |
} else if (event.key == Qt.Key_F2) { |
| 317 |
var x = Math.max(0.0, Math.min(1.0, |
| 318 |
shape.overlayRect.x + ((event.modifiers & shift) ? 0.005 : -0.005))); |
| 319 |
shape.overlayRect = Qt.rect( |
| 320 |
x, shape.overlayRect.y, shape.overlayRect.width, |
| 321 |
shape.overlayRect.height); |
| 322 |
} else if (event.key == Qt.Key_F3) { |
| 323 |
var y = Math.max(0.0, Math.min(1.0, |
| 324 |
shape.overlayRect.y + ((event.modifiers & shift) ? 0.005 : -0.005))); |
| 325 |
shape.overlayRect = Qt.rect( |
| 326 |
shape.overlayRect.x, y, shape.overlayRect.width, |
| 327 |
shape.overlayRect.height); |
| 328 |
} else if (event.key == Qt.Key_F4) { |
| 329 |
var width = Math.max(0.0, Math.min(1.0, |
| 330 |
shape.overlayRect.width + ((event.modifiers & shift) ? 0.005 : -0.005))); |
| 331 |
shape.overlayRect = Qt.rect( |
| 332 |
shape.overlayRect.x, shape.overlayRect.y, width, |
| 333 |
shape.overlayRect.height); |
| 334 |
} else if (event.key == Qt.Key_F5) { |
| 335 |
var height = Math.max(0.0, Math.min(1.0, |
| 336 |
shape.overlayRect.height + ((event.modifiers & shift) ? 0.005 : -0.005))); |
| 337 |
shape.overlayRect = Qt.rect( |
| 338 |
shape.overlayRect.x, shape.overlayRect.y, shape.overlayRect.width, |
| 339 |
height); |
| 340 |
} |
| 341 |
} |
| 342 |
} |