1
0
Fork 0

[UI+Backend] Moved to a Enum based command API for backend commands.

This commit is contained in:
NamedKitten 2018-11-17 19:33:32 +00:00
parent 8738be39f1
commit 32f1007fd5
9 changed files with 247 additions and 221 deletions

View file

@ -239,10 +239,32 @@ MpvPlayerBackend::updateDiscord()
Discord_UpdatePresence(&discordPresence);
}
#endif
QVariant
MpvPlayerBackend::playerCommand(const Enums::Commands& cmd)
{
return playerCommand(cmd, QVariant("NoArgProvided"));
}
QVariant
MpvPlayerBackend::getaudioDevices() const
MpvPlayerBackend::playerCommand(const Enums::Commands& cmd,
const QVariant& args)
{
switch (cmd) {
case Enums::Commands::TogglePlayPause: {
command(QVariantList() << "cycle"
<< "pause");
break;
}
case Enums::Commands::ToggleMute: {
command(QVariantList() << "cycle"
<< "mute");
break;
}
case Enums::Commands::SetAudioDevice: {
setProperty("audio-device", args.toString());
break;
}
case Enums::Commands::GetAudioDevices: {
QVariant drivers = getProperty("audio-device-list");
QVariant currentDevice = getProperty("audio-device");
@ -260,115 +282,145 @@ MpvPlayerBackend::getaudioDevices() const
pulseItem["selected"] = currentDevice == "pulse";
newDrivers[pulseItem["description"].toString()] = pulseItem;
return QVariant(newDrivers);
}
}
case Enums::Commands::SetVolume: {
command(QVariantList() << "set"
<< "volume" << args);
break;
}
void
MpvPlayerBackend::setAudioDevice(const QString& name)
{
setProperty("audio-device", name);
}
case Enums::Commands::AddVolume: {
void
MpvPlayerBackend::togglePlayPause()
{
command(QVariantList() << "cycle"
<< "pause");
}
command(QVariantList() << "add"
<< "volume" << args);
break;
}
void
MpvPlayerBackend::toggleMute()
{
command(QVariantList() << "cycle"
<< "mute");
}
case Enums::Commands::AddSpeed: {
QString speedString =
QString::number(getProperty("speed").toDouble() + args.toDouble());
QVariant newSpeed =
QVariant(speedString.left(speedString.lastIndexOf('.') + 2));
playerCommand(Enums::Commands::SetSpeed, newSpeed);
break;
}
case Enums::Commands::SubtractSpeed: {
QString speedString =
QString::number(getProperty("speed").toDouble() - args.toDouble());
QVariant newSpeed =
QVariant(speedString.left(speedString.lastIndexOf('.') + 2));
playerCommand(Enums::Commands::SetSpeed, newSpeed);
break;
}
case Enums::Commands::ChangeSpeed: {
playerCommand(
Enums::Commands::SetSpeed,
QVariant(getProperty("speed").toDouble() * args.toDouble()));
break;
}
case Enums::Commands::SetSpeed: {
command(QVariantList() << "set"
<< "speed" << args.toString());
break;
}
case Enums::Commands::ToggleStats: {
command(QVariantList() << "script-binding"
<< "stats/display-stats-toggle");
break;
}
case Enums::Commands::NextAudioTrack: {
void
MpvPlayerBackend::nextAudioTrack()
{
command(QVariantList() << "cycle"
<< "audio");
}
void
MpvPlayerBackend::nextSubtitleTrack()
{
break;
}
case Enums::Commands::NextSubtitleTrack: {
command(QVariantList() << "cycle"
<< "sub");
}
void
MpvPlayerBackend::nextVideoTrack()
{
break;
}
case Enums::Commands::NextVideoTrack: {
command(QVariantList() << "cycle"
<< "video");
}
break;
}
case Enums::Commands::PreviousPlaylistItem: {
void
MpvPlayerBackend::prevPlaylistItem()
{
command(QVariantList() << "playlist-prev");
}
void
MpvPlayerBackend::nextPlaylistItem()
{
break;
}
case Enums::Commands::NextPlaylistItem: {
command(QVariantList() << "playlist-next"
<< "force");
}
break;
}
case Enums::Commands::LoadFile: {
command(QVariantList() << "loadfile" << args);
void
MpvPlayerBackend::loadFile(const QVariant& filename)
{
command(QVariantList() << "loadfile" << filename);
}
break;
}
case Enums::Commands::AppendFile: {
void
MpvPlayerBackend::appendFile(const QVariant& filename)
{
command(QVariantList() << "loadfile" << filename << "append-play");
}
command(QVariantList() << "loadfile" << args << "append-play");
break;
}
case Enums::Commands::Seek: {
void
MpvPlayerBackend::setVolume(const QVariant& volume)
{
command(QVariantList() << "set"
<< "volume" << volume);
}
command(QVariantList() << "seek" << args);
void
MpvPlayerBackend::addVolume(const QVariant& volume)
{
command(QVariantList() << "add"
<< "volume" << volume);
}
break;
}
case Enums::Commands::SeekAbsolute: {
void
MpvPlayerBackend::seekAbsolute(const QVariant& seekTime)
{
command(QVariantList() << "seek" << seekTime << "absolute");
}
command(QVariantList() << "seek" << args << "absolute");
void
MpvPlayerBackend::seek(const QVariant& seekTime)
{
command(QVariantList() << "seek" << seekTime);
}
break;
}
case Enums::Commands::GetTracks: {
QVariant
MpvPlayerBackend::getTracks() const
{
return mpv::qt::get_property_variant(mpv, "track-list");
}
void
MpvPlayerBackend::setTrack(const QVariant& track, const QVariant& id)
{
command(QVariantList() << "set" << track << id);
}
break;
}
case Enums::Commands::ForwardFrame: {
QVariant
MpvPlayerBackend::getTrack(const QString& track)
{
return mpv::qt::get_property_variant(mpv, track);
command(QVariantList() << "frame-step");
break;
}
case Enums::Commands::BackwardFrame: {
command(QVariantList() << "frame-back-step");
break;
}
case Enums::Commands::SetTrack: {
command(QVariantList() << "set" << args.toList()[0] << args.toList()[1]);
break;
}
default: {
qDebug() << "Command not found: " << cmd;
break;
}
}
return QVariant("NoOutput");
}
QVariant
@ -394,44 +446,6 @@ MpvPlayerBackend::toggleOnTop()
AlwaysOnTop(window()->winId(), onTop);
}
void
MpvPlayerBackend::toggleStats()
{
command(QVariantList() << "script-binding"
<< "stats/display-stats-toggle");
}
void
MpvPlayerBackend::addSpeed(const QVariant& speed)
{
QString speedString =
QString::number(getProperty("speed").toDouble() + speed.toDouble());
speedString = speedString.left(speedString.lastIndexOf('.') + 2);
setSpeed(QVariant(speedString));
}
void
MpvPlayerBackend::subtractSpeed(const QVariant& speed)
{
QString speedString =
QString::number(getProperty("speed").toDouble() - speed.toDouble());
speedString = speedString.left(speedString.lastIndexOf('.') + 2);
setSpeed(QVariant(speedString));
}
void
MpvPlayerBackend::changeSpeed(const QVariant& speedFactor)
{
setSpeed(QVariant(getProperty("speed").toDouble() * speedFactor.toDouble()));
}
void
MpvPlayerBackend::setSpeed(const QVariant& speed)
{
command(QVariantList() << "set"
<< "speed" << speed.toString());
}
void
MpvPlayerBackend::on_mpv_events()
{

View file

@ -30,43 +30,23 @@ public:
virtual Renderer* createRenderer() const;
public slots:
// All 4 required for Player API
QVariant playerCommand(const Enums::Commands& command, const QVariant& args);
QVariant playerCommand(const Enums::Commands& command);
void launchAboutQt();
#ifdef DISCORD
void updateDiscord();
#endif
void togglePlayPause();
void toggleMute();
void nextAudioTrack();
void nextVideoTrack();
void nextSubtitleTrack();
void prevPlaylistItem();
void nextPlaylistItem();
void toggleOnTop();
void toggleStats();
QVariant getTracks() const;
QVariant getaudioDevices() const;
void setAudioDevice(const QString& name);
void addSpeed(const QVariant& speed);
void subtractSpeed(const QVariant& speed);
void changeSpeed(const QVariant& speedFactor);
void setSpeed(const QVariant& speed);
QVariant getTrack(const QString& track);
void setTrack(const QVariant& track, const QVariant& id);
void setVolume(const QVariant& volume);
void addVolume(const QVariant& volume);
void loadFile(const QVariant& filename);
void appendFile(const QVariant& filename);
void seek(const QVariant& seekTime);
void seekAbsolute(const QVariant& seekTime);
// Optional but handy for MPV or custom backend settings.
void command(const QVariant& params);
void setProperty(const QString& name, const QVariant& value);
void setOption(const QString& name, const QVariant& value);
QVariant getProperty(const QString& name) const;
// Misc function.
QVariant createTimestamp(const QVariant& seconds) const;
signals:
void onUpdate();
void mpv_events();
// All below required for Player API
void playStatusChanged(const Enums::PlayStatus& status);
void volumeStatusChanged(const Enums::VolumeStatus& status);
void volumeChanged(const int& volume);
@ -87,6 +67,9 @@ private slots:
private:
void handle_mpv_event(mpv_event* event);
#ifdef DISCORD
void updateDiscord();
#endif
};
#endif

