added android components
[mardrone] / mardrone / imports / com / nokia / android.1.1 / MenuItem.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.1
43
44 Item {
45     id: root
46
47     property alias text: textArea.text
48
49     // Symbian specific API
50     property bool platformInverted: false
51     property bool platformSubItemIndicator: false
52     property real platformLeftMargin: platformStyle.paddingLarge
53
54     signal clicked
55
56     width: parent.width; height: privateStyle.menuItemHeight
57
58     QtObject {
59         id: internal
60
61         function bg_postfix() {
62             if (activeFocus && android.listInteractionMode == Android.KeyNavigation)
63                 return "highlighted"
64             else if (mouseArea.pressed && mouseArea.containsMouse && !mouseArea.canceled)
65                 return "pressed"
66             else
67                 return "popup_normal"
68         }
69
70         function textColor() {
71             if (!enabled)
72                 return root.platformInverted ? platformStyle.colorDisabledLightInverted
73                                              : platformStyle.colorDisabledLight
74             else if (activeFocus && android.listInteractionMode == Android.KeyNavigation)
75                 return root.platformInverted ? platformStyle.colorHighlightedInverted
76                                              : platformStyle.colorHighlighted
77             else if (mouseArea.pressed && mouseArea.containsMouse)
78                 return root.platformInverted ? platformStyle.colorPressedInverted
79                                              : platformStyle.colorPressed
80             else
81                 return root.platformInverted ? platformStyle.colorNormalLightInverted
82                                              : platformStyle.colorNormalLight
83         }
84     }
85
86     BorderImage {
87         source: privateStyle.imagePath("qtg_fr_list_" + internal.bg_postfix(), root.platformInverted)
88         border { left: 20; top: 20; right: 20; bottom: 20 }
89         anchors.fill: parent
90     }
91
92     Text {
93         id: textArea
94         anchors {
95             verticalCenter: parent.verticalCenter
96             left: parent.left
97             leftMargin: platformLeftMargin
98             right: iconLoader.status == Loader.Ready ? iconLoader.left : parent.right
99             rightMargin: iconLoader.status == Loader.Ready ? platformStyle.paddingMedium : privateStyle.scrollBarThickness
100         }
101         font { family: platformStyle.fontFamilyRegular; pixelSize: platformStyle.fontSizeMedium }
102         color: internal.textColor()
103         horizontalAlignment: Text.AlignLeft
104         elide: Text.ElideRight
105     }
106
107     Loader {
108         id: iconLoader
109         sourceComponent: root.platformSubItemIndicator ? subItemIcon : undefined
110         anchors {
111             right: parent.right
112             rightMargin: privateStyle.scrollBarThickness
113             verticalCenter: parent.verticalCenter
114         }
115     }
116
117     Component {
118         id: subItemIcon
119
120         Image {
121             source: privateStyle.imagePath("qtg_graf_drill_down_indicator", platformInverted)
122             sourceSize.width: platformStyle.graphicSizeSmall
123             sourceSize.height: platformStyle.graphicSizeSmall
124             mirror: LayoutMirroring.enabled
125         }
126     }
127
128
129     MouseArea {
130         id: mouseArea
131
132         property bool canceled: false
133
134         anchors.fill: parent
135
136         onPressed: {
137             canceled = false
138             android.listInteractionMode = Android.TouchInteraction
139             privateStyle.play(Android.BasicItem)
140         }
141         onClicked: {
142             if (!canceled)
143                 root.clicked()
144         }
145         onReleased: {
146             if (!canceled)
147                 privateStyle.play(Android.PopupClose)
148         }
149         onExited: canceled = true
150     }
151
152     Keys.onPressed: {
153         event.accepted = true
154         switch (event.key) {
155             case Qt.Key_Select:
156             case Qt.Key_Enter:
157             case Qt.Key_Return: {
158                 if (!event.isAutoRepeat) {
159                     if (android.listInteractionMode != Android.KeyNavigation)
160                         android.listInteractionMode = Android.KeyNavigation
161                     else
162                         root.clicked()
163                 }
164                 break
165             }
166
167             case Qt.Key_Up: {
168                 if (android.listInteractionMode != Android.KeyNavigation) {
169                     android.listInteractionMode = Android.KeyNavigation
170                     if (ListView.view != null)
171                         ListView.view.positionViewAtIndex(index, ListView.Beginning)
172                 } else {
173                     if (ListView.view != null)
174                         ListView.view.decrementCurrentIndex()
175                     else
176                         event.accepted = false
177                 }
178                 break
179             }
180
181             case Qt.Key_Down: {
182                 if (android.listInteractionMode != Android.KeyNavigation) {
183                     android.listInteractionMode = Android.KeyNavigation
184                     if (ListView.view != null)
185                         ListView.view.positionViewAtIndex(index, ListView.Beginning)
186                 } else {
187                     if (ListView.view != null)
188                         ListView.view.incrementCurrentIndex()
189                     else
190                         event.accepted = false
191                 }
192                 break
193             }
194             case Qt.Key_Left:
195             case Qt.Key_Right: {
196                 // If this MenuItem belongs to Menu invoke highlight
197                 // in other cases consume event but do nothing
198                 if (ListView.view == null && android.listInteractionMode != Android.KeyNavigation)
199                     android.listInteractionMode = Android.KeyNavigation
200                 break
201             }
202             default: {
203                 event.accepted = false
204                 break
205             }
206         }
207     }
208 }