examples/declarative/toys/dynamicscene/dynamicscene.qml
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 QtDeclarative module 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 Qt 4.7
       
    42 import Qt.labs.particles 1.0
       
    43 import "qml"
       
    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     //This is the message box that pops up when there's an error
       
    55     Rectangle {
       
    56         id: dialog
       
    57 
       
    58         opacity: 0
       
    59         anchors.centerIn: parent
       
    60         width: dialogText.width + 6; height: dialogText.height + 6
       
    61         border.color: 'black'
       
    62         color: 'lightsteelblue'
       
    63         z: 65535 //Arbitrary number chosen to be above all the items, including the scaled perspective ones.
       
    64 
       
    65         function show(str){
       
    66             dialogText.text = str;
       
    67             dialogAnim.start();
       
    68         }
       
    69 
       
    70         Text {
       
    71             id: dialogText
       
    72             x: 3; y: 3
       
    73             font.pixelSize: 14
       
    74         }
       
    75 
       
    76         SequentialAnimation {
       
    77             id: dialogAnim
       
    78             NumberAnimation { target: dialog; property:"opacity"; to: 1; duration: 1000 }
       
    79             PauseAnimation { duration: 5000 }
       
    80             NumberAnimation { target: dialog; property:"opacity"; to: 0; duration: 1000 }
       
    81         }
       
    82     }
       
    83 
       
    84     // sky
       
    85     Rectangle {
       
    86         id: sky
       
    87         anchors { left: parent.left; top: parent.top; right: toolbox.right; bottom: parent.verticalCenter }
       
    88         gradient: Gradient {
       
    89             GradientStop { id: gradientStopA; position: 0.0; color: "#0E1533" }
       
    90             GradientStop { id: gradientStopB; position: 1.0; color: "#437284" }
       
    91         }
       
    92     }
       
    93 
       
    94     // stars (when there's no sun)
       
    95     Particles {
       
    96         id: stars
       
    97         x: 0; y: 0; width: parent.width; height: parent.height / 2
       
    98         source: "images/star.png"
       
    99         angleDeviation: 360
       
   100         velocity: 0; velocityDeviation: 0
       
   101         count: parent.width / 10
       
   102         fadeInDuration: 2800
       
   103         opacity: 1
       
   104     }
       
   105 
       
   106     // ground
       
   107     Rectangle {
       
   108         id: ground
       
   109         z: 2    // just above the sun so that the sun can set behind it
       
   110         anchors { left: parent.left; top: parent.verticalCenter; right: toolbox.left; bottom: parent.bottom }
       
   111         gradient: Gradient {
       
   112             GradientStop { position: 0.0; color: "ForestGreen" }
       
   113             GradientStop { position: 1.0; color: "DarkGreen" }
       
   114         }
       
   115     }
       
   116 
       
   117     SystemPalette { id: activePalette }
       
   118 
       
   119     // right-hand panel
       
   120     Rectangle {
       
   121         id: toolbox
       
   122 
       
   123         width: 380
       
   124         color: activePalette.window
       
   125         anchors { right: parent.right; top: parent.top; bottom: parent.bottom }
       
   126 
       
   127         Column {
       
   128             anchors.centerIn: parent
       
   129             spacing: 8
       
   130 
       
   131             Text { text: "Drag an item into the scene." }
       
   132 
       
   133             Rectangle {
       
   134                 width: childrenRect.width + 10; height: childrenRect.height + 10
       
   135                 border.color: "black"
       
   136 
       
   137                 Row {
       
   138                     anchors.centerIn: parent
       
   139                     spacing: 8
       
   140 
       
   141                     PaletteItem {
       
   142                         anchors.verticalCenter: parent.verticalCenter
       
   143                         componentFile: "Sun.qml"
       
   144                         image: "../images/sun.png"
       
   145                     }
       
   146                     PaletteItem {
       
   147                         anchors.verticalCenter: parent.verticalCenter
       
   148                         componentFile: "GenericSceneItem.qml"
       
   149                         image: "../images/moon.png"
       
   150                     }
       
   151                     PaletteItem {
       
   152                         anchors.verticalCenter: parent.verticalCenter
       
   153                         componentFile: "PerspectiveItem.qml"
       
   154                         image: "../images/tree_s.png"
       
   155                     }
       
   156                     PaletteItem {
       
   157                         anchors.verticalCenter: parent.verticalCenter
       
   158                         componentFile: "PerspectiveItem.qml"
       
   159                         image: "../images/rabbit_brown.png"
       
   160                     }
       
   161                     PaletteItem {
       
   162                         anchors.verticalCenter: parent.verticalCenter
       
   163                         componentFile: "PerspectiveItem.qml"
       
   164                         image: "../images/rabbit_bw.png"
       
   165                     }
       
   166                 }
       
   167             }
       
   168 
       
   169             Text { text: "Active Suns: " + activeSuns }
       
   170 
       
   171             Rectangle { width: parent.width; height: 1; color: "black" }
       
   172 
       
   173             Text { text: "Arbitrary QML:" }
       
   174 
       
   175             Rectangle {
       
   176                 width: 360; height: 240
       
   177 
       
   178                 TextEdit {
       
   179                     id: qmlText
       
   180                     anchors.fill: parent; anchors.margins: 5
       
   181                     readOnly: false
       
   182                     focusOnPress: true
       
   183                     font.pixelSize: 14
       
   184                     wrapMode: TextEdit.WordWrap
       
   185 
       
   186                     text: "import Qt 4.7\nImage {\n    id: smile\n    x: 360 * Math.random()\n    y: 180 * Math.random() \n    source: 'images/face-smile.png'\n    NumberAnimation on opacity { \n        to: 0; duration: 1500\n    }\n    Component.onCompleted: smile.destroy(1500);\n}"
       
   187                 }
       
   188             }
       
   189 
       
   190             Button {
       
   191                 text: "Create"
       
   192                 onClicked: {
       
   193                     try { 
       
   194                         Qt.createQmlObject(qmlText.text, window, 'CustomObject');
       
   195                     } catch(err) {
       
   196                         dialog.show('Error on line ' + err.qmlErrors[0].lineNumber + '\n' + err.qmlErrors[0].message);
       
   197                     }
       
   198                 }
       
   199             }
       
   200         }
       
   201     }
       
   202 
       
   203     //Day state, for when a sun is added to the scene
       
   204     states: State {
       
   205         name: "Day"
       
   206         when: window.activeSuns > 0
       
   207 
       
   208         PropertyChanges { target: gradientStopA; color: "DeepSkyBlue" }
       
   209         PropertyChanges { target: gradientStopB; color: "SkyBlue" }
       
   210         PropertyChanges { target: stars; opacity: 0 }
       
   211     }
       
   212 
       
   213     transitions: Transition {
       
   214         PropertyAnimation { duration: 3000 }
       
   215         ColorAnimation { duration: 3000 }
       
   216     }
       
   217 
       
   218 }