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