YouTube.qml 8.32 KB
Newer Older
1 2 3 4
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import "../../view_models"
5 6
import "../../view_models/BabeTable"
import org.kde.kirigami 2.2 as Kirigami
7
import org.kde.maui 1.0 as Maui
8 9 10

Page
{
11 12 13
    id: youtubeViewRoot
    property var searchRes : []
    clip: true
Camilo Higuita's avatar
Camilo Higuita committed
14 15 16 17

    property alias viewer : youtubeViewer
    property int openVideo : 0

18 19 20 21 22 23 24 25
    Connections
    {
        target: youtube
        onQueryResultsReady:
        {
            searchRes = res;
            populate(searchRes)
            youtubeTable.forceActiveFocus()
Camilo Higuita's avatar
Camilo Higuita committed
26 27 28 29 30 31 32

            if(openVideo > 0)
            {
                console.log("trying to open video")
                watchVideo(youtubeTable.model.get(openVideo-1))
                openVideo = 0
            }
33 34 35
        }
    }

Camilo Higuita's avatar
Camilo Higuita committed
36 37 38 39 40 41 42 43
    function watchVideo(track)
    {
        if(track && track.url)
        {
            var url = track.url
            if(url && url.length > 0)
            {
                youtubeViewer.currentYt = track
44
                youtubeViewer.webView.url = url+"?autoplay=1"
Camilo Higuita's avatar
Camilo Higuita committed
45
                stackView.push(youtubeViewer)
46

Camilo Higuita's avatar
Camilo Higuita committed
47 48 49 50 51
            }
        }
    }

    function playTrack(url)
52
    {
Camilo Higuita's avatar
Camilo Higuita committed
53 54 55 56 57 58 59 60
        if(url && url.length > 0)
        {
            var newURL = url.replace("embed/", "watch?v=")
            console.log(newURL)
            youtubePlayer.item.url = newURL+"?autoplay=1+&vq=tiny"
            youtubePlayer.item.runJavaScript("document.title", function(result) { console.log(result); });
        }
    }
61

Camilo Higuita's avatar
Camilo Higuita committed
62 63 64
    function runSearch(searchTxt)
    {
        if(searchTxt)
65
            if(searchTxt !== youtubeTable.headBarTitle)
Camilo Higuita's avatar
Camilo Higuita committed
66
            {
67
                youtubeTable.headBarTitle = searchTxt
Camilo Higuita's avatar
Camilo Higuita committed
68 69
                youtube.getQuery(searchTxt, bae.loadSetting("YOUTUBELIMIT", "BABE", 25))
            }
70 71
    }

Camilo Higuita's avatar
Camilo Higuita committed
72 73 74 75
    function clearSearch()
    {
        searchInput.clear()
        youtubeTable.clearTable()
76
        youtubeTable.headBarTitle = ""
Camilo Higuita's avatar
Camilo Higuita committed
77 78 79 80 81 82 83 84 85 86
        searchRes = []
    }

    function populate(tracks)
    {
        youtubeTable.clearTable()
        for(var i in tracks)
            youtubeTable.model.append(tracks[i])
    }

Camilo Higuita's avatar
Camilo Higuita committed
87
    /*this is for playing the track sin the background without showing the actual video*/
Camilo Higuita's avatar
Camilo Higuita committed
88 89 90
    Loader
    {
        id: youtubePlayer
Camilo Higuita's avatar
Camilo Higuita committed
91 92
        source: isAndroid ? "qrc:/services/web/YoutubePlayer_A.qml" :
                            "qrc:/services/web/YoutubePlayer.qml"
Camilo Higuita's avatar
Camilo Higuita committed
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

    BabePopup
    {
        id: configPopup
        parent: parent
        margins: contentMargins

        GridLayout
        {
            anchors.centerIn: parent
            width: parent.width*0.8
            height: parent.height*0.9
            columns: 1
            rows: 6

            Item
            {
                Layout.column: 1
                Layout.row: 1
                Layout.fillWidth: true
                Layout.fillHeight: true
            }

            Label
            {
                text: qsTr("Custom API Key")
                verticalAlignment:  Qt.AlignVCenter
                elide: Text.ElideRight
122
                font.pointSize: fontSizes.default
123 124 125 126 127 128 129 130 131 132
                Layout.column: 1
                Layout.row: 2
                Layout.fillWidth: true
            }

            TextField
            {
                Layout.column: 1
                Layout.row: 3
                Layout.fillWidth: true
133
                text: bae.loadSetting("YOUTUBEKEY", "BABE",  youtube.getKey())
134 135 136 137 138 139 140
            }

            Label
            {
                text: qsTr("Search results")
                verticalAlignment:  Qt.AlignVCenter
                elide: Text.ElideRight
141
                font.pointSize: fontSizes.default
142 143 144 145 146 147 148 149 150 151 152 153 154
                Layout.column: 1
                Layout.row: 4
                Layout.fillWidth: true
            }

            SpinBox
            {
                Layout.alignment: Qt.AlignRight
                Layout.column: 1
                Layout.row: 5
                Layout.fillWidth: true
                from: 1
                to: 50
155 156 157 158 159 160
                value: bae.loadSetting("YOUTUBELIMIT", "BABE", 25)
                editable: true
                onValueChanged:
                {
                    bae.saveSetting("YOUTUBELIMIT", value, "BABE")
                }
161 162 163 164 165 166 167 168 169 170 171 172 173
            }

            Item
            {
                Layout.column: 1
                Layout.row: 6
                Layout.fillWidth: true
                Layout.fillHeight: true
            }

        }
    }

174

Camilo Higuita's avatar
Camilo Higuita committed
175
    StackView
176
    {
Camilo Higuita's avatar
Camilo Higuita committed
177
        id: stackView
178
        anchors.fill: parent
Camilo Higuita's avatar
Camilo Higuita committed
179
        focus: true
180

Camilo Higuita's avatar
Camilo Higuita committed
181
        pushEnter: Transition
182
        {
Camilo Higuita's avatar
Camilo Higuita committed
183
            PropertyAnimation
184
            {
Camilo Higuita's avatar
Camilo Higuita committed
185 186 187 188
                property: "opacity"
                from: 0
                to:1
                duration: 200
189
            }
Camilo Higuita's avatar
Camilo Higuita committed
190
        }
191

Camilo Higuita's avatar
Camilo Higuita committed
192 193 194
        pushExit: Transition
        {
            PropertyAnimation
195
            {
Camilo Higuita's avatar
Camilo Higuita committed
196 197 198 199
                property: "opacity"
                from: 1
                to:0
                duration: 200
200 201 202
            }
        }

Camilo Higuita's avatar
Camilo Higuita committed
203
        popEnter: Transition
204
        {
Camilo Higuita's avatar
Camilo Higuita committed
205 206 207 208 209 210
            PropertyAnimation {
                property: "opacity"
                from: 0
                to:1
                duration: 200
            }
211 212
        }

Camilo Higuita's avatar
Camilo Higuita committed
213
        popExit: Transition
214
        {
Camilo Higuita's avatar
Camilo Higuita committed
215
            PropertyAnimation
216
            {
Camilo Higuita's avatar
Camilo Higuita committed
217 218 219 220
                property: "opacity"
                from: 1
                to:0
                duration: 200
221
            }
Camilo Higuita's avatar
Camilo Higuita committed
222
        }
223 224


Camilo Higuita's avatar
Camilo Higuita committed
225 226 227 228
        initialItem: Item
        {
            id: youtubeList
            ColumnLayout
229 230
            {
                anchors.fill: parent
Camilo Higuita's avatar
Camilo Higuita committed
231 232
                width: parent.width
                height: parent.height
233

Camilo Higuita's avatar
Camilo Higuita committed
234 235
                Layout.margins: 0
                spacing: 0
236

Camilo Higuita's avatar
Camilo Higuita committed
237
                BabeTable
238
                {
Camilo Higuita's avatar
Camilo Higuita committed
239
                    id: youtubeTable
240
                    Layout.fillHeight: true
Camilo Higuita's avatar
Camilo Higuita committed
241 242
                    Layout.fillWidth: true
                    trackNumberVisible: false
243 244 245 246
                    headBarVisible: true
                    headBarExit: true
                    headBarExitIcon: "edit-clear"
                    headBarTitle: "YouTube"
Camilo Higuita's avatar
Camilo Higuita committed
247 248 249 250 251 252
                    holder.message: "No YouTube results!"
                    coverArtVisible: true
                    trackDuration: true
                    trackRating: true
                    onExit: clearSearch()
                    isArtworkRemote: true
253
                    allowMenu: false
Camilo Higuita's avatar
Camilo Higuita committed
254 255 256 257 258

                    appendBtn.visible: false
                    playAllBtn.visible: false
                    menuBtn.visible: false

259
                    headBar.rightContent: Maui.ToolButton
Camilo Higuita's avatar
Camilo Higuita committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
                    {
                        id: menuBtn
                        iconName: "application-menu"
                        onClicked: configPopup.open()
                    }

                    onRowClicked:
                    {
                        watchVideo(youtubeTable.model.get(index))
                    }

                    onQuickPlayTrack:
                    {
                        playTrack(youtubeTable.model.get(index).url)
                    }
275 276
                }

Camilo Higuita's avatar
Camilo Higuita committed
277 278 279 280
                ToolBar
                {
                    id: searchBox
                    Layout.fillWidth: true
281
                    position: ToolBar.Footer
Camilo Higuita's avatar
Camilo Higuita committed
282 283 284 285 286 287 288 289

                    RowLayout
                    {
                        anchors.fill: parent

                        TextInput
                        {
                            id: searchInput
290
                            color: textColor
Camilo Higuita's avatar
Camilo Higuita committed
291 292 293 294
                            Layout.fillWidth: true
                            Layout.fillHeight: true
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment:  Text.AlignVCenter
295 296 297
                            selectByMouse: !isMobile
                            selectionColor: highlightColor
                            selectedTextColor: highlightedTextColor
Camilo Higuita's avatar
Camilo Higuita committed
298 299 300 301 302 303 304
                            focus: true
                            text: ""
                            wrapMode: TextEdit.Wrap
                            onAccepted: runSearch(searchInput.text)

                        }

305
                        Maui.ToolButton
Camilo Higuita's avatar
Camilo Higuita committed
306 307 308 309 310 311 312 313
                        {
                            Layout.rightMargin: contentMargins
                            iconName: "edit-clear"
                            onClicked: searchInput.clear()
                        }

                    }
                }
314
            }
315
        }
Camilo Higuita's avatar
Camilo Higuita committed
316 317 318 319 320

        YoutubeViewer
        {
            id: youtubeViewer
        }
321 322
    }
}