summaryrefslogtreecommitdiffstats
path: root/examples/wifi/wifi-qml/WifiScanner.qml
blob: f5ebec22f8f0ec6f0e65ebfe2d592b92a92554b8 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use the contact form at
** http://www.qt.io
**
** This file is part of Qt Enterprise Embedded.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** the contact form at http://www.qt.io
**
****************************************************************************/
import QtQuick 2.3
import QtQuick.Controls 1.2
import B2Qt.Wifi 1.0

Item {
    anchors.fill: parent

    Binding {
        target: WifiManager
        property: "scanning"
        value: networkView.visible
    }

    Button {
        id: wifiOnOffButton
        anchors.top: parent.top
        anchors.topMargin: 20
        anchors.left: parent.left
        anchors.right: parent.right
        onClicked: {
            if (WifiManager.backendState === WifiManager.Running) {
                if (networkView.visible)
                    networkView.visible = false
                WifiManager.stop()
            } else if (WifiManager.backendState === WifiManager.NotRunning) {
                WifiManager.start()
            }
        }

        Component.onCompleted: updateButtonText(WifiManager.backendState)
        Connections {
            target: WifiManager
            onBackendStateChanged: wifiOnOffButton.updateButtonText(backendState)
        }

        function updateButtonText(backendState)
        {
            if (backendState === WifiManager.Initializing)
                wifiOnOffButton.text = "Initializing..."
            if (backendState === WifiManager.Terminating)
                wifiOnOffButton.text = "Terminating..."
            if (backendState === WifiManager.NotRunning)
                wifiOnOffButton.text = "Switch On"
            if (backendState === WifiManager.Running)
                wifiOnOffButton.text = "Switch Off"
        }
    }

    Button {
        id: listNetworksButton
        anchors.top: wifiOnOffButton.bottom
        anchors.topMargin: 30
        anchors.left: parent.left
        anchors.right: parent.right
        visible: WifiManager.backendState === WifiManager.Running
        text: networkView.visible ? "Hide wifi networks"
                                  : "List available wifi networks"
        onClicked: networkView.visible = !networkView.visible
    }
    //! [0]
    ListView {
        id: networkView
        model: WifiManager.networks
        delegate: listDelegate
        implicitHeight: 800
        anchors.top: listNetworksButton.bottom
        anchors.topMargin: 30
        anchors.left: parent.left
        anchors.right: parent.right
        visible: false
        clip: true

        property string networkStateText: ""
        property QtObject expandedNetworkBox: null
        property bool hasExpandedNetworkBox: expandedNetworkBox !== null

        function setNetworkStateText(networkState) {
            if (networkState === WifiManager.ObtainingIPAddress)
                networkView.networkStateText = " (obtaining ip..)"
            else if (networkState === WifiManager.DhcpRequestFailed)
                networkView.networkStateText = " (dhcp request failed)"
            else if (networkState === WifiManager.Connected)
                networkView.networkStateText = " (connected)"
            else if (networkState === WifiManager.Authenticating)
                networkView.networkStateText = " (authenticating..)"
            else if (networkState === WifiManager.HandshakeFailed)
                networkView.networkStateText = " (wrong password)"
            else if (networkState === WifiManager.Disconnected)
                networkView.networkStateText = ""
        }

        Connections {
            target: WifiManager
            onNetworkStateChanged: networkView.setNetworkStateText(networkState)
        }
    }
    //! [0]
    //! [2]
    WifiConfiguration { id: config }
    //! [2]
    Component {
        id: listDelegate
        Rectangle {
            id: networkBox
            property bool expanded: false
            property bool isCurrentNetwork: WifiManager.currentSSID === ssid
            property bool connected: isCurrentNetwork && WifiManager.networkState === WifiManager.Connected
            property int notExpandedHeight: ssidLabel.height + bssidLabel.height + 20
            property int expandedHeight: notExpandedHeight + passwordInput.height + connectionButton.height + 54
            property int connectedExpandedHeight: notExpandedHeight + connectionButton.height + 30
            height: expanded ? (connected ? connectedExpandedHeight : expandedHeight) : notExpandedHeight
            width: parent.width
            clip: true
            color: "#5C5C5C"
            border.color: "black"
            border.width: 1

            Component.onDestruction: if (expanded) networkView.expandedNetworkBox = null
            onHeightChanged: if (expanded) networkView.positionViewAtIndex(index, ListView.Contain)

            Behavior on height { NumberAnimation { duration: 500; easing.type: Easing.InOutCubic } }
            //! [1]
            Text {
                id: ssidLabel
                anchors.top: parent.top
                anchors.left: parent.left
                anchors.margins: 5
                anchors.leftMargin: 10
                font.pixelSize: 26
                font.bold: true
                color: "#E6E6E6"
                text: isCurrentNetwork ? ssid + networkView.networkStateText : ssid
                Component.onCompleted: networkView.setNetworkStateText(WifiManager.networkState)
            }

            Text {
                id: bssidLabel
                anchors.top: ssidLabel.bottom
                anchors.left: parent.left
                anchors.margins: 5
                anchors.leftMargin: 30
                text: bssid
                color: "#E6E6E6"
                font.pixelSize: ssidLabel.font.pixelSize * 0.8
            }

            Text {
                id: flagsLabel
                anchors.top: bssidLabel.top
                anchors.left: bssidLabel.right
                anchors.leftMargin: 35
                text: (supportsWPA2 ? "WPA2 " : "")
                     + (supportsWPA ? "WPA " : "")
                     + (supportsWEP ? "WEP " : "")
                     + (supportsWPS ? "WPS " : "");
                color: "#E6E6E6"
                font.pixelSize: ssidLabel.font.pixelSize * 0.8
                font.italic: true
            }

            Rectangle {
                id: signalStrengthBar
                height: 15
                radius: 20
                antialiasing: true
                anchors.margins: 10
                anchors.right: parent.right
                anchors.top: parent.top
                color: "#BF8888"
                border.color: "#212126"
                property int strengthBarWidth: Math.max(100 + signalStrength, 0) / 100 * parent.width
                onStrengthBarWidthChanged: {
                    if (strengthBarWidth > parent.width * 0.55)
                        signalStrengthBar.width = parent.width * 0.55
                    else
                        signalStrengthBar.width = strengthBarWidth
                }
            }
            //! [1]
            MouseArea {
                anchors.fill: parent
                onClicked: handleNetworkBoxExpanding()
            }

            function handleNetworkBoxExpanding()
            {
                expanded = !expanded
                if (expanded) {
                    if (networkView.hasExpandedNetworkBox)
                        networkView.expandedNetworkBox.expanded = false
                    networkView.expandedNetworkBox = networkBox
                } else {
                    networkView.expandedNetworkBox = null
                }
            }

            TextField {
                id: passwordInput
                anchors.top: flagsLabel.bottom
                anchors.topMargin: 15
                anchors.horizontalCenter: parent.horizontalCenter
                width: parent.width * 0.36
                height: connectionButton.height * 1.1
                placeholderText: "Enter Password"
                visible: !connected
                font.pixelSize: 16
                echoMode: TextInput.Password
                inputMethodHints: Qt.ImhNoPredictiveText
            }

            Button {
                id: connectionButton
                y: connected ? passwordInput.y
                             : passwordInput.y + passwordInput.height + 10
                width: passwordInput.width
                anchors.horizontalCenter: parent.horizontalCenter
                text: connected ? "Disconnect" : "Connect"
                //! [3]
                onClicked: {
                    if (connected) {
                        WifiManager.disconnect()
                    } else {
                        config.ssid = ssid;
                        config.passphrase = passwordInput.text
                        if (!WifiManager.connect(config))
                            print("failed to connect: " + WifiManager.lastError)
                    }
                }
                //! [3]
            }
        }
    }
}