1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Components project.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
42 import Qt.labs.components 1.1
49 property alias checkable: checkableItem.enabled
50 property alias checked: checkableItem.checked
51 property bool enabled: true // overridden from base class
52 property alias text: label.text
53 property url iconSource
54 property bool flat: (!text && iconSource != "" && parent && !internal.isButtonRow(parent))
55 property bool pressed: mouseArea.containsMouse && (stateGroup.state == "Pressed" || stateGroup.state == "PressAndHold")
58 property alias platformExclusiveGroup: checkableItem.exclusiveGroup
59 property bool platformInverted: false
65 signal platformReleased
66 signal platformPressAndHold
69 background.visible = !flat || (checkableItem.enabled && checkableItem.checked && !internal.isButtonRow(parent))
74 return internal.iconButtonWidth()
75 else if (iconSource == "")
76 return Math.max(internal.iconButtonWidth(), internal.textButtonWidth())
78 return internal.iconButtonWidth() + internal.textButtonWidth()
81 if ((iconSource != "") && !text)
82 // if there is just an icon, then it's full button height
83 return internal.iconButtonWidth()
85 // Otherwise frame height is always tool bar's height in landscape, regardless of the current orientation
86 return privateStyle.toolBarHeightLandscape
92 source: privateStyle.imagePath(internal.imageName() + internal.modeName(), root.platformInverted)
94 left: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
95 top: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
96 right: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
97 bottom: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
106 left: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
107 top: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
108 right: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
109 bottom: internal.isFrameGraphic ? platformStyle.borderSizeMedium : 0;
112 anchors.fill: background
119 source: privateStyle.toolBarIconPath(iconSource, root.platformInverted)
120 visible: iconSource != ""
121 sourceSize.width: platformStyle.graphicSizeSmall
122 sourceSize.height: platformStyle.graphicSizeSmall
124 centerIn: !text ? parent : undefined
125 verticalCenter: !text ? undefined : parent.verticalCenter
126 left: !text ? undefined : parent.left
127 leftMargin: !text ? 0 : 2 * platformStyle.paddingMedium
135 horizontalAlignment: Text.AlignHCenter
136 verticalAlignment: Text.AlignVCenter
137 elide: Text.ElideRight
138 font { family: platformStyle.fontFamilyRegular; pixelSize: platformStyle.fontSizeLarge }
141 return root.platformInverted ? platformStyle.colorDisabledLightInverted
142 : platformStyle.colorDisabledLight
143 else if (stateGroup.state == "Pressed" || stateGroup.state == "PressAndHold")
144 return root.platformInverted ? platformStyle.colorPressedInverted
145 : platformStyle.colorPressed
147 return root.platformInverted ? platformStyle.colorNormalLightInverted
148 : platformStyle.colorNormalLight
152 top: parent.top; bottom: parent.bottom
153 left: iconSource != "" ? contentIcon.right : parent.left; right: parent.right
154 leftMargin: iconSource != "" ? platformStyle.paddingSmall : platformStyle.paddingMedium
155 rightMargin: platformStyle.paddingMedium
164 onPressed: if (root.enabled) stateGroup.state = "Pressed"
165 onReleased: if (root.enabled) stateGroup.state = ""
169 stateGroup.state = "Canceled"
170 // Reset state. Can't expect a release since mouse was ungrabbed
171 stateGroup.state = ""
174 onPressAndHold: if (stateGroup.state != "Canceled" && root.enabled) stateGroup.state = "PressAndHold"
175 onExited: if (root.enabled) stateGroup.state = "Canceled"
181 property bool isFrameGraphic : imageName().search("_fr") > 0
183 function belongsToExclusiveGroup() {
184 return checkableItem.exclusiveGroup
186 && root.parent.hasOwnProperty("checkedButton")
187 && root.parent.exclusive)
190 function modeName() {
191 if (isButtonRow(parent))
192 return parent.privateModeName(root, 1)
195 else if (flat && checkableItem.checked && !internal.isButtonRow(parent))
201 function iconButtonWidth() {
202 return (screen.width < screen.height) ? privateStyle.toolBarHeightPortrait : privateStyle.toolBarHeightLandscape
205 function textButtonWidth() {
206 return platformStyle.paddingMedium * ((screen.width < screen.height) ? 15 : 25)
210 if (!belongsToExclusiveGroup()) {
211 if (checkableItem.enabled && checkableItem.checked)
212 privateStyle.play(Android.SensitiveButton)
214 privateStyle.play(Android.BasicButton)
215 } else if (checkableItem.enabled && !checkableItem.checked) {
216 privateStyle.play(Android.BasicButton)
220 background.visible = true
221 highlight.source = privateStyle.imagePath(internal.imageName() + "pressed", root.platformInverted)
223 contentIcon.scale = 0.95
224 highlight.opacity = 1
229 contentIcon.scale = 1
230 highlight.opacity = 0
232 if (((checkableItem.enabled && checkableItem.checked && !belongsToExclusiveGroup())
233 || !checkableItem.enabled) && stateGroup.state != "Canceled")
234 privateStyle.play(Android.BasicButton)
236 if (flat && isButtonRow(parent))
237 visibleEffect.restart() //Background invisible
238 else if (flat && !checkableItem.enabled)
239 visibleEffect.restart() //Background invisible
241 clickedEffect.restart() //Background stays visible
243 root.platformReleased()
247 checkableItem.toggle()
252 root.platformPressAndHold()
255 function isButtonRow(item) {
257 item.hasOwnProperty("checkedButton") &&
258 item.hasOwnProperty("privateDirection") &&
259 item.privateDirection == Qt.Horizontal)
262 // The function imageName() handles fetching correct graphics for the ToolButton.
263 // If the parent of a ToolButton is ButtonRow, segmented-style graphics are used to create a
264 // seamless row of buttons. Otherwise normal ToolButton graphics are utilized.
265 function imageName() {
266 var mirror = root.LayoutMirroring.enabled // To create binding
267 if (isButtonRow(parent))
268 return parent.privateGraphicsName(root, 1)
270 return (!flat || text || iconSource == "") ? "qtg_fr_toolbutton_text_" : "qtg_graf_toolbutton_"
278 State { name: "Pressed" },
279 State { name: "PressAndHold" },
280 State { name: "Canceled" }
286 ScriptAction { script: internal.press() }
291 ScriptAction { script: internal.hold() }
296 ScriptAction { script: internal.release() }
297 ScriptAction { script: internal.click() }
302 ScriptAction { script: internal.release() }
303 ScriptAction { script: internal.click() }
308 ScriptAction { script: internal.release() }
313 ScriptAction { script: internal.release() }
325 easing.type: Easing.Linear
333 easing.type: Easing.Linear
341 easing.type: Easing.Linear
346 SequentialAnimation {
354 easing.type: Easing.Linear
362 easing.type: Easing.Linear
370 easing.type: Easing.Linear