|
| 1 | +//Import the declarative plugins |
| 2 | +import QtQuick 2.0 |
| 3 | +import QtQuick.Layouts 1.3 |
| 4 | +import QtQuick.Controls 2.5 |
| 5 | +//Implementation of the Button control. |
| 6 | +Item { |
| 7 | + id: button |
| 8 | + width:innerText.width + 40 |
| 9 | + height: 30 |
| 10 | + property alias buttonText: innerText.text; |
| 11 | + property alias innerText: innerText |
| 12 | + property alias buttonText2: innerText2.text; |
| 13 | + property alias innerText2: innerText2 |
| 14 | + property color color: "transparent" |
| 15 | + property color hoverColor: "#aaaaaa" |
| 16 | + property color pressColor: "slategray" |
| 17 | + property color borderColor: "white" |
| 18 | + property string sourceIcon: "" |
| 19 | + property int fontSize: 16 |
| 20 | + property int borderWidth: 1 |
| 21 | + property int borderRadius: 2 |
| 22 | + property int iconWidth: 20 |
| 23 | + property int iconHeight: 20 |
| 24 | + scale: state === "Pressed" ? 0.95 : 1.0 |
| 25 | + onEnabledChanged: state = "" |
| 26 | + property bool iconButton: false |
| 27 | + signal clicked |
| 28 | + |
| 29 | + //define a scale animation |
| 30 | + Behavior on scale { |
| 31 | + NumberAnimation { |
| 32 | + duration: 100 |
| 33 | + easing.type: Easing.InOutQuad |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + //Rectangle to draw the button |
| 38 | + Rectangle { |
| 39 | + id: rectangleButton |
| 40 | + anchors.fill: parent |
| 41 | + radius: borderRadius |
| 42 | + color: button.enabled ? button.color : "grey" |
| 43 | + border.width: borderWidth |
| 44 | + border.color:borderColor |
| 45 | + visible: !iconButton |
| 46 | + opacity: 0.8 |
| 47 | + Image{ |
| 48 | + visible: sourceIcon |
| 49 | + source: sourceIcon |
| 50 | + sourceSize: Qt.size(iconWidth,iconHeight) |
| 51 | + anchors.centerIn: parent |
| 52 | + clip: true |
| 53 | + smooth: true |
| 54 | + opacity: 0.54 |
| 55 | + } |
| 56 | + |
| 57 | + Label { |
| 58 | + id: innerText |
| 59 | + font.pointSize: fontSize |
| 60 | + anchors.centerIn: parent |
| 61 | + visible: text |
| 62 | + } |
| 63 | + } |
| 64 | + Rectangle { |
| 65 | + id: rectangleButton2 |
| 66 | + anchors.fill: parent |
| 67 | + radius: borderRadius |
| 68 | + color: button.enabled ? button.color : "grey" |
| 69 | + border.width: borderWidth |
| 70 | + border.color:borderColor |
| 71 | + visible: iconButton |
| 72 | + ColumnLayout{ |
| 73 | + Layout.fillWidth: true |
| 74 | + Layout.fillHeight: true |
| 75 | + anchors.centerIn: parent |
| 76 | + Image{ |
| 77 | + source:sourceIcon |
| 78 | + sourceSize: Qt.size(iconWidth,iconHeight) |
| 79 | + clip: true |
| 80 | + smooth: true |
| 81 | + opacity: 0.54 |
| 82 | + Layout.alignment: Qt.AlignHCenter |
| 83 | + } |
| 84 | + |
| 85 | + Text { |
| 86 | + id: innerText2 |
| 87 | + font.pointSize: fontSize |
| 88 | + visible: text |
| 89 | + Layout.alignment: Qt.AlignHCenter |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + //change the color of the button in differen button states |
| 95 | + states: [ |
| 96 | + State { |
| 97 | + name: "Hovering" |
| 98 | + PropertyChanges { |
| 99 | + target:iconButton ? rectangleButton2 : rectangleButton |
| 100 | + color: hoverColor |
| 101 | + } |
| 102 | + }, |
| 103 | + State { |
| 104 | + name: "Pressed" |
| 105 | + PropertyChanges { |
| 106 | + target: iconButton ? rectangleButton2 : rectangleButton |
| 107 | + color: pressColor |
| 108 | + } |
| 109 | + } |
| 110 | + ] |
| 111 | + |
| 112 | + //define transmission for the states |
| 113 | + transitions: [ |
| 114 | + Transition { |
| 115 | + from: ""; to: "Hovering" |
| 116 | + ColorAnimation { duration: 200 } |
| 117 | + }, |
| 118 | + Transition { |
| 119 | + from: "*"; to: "Pressed" |
| 120 | + ColorAnimation { duration: 10 } |
| 121 | + } |
| 122 | + ] |
| 123 | + |
| 124 | + //Mouse area to react on click events |
| 125 | + MouseArea { |
| 126 | + hoverEnabled: true |
| 127 | + anchors.fill: button |
| 128 | + onEntered: { button.state='Hovering'} |
| 129 | + onExited: { button.state=''} |
| 130 | + onClicked: { button.clicked();} |
| 131 | + onPressed: { button.state="Pressed" } |
| 132 | + onReleased: { |
| 133 | + if (containsMouse) |
| 134 | + button.state="Hovering"; |
| 135 | + else |
| 136 | + button.state=""; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments