improved desktop UI
[mardrone] / mardrone / imports / com / nokia / meego / MultiSelectionDialog.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 import "MultiSelectionDialog.js" as MultiSelectionDialog
45
46 CommonDialog {
47     id: root
48
49     property alias model: selectionListView.model
50     // Common API: property list<int> selectedIndexes (currently not possible due to QTBUG-10822)
51     property variant selectedIndexes: []   // read & write, variant is supposed to be list<int>
52     property alias acceptButtonText: acceptButton.text      //Convenience wrapper on top of the buttons
53     property alias rejectButtonText: rejectButton.text      //Convenience wrapper on top of the buttons
54     //property alias titleText: titleLabel.text
55
56     property Component delegate:          // Note that this is the default delegate for the list
57         Component {
58             id: defaultDelegate
59
60             Item {
61                 id: delegateItem
62
63                 height: root.platformStyle.itemHeight
64                 anchors.left: parent.left
65                 anchors.right: parent.right
66
67                 MouseArea {
68                     id: delegateMouseArea
69                     anchors.fill: parent;
70                     onPressed: MultiSelectionDialog.__toggleIndex(index);
71                 }
72
73                 Rectangle {
74                     id: backgroundRect
75                     anchors.fill: parent
76                     color: MultiSelectionDialog.__isSelected(index) ? 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                             MultiSelectionDialog.__isSelected(index) ? root.platformStyle.itemSelectedBackground :
85                             root.platformStyle.itemBackground
86                 }
87
88                 Text {
89                     id: itemText
90                     elide: Text.ElideRight
91                     color: MultiSelectionDialog.__isSelected(index) ? 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                     font: root.platformStyle.itemFont
98                 }
99                 Component.onCompleted: {
100                     try {
101                         // Legacy. "name" used to be the role which was used by delegate
102                         itemText.text = name
103                     } catch(err) {
104                         try {
105                             // "modelData" available for JS array and for models with one role
106                             itemText.text = modelData
107                         } catch (err) {
108                             try {
109                                  // C++ models have "display" role available always
110                                 itemText.text = display
111                             } catch(err) {
112                             }
113                         }
114                     }
115                 }
116             }
117         }
118
119     // Style API
120     property Style platformStyle: SelectionDialogStyle {}
121
122     //Deprecated, TODO Remove this on w13
123     property alias style: root.platformStyle
124
125     // private api
126     property int __pressDelay: platformStyle.pressDelay
127     property variant __selectedIndexesHash: []
128
129     QtObject {
130         id: backup
131         property variant oldSelectedIndexes: []
132     }
133     onStatusChanged: {
134       if (status == DialogStatus.Opening) {
135           selectionListView.positionViewAtIndex(selectedIndexes[0], ListView.Center)
136       }
137       if (status == DialogStatus.Open)
138           backup.oldSelectedIndexes = selectedIndexes
139     }
140     onRejected: { selectedIndexes = backup.oldSelectedIndexes }
141
142     onSelectedIndexesChanged: {
143         MultiSelectionDialog.__syncHash();
144     }
145
146     // the title field consists of the following parts: title string and
147     // a close button (which is in fact an image)
148     // it can additionally have an icon
149     titleText: "Multi-Selection Dialog"
150
151     // the content field which contains the selection content
152     content: Item {
153
154         id: selectionContent
155         property int listViewHeight
156         property int maxListViewHeight : visualParent
157                                          ? visualParent.height * 0.87
158                                                  - buttonRow.childrenRect.height - root.platformStyle.contentSpacing - root.platformStyle.buttonsTopMargin
159                                                  - root.platformStyle.titleBarHeight
160                                          : root.parent
161                                                  ? root.parent.height * 0.87
162                                                          - buttonRow.childrenRect.height - root.platformStyle.contentSpacing - root.platformStyle.buttonsTopMargin
163                                                          - root.platformStyle.titleBarHeight
164                                                  : 350
165         height: listViewHeight > maxListViewHeight ? maxListViewHeight : listViewHeight
166         width: root.width
167         y : root.platformStyle.contentSpacing
168
169         ListView {
170             id: selectionListView
171             model: ListModel {}
172
173             currentIndex : -1
174             anchors.fill: parent
175             delegate: root.delegate
176             focus: true
177             clip: true
178             pressDelay: __pressDelay
179
180             ScrollDecorator {
181                 id: scrollDecorator
182                 flickableItem: selectionListView
183                 platformStyle.inverted: true
184             }
185             onModelChanged: selectionContent.listViewHeight = model.count * platformStyle.itemHeight
186         }
187
188     }
189
190     buttons: Item {
191         id: buttonRowFiller
192         width: parent.width
193         height:  childrenRect.height //+ root.platformStyle.buttonsTopMargin
194         y: root.platformStyle.buttonsTopMargin
195
196         onWidthChanged: {
197             if (acceptButton.width + rejectButton.width > width) {
198                 acceptButton.width = width / 2
199                 rejectButton.width = width / 2
200             } else {
201                 acceptButton.width = acceptButton.implicitWidth
202                 rejectButton.width = rejectButton.implicitWidth
203             }
204         }
205
206         Row {
207             id: buttonRow
208             height: childrenRect.height
209             anchors.horizontalCenter: parent.horizontalCenter
210             Button {
211                 id: acceptButton
212                 height: implicitHeight
213                 objectName: "acceptButton"
214                 text: ""
215                 onClicked: accept()
216                 visible: text != ""
217                 __dialogButton: true
218                 platformStyle: ButtonStyle {inverted: true}
219             }
220             Button {
221                 id: rejectButton
222                 height: implicitHeight
223                 objectName: "rejectButton"
224                 text: ""
225                 onClicked: reject()
226                 visible: text != ""
227                 __dialogButton: true
228                 platformStyle: ButtonStyle {inverted: true}
229             }
230         }
231     }
232 }
233