~mzanetti/+junk/ubtd

« back to all changes in this revision

Viewing changes to shareplugin/Main.qml

  • Committer: Michael Zanetti
  • Date: 2016-04-14 23:34:10 UTC
  • Revision ID: michael.zanetti@canonical.com-20160414233410-17jg7ez3w1qc5e16
make it work, polish a bit, 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import QtQuick 2.0
 
1
import QtQuick 2.4
2
2
import QtQuick.Layouts 1.1
3
 
import Ubuntu.Components 1.2
4
 
import QtBluetooth 5.2
 
3
import Ubuntu.Components 1.3
 
4
import QtBluetooth 5.4
5
5
import Shareplugin 0.1
6
 
import Ubuntu.Content 1.1
 
6
import Ubuntu.Content 1.3
7
7
 
8
8
MainView {
9
9
    id: root
13
13
    height: units.gu(75)
14
14
 
15
15
    property string fileName
 
16
    property bool peerSelected: false
16
17
 
17
18
    BtTransfer {
18
19
        id: transfer
28
29
        }
29
30
    }
30
31
 
 
32
    Timer { id: scheduleRestart; interval: 1000; onTriggered: btModel.running = true; }
 
33
 
31
34
    BluetoothDiscoveryModel {
32
35
        id: btModel
33
 
        running: true
 
36
        property bool continuousDiscovery: true
 
37
        onContinuousDiscoveryChanged: {
 
38
            if (continuousDiscovery && !running) {
 
39
                running = true;
 
40
            } else if (!continuousDiscovery && running) {
 
41
                running = false;
 
42
            }
 
43
        }
 
44
 
 
45
        // Workaround for width not being set when we start the animation
 
46
        Component.onCompleted: scheduleRestart.start()
 
47
        running: false
 
48
 
 
49
        onRunningChanged: {
 
50
            if (!running && continuousDiscovery) {
 
51
                scheduleRestart.start()
 
52
            }
 
53
        }
 
54
 
34
55
        discoveryMode: BluetoothDiscoveryModel.DeviceDiscovery
35
56
        onDiscoveryModeChanged: console.log("Discovery mode: " + discoveryMode)
36
57
        onServiceDiscovered: console.log("Found new service " + service.deviceAddress + " " + service.deviceName + " " + service.serviceName);
52
73
    }
53
74
 
