/components/2.0/ImageButton.qml

https://github.com/davidedmundson/sddm · QML · 96 lines · 56 code · 16 blank · 24 comment · 9 complexity · 9d741bf602af504d9072c5ba9414ff1a MD5 · raw file

  1. /***************************************************************************
  2. * Copyright (c) 2013 Reza Fatahilah Shah <rshah0385@kireihana.com>
  3. * Copyright (c) 2013 Abdurrahman AVCI <abdurrahmanavci@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without restriction,
  8. * including without limitation the rights to use, copy, modify, merge,
  9. * publish, distribute, sublicense, and/or sell copies of the Software,
  10. * and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  22. * OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. ***************************************************************************/
  25. import QtQuick 2.0
  26. Image {
  27. id: container
  28. opacity: 0.6
  29. property bool enabled: true
  30. property bool spaceDown: false
  31. property bool isFocused: activeFocus || mouseArea.containsMouse
  32. property bool isPressed: spaceDown || mouseArea.pressed
  33. signal pressed()
  34. signal released()
  35. signal clicked()
  36. states: [
  37. State {
  38. name: "disabled"; when: (container.enabled === false)
  39. },
  40. State {
  41. name: "active"; when: container.enabled && container.isFocused && !container.isPressed
  42. PropertyChanges { target: container; opacity: 1.0 }
  43. },
  44. State {
  45. name: "pressed"; when: container.enabled && container.isPressed
  46. }
  47. ]
  48. Behavior on opacity { NumberAnimation { duration: 200 } }
  49. clip: true
  50. smooth: true
  51. fillMode: Image.PreserveAspectFit
  52. MouseArea {
  53. id: mouseArea
  54. anchors.fill: parent
  55. cursorShape: Qt.PointingHandCursor
  56. hoverEnabled: true
  57. acceptedButtons: Qt.LeftButton
  58. onPressed: { container.focus = true; container.pressed() }
  59. onClicked: { container.focus = true; container.clicked() }
  60. onReleased: { container.focus = true; container.released() }
  61. }
  62. Keys.onPressed: {
  63. if (event.key === Qt.Key_Space) {
  64. container.spaceDown = true;
  65. container.pressed()
  66. event.accepted = true
  67. } else if (event.key === Qt.Key_Return) {
  68. container.clicked()
  69. event.accepted = true
  70. }
  71. }
  72. Keys.onReleased: {
  73. if (event.key === Qt.Key_Space) {
  74. container.spaceDown = false;
  75. container.released()
  76. container.clicked()
  77. event.accepted = true
  78. }
  79. }
  80. }