fe4d15cb3112424c4bd379461881403e60fa1c71
[mardrone] / mardrone / imports / com / nokia / meego / SelectionDialog.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 Qt Components project.
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.1
42 import "." 1.0
43 import "UIConstants.js" as UI
44
45 CommonDialog {
46     id: root
47
48     // Common API
49     property alias model: selectionListView.model
50     property int selectedIndex: -1   // read & write
51     //property string titleText: "Selection Dialog"
52
53     property Component delegate:          // Note that this is the default delegate for the list
54         Component {
55             id: defaultDelegate
56
57             Item {
58                 id: delegateItem
59                 property bool selected: index == selectedIndex;
60
61                 height: root.platformStyle.itemHeight
62                 anchors.left: parent.left
63                 anchors.right: parent.right
64
65                 MouseArea {
66                     id: delegateMouseArea
67                     anchors.fill: parent;
68                     onPressed: selectedIndex = index;
69                     onClicked:  accept();
70                 }
71
72
73                 Rectangle {
74                     id: backgroundRect
75                     anchors.fill: parent
76                     color: delegateItem.selected ? root.platformStyle.itemSelectedBackgroundColor : root.platformStyle.itemBackgroundColor
77                 }
78
79                 BorderImage {
80                     id: background
81                     anchors.fill: parent
82                     border { left: UI.CORNER_MARGINS; top: UI.CORNER_MARGINS; right: UI.CORNER_MARGINS; bottom: UI.CORNER_MARGINS }
83                     source: delegateMouseArea.pressed ? root.platformStyle.itemPressedBackground :
84                             delegateItem.selected ? root.platformStyle.itemSelectedBackground :
85                             root.platformStyle.itemBackground
86                 }
87
88                 Text {
89                     id: itemText
90                     elide: Text.ElideRight
91                     color: delegateItem.selected ? root.platformStyle.itemSelectedTextColor : root.platformStyle.itemTextColor
92                     anchors.verticalCenter: delegateItem.verticalCenter
93                     anchors.left: parent.left
94                     anchors.right: parent.right
95                     anchors.leftMargin: root.platformStyle.itemLeftMargin
96                     anchors.rightMargin: root.platformStyle.itemRightMargin
97                     text: modelData
98                     font: root.platformStyle.itemFont
99                 }
100                 Component.onCompleted: {
101                     try {
102                         // Legacy. "name" used to be the role which was used by delegate
103                         itemText.text = name
104                     } catch(err) {
105                         try {
106                             // "modelData" available for JS array and for models with one role
107                             itemText.text = modelData
108                         } catch (err) {
109                             try {
110                                  // C++ models have "display" role available always
111                                 itemText.text = display
112                             } catch(err) {
113                             }
114                         }
115                     }
116                 }
117             }
118         }
119
120     onStatusChanged: {
121       if (status == DialogStatus.Opening && selectedIndex >= 0) {
122           selectionListView.positionViewAtIndex(selectedIndex, ListView.Center)
123       }
124     }
125
126     // Style API
127     property Style platformStyle: SelectionDialogStyle {}
128
129     //Deprecated, TODO Remove this on w13
130     property alias style: root.platformStyle
131
132     // private api
133     property int __pressDelay: platformStyle.pressDelay
134
135     // the title field consists of the following parts: title string and
136     // a close button (which is in fact an image)
137     // it can additionally have an icon
138     titleText:"Selection Dialog"
139
140     // the content field which contains the selection content
141     content: Item {
142
143         id: selectionContent
144         property int listViewHeight
145         property int maxListViewHeight : visualParent
146         ? visualParent.height * 0.87
147                 - root.platformStyle.titleBarHeight - root.platformStyle.contentSpacing - 50
148         : root.parent
149                 ? root.parent.height * 0.87
150                         - root.platformStyle.titleBarHeight - root.platformStyle.contentSpacing - 50
151                 : 350
152         height: listViewHeight > maxListViewHeight ? maxListViewHeight : listViewHeight
153         width: root.width
154         y : root.platformStyle.contentSpacing
155
156         ListView {
157             id: selectionListView
158             model: ListModel {}
159
160             currentIndex : -1
161             anchors.fill: parent
162             delegate: root.delegate
163             focus: true
164             clip: true
165             pressDelay: __pressDelay
166
167             ScrollDecorator {
168                 id: scrollDecorator
169                 flickableItem: selectionListView
170                 platformStyle.inverted: true
171             }
172             onModelChanged: selectionContent.listViewHeight = model.count * platformStyle.itemHeight
173         }
174
175     }
176 }
177
178