2011-07-08 20:11:55 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2011-10-10 13:04:22 +02:00
|
|
|
* GNU Library General Public License for more details
|
2011-07-08 20:11:55 +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.
|
|
|
|
*/
|
|
|
|
|
2013-02-06 13:37:56 +01:00
|
|
|
import QtQuick 2.0
|
2011-07-08 20:11:55 +02:00
|
|
|
import org.kde.plasma.core 0.1 as PlasmaCore
|
|
|
|
|
2012-12-14 17:09:16 +01:00
|
|
|
/**
|
|
|
|
* Private base component for several public components. Any element documented here
|
|
|
|
* can be used in components inheriting from DualStateButton.
|
|
|
|
*/
|
2011-07-08 20:11:55 +02:00
|
|
|
Item {
|
|
|
|
id: dualButton
|
|
|
|
|
|
|
|
// Common API
|
2012-12-14 17:09:16 +01:00
|
|
|
/**
|
|
|
|
* If the button is checked, its checked property is true; otherwise false. The property is false by default.
|
|
|
|
*/
|
2011-07-08 20:11:55 +02:00
|
|
|
property bool checked
|
2012-12-14 17:09:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:bool
|
|
|
|
* If the button is pressed, its pressed property is true.
|
|
|
|
* @see clicked
|
|
|
|
*/
|
2011-07-08 20:11:55 +02:00
|
|
|
property alias pressed: mouseArea.pressed
|
|
|
|
|
2012-12-14 17:09:16 +01:00
|
|
|
/**
|
|
|
|
* Emitted when the user clicked a mouse button over the button (or
|
|
|
|
* tapped on the touch screen)
|
|
|
|
*/
|
2011-07-08 20:11:55 +02:00
|
|
|
signal clicked()
|
|
|
|
|
|
|
|
// Plasma API
|
2012-12-14 17:09:16 +01:00
|
|
|
/**
|
|
|
|
* The text is shown beside the button. By default text is an empty string.
|
|
|
|
*/
|
2011-07-08 20:11:55 +02:00
|
|
|
property alias text: label.text // TODO: Not yet part of the common API
|
2011-10-10 13:04:22 +02:00
|
|
|
property alias view: surfaceLoader.sourceComponent
|
|
|
|
property alias shadow: shadowLoader.sourceComponent
|
2011-07-08 20:11:55 +02:00
|
|
|
|
2011-10-10 13:04:22 +02:00
|
|
|
width: surfaceLoader.width + label.paintedWidth
|
2011-11-17 20:14:47 +01:00
|
|
|
height: theme.defaultFont.mSize.height*1.6
|
2011-07-18 23:49:45 +02:00
|
|
|
// TODO: needs to define if there will be specific graphics for
|
|
|
|
// disabled buttons
|
|
|
|
opacity: dualButton.enabled ? 1.0 : 0.5
|
2011-07-08 20:11:55 +02:00
|
|
|
|
|
|
|
function released() {
|
2011-07-18 23:41:07 +02:00
|
|
|
if (dualButton.enabled) {
|
|
|
|
dualButton.checked = !dualButton.checked;
|
|
|
|
dualButton.clicked();
|
|
|
|
}
|
2011-07-08 20:11:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Keys.onReleased: {
|
|
|
|
if(event.key == Qt.Key_Space ||
|
|
|
|
event.key == Qt.Key_Return)
|
|
|
|
released();
|
|
|
|
}
|
|
|
|
|
2011-10-10 13:04:22 +02:00
|
|
|
Loader {
|
|
|
|
id: shadowLoader
|
|
|
|
anchors.fill: surfaceLoader
|
2012-06-20 20:09:34 +02:00
|
|
|
state: (dualButton.enabled && (dualButton.focus || mouseArea.containsMouse)) ? "hover" : "shadow"
|
2011-07-08 20:11:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Loader {
|
2011-10-10 13:04:22 +02:00
|
|
|
id: surfaceLoader
|
2011-07-08 20:11:55 +02:00
|
|
|
|
|
|
|
anchors {
|
|
|
|
verticalCenter: parent.verticalCenter
|
|
|
|
left: text ? parent.left : undefined
|
|
|
|
horizontalCenter: text ? undefined : parent.horizontalCenter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Text {
|
|
|
|
id: label
|
|
|
|
|
|
|
|
text: dualButton.text
|
|
|
|
anchors {
|
|
|
|
top: parent.top
|
|
|
|
bottom: parent.bottom
|
2011-10-10 13:04:22 +02:00
|
|
|
left: surfaceLoader.right
|
2011-07-08 20:11:55 +02:00
|
|
|
right: parent.right
|
2011-10-10 13:39:16 +02:00
|
|
|
//FIXME: see how this margin will be set
|
|
|
|
leftMargin: height/4
|
2011-07-08 20:11:55 +02:00
|
|
|
}
|
|
|
|
color: theme.textColor
|
|
|
|
verticalAlignment: Text.AlignVCenter
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
id: mouseArea
|
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
hoverEnabled: true
|
|
|
|
|
|
|
|
onReleased: dualButton.released();
|
|
|
|
onPressed: dualButton.forceActiveFocus();
|
|
|
|
}
|
|
|
|
}
|