diff options
Diffstat (limited to 'examples/declarative/dynamic')
-rw-r--r-- | examples/declarative/dynamic/dynamic.qml | 117 | ||||
-rw-r--r-- | examples/declarative/dynamic/images/NOTE | 1 | ||||
-rw-r--r-- | examples/declarative/dynamic/images/face-smile.png | bin | 0 -> 15408 bytes | |||
-rw-r--r-- | examples/declarative/dynamic/images/moon.png | bin | 0 -> 1757 bytes | |||
-rw-r--r-- | examples/declarative/dynamic/images/rabbit_brown.png | bin | 0 -> 1245 bytes | |||
-rw-r--r-- | examples/declarative/dynamic/images/rabbit_bw.png | bin | 0 -> 1759 bytes | |||
-rw-r--r-- | examples/declarative/dynamic/images/star.png | bin | 0 -> 349 bytes | |||
-rw-r--r-- | examples/declarative/dynamic/images/sun.png | bin | 0 -> 8153 bytes | |||
-rw-r--r-- | examples/declarative/dynamic/images/tree_s.png | bin | 0 -> 3406 bytes | |||
-rw-r--r-- | examples/declarative/dynamic/qml/Button.qml | 24 | ||||
-rw-r--r-- | examples/declarative/dynamic/qml/GenericItem.qml | 13 | ||||
-rw-r--r-- | examples/declarative/dynamic/qml/PaletteItem.qml | 13 | ||||
-rw-r--r-- | examples/declarative/dynamic/qml/PerspectiveItem.qml | 15 | ||||
-rw-r--r-- | examples/declarative/dynamic/qml/Sun.qml | 24 | ||||
-rw-r--r-- | examples/declarative/dynamic/qml/itemCreation.js | 82 |
15 files changed, 289 insertions, 0 deletions
diff --git a/examples/declarative/dynamic/dynamic.qml b/examples/declarative/dynamic/dynamic.qml new file mode 100644 index 0000000..f420a1c --- /dev/null +++ b/examples/declarative/dynamic/dynamic.qml @@ -0,0 +1,117 @@ +import Qt 4.6 +import "qml" + +Item { + id: window + //This is a desktop-sized example + width: 1024; height: 512 + property int activeSuns: 0 + + // sky + Rectangle { id: sky + anchors { left: parent.left; top: parent.top; right: toolbox.right; bottom: parent.verticalCenter } + gradient: Gradient { + GradientStop { id: stopA; position: 0.0; color: "#0E1533" } + GradientStop { id: stopB; position: 1.0; color: "#437284" } + } + } + + // stars (when there's no sun) + Particles { id: stars + x: 0; y: 0; width: parent.width; height: parent.height/2 + source: "images/star.png"; angleDeviation: 360; velocity: 0 + velocityDeviation: 0; count: parent.width / 10; fadeInDuration: 2800 + opacity: 1 + } + + // ground, which has a z such that the sun can set behind it + Rectangle { id: ground + z: 2 + anchors { left: parent.left; top: parent.verticalCenter; right: toolbox.right; bottom: parent.bottom } + gradient: Gradient { + GradientStop { position: 0.0; color: "ForestGreen" } + GradientStop { position: 1.0; color: "DarkGreen" } + } + } + + //Day state, for when you place a sun + states: State { name: "Day"; when: window.activeSuns > 0 + PropertyChanges { target: stopA; color: "DeepSkyBlue"} + PropertyChanges { target: stopB; color: "SkyBlue"} + PropertyChanges { target: stars; opacity: 0 } + } + + transitions: Transition { + PropertyAnimation { duration: 3000 } + ColorAnimation { duration: 3000 } + } + + SystemPalette { id: activePalette } + + // toolbox + Rectangle { + id: toolbox + z: 3 //Above ground + color: activePalette.window; + width: 480 + anchors { right: parent.right; top:parent.top; bottom: parent.bottom } + Rectangle { //Not a child of any positioner + color: "white"; border.color: "black"; + width: toolRow.width + 4 + height: toolRow.height + 4 + x: toolboxPositioner.x + toolRow.x - 2 + y: toolboxPositioner.y + toolRow.y - 2 + } + Column{ + id: toolboxPositioner + anchors.centerIn: parent + spacing: 8 + Text{ text: "Drag an item into the scene." } + Row{ id: toolRow + spacing: 8; + PaletteItem{ + anchors.verticalCenter: parent.verticalCenter + file: "Sun.qml"; + image: "../images/sun.png" + } + PaletteItem{ + file: "GenericItem.qml" + image: "../images/moon.png" + } + PaletteItem{ + anchors.verticalCenter: parent.verticalCenter + file: "PerspectiveItem.qml" + image: "../images/tree_s.png" + } + PaletteItem{ + anchors.verticalCenter: parent.verticalCenter + file: "PerspectiveItem.qml" + image: "../images/rabbit_brown.png" + } + PaletteItem{ + anchors.verticalCenter: parent.verticalCenter + file: "PerspectiveItem.qml" + image: "../images/rabbit_bw.png" + } + } + Text{ text: "Active Suns: " + activeSuns } + Rectangle { width: 440; height: 1; color: "black" } + Text{ text: "Arbitrary QML: " } + TextEdit { + id: qmlText + width: 460 + height: 220 + readOnly: false + focusOnPress: true + font.pixelSize: 16 + + text: "import Qt 4.6\nImage { id: smile;\n x: 500*Math.random();\n y: 200*Math.random(); \n source: 'images/face-smile.png';\n opacity: NumberAnimation{ \n running:true; to: 0; duration: 1500;\n }\n Component.onCompleted: smile.destroy(1500);\n}" + } + Button { + text: "Create" + onClicked: createQmlObject(qmlText.text, window, 'CustomObject'); + } + } + } + +} diff --git a/examples/declarative/dynamic/images/NOTE b/examples/declarative/dynamic/images/NOTE new file mode 100644 index 0000000..fcd87f9 --- /dev/null +++ b/examples/declarative/dynamic/images/NOTE @@ -0,0 +1 @@ +Images (except star.png) are from the KDE project. diff --git a/examples/declarative/dynamic/images/face-smile.png b/examples/declarative/dynamic/images/face-smile.png Binary files differnew file mode 100644 index 0000000..3d66d72 --- /dev/null +++ b/examples/declarative/dynamic/images/face-smile.png diff --git a/examples/declarative/dynamic/images/moon.png b/examples/declarative/dynamic/images/moon.png Binary files differnew file mode 100644 index 0000000..1c0d606 --- /dev/null +++ b/examples/declarative/dynamic/images/moon.png diff --git a/examples/declarative/dynamic/images/rabbit_brown.png b/examples/declarative/dynamic/images/rabbit_brown.png Binary files differnew file mode 100644 index 0000000..ebfdeed --- /dev/null +++ b/examples/declarative/dynamic/images/rabbit_brown.png diff --git a/examples/declarative/dynamic/images/rabbit_bw.png b/examples/declarative/dynamic/images/rabbit_bw.png Binary files differnew file mode 100644 index 0000000..7bff9b9 --- /dev/null +++ b/examples/declarative/dynamic/images/rabbit_bw.png diff --git a/examples/declarative/dynamic/images/star.png b/examples/declarative/dynamic/images/star.png Binary files differnew file mode 100644 index 0000000..27ef924 --- /dev/null +++ b/examples/declarative/dynamic/images/star.png diff --git a/examples/declarative/dynamic/images/sun.png b/examples/declarative/dynamic/images/sun.png Binary files differnew file mode 100644 index 0000000..7713ca5 --- /dev/null +++ b/examples/declarative/dynamic/images/sun.png diff --git a/examples/declarative/dynamic/images/tree_s.png b/examples/declarative/dynamic/images/tree_s.png Binary files differnew file mode 100644 index 0000000..6eac35a --- /dev/null +++ b/examples/declarative/dynamic/images/tree_s.png diff --git a/examples/declarative/dynamic/qml/Button.qml b/examples/declarative/dynamic/qml/Button.qml new file mode 100644 index 0000000..cf2ffa7 --- /dev/null +++ b/examples/declarative/dynamic/qml/Button.qml @@ -0,0 +1,24 @@ +import Qt 4.6 + +Rectangle { + id: container + + property var text + signal clicked + + SystemPalette { id: activePalette } + height: text.height + 10 + width: text.width + 20 + border.width: 1 + radius: 4; smooth: true + gradient: Gradient { + GradientStop { position: 0.0; + color: if(!mr.pressed){activePalette.light;}else{activePalette.button;} + } + GradientStop { position: 1.0; + color: if(!mr.pressed){activePalette.button;}else{activePalette.dark;} + } + } + MouseRegion { id:mr; anchors.fill: parent; onClicked: container.clicked() } + Text { id: text; anchors.centerIn:parent; font.pointSize: 10; text: parent.text; color: activePalette.buttonText } +} diff --git a/examples/declarative/dynamic/qml/GenericItem.qml b/examples/declarative/dynamic/qml/GenericItem.qml new file mode 100644 index 0000000..10e3dba --- /dev/null +++ b/examples/declarative/dynamic/qml/GenericItem.qml @@ -0,0 +1,13 @@ +import Qt 4.6 + +Item{ + property bool created: false + property string image + width: imageItem.width + height: imageItem.height + z: 2 + Image{ + id: imageItem + source: image; + } +} diff --git a/examples/declarative/dynamic/qml/PaletteItem.qml b/examples/declarative/dynamic/qml/PaletteItem.qml new file mode 100644 index 0000000..bb6036d --- /dev/null +++ b/examples/declarative/dynamic/qml/PaletteItem.qml @@ -0,0 +1,13 @@ +import Qt 4.6 + +GenericItem { + id: itemButton + property string file + Script { source: "itemCreation.js" } + MouseRegion { + anchors.fill: parent; + onPressed: startDrag(mouse); + onPositionChanged: moveDrag(mouse); + onReleased: endDrag(mouse); + } +} diff --git a/examples/declarative/dynamic/qml/PerspectiveItem.qml b/examples/declarative/dynamic/qml/PerspectiveItem.qml new file mode 100644 index 0000000..728c3a5 --- /dev/null +++ b/examples/declarative/dynamic/qml/PerspectiveItem.qml @@ -0,0 +1,15 @@ +import Qt 4.6 + +Image { + id: tree + property bool created: false + property double scaleFactor: Math.max((y+height-250)*0.01, 0.3) + property double scaledBottom: y + (height+height*scaleFactor)/2 + property bool onLand: scaledBottom > window.height/2 + opacity: onLand ? 1 : 0.25 + onCreatedChanged: if (created && !onLand) { tree.destroy() } else { z = scaledBottom } + scale: scaleFactor + transformOrigin: "Center" + source: image; smooth: true + onYChanged: z = scaledBottom +} diff --git a/examples/declarative/dynamic/qml/Sun.qml b/examples/declarative/dynamic/qml/Sun.qml new file mode 100644 index 0000000..796a370 --- /dev/null +++ b/examples/declarative/dynamic/qml/Sun.qml @@ -0,0 +1,24 @@ +import Qt 4.6 + +Image { + id: sun + property bool created: false + property string image: "../images/sun.png" + onCreatedChanged: if(created){window.activeSuns++;}else{window.activeSuns--;} + + source: image; + z: 1 + + //x and y get set when instantiated + //head offscreen + y: NumberAnimation { + to: parent.height; + duration: 10000; + running: created + } + + states: State { + name: "OffScreen"; when: created && y > window.height / 2;//Below the ground + StateChangeScript { script: { sun.created = false; sun.destroy() } } + } +} diff --git a/examples/declarative/dynamic/qml/itemCreation.js b/examples/declarative/dynamic/qml/itemCreation.js new file mode 100644 index 0000000..ccc03aa --- /dev/null +++ b/examples/declarative/dynamic/qml/itemCreation.js @@ -0,0 +1,82 @@ +var itemComponent = null; +var draggedItem = null; +var startingMouse; +var startingZ; +//Until QT-2385 is resolved we need to convert to scene coordinates manually +var xOffset; +var yOffset; +function setSceneOffset() +{ + xOffset = 0; + yOffset = 0; + var p = itemButton; + while(p != window){ + xOffset += p.x; + yOffset += p.y; + p = p.parent; + } +} + +function startDrag(mouse) +{ + setSceneOffset(); + startingMouse = { x: mouse.x, y: mouse.y } + loadComponent(); +} + +//Creation is split into two functions due to an asyncronous wait while +//possible external files are loaded. + +function loadComponent() { + if (itemComponent != null) //Already loaded the component + createItem(); + + itemComponent = createComponent(itemButton.file); + //print(itemButton.file) + if(itemComponent.isLoading){ + component.statusChanged.connect(finishCreation); + }else{//Depending on the content, it can be ready or error immediately + createItem(); + } +} + +function createItem() { + if (itemComponent.isReady && draggedItem == null) { + draggedItem = itemComponent.createObject(); + draggedItem.parent = window; + draggedItem.image = itemButton.image; + draggedItem.x = xOffset; + draggedItem.y = yOffset; + startingZ = draggedItem.z; + draggedItem.z = 4;//On top + } else if (itemComponent.isError) { + draggedItem = null; + print("error creating component"); + print(component.errorsString()); + } +} + +function moveDrag(mouse) +{ + if(draggedItem == null) + return; + + draggedItem.x = mouse.x + xOffset - startingMouse.x; + draggedItem.y = mouse.y + yOffset - startingMouse.y; +} + +function endDrag(mouse) +{ + if(draggedItem == null) + return; + + if(draggedItem.x + draggedItem.width > toolbox.x){ //Don't drop it in the toolbox + draggedItem.destroy(); + draggedItem = null; + }else{ + draggedItem.z = startingZ; + draggedItem.created = true; + draggedItem = null; + } +} + |