libandroidplugin added
[mardrone] / mardrone / imports / com / nokia / android.1.1 / CommonDialog.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 Dialog {
45     id: root
46
47     property alias titleText: titleAreaText.text
48     property url titleIcon
49     property variant buttonTexts: []
50     property bool privateCloseIcon: false
51
52     signal buttonClicked(int index)
53
54     onButtonTextsChanged: {
55         for (var i = buttonRow.children.length; i > 0; --i) {
56             buttonRow.children[i - 1].destroy()
57         }
58         for (var j = 0; j < buttonTexts.length; ++j) {
59             var button = buttonComponent.createObject(buttonRow)
60             button.text = buttonTexts[j]
61             button.index = j
62         }
63     }
64
65     Component {
66         id: buttonComponent
67         ToolButton {
68             property int index
69
70             width: internal.buttonWidth()
71             height: privateStyle.toolBarHeightLandscape
72             platformInverted: root.platformInverted
73
74             onClicked: {
75                 if (root.status == DialogStatus.Open) {
76                     root.buttonClicked(index)
77                     root.close()
78                 }
79             }
80         }
81     }
82
83     QtObject {
84         id: internal
85
86         function buttonWidth() {
87             switch (buttonTexts.length) {
88                 case 0: return 0
89                 case 1: return Math.round((privateStyle.dialogMaxSize - 3 * platformStyle.paddingMedium) / 2)
90                 default: return (buttonContainer.width - (buttonTexts.length + 1) *
91                     platformStyle.paddingMedium) / buttonTexts.length
92             }
93         }
94
95         function iconSource() {
96             if (privateCloseIcon) {
97                 return privateStyle.imagePath((iconMouseArea.pressed && !iconMouseArea.pressCancelled
98                     ? "qtg_graf_popup_close_pressed"
99                     : "qtg_graf_popup_close_normal"),
100                     root.platformInverted)
101             } else {
102                 return root.titleIcon
103             }
104         }
105     }
106
107     title: Item {
108         anchors.left: parent.left
109         anchors.right: parent.right
110         height: platformStyle.graphicSizeSmall + 2 * platformStyle.paddingLarge
111
112         LayoutMirroring.enabled: privateCloseIcon ? false : undefined
113         LayoutMirroring.childrenInherit: true
114
115         Item {
116             id: titleLayoutHelper // needed to make the text mirror correctly
117
118             anchors.left: parent.left
119             anchors.right: titleAreaIcon.source == "" ? parent.right : titleAreaIcon.left
120             anchors.top: parent.top
121             anchors.bottom: parent.bottom
122             anchors.margins: platformStyle.paddingLarge
123
124             Text {
125                 id: titleAreaText
126
127                 LayoutMirroring.enabled: root.LayoutMirroring.enabled
128
129                 anchors.fill: parent
130                 font { family: platformStyle.fontFamilyRegular; pixelSize: platformStyle.fontSizeLarge }
131                 color: root.platformInverted ? platformStyle.colorNormalLinkInverted
132                                              : platformStyle.colorNormalLink
133                 elide: Text.ElideRight
134                 horizontalAlignment: Text.AlignLeft
135                 verticalAlignment: Text.AlignVCenter
136             }
137         }
138
139         Image {
140             id: titleAreaIcon
141
142             anchors.right: parent.right
143             anchors.rightMargin: platformStyle.paddingLarge
144             anchors.verticalCenter: parent.verticalCenter
145             source: internal.iconSource()
146             sourceSize.height: platformStyle.graphicSizeSmall
147             sourceSize.width: platformStyle.graphicSizeSmall
148
149             MouseArea {
150                 id: iconMouseArea
151
152                 property bool pressCancelled
153
154                 anchors.centerIn: parent
155                 width: parent.width + 2 * platformStyle.paddingLarge
156                 height: parent.height + 2 * platformStyle.paddingLarge
157                 enabled: privateCloseIcon && root.status == DialogStatus.Open
158
159                 onPressed: {
160                     pressCancelled = false
161                     privateStyle.play(Android.BasicButton)
162                 }
163                 onClicked: {
164                     if (!pressCancelled)
165                         root.reject()
166                 }
167                 onReleased: {
168                     if (!pressCancelled)
169                         privateStyle.play(Android.PopupClose)
170                 }
171                 onExited: pressCancelled = true
172             }
173         }
174     }
175
176     buttons: Item {
177         id: buttonContainer
178
179         LayoutMirroring.enabled: false
180         LayoutMirroring.childrenInherit: true
181
182         width: parent.width
183         height: buttonTexts.length ? privateStyle.toolBarHeightLandscape + 2 * platformStyle.paddingSmall : 0
184
185         Row {
186             id: buttonRow
187             objectName: "buttonRow"
188             anchors.centerIn: parent
189             spacing: platformStyle.paddingMedium
190         }
191     }
192 }