View file

@ -19,6 +19,34 @@ enum class VolumeStatus : int
Normal = 2
};
Q_ENUM_NS(VolumeStatus)
enum class Commands : int
{
TogglePlayPause = 0,
ToggleMute = 1,
SetAudioDevice = 2,
GetAudioDevices = 3,
AddVolume = 4,
SetVolume = 5,
AddSpeed = 6,
SubtractSpeed = 7,
ChangeSpeed = 8,
SetSpeed = 9,
ToggleStats = 10,
NextAudioTrack = 11,
NextVideoTrack = 12,
NextSubtitleTrack = 13,
PreviousPlaylistItem = 14,
NextPlaylistItem = 15,
LoadFile = 16,
AppendFile = 17,
Seek = 18,
SeekAbsolute = 19,
GetTracks = 20,
ForwardFrame = 21,
BackwardFrame = 22,
SetTrack = 23
};
Q_ENUM_NS(Commands)
}
#endif

View file

@ -122,6 +122,7 @@ main(int argc, char* argv[])
);
qRegisterMetaType<Enums::PlayStatus>("Enums.PlayStatus");
qRegisterMetaType<Enums::VolumeStatus>("Enums.VolumeStatus");
qRegisterMetaType<Enums::Commands>("Enums.Commands");
qmlRegisterType<MpvPlayerBackend>("player", 1, 0, "PlayerBackend");
std::setlocale(LC_NUMERIC, "C");

