-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSegmentedButton.qml
170 lines (152 loc) · 5.23 KB
/
SegmentedButton.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import QtQuick 2.4
Item {
id: root
property var buttons
property var buttonsEnabled
property int activeButton: 0
property alias fontFamily: defaultLabel.font.family
property int fontPixelSize: 18
property int orientation: Qt.Horizontal
// Disabling exclusive mode makes the Segmented button behave as a list of independent buttons.
property bool exclusiveMode: true
property bool nonExclusiveActive: true
property var activeButtonColor: "blue"
property var inactiveButtonColor: "white"
property int buttonHeight: 36
height: orientation == ListView.Horizontal ? root.buttonHeight : root.buttonHeight * listModel.count
signal clicked (int buttonIndex)
function setLabel(buttonIndex, newLabel)
{
listModel.setProperty(buttonIndex, "name", newLabel)
}
function setButtonEnabled(buttonIndex, isEnabled)
{
listModel.setProperty(buttonIndex, "buttonEnabled", isEnabled)
}
ListModel {
id: listModel
property bool hasIcons: false
}
Text {
id: defaultLabel
visible: false
}
Component.onCompleted: {
listModel.hasIcons = false
for(var i in buttons) {
var name = buttons[i]
var icon = ""
if(name.substring(0, 3) == "qrc") {
icon = name
name = ""
listModel.hasIcons = true
}
listModel.append({"name": name, "icon": icon, "buttonEnabled": true})
}
for(var i in buttonsEnabled) {
setButtonEnabled(i, buttonsEnabled[i])
}
listView.currentIndex = activeButton
}
onActiveButtonChanged: {
if(listView.currentIndex != activeButton)
listView.currentIndex = activeButton
}
ListView {
id: listView
anchors.fill: parent
orientation: root.orientation == Qt.Horizontal ? ListView.Horizontal : ListView.Vertical
model: listModel
interactive: false
delegate: Rectangle {
id: oneButton
property int buttonIndex: index
property bool active: (exclusiveMode && buttonIndex == listView.currentIndex) || (!exclusiveMode && nonExclusiveActive)
color: {
var color = active ? root.activeButtonColor : root.inactiveButtonColor;
return mouseArea.pressed ? "lightblue" : color
}
height: root.orientation == Qt.Horizontal ? parent.height : root.buttonHeight
width: root.orientation == Qt.Horizontal && listView.model ? root.width / listView.model.count : root.width
radius: 5
Item {
visible: root.orientation == Qt.Horizontal
anchors.fill: parent
Rectangle {
color: oneButton.color
width: oneButton.radius
x: 0
height: parent.height
visible: index > 0
}
Rectangle {
color: oneButton.color
width: oneButton.radius
x: parent.width - width
height: parent.height
visible: index < (listView.count - 1)
}
Rectangle {
color: "blue"
width: 1
x: -1
height: parent.height
visible: index > 0
}
}
Item {
visible: root.orientation == Qt.Vertical
anchors.fill: parent
Rectangle {
color: oneButton.color
height: oneButton.radius
y: 0
width: parent.width
visible: index > 0
}
Rectangle {
color: oneButton.color
height: oneButton.radius
y: parent.height - height
width: parent.width
visible: index < (listView.count - 1)
}
Rectangle {
color: "blue"
height: 1
y: -1
width: root.width
visible: index > 0
}
}
Text {
anchors.fill: parent
text: name
horizontalAlignment: Text.AlignHCenter;
verticalAlignment: Text.AlignVCenter;
font.family: root.fontFamily
font.pixelSize: fontPixelSize
color: active ? "white" : "blue"
visible: name.length
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
enabled: buttonEnabled
onClicked: {
root.activeButton = listView.currentIndex = index
root.forceActiveFocus()
root.clicked(index)
}
}
}
}
Rectangle {
color: "transparent"
anchors.fill: parent
border.width: 1
border.color: root.activeButtonColor
radius: 5
}
}