plasma-framework/declarativeimports/plasmacomponents/Button.qml
Daker Fernandes Pinheiro c0bb0bb955 Add Button component with QtComponents compatible api
Signed-off-by: Daker Fernandes Pinheiro <dakerfp@gmail.com>
2011-04-26 19:52:47 -03:00

153 lines
3.9 KiB
QML

/*
* Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
*
* 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 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.
*/
import QtQuick 1.0
import org.kde.plasma.core 0.1 as PlasmaCore
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()
// Plasma API
property QtObject theme: PlasmaCore.Theme { }
width: 50
height: 20
PlasmaCore.FrameSvgItem {
id: hover
anchors.fill: parent
opacity: 0
anchors.leftMargin: -margins.left
anchors.topMargin: -margins.top
anchors.rightMargin: -margins.right
anchors.bottomMargin: -margins.bottom
imagePath: "widgets/button"
prefix: "hover"
Behavior on opacity {
PropertyAnimation { duration: 250 }
}
}
PlasmaCore.FrameSvgItem {
id: shadow
anchors {
fill: parent
leftMargin: -margins.left
topMargin: -margins.top
rightMargin: -margins.right
bottomMargin: -margins.bottom
}
imagePath: "widgets/button"
prefix: "shadow"
Behavior on opacity {
PropertyAnimation { duration: 250 }
}
}
PlasmaCore.FrameSvgItem {
id: surface
anchors.fill: parent
imagePath: "widgets/button"
prefix: "normal"
}
Item {
anchors {
fill: parent
leftMargin: surface.margins.left
topMargin: surface.margins.top
rightMargin: surface.margins.right
bottomMargin: surface.margins.bottom
}
Image {
id: icon
anchors {
fill: label.text ? undefined : parent
top: label.text ? parent.top : undefined
left: label.text ? parent.left : undefined
bottom: label.text ? parent.bottom : undefined
}
fillMode: Image.PreserveAspectFit
}
Text {
id: label
anchors {
top: parent.top
bottom: parent.bottom
left: icon.right
right: parent.right
}
color: theme.buttonTextColor
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
}
MouseArea {
id: mouse
anchors.fill: parent
hoverEnabled: true
onPressed: {
surface.prefix = "pressed";
}
onReleased: {
if (button.checkable)
button.checked = !button.checked;
// TODO: "checked" state must have special graphics?
if (button.checked)
surface.prefix = "pressed";
else
surface.prefix = "normal";
button.clicked();
}
onEntered: {
shadow.opacity = 0;
hover.opacity = 1;
}
onExited: {
shadow.opacity = 1;
hover.opacity = 0;
}
}
}