View file

@ -1,6 +1,7 @@
import QtQuick 2.11
import QtQuick.Controls 2.4
import Qt.labs.settings 1.0
import player 1.0
Action {
id: audioDeviceItem
@ -9,6 +10,6 @@ Action {
checked: false
onTriggered: {
player.setAudioDevice(deviceID)
player.playerCommand(Enums.Commands.SetAudioDevice, deviceID)
}
}

View file

@ -9,7 +9,6 @@ import player 1.0
Item {
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
@ -129,7 +128,7 @@ Item {
})
}
onMoved: {
player.seekAbsolute(progressBar.value)
player.playerCommand(Enums.Commands.SeekAbsolute, value)
}
function getProgressBarHeight(nyan, isMouse) {
@ -227,7 +226,7 @@ Item {
visible: false
width: visible ? playPauseButton.width : 0
onClicked: {
player.prevPlaylistItem()
player.playerCommand(Enums.Commands.PreviousPlaylistItem)
}
background: Rectangle {
color: "transparent"
@ -252,7 +251,7 @@ Item {
anchors.bottom: parent.bottom
anchors.left: playlistPrevButton.right
onClicked: {
player.togglePlayPause()
player.playerCommand(Enums.Commands.TogglePlayPause)
}
background: Rectangle {
color: "transparent"
@ -278,7 +277,7 @@ Item {
anchors.bottom: parent.bottom
anchors.left: playPauseButton.right
onClicked: {
player.nextPlaylistItem()
player.playerCommand(Enums.Commands.NextPlaylistItem)
}
background: Rectangle {
color: "transparent"
@ -295,7 +294,7 @@ Item {
anchors.bottom: parent.bottom
anchors.left: playlistNextButton.right
onClicked: {
player.toggleMute()
player.playerCommand(Enums.Commands.ToggleMute)
}
background: Rectangle {
color: "transparent"
@ -331,7 +330,7 @@ Item {
anchors.top: parent.top
anchors.bottom: parent.bottom
onMoved: {
player.setVolume(Math.round(volumeBar.value).toString())
player.playerCommand(Enums.Commands.SetVolume, Math.round(volumeBar.value).toString())
}
Component.onCompleted: {
player.volumeChanged.connect(function(volume) {

View file

@ -44,7 +44,7 @@ MenuBar {
subMenu.removeAction(subAction)
}
}
var newTracks = player.getTracks()
var newTracks = player.playerCommand(Enums.Commands.GetTracks)
for (var i = 0, len = newTracks.length; i < len; i++) {
var track = newTracks[i]
@ -111,7 +111,7 @@ MenuBar {
title: translate.getTranslation("OPEN_FILE", i18n.language)
nameFilters: ["All files (*)"]
onAccepted: {
player.loadFile(String(fileDialog.file))
player.playerCommand(Enums.Commands.LoadFile, String(fileDialog.file))
fileDialog.close()
}
onRejected: {
@ -124,7 +124,7 @@ MenuBar {
title: translate.getTranslation("URL_FILE_PATH", i18n.language)
standardButtons: StandardButton.Cancel | StandardButton.Open
onAccepted: {
player.loadFile(pathText.text)
player.playerCommand(Enums.Commands.LoadFile, pathText.text)
pathText.text = ""
}
TextField {
@ -225,35 +225,35 @@ MenuBar {
Action {
text: translate.getTranslation("PLAY_PAUSE", i18n.language)
onTriggered: {
player.togglePlayPause()
player.playerCommand(Enums.Commands.TogglePlayPause)
}
shortcut: String(keybinds.playPause)
}
Action {
text: translate.getTranslation("REWIND_10S", i18n.language)
onTriggered: {
player.seek("-10")
player.playerCommand(Enums.Commands.Seek, "-10")
}
shortcut: keybinds.rewind10
}
Action {
text: translate.getTranslation("FORWARD_10S", i18n.language)
onTriggered: {
player.seek("10")
player.playerCommand(Enums.Commands.Seek, "10")
}
shortcut: keybinds.forward10
}
Action {
text: translate.getTranslation("FORWARD_5S", i18n.language)
onTriggered: {
player.seek("-5")
player.playerCommand(Enums.Commands.Seek, "-5")
}
shortcut: keybinds.rewind5
}
Action {
text: translate.getTranslation("FORWARD_5S", i18n.language)
onTriggered: {
player.seek("5")
player.playerCommand(Enums.Commands.Seek, "5")
}
shortcut: keybinds.forward5
}
@ -261,7 +261,7 @@ MenuBar {
text: translate.getTranslation("SPEED_DECREASE_POINT_ONE",
i18n.language)
onTriggered: {
player.subtractSpeed(0.1)
player.playerCommand(Enums.Commands.SubtractSpeed, 0.1)
}
shortcut: keybinds.decreaseSpeedByPointOne
}
@ -269,35 +269,35 @@ MenuBar {
text: translate.getTranslation("SPEED_INCREASE_POINT_ONE",
i18n.language)
onTriggered: {
player.addSpeed(0.1)
player.playerCommand(Enums.Commands.AddSpeed, 0.1)
}
shortcut: keybinds.increaseSpeedByPointOne
}
Action {
text: translate.getTranslation("HALVE_SPEED", i18n.language)
onTriggered: {
player.changeSpeed(0.5)
player.playerCommand(Enums.Commands.ChangeSpeed, 0.5)
}
shortcut: keybinds.halveSpeed
}
Action {
text: translate.getTranslation("DOUBLE_SPEED", i18n.language)
onTriggered: {
player.changeSpeed(2)
player.playerCommand(Enums.Commands.ChangeSpeed, 2)
}
shortcut: keybinds.doubleSpeed
}
Action {
text: translate.getTranslation("FORWARD_FRAME", i18n.language)
onTriggered: {
player.command(["frame-step"])
player.playerCommand(Enums.Commands.ForwardFrame)
}
shortcut: keybinds.forwardFrame
}
Action {
text: translate.getTranslation("BACKWARD_FRAME", i18n.language)
onTriggered: {
player.command(["frame-back-step"])
player.playerCommand(Enums.Commands.BackwardFrame)
}
shortcut: keybinds.backwardFrame
}
@ -309,28 +309,28 @@ MenuBar {
Action {
text: translate.getTranslation("CYCLE_AUDIO_TRACK", i18n.language)
onTriggered: {
player.nextAudioTrack()
player.playerCommand(Enums.Commands.NextAudioTrack)
}
shortcut: keybinds.cycleAudio
}
Action {
text: translate.getTranslation("INCREASE_VOLUME", i18n.language)
onTriggered: {
player.addVolume("2")
player.playerCommand(Enums.Commands.AddVolume, "2")
}
shortcut: keybinds.increaseVolume
}
Action {
text: translate.getTranslation("DECREASE_VOLUME", i18n.language)
onTriggered: {
player.addVolume("-2")
player.playerCommand(Enums.Commands.AddVolume, "-2")
}
shortcut: keybinds.decreaseVolume
}
Action {
text: translate.getTranslation("MUTE_VOLUME", i18n.language)
onTriggered: {
player.toggleMute()
player.playerCommand(Enums.Commands.ToggleMute)
}
shortcut: keybinds.mute
}
@ -347,7 +347,7 @@ MenuBar {
player.audioDevicesChanged.connect(updateAudioDevices)
}
function updateAudioDevices() {
var audioDevices = player.getaudioDevices()
var audioDevices = player.playerCommand(Enums.Commands.GetAudioDevices)
for (var i = 0, len = audioDeviceMenu.count; i < len; i++) {
audioDeviceMenu.takeAction(0)
@ -468,7 +468,7 @@ MenuBar {
Action {
text: translate.getTranslation("STATS", i18n.language)
onTriggered: {
player.toggleStats()
player.playerCommand(Enums.Commands.ToggleStats)
}
shortcut: keybinds.statsForNerds
}

View file

@ -1,6 +1,7 @@
import QtQuick 2.11
import QtQuick.Controls 2.4
import Qt.labs.settings 1.0
import player 1.0
Action {
id: trackItem
@ -10,7 +11,6 @@ Action {
checked: false
onTriggered: {
checked = player.getTrack(trackType)
player.setTrack(trackType, trackID)
player.playerCommand(Enums.Commands.SetTrack, [trackType, trackID])
}
}

View file

@ -115,7 +115,7 @@ ApplicationWindow {
}
} else {
player.appendFile(argument)
player.playerCommand(Enums.Commands.AppendFile, argument)
}
}
}
@ -126,7 +126,7 @@ ApplicationWindow {
if (val != 0) {
skipto = Math.floor(controlsBar.controls.progress.to / 9 * val)
}
player.seekAbsolute(skipto)
player.playerCommand(Enums.Commands.SeekAbsolute, skipto)
}
function isAnyMenuOpen() {
@ -190,7 +190,7 @@ ApplicationWindow {
cursorShape: controlsBar.visible ? Qt.ArrowCursor : Qt.BlankCursor
onClicked: {
if (appearance.clickToPause) {
player.togglePlayPause()
player.playerCommand(Enums.Commands.TogglePlayPause)
}
}
Timer {