2011-10-13 11:19:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
|
2011-11-08 20:48:33 +01:00
|
|
|
* 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.
|
|
|
|
|
2012-06-08 12:49:12 +02:00
|
|
|
bool enabled:
|
2012-08-14 19:05:57 +02:00
|
|
|
Returns whether the button is currently enabled and receives user input.
|
2012-06-08 12:49:12 +02:00
|
|
|
|
2011-11-08 08:33:08 +01:00
|
|
|
Signals:
|
|
|
|
onClicked:
|
|
|
|
The signal is being emmited when the button is being clicked.
|
2012-10-14 19:44:36 +02:00
|
|
|
|
|
|
|
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
|
|
|
**/
|
|
|
|
|
2011-10-29 21:19:51 +02:00
|
|
|
import QtQuick 1.1
|
2011-10-13 11:19:39 +02:00
|
|
|
import org.kde.plasma.core 0.1 as PlasmaCore
|
2011-12-22 14:16:01 +01:00
|
|
|
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
|
|
|
|
2012-10-14 19:44:36 +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
|
2012-10-14 19:44:36 +02:00
|
|
|
|
2011-11-09 23:31:52 +01:00
|
|
|
implicitWidth: {
|
2012-10-14 19:44:36 +02:00
|
|
|
if (label.text.length == 0) {
|
|
|
|
height;
|
2011-11-09 23:31:52 +01:00
|
|
|
} else {
|
2012-10-14 19:44:36 +02:00
|
|
|
Math.max(theme.defaultFont.mSize.width*12, minimumWidth);
|
2011-11-09 23:31:52 +01:00
|
|
|
}
|
2011-10-13 11:19:39 +02:00
|
|
|
}
|
2012-10-14 19:44:36 +02:00
|
|
|
|
|
|
|
implicitHeight: Math.max(theme.defaultFont.mSize.height*1.6, minimumHeight)
|
2011-10-29 21:19:51 +02:00
|
|
|
|
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
|
|
|
|
|
2011-11-28 21:41:26 +01:00
|
|
|
Keys.onSpacePressed: internal.userPressed = true
|
|
|
|
Keys.onReturnPressed: internal.userPressed = true
|
2011-10-13 11:19:39 +02:00
|
|
|
Keys.onReleased: {
|
2011-11-28 21:41:26 +01:00
|
|
|
internal.userPressed = false
|
2011-10-13 11:19:39 +02:00
|
|
|
if (event.key == Qt.Key_Space ||
|
|
|
|
event.key == Qt.Key_Return)
|
2011-11-28 21:41:26 +01:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 19:27:44 +01:00
|
|
|
QtObject {
|
|
|
|
id: internal
|
|
|
|
property bool userPressed: false
|
|
|
|
|
2011-11-28 21:41:26 +01:00
|
|
|
function clickButton()
|
2011-11-08 19:27:44 +01:00
|
|
|
{
|
|
|
|
if (!button.enabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-28 13:49:28 +01:00
|
|
|
if (defaultAction && defaultAction.checkable) {
|
|
|
|
defaultAction.checked = !defaultAction.checked
|
|
|
|
} else if (button.checkable) {
|
2011-11-08 19:27:44 +01:00
|
|
|
button.checked = !button.checked
|
|
|
|
}
|
|
|
|
|
2012-04-25 12:36:00 +02:00
|
|
|
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();
|
|
|
|
}
|
2012-04-23 19:29:57 +02:00
|
|
|
button.clicked()
|
2011-11-28 13:49:28 +01:00
|
|
|
|
|
|
|
if (defaultAction) {
|
|
|
|
defaultAction.trigger()
|
|
|
|
}
|
2011-11-08 19:27:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 22:10:33 +02:00
|
|
|
Loader {
|
|
|
|
id: delegate
|
|
|
|
anchors.fill:parent
|
|
|
|
property QtObject margins: item.margins
|
|
|
|
property string shadowState: "shadow"
|
|
|
|
sourceComponent: {
|
2012-10-14 19:44:36 +02:00
|
|
|
if (label.text.length == 0 && button.width == button.height && button.parent.checkedButton === undefined && !flat) {
|
2012-09-10 22:10:33 +02:00
|
|
|
return roundButtonComponent
|
|
|
|
} else {
|
|
|
|
return buttonComponent
|
|
|
|
}
|
|
|
|
}
|
2011-10-13 11:19:39 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 22:10:33 +02:00
|
|
|
Component {
|
|
|
|
id: buttonComponent
|
|
|
|
Item {
|
2012-10-11 13:30:24 +02:00
|
|
|
parent: delegate
|
2012-09-10 22:10:33 +02:00
|
|
|
anchors.fill: parent
|
|
|
|
property alias margins: surface.margins
|
2012-10-14 20:04:54 +02:00
|
|
|
property alias hasOverState: shadow.hasOverState
|
2012-09-10 22:10:33 +02:00
|
|
|
Private.ButtonShadow {
|
|
|
|
id: shadow
|
|
|
|
anchors.fill: parent
|
2012-10-14 19:44:36 +02:00
|
|
|
visible: !flat && (surface.enabledBorders == "AllBorders" || state == "hover" || state == "focus")
|
2012-09-10 22:10:33 +02:00
|
|
|
state: delegate.shadowState
|
|
|
|
}
|
2011-10-13 11:19:39 +02:00
|
|
|
|
2012-09-10 22:10:33 +02:00
|
|
|
PlasmaCore.FrameSvgItem {
|
|
|
|
id: surface
|
|
|
|
|
2012-10-14 19:44:36 +02:00
|
|
|
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("|")
|
|
|
|
}
|
|
|
|
|
2012-09-10 22:10:33 +02:00
|
|
|
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
|
2012-09-10 22:10:33 +02:00
|
|
|
Behavior on opacity {
|
|
|
|
PropertyAnimation { duration: 250 }
|
|
|
|
}
|
2012-10-14 19:44:36 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-10 22:10:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Component {
|
|
|
|
id: roundButtonComponent
|
|
|
|
Item {
|
2012-10-11 13:30:24 +02:00
|
|
|
id: roundButtonDelegate
|
|
|
|
parent: delegate
|
2012-09-10 22:10:33 +02:00
|
|
|
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
|
2012-09-10 22:10:33 +02:00
|
|
|
}
|
2012-10-14 20:04:54 +02:00
|
|
|
property alias hasOverState: roundShadow.hasOverState
|
2012-09-10 22:10:33 +02:00
|
|
|
Private.RoundShadow {
|
2012-10-10 18:35:06 +02:00
|
|
|
id: roundShadow
|
|
|
|
visible: !flat
|
2012-09-10 22:10:33 +02:00
|
|
|
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
|
2012-10-10 18:35:06 +02:00
|
|
|
//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 }
|
|
|
|
}
|
2012-09-10 22:10:33 +02:00
|
|
|
}
|
2011-10-13 11:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Item {
|
|
|
|
anchors {
|
|
|
|
fill: parent
|
2012-09-10 22:10:33 +02:00
|
|
|
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
|
2012-09-10 22:10:33 +02:00
|
|
|
leftMargin: icon.valid ? delegate.margins.left : 0
|
2011-10-13 11:19:39 +02:00
|
|
|
right: parent.right
|
|
|
|
}
|
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
|
2012-06-28 19:40:33 +02:00
|
|
|
|
|
|
|
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
|
2012-10-14 20:04:54 +02:00
|
|
|
hoverEnabled: delegate.item.hasOverState
|
2011-10-13 11:19:39 +02:00
|
|
|
|
2011-11-28 21:41:26 +01:00
|
|
|
onPressed: internal.userPressed = true
|
|
|
|
onReleased: internal.userPressed = false
|
2012-09-10 22:10:33 +02:00
|
|
|
onCanceled: {
|
|
|
|
internal.userPressed = false
|
|
|
|
delegate.shadowState = "shadow"
|
|
|
|
}
|
2011-11-28 21:41:26 +01:00
|
|
|
onClicked: internal.clickButton()
|
2011-11-08 19:27:44 +01:00
|
|
|
|
2011-10-13 11:19:39 +02:00
|
|
|
onEntered: {
|
2012-10-14 20:04:54 +02:00
|
|
|
if (delegate.item.hasOverState && !flat && !internal.userPressed && !checked) {
|
2012-09-10 22:10:33 +02:00
|
|
|
delegate.shadowState = "hover"
|
2011-10-13 11:19:39 +02:00
|
|
|
}
|
2012-10-14 19:44:36 +02:00
|
|
|
button.z += 2
|
2011-10-13 11:19:39 +02:00
|
|
|
}
|
|
|
|
onExited: {
|
2011-11-08 19:27:44 +01:00
|
|
|
if (!flat) {
|
2011-10-13 11:19:39 +02:00
|
|
|
if (button.activeFocus) {
|
2012-09-10 22:10:33 +02:00
|
|
|
delegate.shadowState = "focus"
|
2011-10-13 11:19:39 +02:00
|
|
|
} else if (checked) {
|
2012-09-10 22:10:33 +02:00
|
|
|
delegate.shadowState = "hidden"
|
2011-10-13 11:19:39 +02:00
|
|
|
} else {
|
2012-09-10 22:10:33 +02:00
|
|
|
delegate.shadowState = "shadow"
|
2011-10-13 11:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-14 19:44:36 +02:00
|
|
|
button.z -= 2
|
2011-10-13 11:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|