Skip to content

Commit

Permalink
Добавил возможность управления воспроизведением с предпросмотра
Browse files Browse the repository at this point in the history
приложения
  • Loading branch information
levovix0 committed Mar 24, 2021
1 parent 06f2211 commit 2eedb86
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 15 deletions.
18 changes: 9 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ endif()
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick LinguistTools Multimedia DBus ${qt_winextras} REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick LinguistTools Multimedia DBus ${qt_winextras} REQUIRED)

set(TS_FILES ${PROJECT_NAME}_ru_RU.ts)
set(TS_FILES russian.ts)

file(GLOB PROJECT_SOURCES "*.cpp")
file(GLOB project_headers "*.hpp")
file(GLOB project_qml "*.qml")
set(PROJECT_SOURCES ${PROJECT_SOURCES} ${TS_FILES})

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${project_headers} qml.qrc)
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})

qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
qt_add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${project_headers} qml.qrc ${QM_FILES})
else()
if(ANDROID)
add_library(${PROJECT_NAME} SHARED ${PROJECT_SOURCES} ${project_headers} qml.qrc)
else()
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${project_headers} qml.qrc)
endif()
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})

qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
if(ANDROID)
add_library(${PROJECT_NAME} SHARED ${PROJECT_SOURCES} ${project_headers} qml.qrc ${QM_FILES})
else()
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${project_headers} qml.qrc ${QM_FILES})
endif()
endif()

target_compile_definitions(${PROJECT_NAME} PRIVATE
Expand Down
4 changes: 0 additions & 4 deletions DMusic_ru_RU.ts

This file was deleted.

62 changes: 61 additions & 1 deletion RemoteMediaController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void Mpris2Player::onTrackChanged(Track* track)

void Mpris2Player::onProgressChanged(qint64 ms)
{
if (labs(ms - _prevPosition) > 100 || ms == 0 || _prevPosition == 0) //TODO: seek minimum by Settings
if (std::abs(ms - _prevPosition) > 100 || ms == 0 || _prevPosition == 0) //TODO: seek minimum by Settings
emit Seeked(ms * 1000);
_prevPosition = ms;
}
Expand Down Expand Up @@ -423,7 +423,67 @@ MediaPlayer* RemoteMediaController::target()

void RemoteMediaController::setTarget(MediaPlayer* player)
{
#ifdef Q_OS_WIN
delete _win;
_win = new ThumbnailController(player, this);
#endif
if (!_isDBusServiceCreated) return;
_mpris2Player = new Mpris2Player(player, this);
_target = player;
}

#ifdef Q_OS_WIN

ThumbnailController::ThumbnailController(MediaPlayer* player, QObject* parent) : QObject(parent), _player(player)
{
_toolbar = new QWinThumbnailToolBar(this);
auto windows = QGuiApplication::allWindows();
_toolbar->setWindow(windows[0]);

_pausePlay = new QWinThumbnailToolButton(_toolbar);
_pausePlay->setEnabled(false);
_pausePlay->setToolTip(tr("Play"));
_pausePlay->setIcon(QIcon(":resources/player/play.svg"));
connect(_pausePlay, &QWinThumbnailToolButton::clicked, _player, &MediaPlayer::pause_or_play);

_next = new QWinThumbnailToolButton(_toolbar);
_next->setEnabled(false);
_next->setToolTip(tr("Next"));
_next->setIcon(QIcon(":resources/player/next.svg"));
connect(_next, &QWinThumbnailToolButton::clicked, _player, &MediaPlayer::next);

_prev = new QWinThumbnailToolButton(_toolbar);
_prev->setEnabled(false);
_prev->setToolTip(tr("Previous"));
_prev->setIcon(QIcon(":resources/player/prev.svg"));
connect(_prev, &QWinThumbnailToolButton::clicked, _player, &MediaPlayer::prev);

_toolbar->addButton(_prev);
_toolbar->addButton(_pausePlay);
_toolbar->addButton(_next);

connect(_player, &MediaPlayer::stateChanged, this, &ThumbnailController::updateToolbar);
}

ThumbnailController::~ThumbnailController()
{

}

void ThumbnailController::updateToolbar()
{
if (_player->state() == QMediaPlayer::PlayingState) {
_pausePlay->setToolTip(tr("Pause"));
_pausePlay->setIcon(QIcon(":resources/player/pause.svg"));
} else {
_pausePlay->setToolTip(tr("Play"));
_pausePlay->setIcon(QIcon(":resources/player/play.svg"));
}

bool enabled = _player->state() != QMediaPlayer::StoppedState;
_pausePlay->setEnabled(enabled);
_next->setEnabled(enabled);
_prev->setEnabled(enabled);
}

#endif
29 changes: 29 additions & 0 deletions RemoteMediaController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <QDBusReply>
#include <QDBusAbstractAdaptor>

#ifdef Q_OS_WIN
#include <QtWinExtras>
#endif


class Mpris2Root : public QDBusAbstractAdaptor
{
Expand Down Expand Up @@ -123,6 +127,28 @@ private slots:
QMap<QString, QVariant> _currentTrackMetadata;
};

#ifdef Q_OS_WIN

class ThumbnailController : public QObject
{
Q_OBJECT
public:
explicit ThumbnailController(MediaPlayer* player, QObject* parent = nullptr);
~ThumbnailController();

private slots:
void updateToolbar();

private:
MediaPlayer* _player;
QWinThumbnailToolBar* _toolbar;
QWinThumbnailToolButton* _next;
QWinThumbnailToolButton* _prev;
QWinThumbnailToolButton* _pausePlay;
};

#endif

class RemoteMediaController : public QObject
{
Q_OBJECT
Expand All @@ -144,6 +170,9 @@ public slots:
Mpris2Root* _mpris2Root;
Mpris2Player* _mpris2Player;
MediaPlayer* _target;
#ifdef Q_OS_WIN
ThumbnailController* _win = nullptr;
#endif
inline static qint64 _serviceDuplicateCount = 1;
};

9 changes: 8 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
#include "file.hpp"
#include "settings.hpp"
#include "mediaplayer.hpp"
#include "Log.hpp"
#include "RemoteMediaController.hpp"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QIcon>
#include <QtWinExtras>
#include <QTranslator>

int main(int argc, char *argv[])
{
Py_Initialize();

QTranslator translator;
translator.load("russian");

QGuiApplication app(argc, argv);
app.installTranslator(&translator);

qmlRegisterType<YClient>("DMusic", 1, 0, "YClient");
qmlRegisterType<YTrack>("DMusic", 1, 0, "YTrack");
qmlRegisterType<YArtist>("DMusic", 1, 0, "YArtist");
Expand Down
1 change: 1 addition & 0 deletions qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
<file>VolumeSlider.qml</file>
<file>FloatingPanel.qml</file>
<file>PopupController.qml</file>
<file>russian.ts</file>
</qresource>
</RCC>
23 changes: 23 additions & 0 deletions russian.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="DMusic_ru_RU">
<context>
<name>ThumbnailController</name>
<message>
<source>Play</source>
<translation>Играть</translation>
</message>
<message>
<source>Next</source>
<translation>Следующий</translation>
</message>
<message>
<source>Previous</source>
<translation>Предыдущий</translation>
</message>
<message>
<source>Pause</source>
<translation>Пауза</translation>
</message>
</context>
</TS>

0 comments on commit 2eedb86

Please sign in to comment.