Debian Codegraph @

src/qml/HProgressBar.qml

raw
1 /*
2 * SPDX-FileCopyrightText: 2020 George Florea Bănuș <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 pragma ComponentBehavior: Bound
8
9 import QtQml
10 import QtQuick
11 import QtQuick.Controls
12 import QtQuick.Layouts
13 import QtQuick.Shapes
14
15 import org.kde.kirigami as Kirigami
16 import org.kde.haruna
17 import org.kde.haruna.utilities
18 import org.kde.haruna.settings
19
20 RowLayout {
21 id: root
22
23 required property MpvVideo m_mpv
24
25 property alias loopIndicator: loopIndicator
26 property alias chaptersPopupIsOpen: chaptersPopup.visible
27 property bool showChapterMarkers: GeneralSettings.showChapterMarkers
28 && m_mpv.chaptersModel.rowCount * 20 < width
29
30 function openChapterPopup(triggerItem: Item) {
31 const x = (chaptersPopup.width + triggerItem.width) * -0.5
32 const globalPoint = root.mapToGlobal(x, 0)
33 const localPoint = root.mapFromGlobal(Kirigami.Units.largeSpacing, 0)
34 chaptersPopup.x = globalPoint.x < 0 ? localPoint.x : x
35 chaptersPopup.open()
36 }
37
38 spacing: Kirigami.Units.smallSpacing
39
40 Slider {
41 id: slider
42
43 property bool seekStarted: false
44 property bool prePressPause: root.m_mpv.pause
45 property double mouseX: 0
46
47 function updateTooltip() : string {
48 slider.mouseX = progressBarMouseArea.mouseX
49 const time = progressBarMouseArea.mouseX / progressBarBG.width * slider.to
50 previewMpvLoader.position = time
51 progressBarToolTip.text = MiscUtils.formatTime(time)
52 }
53
54 from: 0
55 to: root.m_mpv.duration
56 implicitWidth: root.width
57 implicitHeight: root.height
58 leftPadding: 0
59 rightPadding: 0
60
61 Layout.fillWidth: true
62
63 handle: Item { visible: false }
64
65 background: Rectangle {
66 id: progressBarBG
67 color: Kirigami.Theme.alternateBackgroundColor
68 scale: slider.mirrored ? -1 : 1
69
70 Rectangle {
71 id: loopIndicator
72 property double startPosition: -1
73 property double endPosition: -1
74 width: endPosition === -1 ? 1 : (endPosition / root.m_mpv.duration * progressBarBG.width) - x
75 height: parent.height
76 color: Qt.hsla(0, 0, 0, 0.4)
77 visible: startPosition !== -1
78 x: startPosition / root.m_mpv.duration * progressBarBG.width
79 z: 110
80 }
81
82 Rectangle {
83 width: slider.position * parent.width
84 height: parent.height
85 color: Kirigami.Theme.highlightColor
86 }
87
88 Rectangle {
89 id: progressBarToolTip
90
91 property string text
92 property int margin: Kirigami.Units.smallSpacing
93
94 x: {
95 if (slider.pressed) {
96 const xPos = (slider.value / slider.to) * slider.width
97 slider.mouseX = xPos
98 return xPos - (progressBarToolTip.width * 0.5)
99 }
100
101 return slider.mouseX - (progressBarToolTip.width * 0.5)
102 }
103 y: -height - Kirigami.Units.largeSpacing
104 z: 10
105 width: previewMpvLoader.visible
106 ? GeneralSettings.previewThumbnailWidth + (margin * 2)
107 : timePositionLabel.width + (margin * 2)
108 height: previewMpvLoader.visible
109 ? Math.ceil(width / previewMpvLoader.aspectRatio) + (margin * 3) + timePositionLabel.height
110 : timePositionLabel.height + (margin * 2)
111 visible: progressBarMouseArea.containsMouse && root.m_mpv.duration > 0
112
113 color: Kirigami.Theme.backgroundColor
114 radius: Kirigami.Units.cornerRadius
115 border.width: 1
116 border.color: Qt.darker(Kirigami.Theme.backgroundColor, 1.2)
117
118 ColumnLayout {
119 anchors.centerIn: parent
120 width: GeneralSettings.previewThumbnailWidth
121
122 Loader {
123 id: previewMpvLoader
124
125 property string file: root.m_mpv.currentUrl
126 property double position: 0
127 property double aspectRatio: 2
128
129 active: GeneralSettings.showPreviewThumbnail
130 && root.m_mpv.playbackState !== MpvItem.PlaybackState.Stopped
131 && previewMpvLoader.file !== ""
132 && root.m_mpv.videoWidth > 0
133 visible: active && (item as MpvPreview).isLocalFile && (item as MpvPreview).isVideo
134 asynchronous: true
135 sourceComponent: MpvPreview {
136 id: mpvPreview
137 accuratePreview: GeneralSettings.accuratePreviewThumbnail
138 position: previewMpvLoader.position
139 file: previewMpvLoader.file
140
141 onAspectRatioChanged: previewMpvLoader.aspectRatio = aspectRatio || 2
142 }
143
144 Layout.preferredWidth: GeneralSettings.previewThumbnailWidth
145 Layout.preferredHeight: Math.ceil(Layout.preferredWidth / previewMpvLoader.aspectRatio)
146 Layout.alignment: Qt.AlignCenter
147 }
148
149 Label {
150 id: timePositionLabel
151
152 text: progressBarToolTip.text
153 Kirigami.Theme.colorSet: Kirigami.Theme.Tooltip
154 Layout.alignment: Qt.AlignCenter
155 }
156 }
157 }
158
159 MouseArea {
160 id: progressBarMouseArea
161
162 anchors.fill: parent
163 hoverEnabled: true
164 acceptedButtons: Qt.MiddleButton | Qt.RightButton
165
166 onClicked: function(mouse) {
167 if (mouse.button === Qt.MiddleButton) {
168 if (!GeneralSettings.showChapterMarkers) {
169 return
170 }
171
172 const time = mouseX / progressBarBG.width * slider.to
173 const chapters = root.m_mpv.getProperty(MpvProperties.ChapterList)
174 const nextChapter = chapters.findIndex(chapter => chapter.time > time)
175 root.m_mpv.chapter = nextChapter
176 }
177 if (mouse.button === Qt.RightButton && root.m_mpv.chaptersModel.rowCount > 0) {
178 chaptersPopup.x = mouseX - chaptersPopup.width * 0.5
179 chaptersPopup.open()
180 }
181 }
182
183 onMouseXChanged: {
184 slider.updateTooltip()
185 }
186
187 onWheel: function(wheel) {
188 if (wheel.angleDelta.y > 0) {
189 HarunaApp.actions.seekForwardMediumAction.trigger()
190 } else if (wheel.angleDelta.y) {
191 HarunaApp.actions.seekBackwardMediumAction.trigger()
192 }
193 }
194 }
195 }
196
197 onToChanged: {
198 value = root.m_mpv.position
199 slider.updateTooltip()
200 }
201
202 onMoved: {
203 if (!pressed) {
204 return
205 }
206
207 root.m_mpv.commandAsync(["seek", slider.value, "absolute"])
208 progressBarToolTip.text = MiscUtils.formatTime(slider.value)
209
210 const previewItem = previewMpvLoader.item as MpvPreview
211 if (previewItem === null || !previewItem.isLocalFile) {
212 return
213 }
214 previewItem.commandAsync(["seek", slider.value, "absolute"])
215 }
216
217 onPressedChanged: {
218 if (pressed) {
219 slider.prePressPause = root.m_mpv.getProperty(MpvProperties.Pause)
220 root.m_mpv.setPropertyBlocking(MpvProperties.Pause, true)
221 } else {
222 root.m_mpv.setPropertyBlocking(MpvProperties.Pause, slider.prePressPause)
223 }
224 }
225
226 // create markers for the chapters
227 Repeater {
228 id: chaptersInstantiator
229 model: root.showChapterMarkers
230 ? root.m_mpv.chaptersModel
231 : 0
232 delegate: Shape {
233 id: chapterMarkerShape
234
235 required property int index
236 required property string title
237 required property double startTime
238
239 // where the chapter marker should be positioned on the progress bar
240 property int position: slider.mirrored
241 ? progressBarBG.width - (chapterMarkerShape.startTime / root.m_mpv.duration * progressBarBG.width)
242 : chapterMarkerShape.startTime / root.m_mpv.duration * progressBarBG.width
243
244 antialiasing: true
245 ShapePath {
246 id: shape
247 strokeWidth: 1
248 strokeColor: Kirigami.Theme.textColor
249 startX: chapterMarkerShape.position
250 startY: slider.height
251 fillColor: Kirigami.Theme.textColor
252 PathLine { x: shape.startX; y: -1 }
253 PathLine { x: shape.startX + 6; y: -7 }
254 PathLine { x: shape.startX - 7; y: -7 }
255 PathLine { x: shape.startX - 1; y: -1 }
256 }
257 Rectangle {
258 x: chapterMarkerShape.position - 8
259 y: -11
260 width: 15
261 height: 11
262 color: "transparent"
263 ToolTip {
264 id: chapterTitleToolTip
265 text: chapterMarkerShape.title
266 visible: false
267 delay: 0
268 timeout: 10000
269 }
270 MouseArea {
271 anchors.fill: parent
272 hoverEnabled: true
273 onEntered: chapterTitleToolTip.visible = true
274 onExited: chapterTitleToolTip.visible = false
275 onClicked: root.m_mpv.chapter = chapterMarkerShape.index
276 }
277 }
278 }
279 }
280
281 Popup {
282 id: chaptersPopup
283
284 property int itemHeight
285 property int itemBiggestWidth: 1
286 property int maxWidth: Math.max(Kirigami.Units.gridUnit * 40, root.width * 0.8)
287
288 y: -height - slider.height
289 z: 20
290 width: Math.min(itemBiggestWidth, maxWidth)
291 height: Math.min(root.m_mpv.height - Kirigami.Units.gridUnit * 2,
292 listViewPage.contentHeight + listViewPage.footer.height)
293 modal: true
294 padding: 0
295 onOpened: {
296 listView.positionViewAtIndex(root.m_mpv.chapter, ListView.Beginning)
297 }
298
299 Kirigami.ScrollablePage {
300 id: listViewPage
301
302 padding: 0
303
304 anchors.fill: parent
305 footer: ToolBar {
306 z: 100
307 width: parent.width
308 CheckBox {
309 anchors.verticalCenter: parent.verticalCenter
310 text: i18nc("@action:inmenu", "Skip Chapters")
311 checked: PlaybackSettings.skipChapters
312 onCheckedChanged: {
313 PlaybackSettings.skipChapters = checked
314 PlaybackSettings.save()
315 }
316
317 ToolTip {
318 text: i18nc("@info:tooltip", "Automatically skips chapters containing certain words/characters.\nCheck “Playback” settings for more details.")
319 }
320 }
321 }
322
323 ListView {
324 id: listView
325
326 model: root.m_mpv.chaptersModel
327 reuseItems: true
328 delegate: RadioDelegate {
329 id: menuitem
330
331 required property int index
332 required property string title
333 required property double startTime
334
335 property int scrollBarWidth: listViewPage.contentItem.ScrollBar.vertical.width
336
337 text: `${MiscUtils.formatTime(menuitem.startTime)} - ${menuitem.title}`
338 checked: menuitem.index === root.m_mpv.chapter
339 width: listViewPage.width - scrollBarWidth
340 onClicked: {
341 chaptersPopup.close()
342 root.m_mpv.chapter = menuitem.index
343 }
344 onImplicitWidthChanged: {
345 let popup = chaptersPopup
346 popup.itemBiggestWidth = Math.max(implicitWidth + scrollBarWidth, popup.width)
347 popup.itemHeight = height
348 }
349 }
350 }
351 }
352 }
353
354 Connections {
355 target: root.m_mpv
356 function onFileStarted() {
357 loopIndicator.startPosition = -1
358 loopIndicator.endPosition = -1
359 previewMpvLoader.file = root.m_mpv.currentUrl
360 }
361 function onPositionChanged() {
362 if (!slider.seekStarted) {
363 slider.value = root.m_mpv.position
364 }
365 }
366 }
367 }
368 }