Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / examples / declarative / modelviews / listview / expandingdelegates.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 1.0
42 import "content"
43
44 // This example illustrates expanding a list item to show a more detailed view.
45
46 Rectangle {
47     id: page
48     width: 400; height: 240
49     color: "black"
50
51     // Delegate for the recipes.  This delegate has two modes:
52     // 1. List mode (default), which just shows the picture and title of the recipe.
53     // 2. Details mode, which also shows the ingredients and method.
54     Component {
55         id: recipeDelegate
56
57         Item {
58             id: recipe
59
60             // Create a property to contain the visibility of the details.
61             // We can bind multiple element's opacity to this one property,
62             // rather than having a "PropertyChanges" line for each element we
63             // want to fade.
64             property real detailsOpacity : 0
65
66             width: listView.width
67             height: 70
68
69             // A simple rounded rectangle for the background
70             Rectangle {
71                 id: background
72                 x: 2; y: 2; width: parent.width - x*2; height: parent.height - y*2
73                 color: "ivory"
74                 border.color: "orange"
75                 radius: 5
76             }
77
78             // This mouse region covers the entire delegate.
79             // When clicked it changes mode to 'Details'.  If we are already
80             // in Details mode, then no change will happen.
81             MouseArea {
82                 anchors.fill: parent
83                 onClicked: recipe.state = 'Details';
84             }
85
86             // Lay out the page: picture, title and ingredients at the top, and method at the
87             // bottom.  Note that elements that should not be visible in the list
88             // mode have their opacity set to recipe.detailsOpacity.
89             Row {
90                 id: topLayout
91                 x: 10; y: 10; height: recipeImage.height; width: parent.width
92                 spacing: 10
93
94                 Image {
95                     id: recipeImage
96                     width: 50; height: 50
97                     source: picture
98                 }
99
100                 Column {
101                     width: background.width - recipeImage.width - 20; height: recipeImage.height
102                     spacing: 5
103
104                     Text { 
105                         text: title
106                         font.bold: true; font.pointSize: 16
107                     }
108
109                     Text {
110                         text: "Ingredients"
111                         font.pointSize: 12; font.bold: true
112                         opacity: recipe.detailsOpacity
113                     }
114
115                     Text {
116                         text: ingredients
117                         wrapMode: Text.WordWrap
118                         width: parent.width
119                         opacity: recipe.detailsOpacity
120                     }
121                 }
122             }
123
124             Item {
125                 id: details
126                 x: 10; width: parent.width - 20
127                 anchors { top: topLayout.bottom; topMargin: 10; bottom: parent.bottom; bottomMargin: 10 }
128                 opacity: recipe.detailsOpacity
129
130                 Text {
131                     id: methodTitle
132                     anchors.top: parent.top
133                     text: "Method"
134                     font.pointSize: 12; font.bold: true
135                 }
136
137                 Flickable {
138                     id: flick
139                     width: parent.width
140                     anchors { top: methodTitle.bottom; bottom: parent.bottom }
141                     contentHeight: methodText.height
142                     clip: true
143
144                     Text { id: methodText; text: method; wrapMode: Text.WordWrap; width: details.width }
145                 }
146
147                 Image {
148                     anchors { right: flick.right; top: flick.top }
149                     source: "content/pics/moreUp.png"
150                     opacity: flick.atYBeginning ? 0 : 1
151                 }
152
153                 Image {
154                     anchors { right: flick.right; bottom: flick.bottom }
155                     source: "content/pics/moreDown.png"
156                     opacity: flick.atYEnd ? 0 : 1
157                 }
158             }
159
160             // A button to close the detailed view, i.e. set the state back to default ('').
161             TextButton {
162                 y: 10
163                 anchors { right: background.right; rightMargin: 10 }
164                 opacity: recipe.detailsOpacity
165                 text: "Close"
166
167                 onClicked: recipe.state = '';
168             }
169
170             states: State {
171                 name: "Details"
172
173                 PropertyChanges { target: background; color: "white" }
174                 PropertyChanges { target: recipeImage; width: 130; height: 130 } // Make picture bigger
175                 PropertyChanges { target: recipe; detailsOpacity: 1; x: 0 } // Make details visible
176                 PropertyChanges { target: recipe; height: listView.height } // Fill the entire list area with the detailed view
177
178                 // Move the list so that this item is at the top.
179                 PropertyChanges { target: recipe.ListView.view; explicit: true; contentY: recipe.y }
180
181                 // Disallow flicking while we're in detailed view
182                 PropertyChanges { target: recipe.ListView.view; interactive: false }
183             }
184
185             transitions: Transition {
186                 // Make the state changes smooth
187                 ParallelAnimation {
188                     ColorAnimation { property: "color"; duration: 500 }
189                     NumberAnimation { duration: 300; properties: "detailsOpacity,x,contentY,height,width" }
190                 }
191             }
192         }
193     }
194
195     // The actual list
196     ListView {
197         id: listView
198         anchors.fill: parent
199         model: RecipesModel {}
200         delegate: recipeDelegate
201     }
202 }