better logic for button pressed handling

This commit is contained in:
Marco Martin 2011-11-08 19:27:44 +01:00
parent 1d4f9a5c77
commit 82bbcc139a
2 changed files with 58 additions and 61 deletions

View File

@ -41,39 +41,37 @@ Item {
// disabled buttons
opacity: enabled ? 1.0 : 0.5
function pressButton()
{
if (button.enabled) {
buttonContent.state = "pressed"
shadow.state = "hidden"
}
}
QtObject {
id: internal
property bool userPressed: false
function releaseButton()
{
if (button.enabled) {
buttonContent.state = "normal"
if (!checked) {
shadow.state = "shadow"
function pressButton()
{
userPressed = true
}
function releaseButton()
{
userPressed = false
if (!button.enabled) {
return
}
if (button.checkable) {
button.checked = !button.checked;
button.checked = !button.checked
}
// TODO: "checked" state must have special graphics?
button.clicked();
button.forceActiveFocus();
button.clicked()
button.forceActiveFocus()
}
}
Keys.onSpacePressed: pressButton()
Keys.onReturnPressed: pressButton()
Keys.onSpacePressed: internal.pressButton()
Keys.onReturnPressed: internal.pressButton()
Keys.onReleased: {
if (event.key == Qt.Key_Space ||
event.key == Qt.Key_Return)
releaseButton();
internal.releaseButton();
}
onActiveFocusChanged: {
@ -112,6 +110,7 @@ Item {
Item {
id: buttonContent
state: (internal.userPressed || checked) ? "pressed" : "normal"
states: [
State { name: "normal" },
@ -185,8 +184,8 @@ Item {
anchors.fill: parent
hoverEnabled: true
onPressed: pressButton()
onReleased: releaseButton()
onPressed: internal.pressButton()
onReleased: internal.releaseButton()
onEntered: {
shadow.state = "hover"
}

View File

@ -39,27 +39,6 @@ Item {
surface.opacity = 1
}
function pressButton() {
if (button.enabled)
surface.prefix = "pressed";
}
function releaseButton() {
if (!button.enabled)
return;
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();
button.forceActiveFocus();
}
implicitWidth: Math.max(50, icon.width + label.paintedWidth + surface.margins.left + surface.margins.right)
implicitHeight: Math.max(20, Math.max(icon.height, label.paintedHeight) + surface.margins.top + surface.margins.bottom)
@ -68,12 +47,12 @@ Item {
// disabled buttons
opacity: enabled ? 1.0 : 0.5
Keys.onSpacePressed: pressButton();
Keys.onReturnPressed: pressButton();
Keys.onSpacePressed: internal.pressButton()
Keys.onReturnPressed: internal.pressButton()
Keys.onReleased: {
if (event.key == Qt.Key_Space ||
event.key == Qt.Key_Return)
releaseButton();
internal.releaseButton()
}
onActiveFocusChanged: {
@ -86,6 +65,31 @@ Item {
}
}
QtObject {
id: internal
property bool userPressed: false
function pressButton()
{
userPressed = true
}
function releaseButton()
{
userPressed = false
if (!button.enabled) {
return
}
if (button.checkable) {
button.checked = !button.checked
}
button.clicked()
button.forceActiveFocus()
}
}
ButtonShadow {
id: shadow
anchors.fill: parent
@ -97,8 +101,8 @@ Item {
anchors.fill: parent
imagePath: "widgets/button"
prefix: "normal"
opacity: 0
prefix: (internal.userPressed || checked) ? "pressed" : "normal"
opacity: (internal.userPressed || checked || !flat || mouse.containsMouse) ? 1 : 0
Behavior on opacity {
PropertyAnimation { duration: 250 }
}
@ -153,23 +157,17 @@ Item {
anchors.fill: parent
hoverEnabled: true
onPressed: {
pressButton();
}
onReleased: {
releaseButton();
}
onPressed: internal.pressButton();
onReleased: internal.releaseButton();
onEntered: {
if (flat) {
surface.opacity = 1
} else {
if (!flat) {
shadow.state = "hover"
}
}
onExited: {
if (flat) {
surface.opacity = 0
} else {
if (!flat) {
if (button.activeFocus) {
shadow.state = "focus"
} else if (checked) {