improved desktop UI
[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                     console.debug("SelectionDialog delegate ",name,file)
102                     try {
103                         // Legacy. "name" used to be the role which was used by delegate
104                         itemText.text = name
105                     } catch(err) {
106                         try {
107                             // "modelData" available for JS array and for models with one role
108                             itemText.text = modelData
109                         } catch (err) {
110                             try {
111                                  // C++ models have "display" role available always
112                                 itemText.text = display
113                             } catch(err) {
114                             }
115                         }
116                     }
117                 }
118             }
119         }
120
121     onStatusChanged: {
122       if (status == DialogStatus.Opening && selectedIndex >= 0) {
123           selectionListView.positionViewAtIndex(selectedIndex, ListView.Center)
124       }
125     }
126
127     // Style API
128     property Style platformStyle: SelectionDialogStyle {}
129
130     //Deprecated, TODO Remove this on w13
131     property alias style: root.platformStyle
132
133     // private api
134     property int __pressDelay: platformStyle.pressDelay
135
136     // the title field consists of the following parts: title string and
137     // a close button (which is in fact an image)
138     // it can additionally have an icon
139     titleText:"Selection Dialog"
140
141     // the content field which contains the selection content
142     content: Item {
143
144         id: selectionContent
145         property int listViewHeight
146         property int maxListViewHeight : visualParent
147         ? visualParent.height * 0.87
148                 - root.platformStyle.titleBarHeight - root.platformStyle.contentSpacing - 50
149         : root.parent
150                 ? root.parent.height * 0.87
151                         - root.platformStyle.titleBarHeight - root.platformStyle.contentSpacing - 50
152                 : 350
153         height: listViewHeight > maxListViewHeight ? maxListViewHeight : listViewHeight
154         width: root.width
155         y : root.platformStyle.contentSpacing
156
157         ListView {
158             id: selectionListView
159             model: ListModel {}
160
161             currentIndex : -1
162             anchors.fill: parent
163             delegate: root.delegate
164             focus: true
165             clip: true
166             pressDelay: __pressDelay
167
168             ScrollDecorator {
169                 id: scrollDecorator
170                 flickableItem: selectionListView
171                 platformStyle.inverted: true
172             }
173             onCountChanged: selectionContent.listViewHeight = (typeof model.count === 'undefined' ? model.length : model.count) * platformStyle.itemHeight
174             onModelChanged: selectionContent.listViewHeight = (typeof model.count === 'undefined' ? model.length : model.count) * platformStyle.itemHeight
175         }
176
177     }
178 }
179
180