1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
43 // This example shows how items can be dynamically added to and removed from
44 // a ListModel, and how these list modifications can be animated.
48 width: 500; height: 400
56 name: "Apple"; cost: 2.45
58 ListElement { description: "Core" },
59 ListElement { description: "Deciduous" }
63 name: "Banana"; cost: 1.95
65 ListElement { description: "Tropical" },
66 ListElement { description: "Seedless" }
70 name: "Cumquat"; cost: 3.25
72 ListElement { description: "Citrus" }
76 name: "Durian"; cost: 9.95
78 ListElement { description: "Tropical" },
79 ListElement { description: "Smelly" }
84 // The delegate for each fruit in the model:
90 width: listView.width; height: 55
94 anchors.verticalCenter: parent.verticalCenter
99 source: "content/pics/arrow-up.png"
100 MouseArea { anchors.fill: parent; onClicked: fruitModel.move(index, index-1, 1) }
102 Image { source: "content/pics/arrow-down.png"
103 MouseArea { anchors.fill: parent; onClicked: fruitModel.move(index, index+1, 1) }
108 anchors.verticalCenter: parent.verticalCenter
119 Text { text: description; color: "White" }
126 anchors.verticalCenter: parent.verticalCenter
127 anchors.right: parent.right
131 anchors.verticalCenter: parent.verticalCenter
132 source: "content/pics/plus-sign.png"
133 onClicked: fruitModel.setProperty(index, "cost", cost + 0.25)
138 anchors.verticalCenter: parent.verticalCenter
139 text: '$' + Number(cost).toFixed(2)
146 anchors.verticalCenter: parent.verticalCenter
147 source: "content/pics/minus-sign.png"
148 onClicked: fruitModel.setProperty(index, "cost", Math.max(0,cost-0.25))
152 source: "content/pics/list-delete.png"
153 MouseArea { anchors.fill:parent; onClicked: fruitModel.remove(index) }
157 // Animate adding and removing of items:
159 ListView.onAdd: SequentialAnimation {
160 PropertyAction { target: delegateItem; property: "height"; value: 0 }
161 NumberAnimation { target: delegateItem; property: "height"; to: 55; duration: 250; easing.type: Easing.InOutQuad }
164 ListView.onRemove: SequentialAnimation {
165 PropertyAction { target: delegateItem; property: "ListView.delayRemove"; value: true }
166 NumberAnimation { target: delegateItem; property: "height"; to: 0; duration: 250; easing.type: Easing.InOutQuad }
168 // Make sure delayRemove is set back to false so that the item can be destroyed
169 PropertyAction { target: delegateItem; property: "ListView.delayRemove"; value: false }
177 anchors.fill: parent; anchors.margins: 20
179 delegate: listDelegate
183 anchors { left: parent.left; bottom: parent.bottom; margins: 20 }
190 "name": "Pizza Margarita",
192 "attributes": [{"description": "Cheese"}, {"description": "Tomato"}]
198 text: "Remove all items"
199 onClicked: fruitModel.clear()