54
75
    Page {
55
 
        title: i18n.tr("Bluetooth file transfer")
56
 
 
57
 
        ColumnLayout {
58
 
            spacing: units.gu(1)
 
76
        id: page
 
77
        header: PageHeader {
 
78
            title: i18n.tr("Share via Bluetooth")
 
79
            leadingActionBar.actions: [
 
80
                Action {
 
81
                    iconName: "close"
 
82
                    onTriggered: Qt.quit()
 
83
                }
 
84
 
 
85
            ]
 
86
        }
 
87
 
 
88
        Rectangle {
 
89
            id: progressBar
59
90
            anchors {
60
 
                margins: units.gu(1)
61
 
                fill: parent
62
 
            }
63
 
 
64
 
            Label {
65
 
                id: label
66
 
                text: i18n.tr("Searching for bluetooth devices")
67
 
                fontSize: "large"
68
 
                Layout.fillWidth: true
69
 
                wrapMode: Text.WordWrap
70
 
            }
71
 
            Label {
72
 
                text: i18n.tr("Send a file via Bluetooth to your Ubuntu phone")
73
 
                fontSize: "x-small"
74
 
                Layout.fillWidth: true
75
 
                wrapMode: Text.WordWrap
76
 
            }
77
 
            ListView {
78
 
                Layout.fillHeight: true
79
 
                Layout.fillWidth: true
80
 
 
81
 
                model: btModel
82
 
 
83
 
                ActivityIndicator {
 
91
                left: parent.left; top: parent.top; right: parent.right; topMargin: page.header.height
 
92
            }
 
93
            height: units.dp(5)
 
94
            visible: btModel.running
 
95
            Rectangle {
 
96
                id: progressElement
 
97
                width: parent.width / 3
 
98
                height: parent.height
 
99
                color: UbuntuColors.blue
 
100
            }
 
101
            property int targetX: page.width - progressElement.width
 
102
            onTargetXChanged: {
 
103
                if (visible) {
 
104
                    inquiryAnimation.restart()
 
105
                }
 
106
            }
 
107
            SequentialAnimation {
 
108
                id: inquiryAnimation
 
109
                loops: Animation.Infinite
 
110
                running: progressBar.visible
 
111
                NumberAnimation {
 
112
                    target: progressElement
 
113
                    duration: 2000
 
114
                    property: "x"
 
115
                    from: 0
 
116
                    to: progressBar.targetX
 
117
                }
 
118
                NumberAnimation {
 
119
                    target: progressElement
 
120
                    property: "x"
 
121
                    duration: 2000
 
122
                    from: progressBar.targetX
 
123
                    to: 0
 
124
                }
 
125
            }
 
126
        }
 
127
 
 
128
        GridLayout {
 
129
            anchors.fill: parent
 
130
            anchors.topMargin: page.header.height + progressBar.height
 
131
            columns: width > height ? 2 : 1
 
132
 
 
133
            Item {
 
134
                Layout.fillHeight: true
 
135
                Layout.fillWidth: true
 
136
 
 
137
                Image {
 
138
                    id: transferredImage
 
139
                    anchors.fill: parent
 
140
                    anchors.margins: units.gu(2)
 
141
                    source: "file://" + root.fileName
 
142
                    fillMode: Image.PreserveAspectFit
 
143
                }
 
144
                Icon {
 
145
                    anchors.fill: parent
 
146
                    anchors.margins: units.gu(2)
 
147
                    visible: transferredImage.status === Image.Error || !root.fileName
 
148
                    name: {
 
149
                        var extension = root.fileName.split(".").pop()
 
150
                        switch(extension) {
 
151
                        case "pdf":
 
152
                            return "application-pdf-symbolic";
 
153
                        case "tar":
 
154
                        case "gz":
 
155
                        case "gzip":
 
156
                        case "zip":
 
157
                        case "xz":
 
158
                            return "application-x-archive-symbolic";
 
159
                        case "mp3":
 
160
                        case "ogg":
 
161
                        case "wav":
 
162
                        case "flac":
 
163
                            return "audio-x-generic-symbolic";
 
164
                        case "jpg":
 
165
                        case "gif":
 
166
                        case "jpeg":
 
167
                        case "png":
 
168
                        case "webp":
 
169
                            return "image-x-generic-symbolic";
 
170
                        case "click":
 
171
                        case "deb":
 
172
                            return "package-x-generic-symbolic";
 
173
                        case "txt":
 
174
                            return "text-generic-symbolic";
 
175
                        case "mp4":
 
176
                        case "mkv":
 
177
                        case "avi":
 
178
                        case "mpeg":
 
179
                        case "mpg":
 
180
                            return "video-x-generic-symbolic";
 
181
                        default:
 
182
                            return "empty-symbolic";
 
183
                        }
 
184
                    }
 
185
                }
 
186
            }
 
187
 
 
188
            Item {
 
189
                Layout.fillHeight: true
 
190
                Layout.fillWidth: true
 
191
 
 
192
                ListView {
 
193
                    anchors.fill: parent
 
194
                    model: btModel
 
195
                    visible: !root.peerSelected
 
196
                    clip: true
 
197
 
 
198
                    delegate: ListItem {
 
199
                        height: units.gu(6)
 
200
                        Label {
 
201
                            anchors.centerIn: parent
 
202
                            text: (deviceName ? deviceName : name)
 
203
                        }
 
204
                        onClicked: {
 
205
                            btModel.continuousDiscovery = false;
 
206
                            transfer.sendFile(remoteAddress, root.fileName)
 
207
                            root.peerSelected = true;
 
208
                        }
 
209
                    }
 
210
                }
 
211
 
 
212
                ColumnLayout {
 
213
                    width: parent.width - units.gu(4)
84
214
                    anchors.centerIn: parent
85
 
                    running: btModel.running
86
 
                }
87
 
 
88
 
                delegate: ListItem {
89
 
                    height: units.gu(6)
 
215
                    spacing: units.gu(2)
 
216
                    visible: root.peerSelected
90
217
                    Label {
91
 
                        anchors.centerIn: parent
92
 
                        text: (deviceName ? deviceName : name) + "(" + remoteAddress + ")"
93
 
                    }
94
 
                    onClicked: {
95
 
                        transfer.sendFile(remoteAddress, root.fileName)
96
 
                    }
97
 
 
98
 
//                    RowLayout {
99
 
//                        anchors { fill: parent; margins: units.gu(1) }
100
 
//                        Image {
101
 
//                            Layout.fillHeight: true
102
 
//                            Layout.preferredWidth: height
103
 
//                            source: completed ? (success ? "file://" + filePath + filename : "image://theme/close") : "image://theme/empty-symbolic"
104
 
//                            fillMode: Image.PreserveAspectFit
105
 
//                        }
106
 
 
107
 
//                        ColumnLayout {
108
 
//                            Label {
109
 
//                                Layout.fillWidth: true
110
 
//                                text: filename
111
 
//                            }
112
 
//                            UbuntuShape {
113
 
//                                Layout.fillWidth: true
114
 
//                                Layout.preferredHeight: units.dp(5)
115
 
//                                UbuntuShape {
116
 
//                                    anchors.fill: parent
117
 
//                                    color: UbuntuColors.orange
118
 
//                                    // trans : total = x : width
119
 
//                                    anchors.rightMargin: parent.width - (transferred * parent.width / size)
120
 
//                                }
121
 
//                            }
122
 
//                            Label {
123
 
//                                text: completed ? (success ? "Completed" : "Failed") : "Transferring..."
124
 
//                            }
125
 
//                        }
126
 
//                    }
 
218
                        Layout.fillWidth: true
 
219
                        wrapMode: Text.WordWrap
 
220
                        text: transfer.error ? "Transfer failed."
 
221
                                : transfer.finished ? "File transferred."
 
222
                                : "Transferring file..."
 
223
                        fontSize: "large"
 
224
                    }
 
225
 
 
226
                    ProgressBar {
 
227
                        Layout.fillWidth: true
 
228
                        value: transfer.progress
 
229
                        visible: !transfer.finished && !transfer.error
 
230
                    }
 
231
 
 
232
                    Button {
 
233
                        Layout.fillWidth: true
 
234
                        text: i18n.tr("Close")
 
235
                        color: transfer.error ? UbuntuColors.red : UbuntuColors.green
 
236
                        onClicked: Qt.quit();
 
237
                        visible: transfer.finished || transfer.error
 
238
                    }
127
239
                }
 
240
 
128
241
            }
129
242
        }
130
243
    }