~danielholm/musicapp/trunk

« back to all changes in this revision

Viewing changes to MusicApp.qml.autosave

  • Committer: Daniel Holm
  • Date: 2013-05-13 14:49:06 UTC
  • Revision ID: admin@danielholm.se-20130513144906-hjefcuodsytfuqic
Added functions to get track data, worked on previous track function and start of database.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyleft Daniel Holm.
3
 
 *
4
 
 * Authors:
5
 
 *  Daniel Holm <d.holmen@gmail.com>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; version 3.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
import QtQuick 2.0
21
 
import Ubuntu.Components 0.1
22
 
import Ubuntu.Components.ListItems 0.1
23
 
import Ubuntu.Components.Popups 0.1
24
 
import Ubuntu.Components.ListItems 0.1 as ListItem
25
 
import Qt.labs.folderlistmodel 1.0
26
 
import QtMultimedia 5.0
27
 
 
28
 
MainView {
29
 
    objectName: i18n.tr("mainView")
30
 
    applicationName: "Music player"
31
 
 
32
 
    width: units.gu(50)
33
 
    height: units.gu(75)
34
 
 
35
 
    // VARIABLES
36
 
    property string musicName: i18n.tr("Music")
37
 
    property string musicDir: '/home/daniel/Musik/'
38
 
    property string trackStatus: "stopped"
39
 
    property string appVersion: '0.3'
40
 
 
41
 
    // FUNCTIONS
42
 
 
43
 
    // What is the state of the playback?
44
 
    function stateChange() {
45
 
 
46
 
        // state was stopped (0)
47
 
        if (playMusic.playbackState == "0") {
48
 
            console.debug("Debug: Music was stopped. Playing")
49
 
            beenStopped() // run stop function
50
 
            playMusic.play() // then play
51
 
 
52
 
            playTrack.iconSource = Qt.resolvedUrl("images/icon_pause@20.png") // change toolbar icon
53
 
            playTrack.text = i18n.tr("Pause") // change toolbar text
54
 
            trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
55
 
        }
56
 
 
57
 
        // if state was playing (1)
58
 
        else if (playMusic.playbackState == "1") {
59
 
            console.debug("Debug: Track was playing. Pause")
60
 
            playMusic.pause() // pause the music then
61
 
 
62
 
            playTrack.iconSource = Qt.resolvedUrl("images/icon_play@20.png") // change toolbar icon
63
 
            playTrack.text = i18n.tr("Resume") // change toolbar text
64
 
            trackInfo.text = i18n.tr("(Paused) ")+playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
65
 
        }
66
 
 
67
 
        // if state is paused (2)
68
 
        else if (playMusic.playbackState == "2") {
69
 
            console.debug("Debug: Track was paused. Playing")
70
 
            playMusic.play() // resume then
71
 
 
72
 
            playTrack.iconSource = Qt.resolvedUrl("images/icon_pause@20.png") // change toolbar icon
73
 
            playTrack.text = i18n.tr("Pause") // change toolbar text
74
 
            trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
75
 
        }
76
 
    }
77
 
 
78
 
    // stop function
79
 
    function beenStopped() {
80
 
        console.debug("Debug: has a track been played before or did I just start?")
81
 
 
82
 
        // track was just paused or stopped. Resume previous track
83
 
        if (playMusic.source != "") {
84
 
            console.debug("Debug: Resume previous song: "+playMusic.source)
85
 
            trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
86
 
        }
87
 
 
88
 
        // app just started, play random
89
 
        else {
90
 
            playMusic.source = "/home/daniel/Musik/Cyndi Lauper - 80s music - Time After Time.mp3"
91
 
            console.debug("Debug: I was just started. Play random track: "+playMusic.source)
92
 
            trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
93
 
        }
94
 
    }
95
 
 
96
 
    // previous and next track function
97
 
    function nextTrack() {
98
 
        console.debug() // print next songs filename
99
 
        playMusic.source = musicDir+trackQueue.get(0).file
100
 
        playMusic.play()
101
 
        removedTrackQueue.append({"title": trackQueue.get(0).title, "artist": trackQueue.get(0).artist, "file": trackQueue.get(0).file}) // move the track to a list of preious tracks
102
 
        trackQueue.remove(index) // remove the track from queue
103
 
    }
104
 
 
105
 
    // end of track
106
 
    function hasTrackEnded() {
107
 
        if (playMusic.status.EndOfMedia) {
108
 
            console.debug("Debug: media ended.")
109
 
        }
110
 
    }
111
 
 
112
 
    // Queue function
113
 
    function addToQueue(songTrack) {
114
 
        console.debug("Debug: added "+songTrack+" to queue.")
115
 
        // move down the automatically added tracks by using index
116
 
    }
117
 
 
118
 
    // Get song title
119
 
    function getTrackInfo(source) {
120
 
        console.debug('Debug: Got it. Trying to get meta data from: '+source) // debug
121
 
        musicInfo.source = source
122
 
        musicInfo.pause()
123
 
        return musicInfo.metaData.title
124
 
    }
125
 
 
126
 
 
127
 
    // Music stuff
128
 
    Audio {
129
 
        id: playMusic
130
 
        source: ""
131
 
    }
132
 
 
133
 
    // get file meta data
134
 
    Audio {
135
 
        id: musicInfo
136
 
        source: ""
137
 
        volume: 0.0
138
 
        //Keys.onSpacePressed: stateChange()
139
 
    }
140
 
 
141
 
    // list of tracks on startup. This is just during development
142
 
    ListModel {
143
 
        id: trackQueue
144
 
        ListElement {
145
 
            title: "Dancing in the Moonlight"
146
 
            artist: "Thin Lizzy"
147
 
            file: "something"
148
 
        }
149
 
        ListElement {
150
 
            title: "Rock and Roll"
151
 
            artist: "Led Zeppelin"
152
 
            file: "something else"
153
 
        }
154
 
        ListElement {
155
 
            title: "Moonlight Serenade"
156
 
            artist: "Frank Sinatra"
157
 
            file: "something completely else"
158
 
        }
159
 
    }
160
 
 
161
 
    // list of songs, which has been removed.
162
 
    ListModel {
163
 
        id: removedTrackQueue
164
 
        ListElement {
165
 
            title: "Dancing in the Moonlight"
166
 
            artist: "Thin Lizzy"
167
 
            file: "something"
168
 
        }
169
 
    }
170
 
 
171
 
    Tabs {
172
 
        id: tabs
173
 
        anchors.fill: parent
174
 
 
175
 
        // First tab begins here - should be the primary tab
176
 
        Tab {
177
 
            objectName: "Tab1"
178
 
 
179
 
            title: i18n.tr("Playing")
180
 
 
181
 
            // Tab content begins here
182
 
            page: Page {
183
 
                id: playingPage
184
 
 
185
 
                // toolbar
186
 
                tools: ToolbarActions {
187
 
 
188
 
                    // Share
189
 
                    Action {
190
 
                        id: shareTrack
191
 
                        objectName: "share"
192
 
 
193
 
                        iconSource: Qt.resolvedUrl("images/icon_share@20.png")
194
 
                        text: i18n.tr("Share")
195
 
 
196
 
                        onTriggered: {
197
 
                            console.debug('Debug: Share pressed')
198
 
                        }
199
 
                    }
200
 
 
201
 
                    // prevous track
202
 
                    Action {
203
 
                        id: prevTrack
204
 
                        objectName: "prev"
205
 
 
206
 
                        iconSource: Qt.resolvedUrl("images/prev.png")
207
 
                        text: i18n.tr("Previous")
208
 
 
209
 
                        onTriggered: {
210
 
                            console.debug('Debug: Prev track pressed')
211
 
                            console.debug(musicDir+removedTrackQueue.get(0).file) // print next songs filename
212
 
                            playMusic.source = musicDir+removedTrackQueue.get(0).file
213
 
                            playMusic.play()
214
 
                        }
215
 
                    }
216
 
 
217
 
                    // Play
218
 
                    Action {
219
 
                        id: playTrack
220
 
                        objectName: "play"
221
 
 
222
 
                        iconSource: Qt.resolvedUrl("images/icon_play@20.png")
223
 
                        text: i18n.tr("Play")
224
 
 
225
 
                        onTriggered: {
226
 
                            console.debug("Debug: "+trackStatus+" pressed in toolbar.")
227
 
                            console.debug(playMusic.playbackState)
228
 
                            stateChange()
229
 
                        }
230
 
                    }
231
 
 
232
 
                    // Next track
233
 
                    Action {
234
 
                        id: nextTrack
235
 
                        objectName: "next"
236
 
 
237
 
                        iconSource: Qt.resolvedUrl("images/next.png")
238
 
                        text: i18n.tr("Next")
239
 
 
240
 
                        onTriggered: {
241
 
                            console.debug('Debug: next track pressed')
242
 
                            console.debug(musicDir+trackQueue.get(0).file) // print next songs filename
243
 
                            playMusic.source = musicDir+trackQueue.get(0).file
244
 
                            playMusic.play()
245
 
 
246
 
                            // move track, which has been played from queue, to old queue so that prev button works
247
 
                            removedTrackQueue.append({"title": trackQueue.get(0).title, "artist": trackQueue.get(0).artist, "file": trackQueue.get(0).file})
248
 
                            trackQueue.remove(0)
249
 
                        }
250
 
                    }
251
 
 
252
 
                    // Queue
253
 
                    Action {
254
 
                        id: trackQueueAction
255
 
                        objectName: "queuelist"
256
 
                        iconSource: Qt.resolvedUrl("images/icon_settings@20.png")
257
 
                        text: i18n.tr("Queue")
258
 
                        onTriggered: {
259
 
                            PopupUtils.open(Qt.resolvedUrl("QueueDialog.qml"), pageLayout,
260
 
                                        {
261
 
                                            title: i18n.tr("Queue")
262
 
                                        } )
263
 
                        }
264
 
                    }
265
 
 
266
 
                    // Settings
267
 
                    Action {
268
 
                        id: settingsAction
269
 
                        objectName: "settings"
270
 
 
271
 
                        iconSource: Qt.resolvedUrl("images/icon_settings@20.png")
272
 
                        text: i18n.tr("Settings")
273
 
 
274
 
                        onTriggered: {
275
 
                            console.debug('Debug: Settings pressed')
276
 
                            // show settings dialog
277
 
                            PopupUtils.open(Qt.resolvedUrl("MusicSettings.qml"), pageLayout,
278
 
                                        {
279
 
                                            title: i18n.tr("Settings")
280
 
                                        } )
281
 
                        }
282
 
                    }
283
 
 
284
 
                }
285
 
 
286
 
                Column {
287
 
                        id: pageLayout
288
 
 
289
 
                        spacing: units.gu(1)
290
 
 
291
 
                        Row {
292
 
                            spacing: units.gu(1)
293
 
                            // Album cover here
294
 
                            UbuntuShape {
295
 
                                id: trackCoverArt
296
 
                                width: units.gu(50)
297
 
                                height: units.gu(50)
298
 
                                gradientColor: "blue" // test
299
 
                                image: Image {
300
 
                                    source: "images/music.png"
301
 
                                }
302
 
                                /*
303
 
                                onClicked: {
304
 
                                    playMusic.pause()
305
 
                                }
306
 
                                onPressAndHold: {
307
 
                                    playMusic.stop()
308
 
                                } */
309
 
                            }
310
 
                        }
311
 
 
312
 
                        // track progress
313
 
                        Row {
314
 
                            width: parent.width
315
 
                            ProgressBar {
316
 
                                id: trackProgress
317
 
                                minimumValue: 0
318
 
                                maximumValue: 100
319
 
                                value: 25
320
 
                            }
321
 
                        }
322
 
 
323
 
                        // Track info
324
 
                        Row {
325
 
                            spacing: units.gu(1)
326
 
                            Label {
327
 
                                id: trackInfo
328
 
                                text: "Stopped. Press play."
329
 
                                //subText: "Artist: + Year:"
330
 
                            }
331
 
                        }
332
 
                    }
333
 
 
334
 
 
335
 
            }
336
 
        }
337
 
 
338
 
 
339
 
        // Second tab begins here - artists here
340
 
        Tab {
341
 
            objectName: "Artists Tab"
342
 
 
343
 
            title: i18n.tr("Artists")
344
 
            page: Page {
345
 
                anchors.margins: units.gu(2)
346
 
 
347
 
            // foreach artist:
348
 
            ListItem.Standard {
349
 
                height: units.gu(4)
350
 
                // when pressed on this row, change to albums of artist
351
 
                Row {
352
 
                    Label {
353
 
                        text: i18n.tr("Artist 1")
354
 
                    }
355
 
                }
356
 
            }
357
 
 
358
 
                // toolbar here
359
 
 
360
 
            Column {
361
 
                anchors.centerIn: parent
362
 
                anchors.fill: parent
363
 
                ListView {
364
 
                    id: artistFolder
365
 
                    FolderListModel {
366
 
                        id: artistModel
367
 
                        folder: musicDir
368
 
                        nameFilters: ["//\\"];
369
 
                        showDirs: true;
370
 
                    }
371
 
                    width: parent.width
372
 
                    height: parent.height
373
 
                    model: artistModel
374
 
                    delegate: ListItem.Subtitled {
375
 
                        text: fileName
376
 
                        subText: "totatl albums: "
377
 
                        onClicked: {
378
 
                            console.debug('Debug: User pressed '+musicDir+fileName)
379
 
                        /*    playMusic.source = musicDir+dirName
380
 
                            playMusic.play()
381
 
                            trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
382
 
                            */
383
 
                        }
384
 
                    }
385
 
                }
386
 
            }
387
 
            }
388
 
        }
389
 
 
390
 
        // Third tab begins here - albums here
391
 
        Tab {
392
 
            objectName: "Albums Tab"
393
 
 
394
 
            title: i18n.tr("Albums")
395
 
            page: Page {
396
 
                anchors.margins: units.gu(2)
397
 
 
398
 
                Column {
399
 
                    anchors.centerIn: parent
400
 
                    anchors.fill: parent
401
 
                    ListView {
402
 
                        id: albumFolder
403
 
                        FolderListModel {
404
 
                            id: albumModel
405
 
                            folder: musicDir
406
 
                            nameFilters: ["//\\"];
407
 
                            showDirs: true;
408
 
                        }
409
 
                        width: parent.width
410
 
                        height: parent.height
411
 
                        model: albumModel
412
 
                        delegate: ListItem.Subtitled {
413
 
                            text: fileName
414
 
                            subText: "Year: "
415
 
                            onClicked: {
416
 
                                console.debug('Debug: User pressed '+musicDir+fileName)
417
 
                            /*    playMusic.source = musicDir+dirName
418
 
                                playMusic.play()
419
 
                                trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
420
 
                                */
421
 
                            }
422
 
                        }
423
 
                    }
424
 
                }
425
 
 
426
 
                // toolbar here
427
 
 
428
 
                Column {
429
 
                    anchors.centerIn: parent
430
 
                }
431
 
            }
432
 
        }
433
 
 
434
 
        // Fourth tab - tracks in ~/Music
435
 
        Tab {
436
 
            objectName: "Tracks Tab"
437
 
 
438
 
            title: i18n.tr("Tracks")
439
 
            page: Page {
440
 
                anchors.margins: units.gu(2)
441
 
 
442
 
                // toolbar here
443
 
 
444
 
                Column {
445
 
                    anchors.centerIn: parent
446
 
                    anchors.fill: parent
447
 
                    ListView {
448
 
                        id: musicFolder
449
 
                        FolderListModel {
450
 
                            id: folderModel
451
 
                            folder: musicDir
452
 
                            showDirs: false
453
 
                            nameFilters: ["*.ogg","*.mp3","*.oga","*.wav"]
454
 
                        }
455
 
                        width: parent.width
456
 
                        height: parent.height
457
 
                        model: folderModel
458
 
                        delegate: ListItem.Subtitled {
459
 
                            text: fileName
460
 
                            subText: "Artist: "
461
 
                            onClicked: {
462
 
                                playMusic.source = musicDir+fileName
463
 
                                playMusic.play()
464
 
                                console.debug('Debug: User pressed '+musicDir+fileName)
465
 
                                trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
466
 
                            }
467
 
                            onPressAndHold: {
468
 
                                console.debug('Debug: '+fileName+' added to queue.')
469
 
                                trackQueue.append({"title": playMusic.metaData.title, "artist": playMusic.metaData.albumArtist, "file": fileName})
470
 
                            }
471
 
                        }
472
 
                    }
473
 
                }
474
 
            }
475
 
        }
476
 
 
477
 
    } // tabs
478
 
} // main view