ThymioSelectionDeviceDelegate.qml 9.73 KB
Newer Older
1
2
3
4
5
import QtQuick 2.0
import QtQuick.Controls 2.3
import org.mobsya  1.0

Item {
6
    property var device: model.object
7
    property bool selectable: updateSelectable()
8
    property double progress
9

10
    Component.onCompleted: {
11
12
        device.groupChanged.connect(updateSelectable);
        device.statusChanged.connect(updateSelectable);
13
14
15
16
17

        device.firmwareUpdateProgress.connect(function (value) {
            progressBar.progress = value
        })

Corentin Jabot's avatar
Corentin Jabot committed
18
        launcher.selectedAppChanged.connect(updateSelectable);
19
    }
20

21
22
23
24
25
26
    function isThymio(device) {
        return device.type === ThymioNode.Thymio2
                || device.type === ThymioNode.Thymio2Wireless
                || device.type === ThymioNode.SimulatedThymio2
    }

27
28
    function updateSelectable() {
        selectable = (device.status === ThymioNode.Available || launcher.selectedApp.supportsWatchMode)
29
30
        &&  (!device.isInGroup || launcher.selectedApp.supportsGroups)
        &&  (launcher.selectedApp.supportsNonThymioDevices || isThymio(device) );
31
    }
32

33
    function upgradeFirmware() {
34
35
36
37
38


        if(device.type !== ThymioNode.Thymio2)
            return

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        var component = Qt.createComponent("qrc:/qml/FirmwareUpdateDialog.qml");
        var dialog = component.createObject(launcher, {
                                            "oldVersion": device.fwVersion,
                                            "newVersion": device.fwVersionAvailable,
                                            "deviceName" : device.name
       });

       if (dialog === null) {
           console.log("Error creating dialog");
           return
       }
       dialog.yes.connect(function() {
           device.upgradeFirmware()
       })
       dialog.visible = true
    }

56
57
58
    Action {
        text: "Rename"
        id: renameAction
Corentin Jabot's avatar
Corentin Jabot committed
59
        onTriggered: if(textfield.editable) textfield.readOnly = false
60
61
62
63
    }
    Action {
        text: "Upgrade Firmware"
        id: upgradeAction
64
        onTriggered: {
65
            upgradeFirmware()
66
        }
67
68
69
70
71
72
73
74
    }
    Action {
        text: "Reset"
        id: resetAction
    }
    Action {
        text: "Stop"
        id: stopAction
75
76
77
78
79
80
        onTriggered: {
            var result = device.stop()
            Request.onFinished(result, function(status, res) {
                console.log(res)
            })
        }
81
82
83
84
85
    }

    id:item
    height: 172
    width : 172
86
    property bool selected: device_view.selectedDevice === device
87
    property double preferredOpacity: {
88
89
        if(!selectable)
            return 0.3
90
91
92
93
94
95
96
97
        switch(status) {
        case ThymioNode.Ready:
        case ThymioNode.Available:
                return selected ? 1 : 0.8;
        default: return 0.5
        }
    }

98
    property bool device_ready: status === ThymioNode.Ready || status === ThymioNode.Available
99
    property string tooltipText: ""
100

101
    MouseArea {
102

103
104
105
106
107
        id: device_mouse_area
        anchors.fill: parent
        hoverEnabled: true
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
108
109
110
            device_view.currentIndex = -1
            device_view.selectedDevice = undefined

111
112
            if (mouse.button === Qt.RightButton)
                contextMenu.popup(item)
113
            else if(selectable) {
114
                device_view.currentIndex = index
115
                device_view.selectedDevice = device
116
            }
117
        }
118
        onDoubleClicked: {
119
120
            if(!selectable)
                return
121
            selection_view.launchSelectedAppWithSelectedDevice()
122
123
        }

124
125
126
127
128
        onPressAndHold: {
            if (mouse.source === Qt.MouseEventNotSynthesized)
                contextMenu.popup()
        }

129
        onHoveredChanged: {
130
131
132
133
134
135
136
137
            const host = device.endpoint.hostName
            if(host.endsWith(".local"))
                host = host.slice(0, -6)
            if(device.endpoint.isLocalhostPeer)
                tooltipText = qsTr("%1 on this computer").arg(device.name)
            else
                tooltipText = qsTr("%1 on %2").arg(device.name).arg(host)

138
139
            if(!selectable) {
                if(isInGroup && !launcher.selectedApp.supportsGroups) {
140
                    tooltipText += "\n" + qsTr("This device cannot be selected because it is in a group")
141
                }
142
143
144
                else if(!isThymio(device) && !launcher.selectedApp.supportsNonThymioDevices) {
                    tooltipText += "\n" + qsTr("This device is not compatible with %1").arg(launcher.selectedApp.name)
                }
145
                else {
146
                    tooltipText += "\n" + qsTr("This device cannot be selected because it is already being used")
147
148
149
150
                }
            }
        }
        ToolTip.text: tooltipText
151
        ToolTip.visible: device_mouse_area.containsMouse
152

153
154
        cursorShape: device_ready ? Qt.PointingHandCursor : null

155
156
157
158
159
        DeviceContextMenu {
            id: contextMenu
            onOpen: {
                if(!menu)
                    return
160
161

                menu.device = device
Corentin Jabot's avatar
Corentin Jabot committed
162
163
164
165
166
167
                if(capabilities & ThymioNode.Rename) {
                    menu.addAction(renameAction)
                }
                if(capabilities & ThymioNode.ForceResetAndStop) {
                    menu.addAction(stopAction)
                }
168
169
170
                if((capabilities & ThymioNode.FirmwareUpgrade) && device.hasAvailableFirmwareUpdate) {
                    menu.addAction(upgradeAction)
                }
171
172
173
174
175
176
177
            }
        }

    }

    Rectangle {
        id: selectable_area
Corentin Jabot's avatar
Corentin Jabot committed
178
        color: device_mouse_area.containsMouse ? Style.light : "transparent"
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        border.width: item.selected ? 1.3333 : 0
        border.color: "#0a9eeb"
        anchors.fill: parent
        anchors.horizontalCenter: parent.horizontalCenter

        Column {
            width : 142
            height: 120
            anchors.centerIn: parent

            Item {
                width :  90
                anchors.horizontalCenter: parent.horizontalCenter
                height: 12


195
                /*BatteryIndicator {
196
197
198
199
200
                    anchors.verticalCenter: parent.verticalCenter
                    height: 8
                    anchors.left: parent.left
                    anchors.leftMargin: 2

201
                }*/
202
203

                Image {
204
                     opacity: preferredOpacity
205
206
207
208
209
210
211
212
213
                     id: icon;
                     source : "qrc:/assets/update-icon.svg"
                     fillMode: Image.PreserveAspectFit
                     anchors.verticalCenter: parent.verticalCenter
                     height: 12
                     anchors.right: parent.right
                     anchors.leftMargin: 2
                     smooth: true
                     antialiasing: true
214
215
216
217
218
                     visible: device.hasAvailableFirmwareUpdate
                     MouseArea {
                         id:update_icon_ma
                         anchors.fill: parent
                         hoverEnabled: true
219
220
221
                         onClicked: {
                             upgradeFirmware()
                         }
222
                         acceptedButtons: type === ThymioNode.Thymio2 ? MouseArea.LeftButton : null
223
224
225
226
                     }

                     ToolTip {
                         visible: update_icon_ma.containsMouse
227
228
                         text: (type === ThymioNode.Thymio2) ?
                                   qsTr("A new firmware is available!\nClick to install it") :
Corentin Jabot's avatar
Corentin Jabot committed
229
                                   qsTr("A new firmware is available!\nConnect the robot with a usb cable to install it")
230
231
                     }
                }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
            }

            Item {
                width: parent.width
                height: 8
            }

            Image {
                source: {
                    switch (type) {
                     case ThymioNode.Thymio2:
                     case ThymioNode.Thymio2Wireless:
                        return "qrc:/assets/thymio.svg"
                     case ThymioNode.SimulatedThymio2:
                        return "qrc:/assets/simulated_thymio.svg"
                     default:
                        return "qrc:/assets/dummy_node.svg"
                    }
                }
                width :  90
                fillMode:Image.PreserveAspectFit
                anchors.horizontalCenter: parent.horizontalCenter
254
                opacity: preferredOpacity
255
256
257
            }
            Item {
                width: parent.width
Corentin Jabot's avatar
Corentin Jabot committed
258
                height: 10
259
260
            }
            EditableDeviceNameInput {
261
                opacity: preferredOpacity
262
                id: textfield
Corentin Jabot's avatar
Corentin Jabot committed
263
                editable: capabilities & ThymioNode.Rename
264
                width: parent.width
265
                height: 48
Corentin Jabot's avatar
Corentin Jabot committed
266
                deviceName: name
267
268
                wrapMode: Text.WrapAnywhere
                
Corentin Jabot's avatar
Corentin Jabot committed
269
                onAccepted: {
270
                    
271
                    device.name = text
Corentin Jabot's avatar
Corentin Jabot committed
272
273
                    text = deviceName
                }
274
            }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302

            Item {
                id: progressBar
                property double progress: 0
                visible: progress > 0
                anchors.horizontalCenter: parent
                width: parent.width - 6
                anchors.left: parent.left
                anchors.margins: 3
                height: 3
                Rectangle {
                    id: progressBarBackground
                    anchors.left: parent.left
                    anchors.top : parent.top
                    height: parent.height
                    width: parent.width
                    color: Style.dark
                    radius: 2
                }
                Rectangle {
                    anchors.left: parent.left
                    anchors.top : parent.top
                    height: parent.height
                    width: parent.width * parent.progress
                    color: "#0a9eeb"
                    radius: 2
                }
            }
303
304
        }
    }
305
}