From 83bd875a44428c180bbfdddd66d14b99ec48b7e9 Mon Sep 17 00:00:00 2001 From: Daker Fernandes Pinheiro Date: Fri, 15 Jul 2011 21:21:06 -0300 Subject: [PATCH] Add focus and key events handling on Buttton plasma component - When a button is clicked it aqcuires the focus. - The space and return keys now can be used to press the button if it has the activeFocus Signed-off-by: Daker Fernandes Pinheiro --- .../plasmacomponents/Button.qml | 58 ++++++++++++++----- declarativeimports/test/gallery/Buttons.qml | 15 +++++ 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/declarativeimports/plasmacomponents/Button.qml b/declarativeimports/plasmacomponents/Button.qml index 3f2cc9921..066b02b8b 100644 --- a/declarativeimports/plasmacomponents/Button.qml +++ b/declarativeimports/plasmacomponents/Button.qml @@ -36,9 +36,45 @@ Item { // Plasma API property QtObject theme: PlasmaCore.Theme { } + function pressButton() { + surface.prefix = "pressed"; + } + + function releaseButton() { + 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(); + } + width: 50 height: 20 + Keys.onSpacePressed: pressButton(); + Keys.onReturnPressed: pressButton(); + Keys.onReleased: { + if (event.key == Qt.Key_Space || + event.key == Qt.Key_Return) + releaseButton(); + } + + onActiveFocusChanged: { + if (activeFocus) { + shadow.opacity = 0; + hover.opacity = 1; + }else { + shadow.opacity = 1; + hover.opacity = 0; + } + } + PlasmaCore.FrameSvgItem { id: hover @@ -125,27 +161,23 @@ Item { hoverEnabled: true onPressed: { - surface.prefix = "pressed"; + pressButton(); } 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(); + releaseButton(); } onEntered: { shadow.opacity = 0; hover.opacity = 1; } onExited: { - shadow.opacity = 1; - hover.opacity = 0; + if (button.activeFocus) { + shadow.opacity = 0; + hover.opacity = 1; + } else { + shadow.opacity = 1; + hover.opacity = 0; + } } } } diff --git a/declarativeimports/test/gallery/Buttons.qml b/declarativeimports/test/gallery/Buttons.qml index ae238bc4e..8685f1c4c 100644 --- a/declarativeimports/test/gallery/Buttons.qml +++ b/declarativeimports/test/gallery/Buttons.qml @@ -29,6 +29,7 @@ Column { } PlasmaComponents.Button { + id: bt1 width: 140 height: 30 text: "Button" @@ -36,9 +37,12 @@ Column { onClicked: { console.log("Clicked"); } + + Keys.onTabPressed: bt2.forceActiveFocus(); } PlasmaComponents.Button { + id: bt2 width: 140 height: 30 text: "Checkable Button" @@ -50,9 +54,12 @@ Column { else console.log("Button Unchecked"); } + + Keys.onTabPressed: bt3.forceActiveFocus(); } PlasmaComponents.Button { + id: bt3 width: 140 height: 30 text: "Different Font" @@ -60,18 +67,26 @@ Column { pixelSize: 20 family: "Helvetica" } + + Keys.onTabPressed: bt4.forceActiveFocus(); } PlasmaComponents.Button { + id: bt4 width: 140 height: 30 text: "Icon Button" iconSource: "/home/dakerfp/work/comics-reader/ui/images/random.png" + + Keys.onTabPressed: bt5.forceActiveFocus(); } PlasmaComponents.Button { + id: bt5 width: 140 height: 30 iconSource: "/home/dakerfp/work/comics-reader/ui/images/random.png" + + Keys.onTabPressed: bt1.forceActiveFocus(); } }