2011-04-26 23:35:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
|
2011-11-01 23:51:42 +01:00
|
|
|
* Copyright (C) 2011 by Mark Gaiser <markg85@gmail.com>
|
2011-11-08 20:48:33 +01:00
|
|
|
* Copyright (C) 2011 by Marco Martin <mart@kde.org>
|
2011-04-26 23:35:42 +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
|
2011-10-13 11:19:39 +02:00
|
|
|
* GNU Library General Public License for more details
|
2011-04-26 23:35:42 +02:00
|
|
|
*
|
|
|
|
* 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-16 18:53:25 +01:00
|
|
|
|
2011-12-22 11:53:15 +01:00
|
|
|
/**Documented API
|
2011-10-31 05:35:35 +01:00
|
|
|
Inherits:
|
|
|
|
Item
|
|
|
|
|
|
|
|
Imports:
|
|
|
|
org.kde.plasma.core
|
|
|
|
QtQuick 1.0
|
|
|
|
|
|
|
|
Description:
|
2011-11-26 18:16:21 +01:00
|
|
|
A simple button, with optional label and icon which uses the plasma theme.
|
|
|
|
This button component can also be used as a checkable button by using the checkable
|
|
|
|
and checked properties for that.
|
2012-06-15 00:26:51 +02:00
|
|
|
Plasma theme is the theme which changes via the systemsetting-workspace appearance
|
2011-10-31 05:35:35 +01:00
|
|
|
-desktop theme.
|
|
|
|
|
|
|
|
Properties:
|
2011-12-19 14:44:42 +01:00
|
|
|
* bool checked:
|
2012-06-15 00:26:51 +02:00
|
|
|
This property holds whether this button is checked or not.
|
2011-11-26 18:16:21 +01:00
|
|
|
The button must be in the checkable state for enable users check or uncheck it.
|
|
|
|
The default value is false.
|
|
|
|
See also checkable property.
|
2011-10-31 05:35:35 +01:00
|
|
|
|
2011-12-19 14:44:42 +01:00
|
|
|
* bool checkable:
|
2011-11-26 18:16:21 +01:00
|
|
|
This property holds if the button is acting like a checkable button or not.
|
|
|
|
The default value is false.
|
2011-10-31 05:35:35 +01:00
|
|
|
|
2011-12-19 14:44:42 +01:00
|
|
|
* bool pressed:
|
2011-11-26 18:16:21 +01:00
|
|
|
This property holds if the button is pressed or not.
|
|
|
|
Read-only.
|
2011-10-31 05:35:35 +01:00
|
|
|
|
2011-12-19 14:44:42 +01:00
|
|
|
* string text:
|
2011-11-26 18:16:21 +01:00
|
|
|
This property holds the text label for the button.
|
|
|
|
For example,the ok button has text 'ok'.
|
|
|
|
The default value for this property is an empty string.
|
2011-10-31 05:35:35 +01:00
|
|
|
|
2011-12-19 14:44:42 +01:00
|
|
|
* url iconSource:
|
2011-11-26 18:16:21 +01:00
|
|
|
This property holds the source url for the Button's icon.
|
2011-12-28 18:02:44 +01:00
|
|
|
The default value is an empty url, which displays no icon.
|
|
|
|
It can be any image from any protocol supported by the Image element, or a freedesktop-compatible icon name
|
2011-10-31 05:35:35 +01:00
|
|
|
|
2011-12-19 14:44:42 +01:00
|
|
|
* font font:
|
2011-11-26 18:16:21 +01:00
|
|
|
This property holds the font used by the button label.
|
|
|
|
See also Qt documentation for font type.
|
2011-10-31 05:35:35 +01:00
|
|
|
|
|
|
|
Signals:
|
2012-01-10 21:39:18 +01:00
|
|
|
* clicked():
|
2011-11-26 18:16:21 +01:00
|
|
|
This handler is called when there is a click.
|
2011-10-31 05:35:35 +01:00
|
|
|
**/
|
2011-12-16 18:53:25 +01:00
|
|
|
|
2011-11-17 20:03:08 +01:00
|
|
|
import QtQuick 1.1
|
2011-12-16 18:53:25 +01:00
|
|
|
|
2011-04-26 23:35:42 +02:00
|
|
|
import org.kde.plasma.core 0.1 as PlasmaCore
|
2011-12-22 14:16:01 +01:00
|
|
|
import "private" as Private
|
2011-04-26 23:35:42 +02:00
|
|
|
|
|
|
|
Item {
|
|
|
|
id: button
|
|
|
|
|
|
|
|
// Commmon API
|
|
|
|
property bool checked: false
|
|
|
|
property bool checkable: false
|
|
|
|
property alias pressed: mouse.pressed
|
|
|
|
property alias text: label.text
|
|
|
|
property alias iconSource: icon.source
|
|
|
|
property alias font: label.font
|
|
|
|
|
|
|
|
signal clicked()
|
|
|
|
|
2011-11-17 20:03:08 +01:00
|
|
|
implicitWidth: {
|
2011-11-10 14:29:39 +01:00
|
|
|
if (label.paintedWidth == 0) {
|
|
|
|
return height
|
|
|
|
} else {
|
2011-11-17 20:03:08 +01:00
|
|
|
//return Math.max(theme.defaultFont.mSize.width*12, label.paintedWidth)
|
2011-11-10 14:29:39 +01:00
|
|
|
return Math.max(theme.defaultFont.mSize.width*12, icon.width + label.paintedWidth + surfaceNormal.margins.left + surfaceNormal.margins.right) + ((icon.valid) ? surfaceNormal.margins.left : 0)
|
|
|
|
}
|
2011-07-16 02:21:06 +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) + surfaceNormal.margins.top/2 + surfaceNormal.margins.bottom/2)
|
2011-07-16 02:21:06 +02:00
|
|
|
|
2011-11-01 23:51:42 +01:00
|
|
|
// TODO: needs to define if there will be specific graphics for
|
|
|
|
// disabled buttons
|
|
|
|
opacity: enabled ? 1.0 : 0.5
|
2011-07-18 23:20:49 +02:00
|
|
|
|
2011-11-08 19:27:44 +01:00
|
|
|
QtObject {
|
|
|
|
id: internal
|
|
|
|
property bool userPressed: false
|
2011-07-16 02:21:06 +02:00
|
|
|
|
2011-11-08 20:34:39 +01:00
|
|
|
function belongsToButtonGroup()
|
|
|
|
{
|
|
|
|
return button.parent
|
|
|
|
&& button.parent.hasOwnProperty("checkedButton")
|
|
|
|
&& button.parent.exclusive
|
|
|
|
}
|
2011-07-16 02:21:06 +02:00
|
|
|
|
2011-11-28 21:41:26 +01:00
|
|
|
function clickButton()
|
2011-11-08 19:27:44 +01:00
|
|
|
{
|
|
|
|
userPressed = false
|
|
|
|
if (!button.enabled) {
|
|
|
|
return
|
2011-11-08 18:19:25 +01:00
|
|
|
}
|
2011-07-18 23:20:49 +02:00
|
|
|
|
2011-11-08 20:34:39 +01:00
|
|
|
if ((!belongsToButtonGroup() || !button.checked) && button.checkable) {
|
2011-11-08 19:27:44 +01:00
|
|
|
button.checked = !button.checked
|
2011-11-01 23:51:42 +01:00
|
|
|
}
|
2011-04-26 23:35:42 +02:00
|
|
|
|
2011-11-08 19:27:44 +01:00
|
|
|
button.forceActiveFocus()
|
2012-04-23 19:29:57 +02:00
|
|
|
button.clicked()
|
2011-11-01 23:51:42 +01:00
|
|
|
}
|
2011-07-16 02:21:06 +02:00
|
|
|
}
|
|
|
|
|
2011-11-28 21:41:26 +01:00
|
|
|
Keys.onSpacePressed: internal.userPressed = true
|
|
|
|
Keys.onReturnPressed: internal.userPressed = true
|
2011-07-16 02:21:06 +02:00
|
|
|
Keys.onReleased: {
|
2011-11-28 21:41:26 +01:00
|
|
|
internal.userPressed = false
|
2011-07-16 02:21:06 +02:00
|
|
|
if (event.key == Qt.Key_Space ||
|
|
|
|
event.key == Qt.Key_Return)
|
2011-11-28 21:41:26 +01:00
|
|
|
internal.clickButton();
|
2011-07-16 02:21:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:58:11 +01:00
|
|
|
Private.ButtonShadow {
|
2011-04-26 23:35:42 +02:00
|
|
|
id: shadow
|
2011-10-10 15:26:46 +02:00
|
|
|
anchors.fill: parent
|
2011-11-08 20:34:39 +01:00
|
|
|
state: {
|
2011-11-09 17:49:10 +01:00
|
|
|
if (internal.userPressed || checked) {
|
2011-11-08 20:34:39 +01:00
|
|
|
return "hidden"
|
|
|
|
} else if (mouse.containsMouse) {
|
|
|
|
return "hover"
|
|
|
|
} else if (button.activeFocus) {
|
|
|
|
return "focus"
|
|
|
|
} else {
|
|
|
|
return "shadow"
|
|
|
|
}
|
|
|
|
}
|
2011-04-26 23:35:42 +02:00
|
|
|
}
|
|
|
|
|
2011-11-01 23:51:42 +01:00
|
|
|
// The normal button state
|
2011-04-26 23:35:42 +02:00
|
|
|
PlasmaCore.FrameSvgItem {
|
2011-11-01 23:51:42 +01:00
|
|
|
id: surfaceNormal
|
2011-04-26 23:35:42 +02:00
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
imagePath: "widgets/button"
|
|
|
|
prefix: "normal"
|
|
|
|
}
|
|
|
|
|
2011-11-01 23:51:42 +01:00
|
|
|
// The pressed state
|
|
|
|
PlasmaCore.FrameSvgItem {
|
|
|
|
id: surfacePressed
|
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
imagePath: "widgets/button"
|
|
|
|
prefix: "pressed"
|
|
|
|
opacity: 0
|
|
|
|
}
|
|
|
|
|
2011-04-26 23:35:42 +02:00
|
|
|
Item {
|
2011-11-01 23:51:42 +01:00
|
|
|
id: buttonContent
|
2011-11-08 19:27:44 +01:00
|
|
|
state: (internal.userPressed || checked) ? "pressed" : "normal"
|
2011-11-01 23:51:42 +01:00
|
|
|
|
|
|
|
states: [
|
|
|
|
State { name: "normal" },
|
|
|
|
State { name: "pressed" }
|
|
|
|
]
|
|
|
|
transitions: [
|
|
|
|
Transition {
|
|
|
|
to: "normal"
|
|
|
|
// Cross fade from pressed to normal
|
|
|
|
ParallelAnimation {
|
|
|
|
NumberAnimation { target: surfaceNormal; property: "opacity"; to: 1; duration: 100 }
|
|
|
|
NumberAnimation { target: surfacePressed; property: "opacity"; to: 0; duration: 100 }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Transition {
|
|
|
|
to: "pressed"
|
|
|
|
// Cross fade from normal to pressed
|
|
|
|
ParallelAnimation {
|
|
|
|
NumberAnimation { target: surfaceNormal; property: "opacity"; to: 0; duration: 100 }
|
|
|
|
NumberAnimation { target: surfacePressed; property: "opacity"; to: 1; duration: 100 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2011-04-26 23:35:42 +02:00
|
|
|
anchors {
|
|
|
|
fill: parent
|
2011-11-01 23:51:42 +01:00
|
|
|
leftMargin: surfaceNormal.margins.left
|
|
|
|
topMargin: surfaceNormal.margins.top
|
|
|
|
rightMargin: surfaceNormal.margins.right
|
|
|
|
bottomMargin: surfaceNormal.margins.bottom
|
2011-04-26 23:35:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:58:11 +01:00
|
|
|
Private.IconLoader {
|
2011-04-26 23:35:42 +02:00
|
|
|
id: icon
|
|
|
|
|
|
|
|
anchors {
|
2011-10-29 21:41:45 +02:00
|
|
|
verticalCenter: parent.verticalCenter
|
2011-11-17 20:03:08 +01:00
|
|
|
left: label.paintedWidth > 0 ? parent.left : undefined
|
|
|
|
horizontalCenter: label.paintedWidth > 0 ? undefined : parent.horizontalCenter
|
2011-04-26 23:35:42 +02:00
|
|
|
}
|
2012-01-03 21:16:25 +01:00
|
|
|
height: roundToStandardSize(parent.height)
|
|
|
|
width: height
|
2011-04-26 23:35:42 +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-04-26 23:35:42 +02:00
|
|
|
anchors {
|
|
|
|
top: parent.top
|
|
|
|
bottom: parent.bottom
|
|
|
|
right: parent.right
|
2011-11-01 23:51:42 +01:00
|
|
|
left: icon.valid ? icon.right : parent.left
|
|
|
|
leftMargin: icon.valid ? parent.anchors.leftMargin : 0
|
2011-04-26 23:35:42 +02:00
|
|
|
}
|
2011-11-01 23:51:42 +01:00
|
|
|
|
2011-10-13 18:23:40 +02:00
|
|
|
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-04-26 23:35:42 +02:00
|
|
|
color: theme.buttonTextColor
|
2012-02-20 20:57:19 +01:00
|
|
|
horizontalAlignment: icon.valid ? Text.AlignLeft : Text.AlignHCenter
|
2011-04-26 23:35:42 +02:00
|
|
|
verticalAlignment: Text.AlignVCenter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
id: mouse
|
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
hoverEnabled: true
|
2011-11-28 21:41:26 +01:00
|
|
|
onPressed: internal.userPressed = true
|
|
|
|
onReleased: internal.userPressed = false
|
|
|
|
onCanceled: internal.userPressed = false
|
|
|
|
onClicked: internal.clickButton()
|
2011-04-26 23:35:42 +02:00
|
|
|
}
|
|
|
|
}
|