From 02750f65db2fe5fe580426840573d15705da43fe Mon Sep 17 00:00:00 2001 From: Kitteh Date: Thu, 8 Nov 2018 12:46:43 +0000 Subject: [PATCH] [Backend] Move even more things to backend. --- src/MpvPlayerBackend.cpp | 57 +++++++++++++++++++++++++++++++--------- src/MpvPlayerBackend.h | 3 +++ src/qml/main.qml | 51 +++++++---------------------------- 3 files changed, 57 insertions(+), 54 deletions(-) diff --git a/src/MpvPlayerBackend.cpp b/src/MpvPlayerBackend.cpp index 57929f4..09ac318 100644 --- a/src/MpvPlayerBackend.cpp +++ b/src/MpvPlayerBackend.cpp @@ -130,7 +130,7 @@ MpvPlayerBackend::MpvPlayerBackend(QQuickItem* parent) mpv_observe_property(mpv, 0, "track-list", MPV_FORMAT_NODE); mpv_observe_property(mpv, 0, "playlist-pos", MPV_FORMAT_DOUBLE); mpv_observe_property(mpv, 0, "volume", MPV_FORMAT_DOUBLE); - mpv_observe_property(mpv, 0, "muted", MPV_FORMAT_DOUBLE); + mpv_observe_property(mpv, 0, "muted", MPV_FORMAT_NONE); mpv_observe_property(mpv, 0, "duration", MPV_FORMAT_DOUBLE); mpv_observe_property(mpv, 0, "media-title", MPV_FORMAT_STRING); mpv_observe_property(mpv, 0, "sub-text", MPV_FORMAT_STRING); @@ -321,7 +321,6 @@ MpvPlayerBackend::on_mpv_events() { while (mpv) { mpv_event* event = mpv_wait_event(mpv, 0); - if (event->event_id == MPV_EVENT_NONE) { break; } @@ -340,6 +339,45 @@ MpvPlayerBackend::updateDurationStringText() getProperty("speed").toString())); } +void +MpvPlayerBackend::updatePrev(const QVariant& val) +{ + if (val.toDouble() != 0) { + findChild("playlistPrevButton")->setProperty("visible", true); + } else { + findChild("playlistPrevButton")->setProperty("visible", false); + } +} + +void +MpvPlayerBackend::updateVolume(const QVariant& val) +{ + if (getProperty("mute").toBool() || (val.toDouble() == 0)) { + findChild("volumeButton") + ->setProperty("iconSource", "qrc:/player/icons/volume-mute.svg"); + } else { + if (val.toDouble() < 25) { + findChild("volumeButton") + ->setProperty("iconSource", "qrc:/player/icons/volume-down.svg"); + } else { + findChild("volumeButton") + ->setProperty("iconSource", "qrc:/player/icons/volume-up.svg"); + } + } +} + +void +MpvPlayerBackend::updatePlayPause(const QVariant& val) +{ + if (val.toBool()) { + findChild("playPauseButton") + ->setProperty("iconSource", "qrc:/player/icons/play.svg"); + } else { + findChild("playPauseButton") + ->setProperty("iconSource", "qrc:/player/icons/pause.svg"); + } +} + void MpvPlayerBackend::handle_mpv_event(mpv_event* event) { @@ -360,15 +398,10 @@ MpvPlayerBackend::handle_mpv_event(mpv_event* event) } else if (strcmp(prop->name, "volume") == 0) { if (prop->format == MPV_FORMAT_DOUBLE) { double volume = *(double*)prop->data; - QMetaObject::invokeMethod( - this, "updateVolume", Q_ARG(QVariant, volume)); + updateVolume(QVariant(volume)); } } else if (strcmp(prop->name, "muted") == 0) { - if (prop->format == MPV_FORMAT_DOUBLE) { - double muted = *(double*)prop->data; - QMetaObject::invokeMethod( - this, "updateMuted", Q_ARG(QVariant, muted)); - } + updateVolume(getProperty("volume")); } else if (strcmp(prop->name, "media-title") == 0) { if (prop->format == MPV_FORMAT_STRING) { char* title = *(char**)prop->data; @@ -388,17 +421,17 @@ MpvPlayerBackend::handle_mpv_event(mpv_event* event) } else if (strcmp(prop->name, "playlist-pos") == 0) { if (prop->format == MPV_FORMAT_DOUBLE) { double pos = *(double*)prop->data; - QMetaObject::invokeMethod(this, "updatePrev", Q_ARG(QVariant, pos)); + updatePrev(QVariant(pos)); } } else if (strcmp(prop->name, "pause") == 0) { - QMetaObject::invokeMethod(this, "updatePlayPause"); + updatePlayPause(getProperty("pause")); } else if (strcmp(prop->name, "tracks-menu") == 0) { QMetaObject::invokeMethod(this, "tracksUpdate"); } break; } case MPV_EVENT_SHUTDOWN: { - exit(0); + qApp->exit(); break; } default: { diff --git a/src/MpvPlayerBackend.h b/src/MpvPlayerBackend.h index f1e9dba..41aa7da 100644 --- a/src/MpvPlayerBackend.h +++ b/src/MpvPlayerBackend.h @@ -39,6 +39,9 @@ public slots: void toggleOnTop(); void updateDurationStringText(); QVariant getTracks() const; + void updatePrev(const QVariant& val); + void updateVolume(const QVariant& val); + void updatePlayPause(const QVariant& val); QVariant getTrack(const QString& track); void setTrack(const QVariant& track, const QVariant& id); diff --git a/src/qml/main.qml b/src/qml/main.qml index 37a3abc..649757b 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -176,44 +176,6 @@ ApplicationWindow { player.command(["seek", skipto, "absolute"]) } - function updatePrev(val) { - if (val != 0) { - playlistPrevButton.visible = true - playlistPrevButton.width = playPauseButton.width - } else { - playlistPrevButton.visible = false - playlistPrevButton.width = 0 - } - } - - function updateVolume(volume) { - var muted = player.getProperty("mute") - - if (muted || volume === 0) { - volumeButton.icon.source = "qrc:/player/icons/volume-mute.svg" - } else { - if (volume < 25) { - volumeButton.icon.source = "qrc:/player/icons/volume-down.svg" - } else { - volumeButton.icon.source = "qrc:/player/icons/volume-up.svg" - } - } - } - - function updateMuted(muted) { - if (muted) { - volumeButton.icon.source = "qrc:/player/icons/volume-mute.svg" - } - } - function updatePlayPause() { - var paused = player.getProperty("pause") - if (paused) { - playPauseButton.icon.source = "qrc:/player/icons/play.svg" - } else { - playPauseButton.icon.source = "qrc:/player/icons/pause.svg" - } - } - function isAnyMenuOpen() { return settingsMenu.visible || fileMenuBarItem.opened || playbackMenuBarItem.opened || viewMenuBarItem.opened @@ -1072,6 +1034,7 @@ ApplicationWindow { Button { id: playlistPrevButton + objectName: "playlistPrevButton" //icon.name: "prev" icon.source: "icons/prev.svg" icon.color: "white" @@ -1079,7 +1042,7 @@ ApplicationWindow { anchors.top: parent.top anchors.bottom: parent.bottom visible: false - width: 0 + width: visible ? playPauseButton.width : 0 onClicked: { player.prevPlaylistItem() } @@ -1091,7 +1054,9 @@ ApplicationWindow { Button { id: playPauseButton //icon.name: "pause" - icon.source: "icons/pause.svg" + objectName: "playPauseButton" + property string iconSource: "icons/pause.svg" + icon.source: iconSource icon.color: "white" display: AbstractButton.IconOnly anchors.top: parent.top @@ -1124,8 +1089,9 @@ ApplicationWindow { Button { id: volumeButton - //icon.name: "volume-up" - icon.source: "icons/volume-up.svg" + objectName: "volumeButton" + property string iconSource: "icons/volume-up.svg" + icon.source: iconSource icon.color: "white" display: AbstractButton.IconOnly anchors.top: parent.top @@ -1133,6 +1099,7 @@ ApplicationWindow { anchors.left: playlistNextButton.right onClicked: { player.toggleMute() + player.updateVolume(player.getProperty("volume")) } background: Rectangle { color: "transparent"