Update copyright year in license headers.
[profile/ivi/qtdeclarative.git] / examples / declarative / toys / dynamicscene / dynamicscene.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 2.0
42 import QtQuick.Particles 2.0
43 import "content"
44
45 Item {
46     id: window
47
48     property int activeSuns: 0
49
50     //This is a desktop-sized example
51     width: 800; height: 480
52
53
54     MouseArea {
55         anchors.fill: parent
56         onClicked: window.focus = false;
57     }
58
59     //This is the message box that pops up when there's an error
60     Rectangle {
61         id: dialog
62
63         opacity: 0
64         anchors.centerIn: parent
65         width: dialogText.width + 6; height: dialogText.height + 6
66         border.color: 'black'
67         color: 'lightsteelblue'
68         z: 65535 //Arbitrary number chosen to be above all the items, including the scaled perspective ones.
69
70         function show(str){
71             dialogText.text = str;
72             dialogAnim.start();
73         }
74
75         Text {
76             id: dialogText
77             x: 3; y: 3
78             font.pixelSize: 14
79         }
80
81         SequentialAnimation {
82             id: dialogAnim
83             NumberAnimation { target: dialog; property:"opacity"; to: 1; duration: 1000 }
84             PauseAnimation { duration: 5000 }
85             NumberAnimation { target: dialog; property:"opacity"; to: 0; duration: 1000 }
86         }
87     }
88
89     // sky
90     Rectangle {
91         id: sky
92         anchors { left: parent.left; top: parent.top; right: toolbox.right; bottom: parent.verticalCenter }
93         gradient: Gradient {
94             GradientStop { id: gradientStopA; position: 0.0; color: "#0E1533" }
95             GradientStop { id: gradientStopB; position: 1.0; color: "#437284" }
96         }
97     }
98
99     // stars (when there's no sun)
100     ParticleSystem {
101         id: particlesystem
102         anchors.fill: sky
103
104         ImageParticle {
105             id: stars
106             source: "content/images/star.png"
107             groups: ["stars"]
108             opacity: .5
109         }
110
111         Emitter {
112             id: starsemitter
113             anchors.fill: parent
114             emitRate: parent.width / 50
115             lifeSpan: 5000
116             group: "stars"
117         }
118     }
119
120     // ground
121     Rectangle {
122         id: ground
123         z: 2    // just above the sun so that the sun can set behind it
124         anchors { left: parent.left; top: parent.verticalCenter; right: toolbox.left; bottom: parent.bottom }
125         gradient: Gradient {
126             GradientStop { position: 0.0; color: "ForestGreen" }
127             GradientStop { position: 1.0; color: "DarkGreen" }
128         }
129     }
130
131     SystemPalette { id: activePalette }
132
133     // right-hand panel
134     Rectangle {
135         id: toolbox
136
137         width: 380
138         color: activePalette.window
139         anchors { right: parent.right; top: parent.top; bottom: parent.bottom }
140
141         Column {
142             anchors.centerIn: parent
143             spacing: 8
144
145             Text { text: "Drag an item into the scene." }
146
147             Rectangle {
148                 width: palette.width + 10; height: palette.height + 10
149                 border.color: "black"
150
151                 Row {
152                     id: palette
153                     anchors.centerIn: parent
154                     spacing: 8
155
156                     PaletteItem {
157                         anchors.verticalCenter: parent.verticalCenter
158                         componentFile: "Sun.qml"
159                         source: "content/images/sun.png"
160                         image: "images/sun.png"
161                     }
162                     PaletteItem {
163                         anchors.verticalCenter: parent.verticalCenter
164                         componentFile: "GenericSceneItem.qml"
165                         source: "content/images/moon.png"
166                         image: "images/moon.png"
167                     }
168                     PaletteItem {
169                         anchors.verticalCenter: parent.verticalCenter
170                         componentFile: "PerspectiveItem.qml"
171                         source: "content/images/tree_s.png"
172                         image: "images/tree_s.png"
173                     }
174                     PaletteItem {
175                         anchors.verticalCenter: parent.verticalCenter
176                         componentFile: "PerspectiveItem.qml"
177                         source: "content/images/rabbit_brown.png"
178                         image: "images/rabbit_brown.png"
179                     }
180                     PaletteItem {
181                         anchors.verticalCenter: parent.verticalCenter
182                         componentFile: "PerspectiveItem.qml"
183                         source: "content/images/rabbit_bw.png"
184                         image: "images/rabbit_bw.png"
185                     }
186                 }
187             }
188
189             Text { text: "Active Suns: " + activeSuns }
190
191             Rectangle { width: parent.width; height: 1; color: "black" }
192
193             Text { text: "Arbitrary QML:" }
194
195             Rectangle {
196                 width: 360; height: 240
197
198                 TextEdit {
199                     id: qmlText
200                     anchors.fill: parent; anchors.margins: 5
201                     readOnly: false
202                     font.pixelSize: 14
203                     wrapMode: TextEdit.WordWrap
204
205                     text: "import QtQuick 2.0\nImage {\n    id: smile\n    x: 360 * Math.random()\n    y: 180 * Math.random() \n    source: 'content/images/face-smile.png'\n    NumberAnimation on opacity { \n        to: 0; duration: 1500\n    }\n    Component.onCompleted: smile.destroy(1500);\n}"
206                 }
207             }
208
209             Button {
210                 text: "Create"
211                 onClicked: {
212                     try { 
213                         Qt.createQmlObject(qmlText.text, window, 'CustomObject');
214                     } catch(err) {
215                         dialog.show('Error on line ' + err.qmlErrors[0].lineNumber + '\n' + err.qmlErrors[0].message);
216                     }
217                 }
218             }
219         }
220     }
221
222     //Day state, for when a sun is added to the scene
223     states: State {
224         name: "Day"
225         when: window.activeSuns > 0
226
227         PropertyChanges { target: gradientStopA; color: "DeepSkyBlue" }
228         PropertyChanges { target: gradientStopB; color: "SkyBlue" }
229         PropertyChanges { target: stars; opacity: 0 }
230     }
231
232     //! [top-level transitions]
233     transitions: Transition {
234         PropertyAnimation { duration: 3000 }
235         ColorAnimation { duration: 3000 }
236     }
237     //! [top-level transitions]
238 }