plasma-framework/declarativeimports/plasmacomponents/qml/ToolButton.qml

435 lines
15 KiB
QML
Raw Normal View History

2011-10-13 11:19:39 +02:00
/*
* Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
* Copyright (C) 2011 by Marco Martin <mart@kde.org>
2011-10-13 11:19:39 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
2011-12-22 11:53:15 +01:00
/**Documented API
2011-11-08 08:33:08 +01:00
Inherits:
Item
Imports:
QtQuick 1.1
org.kde.plasma.core
Description:
A plasma theme based toolbutton.
Properties:
bool flat:
Returns true if the button is flat.
bool checked: false
Returns true if the button is checked.
bool checkable:
Returns true if the button is checkable.
2012-01-12 11:04:59 +01:00
bool pressed:
2011-11-08 08:33:08 +01:00
Returns true if the button is pressed.
alias text:
Sets the text for the button.
2012-01-12 11:04:59 +01:00
variant iconSource:
2011-11-08 08:33:08 +01:00
Sets the icon for the button.
2011-12-28 18:02:44 +01:00
It can be any image from any protocol supported by the Image element, or a freedesktop-compatible icon name
2011-11-08 08:33:08 +01:00
2012-01-12 11:04:59 +01:00
string font:
2011-11-08 08:33:08 +01:00
Sets the font for the button.
bool enabled:
2012-08-14 19:05:57 +02:00
Returns whether the button is currently enabled and receives user input.
2011-11-08 08:33:08 +01:00
Signals:
onClicked:
The signal is being emmited when the button is being clicked.
Plasma properties:
* real minimumWidth:
This property holds the smallest width this button can be to show all the contents
* real minimumHeight:
This property holds the smallest height this button can be to show all the contents
2011-11-08 08:33:08 +01:00
**/
import QtQuick 1.1
2011-10-13 11:19:39 +02:00
import org.kde.plasma.core 0.1 as PlasmaCore
import "private" as Private
2011-10-13 11:19:39 +02:00
Item {
id: button
// Commmon API
property bool flat: true
2011-11-28 13:49:28 +01:00
property bool checked: defaultAction ? defaultAction.checked : false
property bool checkable: defaultAction ? defaultAction.checkable : false
2011-10-13 11:19:39 +02:00
property alias pressed: mouse.pressed
property alias text: label.text
property alias iconSource: icon.source
property alias font: label.font
signal clicked()
2011-11-28 13:49:28 +01:00
// Plasma extensiuons
property QtObject defaultAction
2012-10-11 13:30:24 +02:00
enabled: defaultAction == undefined || defaultAction.enabled
2011-10-13 11:19:39 +02:00
//icon + label + left margin + right margin + spacing between icon and text
2012-10-18 17:22:34 +02:00
property real minimumWidth: theme.smallIconSize + label.preferredWidth + delegate.margins.left + delegate.margins.right + ((icon.valid) ? delegate.margins.left : 0)
2012-10-18 16:50:59 +02:00
property real minimumHeight: Math.max(theme.smallIconSize, label.paintedHeight) + delegate.margins.top + delegate.margins.bottom
2011-11-09 23:31:52 +01:00
implicitWidth: {
if (label.text.length == 0) {
height;
2011-11-09 23:31:52 +01:00
} else {
Math.max(theme.defaultFont.mSize.width*12, minimumWidth);
2011-11-09 23:31:52 +01:00
}
2011-10-13 11:19:39 +02:00
}
implicitHeight: Math.max(theme.defaultFont.mSize.height*1.6, minimumHeight)
2011-10-13 11:19:39 +02:00
// TODO: needs to define if there will be specific graphics for
// disabled buttons
opacity: enabled ? 1.0 : 0.5
Keys.onSpacePressed: internal.userPressed = true
Keys.onReturnPressed: internal.userPressed = true
2011-10-13 11:19:39 +02:00
Keys.onReleased: {
internal.userPressed = false
2011-10-13 11:19:39 +02:00
if (event.key == Qt.Key_Space ||
event.key == Qt.Key_Return)
internal.clickButton()
2011-10-13 11:19:39 +02:00
}
onActiveFocusChanged: {
if (activeFocus) {
shadow.state = "focus"
} else if (checked) {
shadow.state = "hidden"
} else {
shadow.state = "shadow"
}
}
QtObject {
id: internal
property bool userPressed: false
function clickButton()
{
if (!button.enabled) {
return
}
2011-11-28 13:49:28 +01:00
if (defaultAction && defaultAction.checkable) {
defaultAction.checked = !defaultAction.checked
} else if (button.checkable) {
button.checked = !button.checked
}
if (button.KeyNavigation.tab || button.KeyNavigation.backtab) {
// Only focus the button if it is set up for keyboard
// navigation. This avoid getting a strange focus frame around
// buttons which are usually not focusable, such as buttons in
// a toolbar.
button.forceActiveFocus();
}
button.clicked()
2011-11-28 13:49:28 +01:00
if (defaultAction) {
defaultAction.trigger()
}
}
}
Loader {
id: delegate
anchors.fill:parent
property QtObject margins: item.margins
property string shadowState: "shadow"
sourceComponent: {
if (label.text.length == 0 && button.width == button.height && button.parent.checkedButton === undefined && !flat) {
return roundButtonComponent
} else {
return buttonComponent
}
}
2011-10-13 11:19:39 +02:00
}
Component {
id: buttonComponent
Item {
2012-10-11 13:30:24 +02:00
parent: delegate
anchors.fill: parent
property alias margins: surface.margins
property alias hasOverState: shadow.hasOverState
Private.ButtonShadow {
id: shadow
anchors.fill: parent
visible: !flat && (surface.enabledBorders == "AllBorders" || state == "hover" || state == "focus")
state: delegate.shadowState
}
2011-10-13 11:19:39 +02:00
PlasmaCore.FrameSvgItem {
id: surface
enabledBorders: {
if (flat ||
button.parent.width < button.parent.implicitWidth ||
button.parent.checkedButton === undefined ||
!bordersSvg.hasElement("pressed-hint-compose-over-border")) {
if (shadows !== null) {
shadows.destroy()
}
return "AllBorders"
}
var borders = new Array()
if (button.x == 0) {
borders.push("LeftBorder")
}
if (button.y == 0) {
borders.push("TopBorder")
}
if (button.x + button.width + delegate.margins.right >= button.parent.width) {
borders.push("RightBorder")
}
if (button.y + button.height + delegate.margins.bottom >= button.parent.height) {
borders.push("BottomBorder")
}
if (shadows === null) {
shadows = shadowsComponent.createObject(surface)
}
return borders.join("|")
}
anchors.fill: parent
imagePath: "widgets/button"
prefix: (internal.userPressed || checked) ? "pressed" : "normal"
//internal: if there is no hover status, don't paint on mouse over in touchscreens
2012-10-11 12:32:58 +02:00
opacity: (internal.userPressed || checked || !flat || (shadow.hasOverState && mouse.containsMouse && button.enabled)) ? 1 : 0
Behavior on opacity {
PropertyAnimation { duration: 250 }
}
PlasmaCore.Svg {
id: bordersSvg
imagePath: "widgets/button"
}
property Item shadows
Component {
id: shadowsComponent
Item {
anchors.fill: parent
PlasmaCore.SvgItem {
svg: bordersSvg
width: naturalSize.width
elementId: surface.prefix+"-left"
visible: button.x > 0
anchors {
left: parent.left
top: parent.top
bottom: parent.bottom
margins: 1
leftMargin: -1
}
}
PlasmaCore.SvgItem {
svg: bordersSvg
width: naturalSize.width
elementId: surface.prefix+"-right"
visible: button.x + button.width < button.parent.width
anchors {
right: parent.right
top: parent.top
bottom: parent.bottom
margins: 1
rightMargin: -1
}
}
PlasmaCore.SvgItem {
svg: bordersSvg
height: naturalSize.height
elementId: surface.prefix+"-top"
visible: button.y > 0
anchors {
left: parent.left
top: parent.top
right: parent.right
margins: 1
topMargin: -1
}
}
PlasmaCore.SvgItem {
svg: bordersSvg
width: naturalSize.width
elementId: surface.prefix+"-bottom"
visible: button.y + button.height < button.parent.height
anchors {
left: parent.left
right: parent.right
bottom: parent.bottom
margins: 1
bottomMargin: -1
}
}
}
}
}
}
}
Component {
id: roundButtonComponent
Item {
2012-10-11 13:30:24 +02:00
id: roundButtonDelegate
parent: delegate
anchors.fill: parent
property QtObject margins: QtObject {
2012-10-11 13:30:24 +02:00
property int left: delegate.width/8
property int top: delegate.width/8
property int right: delegate.width/8
property int bottom: delegate.width/8
}
property alias hasOverState: roundShadow.hasOverState
Private.RoundShadow {
id: roundShadow
visible: !flat
anchors.fill: parent
state: delegate.shadowState
}
PlasmaCore.Svg {
id: buttonSvg
imagePath: "widgets/actionbutton"
}
PlasmaCore.SvgItem {
id: buttonItem
svg: buttonSvg
elementId: (internal.userPressed || checked) ? "pressed" : "normal"
width: parent.height
height: width
//internal: if there is no hover status, don't paint on mouse over in touchscreens
opacity: (internal.userPressed || checked || !flat || (roundShadow.hasOverState && mouse.containsMouse)) ? 1 : 0
Behavior on opacity {
PropertyAnimation { duration: 250 }
}
}
2011-10-13 11:19:39 +02:00
}
}
Item {
anchors {
fill: parent
leftMargin: delegate.margins.left
topMargin: delegate.margins.top
rightMargin: delegate.margins.right
bottomMargin: delegate.margins.bottom
2011-10-13 11:19:39 +02:00
}
2011-12-22 14:58:11 +01:00
Private.IconLoader {
2011-10-13 11:19:39 +02:00
id: icon
anchors {
2011-10-29 21:41:45 +02:00
verticalCenter: parent.verticalCenter
2011-10-13 11:19:39 +02:00
left: label.text ? parent.left : undefined
2011-10-29 21:41:45 +02:00
horizontalCenter: label.text ? undefined : parent.horizontalCenter
2011-10-13 11:19:39 +02:00
}
2012-01-03 21:16:25 +01:00
height: roundToStandardSize(parent.height)
width: height
2011-10-13 11:19:39 +02:00
}
Text {
id: label
2011-11-17 20:03:08 +01:00
//FIXME: why this is needed?
onPaintedWidthChanged: {
icon.anchors.horizontalCenter = label.paintedWidth > 0 ? undefined : icon.parent.horizontalCenter
icon.anchors.left = label.paintedWidth > 0 ? icon.parent.left : undefined
}
2012-10-18 17:22:34 +02:00
property int preferredWidth: button.width < button.implicitWidth ? paintedWidth: paintedWidth
2011-10-13 11:19:39 +02:00
anchors {
top: parent.top
bottom: parent.bottom
2011-10-29 21:41:45 +02:00
left: icon.valid ? icon.right : parent.left
leftMargin: icon.valid ? delegate.margins.left : 0
2011-10-13 11:19:39 +02:00
right: parent.right
}
font.capitalization: theme.defaultFont.capitalization
font.family: theme.defaultFont.family
font.italic: theme.defaultFont.italic
font.letterSpacing: theme.defaultFont.letterSpacing
font.pointSize: theme.defaultFont.pointSize
font.strikeout: theme.defaultFont.strikeout
font.underline: theme.defaultFont.underline
font.weight: theme.defaultFont.weight
font.wordSpacing: theme.defaultFont.wordSpacing
color: mouse.containsMouse ? theme.buttonTextColor : theme.textColor
Behavior on color { ColorAnimation { duration: 100 } }
2011-11-10 14:27:35 +01:00
horizontalAlignment: icon.valid ? Text.AlignLeft : Text.AlignHCenter
2011-10-13 11:19:39 +02:00
verticalAlignment: Text.AlignVCenter
}
}
MouseArea {
id: mouse
anchors.fill: parent
hoverEnabled: delegate.item.hasOverState
2011-10-13 11:19:39 +02:00
onPressed: internal.userPressed = true
onReleased: internal.userPressed = false
onCanceled: {
internal.userPressed = false
delegate.shadowState = "shadow"
}
onClicked: internal.clickButton()
2011-10-13 11:19:39 +02:00
onEntered: {
if (delegate.item.hasOverState && !flat && !internal.userPressed && !checked) {
delegate.shadowState = "hover"
2011-10-13 11:19:39 +02:00
}
button.z += 2
2011-10-13 11:19:39 +02:00
}
onExited: {
if (!flat) {
2011-10-13 11:19:39 +02:00
if (button.activeFocus) {
delegate.shadowState = "focus"
2011-10-13 11:19:39 +02:00
} else if (checked) {
delegate.shadowState = "hidden"
2011-10-13 11:19:39 +02:00
} else {
delegate.shadowState = "shadow"
2011-10-13 11:19:39 +02:00
}
}
button.z -= 2
2011-10-13 11:19:39 +02:00
}
}
}