-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.qml
More file actions
592 lines (521 loc) · 18.1 KB
/
Main.qml
File metadata and controls
592 lines (521 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import QtCore
import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Material
import QtQuick.Dialogs
import QtQuick.Layouts
import QtMultimedia
import gavqml
ApplicationWindow {
id: mainWindow
property bool controlsVisibleAlias: mediaComponent.controlsAreVisible
property bool mediaControlsContainsMouse: false
property bool shouldAutoPlay: false
property url source
property bool isDarkTheme: true
// Dynamic theme switching - overrides qtquickcontrols2.conf at runtime
Material.theme: isDarkTheme ? Material.Dark : Material.Light
function getMediaInfo(fileUrl) {
var path = fileUrl.toString();
// On Windows, fileUrl can start with 'file:///'
if (path.startsWith('file:///')) {
path = path.substring(8);
}
// Strip trailing slashes (e.g. directory URLs)
while (path.endsWith('/'))
path = path.substring(0, path.length - 1);
var name = path.substring(path.lastIndexOf('/') + 1);
// Strip query ('?') and fragment ('#') parts from the file name
var queryIndex = name.indexOf("?");
var fragmentIndex = name.indexOf("#");
var cutIndex = name.length;
if (queryIndex !== -1 && queryIndex < cutIndex)
cutIndex = queryIndex;
if (fragmentIndex !== -1 && fragmentIndex < cutIndex)
cutIndex = fragmentIndex;
name = name.substring(0, cutIndex);
if (!name)
return null;
var extension = name.substring(name.lastIndexOf('.') + 1).toLowerCase();
if (AppConstants.isAudioExtension(extension)) {
return {
"name": name,
"path": fileUrl,
"type": "audio",
"icon": "\ue405"
};
}
// Treat everything else (including unknown formats) as video;
// the media player will report an error if it can't play it.
return {
"name": name,
"path": fileUrl,
"type": "video",
"icon": "\ueb87"
};
}
function exitMiniPlayer() {
if (!miniPlayerWindow.visible)
return;
mediaComponent.mediaPlayer.videoOutput = mediaComponent.videoOutput;
miniPlayerWindow.visible = false;
mainWindow.show();
mainWindow.showNormal();
mainWindow.raise();
mainWindow.requestActivate();
}
height: Screen.height * 0.75
minimumHeight: 480
minimumWidth: 640
title: qsTr("GAV")
visible: true
width: Screen.width * 0.7
Settings {
id: appSettings
property bool isDarkTheme: true
property real volume: AppConstants.defaultVolume
property real playbackRate: 1.0
}
Component.onCompleted: {
isDarkTheme = appSettings.isDarkTheme;
if (mediaComponent.audioOutput)
mediaComponent.audioOutput.volume = appSettings.volume;
if (mediaComponent.mediaPlayer)
mediaComponent.mediaPlayer.playbackRate = appSettings.playbackRate;
}
footer: Loader {
id: mediaControlsComponentLoader
active: mainWindow.visibility !== Window.FullScreen
// Collapse space when inactive
height: active && item ? item.implicitHeight : 0
sourceComponent: active ? mediaControlsComponent : null
// The footer property handles positioning and width
// Let the loaded MediaControls fill the Loader
onLoaded: if (item)
item.anchors.fill = mediaControlsComponentLoader
}
// --- Loader for WINDOWED mode ---
menuBar: Loader {
id: windowedMenuBarLoader
active: mainWindow.visibility !== Window.FullScreen
// Make the Loader span the window width
anchors.left: parent.left
anchors.right: parent.right
// Collapse space when inactive
height: active && item ? item.implicitHeight : 0
sourceComponent: active ? menuBarComponent : null
// Let the loaded MenuBar fill the Loader
onLoaded: if (item)
item.anchors.fill = windowedMenuBarLoader
}
onSourceChanged: {
const s = "" + source;
if (!s) {
console.log("No source provided");
return;
}
const mediaInfo = getMediaInfo(source);
if (!mediaInfo)
return;
playList.append(mediaInfo);
mediaComponent.path = mediaInfo.path;
mainWindow.title = "GAV - " + mediaInfo.name;
playlistComponent.playListView.currentIndex = playList.count - 1;
shouldAutoPlay = true;
}
// --- Reusable MenuBar definition ---
Component {
id: menuBarComponent
MenuBar {
background: Rectangle {
color: Material.background.darker(1.2)
}
Menu {
title: qsTr("File")
Action {
text: qsTr("Open")
onTriggered: fileDialog.open()
}
MenuSeparator {
}
Action {
text: qsTr("Exit")
onTriggered: Qt.quit()
}
}
Menu {
title: qsTr("View")
Action {
text: qsTr("Settings")
onTriggered: settingsDialog.open()
}
}
Menu {
title: qsTr("Help")
Action {
text: qsTr("About")
onTriggered: aboutDialog.open()
}
}
}
}
// --- Loader for FULLSCREEN mode ---
Loader {
id: fullscreenMenuBarLoader
active: mainWindow.visibility === Window.FullScreen
enabled: opacity > 0
height: item ? item.implicitHeight : 0
opacity: !controlsVisibleAlias ? 0 : 1
sourceComponent: menuBarComponent
width: parent.width
y: 0
z: 100
Behavior on opacity {
NumberAnimation {
duration: 300
}
}
}
CustomSnackbar {
id: captureSnackbar
}
CustomSnackbar {
id: collageSnackbar
}
Connections {
function onFrameCaptured(success, path) {
if (success) {
captureSnackbar.message = "Frame captured: " + path;
} else {
captureSnackbar.message = "Error: " + path;
}
captureSnackbar.show();
}
target: mediaComponent.mediaPlayer
}
Connections {
function onCollageFinished(successCount, failCount) {
if (successCount > 0) {
collageSnackbar.message = successCount + " collage(s) created successfully";
} else {
collageSnackbar.message = "Collage creation failed";
}
collageSnackbar.show();
}
target: collage
}
// About dialog
Dialog {
id: aboutDialog
modal: true
standardButtons: Dialog.Ok
x: (parent.width - width) / 2
y: (parent.height - height) / 2
ColumnLayout {
spacing: 10
Image {
Layout.alignment: Qt.AlignHCenter
Layout.preferredHeight: 150
Layout.preferredWidth: 150
fillMode: Image.PreserveAspectFit
smooth: true
source: "qrc:/assets/images/logo-bw.png"
}
Text {
Layout.alignment: Qt.AlignHCenter
color: Material.foreground
font.pixelSize: 18
font.bold: true
text: "GAV Media Player"
}
Text {
Layout.alignment: Qt.AlignHCenter
color: Material.foreground
opacity: 0.7
text: "Version " + Qt.application.version
}
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 1
color: Material.dividerColor
}
Text {
Layout.alignment: Qt.AlignHCenter
color: Material.foreground
text: "A simple audio and video player"
}
Text {
Layout.alignment: Qt.AlignHCenter
color: Material.foreground
opacity: 0.5
font.pixelSize: 12
text: "Built with Qt " + Qt.version + " and FFmpeg"
}
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 1
color: Material.dividerColor
}
ColumnLayout {
Layout.alignment: Qt.AlignHCenter
spacing: 4
Text {
Layout.alignment: Qt.AlignHCenter
color: Material.foreground
opacity: 0.5
font.pixelSize: 11
text: "Keyboard Shortcuts:"
}
Text {
color: Material.foreground
opacity: 0.7
font.pixelSize: 11
text: "Space - Play/Pause"
}
Text {
color: Material.foreground
opacity: 0.7
font.pixelSize: 11
text: "Left/Right - Seek 5 seconds"
}
Text {
color: Material.foreground
opacity: 0.7
font.pixelSize: 11
text: "Scroll - Volume"
}
Text {
color: Material.foreground
opacity: 0.7
font.pixelSize: 11
text: "Ctrl+Scroll - Zoom"
}
Text {
color: Material.foreground
opacity: 0.7
font.pixelSize: 11
text: "Double-click - Fullscreen"
}
}
}
}
// If an error occurs with the video/audio
Dialog {
id: playbackErrorDialog
anchors.centerIn: parent
modal: true
standardButtons: Dialog.Ok
title: "Playback Error"
ColumnLayout {
spacing: 10
Text {
color: Material.foreground
text: "Unable to play this file. The format may not be supported."
wrapMode: Text.WordWrap
Layout.maximumWidth: 400
}
Text {
color: Material.foreground
opacity: 0.7
font.pixelSize: 12
text: "File: " + mediaComponent.path
wrapMode: Text.WordWrap
Layout.maximumWidth: 400
}
Text {
color: Material.foreground
opacity: 0.5
font.pixelSize: 11
visible: mediaComponent.mediaPlayer && mediaComponent.mediaPlayer.errorString !== ""
text: "Error: " + (mediaComponent.mediaPlayer ? mediaComponent.mediaPlayer.errorString : "")
wrapMode: Text.WordWrap
Layout.maximumWidth: 400
}
}
}
Collage {
id: collage
}
SettingsDialog {
id: settingsDialog
audioOutput: mediaComponent.audioOutput
mediaPlayer: mediaComponent.mediaPlayer
isDarkTheme: mainWindow.isDarkTheme
x: (parent.width - width) / 2
y: (parent.height - height) / 2
onThemeToggled: function(isDark) {
mainWindow.isDarkTheme = isDark;
appSettings.isDarkTheme = isDark;
}
onDefaultSpeedChanged: function(speed) {
appSettings.playbackRate = speed;
}
}
Connections {
target: mediaComponent.audioOutput
function onVolumeChanged() {
appSettings.volume = mediaComponent.audioOutput.volume;
}
}
DropArea {
anchors.fill: parent
onDropped: function (drop) {
if (drop.urls && drop.urls.length > 0) {
var firstFileSet = false;
for (var i = 0; i < drop.urls.length; i++) {
var mediaInfo = getMediaInfo(drop.urls[i]);
console.debug("Media info for dropped file:", JSON.stringify(mediaInfo));
if (!mediaInfo)
continue;
playList.append(mediaInfo);
if (!firstFileSet) {
mediaComponent.path = mediaInfo.path;
mainWindow.title = "GAV - " + mediaInfo.name;
playlistComponent.playListView.currentIndex = playList.count - 1;
firstFileSet = true;
}
}
}
}
}
FileDialog {
id: fileDialog
currentFolder: StandardPaths.standardLocations(StandardPaths.DownloadLocation)[0]
nameFilters: ["All files (*)"]
onAccepted: {
var mediaInfo = getMediaInfo(selectedFile);
if (!mediaInfo)
return;
playList.append(mediaInfo);
mediaComponent.path = mediaInfo.path;
mainWindow.title = "GAV - " + mediaInfo.name;
playlistComponent.playListView.currentIndex = playList.count - 1;
}
}
MediaComponent {
id: mediaComponent
anchors.fill: parent
focus: true
path: ""
Shortcut {
sequence: "Space"
onActivated: {
// Play/Pause
if (mediaComponent.mediaPlayer.playbackState === MediaPlayer.PlayingState) {
mediaComponent.mediaPlayer.pause();
} else {
mediaComponent.mediaPlayer.play();
}
}
}
Shortcut {
sequence: "Left"
onActivated: {
// Seek backward
mediaComponent.mediaPlayer.position = Math.max(0, mediaComponent.mediaPlayer.position - AppConstants.seekStep);
}
}
Shortcut {
sequence: "Right"
onActivated: {
// Seek forward
mediaComponent.mediaPlayer.position = Math.min(mediaComponent.mediaPlayer.duration, mediaComponent.mediaPlayer.position + AppConstants.seekStep);
}
}
onMediaLoadedChanged: {
if (mediaLoaded) {
mediaPlayer.playbackRate = appSettings.playbackRate;
if (shouldAutoPlay) {
mediaPlayer.play();
shouldAutoPlay = false;
}
}
}
onStopped: {
mediaComponent.path = "";
mainWindow.title = qsTr("GAV");
}
onFullscreenToggleRequested: {
mainWindow.visibility = mainWindow.visibility === Window.FullScreen ? Window.Windowed : Window.FullScreen;
}
}
ListModel {
id: playList
}
PlayListComponent {
id: playlistComponent
anchors.fill: parent
playList: playList
visible: !mediaComponent.isVideoAndPlaying
onItemSelected: function(path, name) {
mediaComponent.path = path;
mainWindow.title = "GAV - " + name;
mediaComponent.mediaPlayer.play();
}
onPlayRequested: {
if (mediaComponent.path === "" && playlistComponent.playListView.currentIndex !== -1) {
var item = playList.get(playlistComponent.playListView.currentIndex);
mediaComponent.path = item.path;
mainWindow.title = "GAV - " + item.name;
}
mediaComponent.mediaPlayer.play();
}
}
Component {
id: mediaControlsComponent
MediaControlsComponent {
id: controlBar
audioOutput: mediaComponent.audioOutput
implicitHeight: 60
mediaLoaded: mediaComponent.mediaLoaded
miniPlayerActive: miniPlayerWindow.visible
player: mediaComponent.mediaPlayer
playlistCount: playList.count
playlistCurrentIndex: playlistComponent.playListView.currentIndex
videoOutput: mediaComponent.videoOutput
onContainsMouseChanged: mainWindow.mediaControlsContainsMouse = containsMouse
onMiniPlayerRequested: {
var px = mainWindow.x + mainWindow.width - miniPlayerWindow.width - 20;
var py = mainWindow.y + mainWindow.height - miniPlayerWindow.height - 60;
miniPlayerWindow.x = Math.max(0, Math.min(px, Screen.width - miniPlayerWindow.width));
miniPlayerWindow.y = Math.max(0, Math.min(py, Screen.height - miniPlayerWindow.height));
miniPlayerWindow.visible = true;
mediaComponent.mediaPlayer.videoOutput = miniPlayerWindow.miniVideoOutput;
mainWindow.hide();
}
onNextTrack: playlistComponent.playListView.currentIndex++
onPreviousTrack: playlistComponent.playListView.currentIndex--
}
}
MiniPlayerWindow {
id: miniPlayerWindow
audioOutput: mediaComponent.audioOutput
mediaPlayer: mediaComponent.mediaPlayer
onCloseRequested: Qt.quit()
onRestoreRequested: mainWindow.exitMiniPlayer()
}
// --- Loader for FULLSCREEN mode ---
Loader {
id: fullscreenMediaControlsComponentLoader
active: mainWindow.visibility === Window.FullScreen
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
enabled: opacity > 0
height: item ? item.implicitHeight : 0
opacity: controlsVisibleAlias ? 1 : 0
sourceComponent: mediaControlsComponent
z: 100
Behavior on opacity {
NumberAnimation {
duration: 300
}
}
onLoaded: if (item)
item.anchors.fill = fullscreenMediaControlsComponentLoader
}
FontLoader {
id: materialSymbolsOutlined
source: "qrc:/assets/fonts/MaterialSymbolsOutlined.ttf"
}
}