summaryrefslogtreecommitdiffstats
path: root/examples/declarative/toys/dynamic/qml
diff options
context:
space:
mode:
Diffstat (limited to 'examples/declarative/toys/dynamic/qml')
-rw-r--r--examples/declarative/toys/dynamic/qml/Button.qml40
-rw-r--r--examples/declarative/toys/dynamic/qml/PaletteItem.qml19
-rw-r--r--examples/declarative/toys/dynamic/qml/PerspectiveItem.qml25
-rw-r--r--examples/declarative/toys/dynamic/qml/Sun.qml38
-rw-r--r--examples/declarative/toys/dynamic/qml/itemCreation.js65
5 files changed, 187 insertions, 0 deletions
diff --git a/examples/declarative/toys/dynamic/qml/Button.qml b/examples/declarative/toys/dynamic/qml/Button.qml
new file mode 100644
index 0000000..963a850
--- /dev/null
+++ b/examples/declarative/toys/dynamic/qml/Button.qml
@@ -0,0 +1,40 @@
+import Qt 4.7
+
+Rectangle {
+ id: container
+
+ property variant text
+ signal clicked
+
+ height: text.height + 10; width: text.width + 20
+ border.width: 1
+ radius: 4
+ smooth: true
+
+ gradient: Gradient {
+ GradientStop {
+ position: 0.0
+ color: !mouseArea.pressed ? activePalette.light : activePalette.button
+ }
+ GradientStop {
+ position: 1.0
+ color: !mouseArea.pressed ? activePalette.button : activePalette.dark
+ }
+ }
+
+ SystemPalette { id: activePalette }
+
+ MouseArea {
+ id: mouseArea
+ 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/toys/dynamic/qml/PaletteItem.qml b/examples/declarative/toys/dynamic/qml/PaletteItem.qml
new file mode 100644
index 0000000..dcb5cc3
--- /dev/null
+++ b/examples/declarative/toys/dynamic/qml/PaletteItem.qml
@@ -0,0 +1,19 @@
+import Qt 4.7
+import "itemCreation.js" as Code
+
+Image {
+ id: paletteItem
+
+ property string componentFile
+ property string image
+
+ source: image
+
+ MouseArea {
+ anchors.fill: parent
+
+ onPressed: Code.startDrag(mouse);
+ onPositionChanged: Code.continueDrag(mouse);
+ onReleased: Code.endDrag(mouse);
+ }
+}
diff --git a/examples/declarative/toys/dynamic/qml/PerspectiveItem.qml b/examples/declarative/toys/dynamic/qml/PerspectiveItem.qml
new file mode 100644
index 0000000..c04d3dc
--- /dev/null
+++ b/examples/declarative/toys/dynamic/qml/PerspectiveItem.qml
@@ -0,0 +1,25 @@
+import Qt 4.7
+
+Image {
+ id: rootItem
+
+ property bool created: false
+ property string image
+
+ property double scaledBottom: y + (height + height*scale) / 2
+ property bool onLand: scaledBottom > window.height / 2
+
+ source: image
+ opacity: onLand ? 1 : 0.25
+ scale: Math.max((y + height - 250) * 0.01, 0.3)
+ smooth: true
+
+ onCreatedChanged: {
+ if (created && !onLand)
+ rootItem.destroy();
+ else
+ z = scaledBottom;
+ }
+
+ onYChanged: z = scaledBottom;
+}
diff --git a/examples/declarative/toys/dynamic/qml/Sun.qml b/examples/declarative/toys/dynamic/qml/Sun.qml
new file mode 100644
index 0000000..43dcb9a
--- /dev/null
+++ b/examples/declarative/toys/dynamic/qml/Sun.qml
@@ -0,0 +1,38 @@
+import Qt 4.7
+
+Image {
+ id: sun
+
+ property bool created: false
+ property string image: "../images/sun.png"
+
+ source: image
+
+ // once item is created, start moving offscreen
+ NumberAnimation on y {
+ to: window.height / 2
+ running: created
+ onRunningChanged: {
+ if (running)
+ duration = (window.height - sun.y) * 10;
+ else
+ state = "OffScreen"
+ }
+ }
+
+ states: State {
+ name: "OffScreen"
+ StateChangeScript {
+ script: { sun.created = false; sun.destroy() }
+ }
+ }
+
+ onCreatedChanged: {
+ if (created) {
+ sun.z = 1; // above the sky but below the ground layer
+ window.activeSuns++;
+ } else {
+ window.activeSuns--;
+ }
+ }
+}
diff --git a/examples/declarative/toys/dynamic/qml/itemCreation.js b/examples/declarative/toys/dynamic/qml/itemCreation.js
new file mode 100644
index 0000000..59750f3
--- /dev/null
+++ b/examples/declarative/toys/dynamic/qml/itemCreation.js
@@ -0,0 +1,65 @@
+var itemComponent = null;
+var draggedItem = null;
+var startingMouse;
+var posnInWindow;
+
+function startDrag(mouse)
+{
+ posnInWindow = paletteItem.mapToItem(null, 0, 0);
+ startingMouse = { x: mouse.x, y: mouse.y }
+ loadComponent();
+}
+
+//Creation is split into two functions due to an asynchronous wait while
+//possible external files are loaded.
+
+function loadComponent() {
+ if (itemComponent != null) { // component has been previously loaded
+ createItem();
+ return;
+ }
+
+ itemComponent = Qt.createComponent(paletteItem.componentFile);
+ if (itemComponent.status == Component.Loading) //Depending on the content, it can be ready or error immediately
+ component.statusChanged.connect(createItem);
+ else
+ createItem();
+}
+
+function createItem() {
+ if (itemComponent.status == Component.Ready && draggedItem == null) {
+ draggedItem = itemComponent.createObject(window);
+ draggedItem.image = paletteItem.image;
+ draggedItem.x = posnInWindow.x;
+ draggedItem.y = posnInWindow.y;
+ draggedItem.z = 3; // make sure created item is above the ground layer
+ } else if (itemComponent.status == Component.Error) {
+ draggedItem = null;
+ console.log("error creating component");
+ console.log(component.errorsString());
+ }
+}
+
+function continueDrag(mouse)
+{
+ if (draggedItem == null)
+ return;
+
+ draggedItem.x = mouse.x + posnInWindow.x - startingMouse.x;
+ draggedItem.y = mouse.y + posnInWindow.y - 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.created = true;
+ draggedItem = null;
+ }
+}
+