255fa152097bc58510a8c9416059ac81eac30b09
[profile/ivi/qtdeclarative.git] / tests / testapplications / elements / content / XmlListModelElement.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 import QtQuick 2.0
43 import QtQuick.XmlListModel 2.0
44
45 Item {
46     id: xmllistmodelelementtest
47     anchors.fill: parent
48     property string testtext: ""
49     property string modelxml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
50     "<cookbook><recipe id=\"MushroomSoup\">"+
51     "<title>Hot chocolate</title>"+
52     "<ingredient name=\"Milk\" quantity=\"1\" unit=\"cup\"/>"+
53     "<time quantity=\"2\" unit=\"minutes\"/>"+
54     "<method><step>1. Place the cup of milk in the microwave for 1 minute.</step>"+
55     "<step>2. Stir in 2 teaspoons of drinking chocolate.</step></method></recipe></cookbook>"
56
57     XmlListModel { id: xmllistmodelelement
58         source: "cookbook.xml"
59         query: "/cookbook/recipe"
60         XmlRole { name: "title"; query: "title/string()" }
61         XmlRole { name: "xmlid"; query: "@id/string()" }
62         XmlRole { name: "method"; query: "method/string()" }
63         XmlRole { name: "time"; query: "time/@quantity/number()" }
64     }
65
66     ListView {
67         id: recipeview
68         model: xmllistmodelelement; height: 300; width: 300; clip: true
69         anchors.centerIn: parent
70         delegate: Component {
71             Rectangle { id: delbox; height: 50; width: 296; color: "orange"; border.color: "black"; state: "closed"; clip: true; radius: 5
72                 anchors.horizontalCenter: parent.horizontalCenter
73                 Text {
74                     id: recipetitle
75                     text: model.title; font.pointSize: 12; font.bold: true
76                     anchors.horizontalCenter: parent.horizontalCenter; y: 20;
77                 }
78
79                 Text {
80                     id: recipetime
81                     width: parent.width; height: 20; text: "<b>Time: </b>" +model.time + " minutes"; visible: opacity != 0
82                     anchors.horizontalCenter: parent.horizontalCenter; anchors.top: recipetitle.bottom
83                     Behavior on opacity { NumberAnimation { duration: 250 } }
84                 }
85
86                 // Sub XmlListModel
87                 XmlListModel { id: subxmllistmodelelement
88                     source: "cookbook.xml"
89                     query: "/cookbook/recipe[@id = '"+model.xmlid+"']/ingredient"
90                     XmlRole { name: "ingredientname"; query: "@name/string()" }
91                     XmlRole { name: "ingredientquantity"; query: "@quantity/string()" }
92                     XmlRole { name: "ingredientunit"; query: "@unit/string()" }
93                 }
94
95                 ListView {
96                     id: ingredientlist
97                     model: subxmllistmodelelement
98                     height: 20 * count; width: parent.width
99                     visible: opacity != 0
100                     Behavior on opacity { NumberAnimation { duration: 250 } }
101                     anchors.horizontalCenter: parent.horizontalCenter; anchors.top: recipetime.bottom; anchors.topMargin: 10
102                     header: Text { text: "<b>Ingredients:</b>" }
103                     delegate: Text {
104                         text: ingredientquantity + " " + ingredientunit + " of " + ingredientname; height: 20;
105                     }
106                 }
107
108                 Text {
109                     id: recipemethod
110                     property string methodtext: ""
111                     width: parent.width; wrapMode: Text.WordWrap; visible: opacity != 0; text: methodtext
112                     anchors.horizontalCenter: parent.horizontalCenter; anchors.top: ingredientlist.bottom
113                     Behavior on opacity { NumberAnimation { duration: 250 } }
114                     Component.onCompleted: { methodtext = model.method; }
115                 }
116                 MouseArea { anchors.fill: parent; onClicked: delbox.state = delbox.state == "open" ? "closed" : "open" }
117                 Behavior on height { NumberAnimation { duration: 250 } }
118                 states: [
119                     State { name: "closed"
120                         PropertyChanges { target: delbox; height: 50 }
121                         PropertyChanges { target: recipemethod; opacity: 0 }
122                         PropertyChanges { target: recipetime; opacity: 0 }
123                         PropertyChanges { target: ingredientlist; opacity: 0 }
124                     },
125                     State { name: "open"
126                         PropertyChanges { target: delbox; height: recipemethod.height+recipetime.height+ingredientlist.height+50 }
127                         PropertyChanges { target: recipemethod; opacity: 1 }
128                         StateChangeScript { script: { recipeview.positionViewAtIndex(model.index, ListView.Beginning); } }
129                     }
130                 ]
131             }
132         }
133     }
134
135     SystemTestHelp { id: helpbubble; visible: statenum != 0
136         anchors { top: parent.top; horizontalCenter: parent.horizontalCenter; topMargin: 50 }
137     }
138     BugPanel { id: bugpanel }
139
140     states: [
141         State { name: "start"; when: statenum == 1
142             PropertyChanges { target: xmllistmodelelementtest
143                 testtext: "This is a ListView populated by an XmlListModel. Clicking on an item will show the recipe details.\n"+
144                 "Next we will change to source of the model data to a local string" }
145         },
146         State { name: "xmlstring"; when: statenum == 2
147             PropertyChanges { target: xmllistmodelelement; source: "" }
148             PropertyChanges { target: xmllistmodelelement; xml: modelxml }
149             PropertyChanges { target: xmllistmodelelementtest
150                 testtext: "The list should now only contain a Hot chocolate recipe.\n"+
151                 "Advance to restart the test." }
152         }
153     ]
154
155 }