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

249 lines
7.3 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.
Signals:
onClicked:
The signal is being emmited when the button is being clicked.
**/
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
enabled: defaultAction==undefined||defaultAction.enabled
2011-10-13 11:19:39 +02:00
onFlatChanged: {
surface.opacity = 1
}
2011-11-09 23:31:52 +01:00
implicitWidth: {
if (label.paintedWidth == 0) {
return implicitHeight
} else {
return Math.max(theme.defaultFont.mSize.width*12, icon.width + label.paintedWidth + surface.margins.left + surface.margins.right) + ((icon.valid) ? surface.margins.left : 0)
}
2011-10-13 11:19:39 +02:00
}
2011-11-17 20:14:47 +01:00
implicitHeight: Math.max(theme.defaultFont.mSize.height*1.6, Math.max(icon.height, label.paintedHeight) + surface.margins.top/2 + surface.margins.bottom/2)
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
}
button.clicked()
button.forceActiveFocus()
2011-11-28 13:49:28 +01:00
if (defaultAction) {
defaultAction.trigger()
}
}
}
2011-12-22 14:58:11 +01:00
Private.ButtonShadow {
2011-10-13 11:19:39 +02:00
id: shadow
anchors.fill: parent
visible: !flat
}
PlasmaCore.FrameSvgItem {
id: surface
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
opacity: (internal.userPressed || checked || !flat || (shadow.hasOverState && mouse.containsMouse)) ? 1 : 0
2011-10-13 11:19:39 +02:00
Behavior on opacity {
2011-11-28 13:49:28 +01:00
PropertyAnimation { duration: 100 }
2011-10-13 11:19:39 +02:00
}
}
Item {
anchors {
fill: parent
leftMargin: surface.margins.left
topMargin: surface.margins.top
rightMargin: surface.margins.right
bottomMargin: surface.margins.bottom
}
2011-11-28 13:49:28 +01:00
scale: internal.userPressed ? 0.9 : 1
Behavior on scale {
PropertyAnimation { duration: 250 }
}
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
}
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 ? parent.anchors.leftMargin : 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
2011-10-13 11:19:39 +02:00
color: theme.buttonTextColor
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: true
onPressed: internal.userPressed = true
onReleased: internal.userPressed = false
onClicked: internal.clickButton()
2011-10-13 11:19:39 +02:00
onEntered: {
if (!flat) {
2011-10-13 11:19:39 +02:00
shadow.state = "hover"
}
}
onExited: {
if (!flat) {
2011-10-13 11:19:39 +02:00
if (button.activeFocus) {
shadow.state = "focus"
} else if (checked) {
shadow.state = "hidden"
} else {
shadow.state = "shadow"
}
}
}
}
}