1/*
2 * Copyright (C) 2016, 2017
3 *      Jean-Luc Barriere <jlbarriere68@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18import QtQuick 2.9
19import QtQuick.Controls 2.2
20
21
22Item {
23    id: renderingBubble
24    anchors {
25        left: parent.left
26        right: parent.right
27        margins: units.gu(2)
28    }
29    property bool opened: false
30    property color backgroundColor: styleMusic.popover.backgroundColor
31    property color foregroundColor: styleMusic.popover.foregroundColor
32    property color labelColor: styleMusic.popover.labelColor
33    readonly property bool displayable: containerLayout.height >= containerLayout.rowHeight
34
35    Popup {
36        id: popover
37        width: parent.width
38        height: containerLayout.height
39        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
40
41        background: Rectangle {
42                id: containerLayoutBackground
43                anchors.fill: parent
44                color: renderingBubble.backgroundColor
45                radius: units.gu(1)
46                opacity: 0.9
47            }
48
49        Item {
50            id: containerLayout
51            anchors.centerIn: parent
52            readonly property double rowHeight: units.gu(7)
53            readonly property double minHeight: rowHeight + units.gu(4)
54            readonly property double maxHeight: mainView.height - /*header-footer*/units.gu(15)
55            property int contentHeight: player.renderingModel.count * rowHeight + /*margins*/units.gu(6)
56            width: parent.width
57            height: contentHeight > maxHeight ? maxHeight : contentHeight
58
59            RenderingControlerView {
60                id: renderingControlerView
61                backgroundColor: "transparent"
62                foregroundColor: renderingBubble.foregroundColor
63                labelColor: renderingBubble.labelColor
64                anchors.fill: parent
65                anchors.topMargin: units.gu(2)
66                anchors.bottomMargin: units.gu(2)
67            }
68
69            Connections {
70                target: renderingControlerView
71                onFinger: {
72                    if (isHeld && bubbleTimer.running)
73                        timer.stop()
74                    else if (!isHeld)
75                        timer.restart()
76                }
77            }
78        }
79
80        Behavior on opacity {
81            NumberAnimation { duration: 500 }
82        }
83
84        onOpened: timer.start()
85    }
86
87    Timer {
88        id: timer
89        interval: 5000
90        onTriggered: {
91            opened = false
92            popover.close()
93        }
94    }
95
96    Connections {
97        target: popover
98        onClosed: {
99            if (renderingBubble.opened) {
100                renderingBubble.opened = false
101                timer.stop()
102            }
103        }
104    }
105
106    function open(caller) {
107        if (!renderingBubble.opened && containerLayout.height > containerLayout.minHeight) {
108            popover.parent = caller;
109            var gc = renderingBubble.parent.mapToItem(null, 0, 0)
110            if (gc.y > (mainView.height - mainView.header.height) / 2) {
111                popover.y = - (popover.height + units.gu(2))
112            } else {
113                popover.y = parent.height + units.gu(2)
114            }
115            player.refreshRendering()
116            renderingBubble.opened = true
117            popover.open()
118        }
119    }
120}
121