PlaylistDelegate.qml 6.66 KB
Newer Older
Shantanu Tushar's avatar
Shantanu Tushar committed
1
2
/***************************************************************************
 *   Copyright 2012 Sinny Kumari <ksinny@gmail.com>                        *
Shantanu Tushar's avatar
Shantanu Tushar committed
3
 *   Copyright 2012 Shantanu Tushar <shantanu@kde.org>                     *
4
 *   Copyright 2013 Saurabh Jain <saurabhskj@hotmail.com>                  *
Shantanu Tushar's avatar
Shantanu Tushar committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

import QtQuick 1.1
import org.kde.plasma.components 0.1 as PlasmaComponents

25
Item {
Shantanu Tushar's avatar
Shantanu Tushar committed
26
    id: listViewItem
27
28
29
    property bool isCurrentlyPlaying: index == playlistModel.currentIndex
    property variant url: mediaUrl

30
    signal playRequested(string url)
Shantanu Tushar's avatar
Shantanu Tushar committed
31
32
33
34
35

    Row {
        anchors.fill: parent
        Rectangle {
            width: parent.width - removeFromPlaylistButton.width; height: parent.height
36
            color: listViewItem.ListView.isCurrentItem ? "#002378" : theme.backgroundColor
Shantanu Tushar's avatar
Shantanu Tushar committed
37
            Text {
Shantanu Tushar's avatar
Shantanu Tushar committed
38
                anchors {
39
40
                    left: parent.left; verticalCenter: parent.verticalCenter
                    right: artistText.left; margins: 5
Shantanu Tushar's avatar
Shantanu Tushar committed
41
                }
Shantanu Tushar's avatar
Shantanu Tushar committed
42
                text: display
43
                color: listViewItem.isCurrentlyPlaying && listViewItem.ListView.view.model.filterString == "" ? "red" : theme.textColor
Shantanu Tushar's avatar
Shantanu Tushar committed
44
45
                elide: Text.ElideRight
                font.pixelSize: 18
46
                style: Text.Sunken
Shantanu Tushar's avatar
Shantanu Tushar committed
47
48
            }

49
50
51
52
53
54
55
            Text {
                id: artistText
                anchors {
                    right: lengthText.left; verticalCenter: parent.verticalCenter
                }
                width: parent.width*0.4
                text: mediaArtist
56
                color: index == playlistModel.currentIndex && listViewItem.ListView.view.model.filterString == "" ? "red" : theme.textColor
57
58
59
60
61
62
63
                elide: Text.ElideRight
                font.pixelSize: 18
                style: Text.Sunken
            }

            Text {
                id: lengthText
Shantanu Tushar's avatar
Shantanu Tushar committed
64
                property int seconds: mediaLength%60
65
66
67
68
                anchors {
                    right: parent.right; verticalCenter: parent.verticalCenter
                    margins: 5
                }
Shantanu Tushar's avatar
Shantanu Tushar committed
69
                text: mediaLength ? Math.floor(mediaLength/60) + ":" + (seconds.toString().length < 2 ? "0" + seconds : seconds) : ""
70
                color: index == playlistModel.currentIndex && listViewItem.ListView.view.model.filterString == "" ? "red" : theme.textColor
71
72
73
74
                font.pixelSize: 18
                style: Text.Sunken
            }

Shantanu Tushar's avatar
Shantanu Tushar committed
75
            MouseArea {
76
                id: dragItemArea
77
                hoverEnabled: true
Shantanu Tushar's avatar
Shantanu Tushar committed
78
                anchors.fill: parent
79
80
81
82
83
84
85
86
87
                property int posStartX: 0
                property int posStartY: 0
                property int posEndX: 0
                property int posEndY: 0
                property int movedX: Math.floor(posEndX - posStartX)
                property int movedY: Math.floor((posEndY - posStartY)/parent.height)
                property bool delegateHeld: false
                property int newPositionY: index + movedY
                drag.axis: Drag.XandYAxis
88
                onPressAndHold: {
89
                    listViewItem.z = 1
90
91
92
                    posStartX = listViewItem.x
                    posStartY = listViewItem.y
                    delegateHeld = true
93
94
                    dragItemArea.drag.target = listViewItem
                    listViewItem.opacity = 0.5
95
                    listViewItem.ListView.interactive = false
96
97
                    drag.maximumX = parent.width
                    drag.minimumX = 0
98
99
                    drag.maximumY = parent.height*listViewItem.ListView.count
                    drag.minimumY = 0
100
101
102
                }

                onPositionChanged: {
103
104
                    posEndX = listViewItem.x
                    posEndY = listViewItem.y
105
106
107
                }

                onReleased: {
108
109
110
                    if(!delegateHeld)
                        return
                    if(Math.abs(movedX) >= parent.width/3)
111
112
                        playlistModel.removeFromPlaylist(index)
                    else
113
114
115
116
117
118
                        listViewItem.x = posStartX
                    if (Math.abs(movedY) == 0 && delegateHeld == true){
                        listViewItem.y = posStartY
                    }
                    else
                    {
119
                        listViewItem.z = 0
120
121
122
123
124
125
126
127
128
129
130
131
132
                        listViewItem.opacity = 1
                        if(newPositionY < 1)
                            newPositionY = 0
                        else if(newPositionY > playlistList.count-1)
                            newPositionY = playlistList.count - 1
                        playlistModel.moveItem(index, newPositionY)
                        listViewItem.x = posStartX
                        listViewItem.y = posStartY
                    }
                    listViewItem.opacity =1
                    dragItemArea.drag.target = null
                    listViewItem.ListView.interactive = true
                    delegateHeld = false
133
134
                }

Shantanu Tushar's avatar
Shantanu Tushar committed
135
                onClicked: requestPlayback()
136
                onEntered: playlistList.currentIndex = index;
Shantanu Tushar's avatar
Shantanu Tushar committed
137
            }
138
         }
Shantanu Tushar's avatar
Shantanu Tushar committed
139
140
141

        PlasmaComponents.ToolButton {
            id: removeFromPlaylistButton
142
            visible: listViewItem.ListView.isCurrentItem
Shantanu Tushar's avatar
Shantanu Tushar committed
143
144
145
146
147
148
149
150
            width: height
            height: parent.height
            iconSource: "list-remove"
            onClicked: {
                playlistModel.removeFromPlaylist (index);
            }
        }
    }
151

Shantanu Tushar's avatar
Shantanu Tushar committed
152
153
154
    Keys.onReturnPressed: requestPlayback()

    function requestPlayback() {
155
        listViewItem.playRequested(mediaUrl)
156
157
    }
}