Compare commits
4 Commits
e2dd7c9307
...
f17b722600
Author | SHA1 | Date | |
---|---|---|---|
f17b722600 | |||
9bbfefaea1 | |||
cdadaa874e | |||
c789d04a6c |
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,3 +6,6 @@
|
|||||||
|
|
||||||
# User config file
|
# User config file
|
||||||
CMakeLists.txt.user*
|
CMakeLists.txt.user*
|
||||||
|
|
||||||
|
# Why, macOS, why?
|
||||||
|
.DS_Store
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.12)
|
cmake_minimum_required(VERSION 3.12)
|
||||||
|
|
||||||
project(pineapple-music LANGUAGES CXX)
|
project(pineapple-music LANGUAGES CXX VERSION 0.3.0)
|
||||||
|
|
||||||
include (GNUInstallDirs)
|
include (GNUInstallDirs)
|
||||||
include (FeatureSummary)
|
include (FeatureSummary)
|
||||||
@ -45,6 +45,7 @@ set (PMUSIC_CPP_FILES
|
|||||||
lrcbar.cpp
|
lrcbar.cpp
|
||||||
lyricsmanager.cpp
|
lyricsmanager.cpp
|
||||||
fftspectrum.cpp
|
fftspectrum.cpp
|
||||||
|
playbackprogressindicator.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set (PMUSIC_HEADER_FILES
|
set (PMUSIC_HEADER_FILES
|
||||||
@ -55,6 +56,7 @@ set (PMUSIC_HEADER_FILES
|
|||||||
lrcbar.h
|
lrcbar.h
|
||||||
lyricsmanager.h
|
lyricsmanager.h
|
||||||
fftspectrum.h
|
fftspectrum.h
|
||||||
|
playbackprogressindicator.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set (PMUSIC_UI_FILES
|
set (PMUSIC_UI_FILES
|
||||||
@ -67,7 +69,7 @@ set (EXE_NAME pmusic)
|
|||||||
file (GLOB PMUSIC_TS_FILES languages/*.ts)
|
file (GLOB PMUSIC_TS_FILES languages/*.ts)
|
||||||
set (PMUSIC_CPP_FILES_FOR_I18N ${PMUSIC_CPP_FILES} ${PMUSIC_UI_FILES})
|
set (PMUSIC_CPP_FILES_FOR_I18N ${PMUSIC_CPP_FILES} ${PMUSIC_UI_FILES})
|
||||||
|
|
||||||
add_executable(${EXE_NAME} WIN32
|
add_executable(${EXE_NAME}
|
||||||
${PMUSIC_HEADER_FILES}
|
${PMUSIC_HEADER_FILES}
|
||||||
${PMUSIC_CPP_FILES}
|
${PMUSIC_CPP_FILES}
|
||||||
${PMUSIC_UI_FILES}
|
${PMUSIC_UI_FILES}
|
||||||
@ -103,8 +105,23 @@ endif()
|
|||||||
|
|
||||||
# Install settings
|
# Install settings
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
# FIXME: try to avoid install to a "bin" subfolder under windows...
|
set_target_properties(${EXE_NAME} PROPERTIES
|
||||||
# when fixed, don't forget to update the CI config file...
|
WIN32_EXECUTABLE TRUE
|
||||||
|
)
|
||||||
|
elseif (APPLE)
|
||||||
|
set_source_files_properties(assets/icons/app-icon.icns PROPERTIES
|
||||||
|
MACOSX_PACKAGE_LOCATION "Resources"
|
||||||
|
)
|
||||||
|
target_sources(${EXE_NAME} PUBLIC assets/icons/app-icon.icns)
|
||||||
|
# See https://cmake.org/cmake/help/v3.15/prop_tgt/MACOSX_BUNDLE_INFO_PLIST.html
|
||||||
|
set_target_properties(${EXE_NAME} PROPERTIES
|
||||||
|
MACOSX_BUNDLE TRUE
|
||||||
|
MACOSX_BUNDLE_BUNDLE_NAME "Pineapple Music"
|
||||||
|
MACOSX_BUNDLE_GUI_IDENTIFIER net.blumia.pineapple-music
|
||||||
|
MACOSX_BUNDLE_ICON_FILE app-icon.icns # contains the .icns file name, *without* the path.
|
||||||
|
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
|
||||||
|
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
|
||||||
|
)
|
||||||
elseif (UNIX)
|
elseif (UNIX)
|
||||||
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||||
set(CMAKE_INSTALL_PREFIX /usr)
|
set(CMAKE_INSTALL_PREFIX /usr)
|
||||||
@ -125,6 +142,7 @@ elseif (UNIX)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
set (INSTALL_TARGETS_DEFAULT_ARGS
|
set (INSTALL_TARGETS_DEFAULT_ARGS
|
||||||
|
BUNDLE DESTINATION .
|
||||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Devel
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Devel
|
||||||
|
@ -7,9 +7,12 @@ Since **I** just need a simple player which *just works* right now, so I did man
|
|||||||
We have the following features:
|
We have the following features:
|
||||||
|
|
||||||
- [Sidecar](https://en.wikipedia.org/wiki/Sidecar_file) lyrics file (`.lrc`) support with an optional desktop lyrics bar widget
|
- [Sidecar](https://en.wikipedia.org/wiki/Sidecar_file) lyrics file (`.lrc`) support with an optional desktop lyrics bar widget
|
||||||
|
- Sidecar chapter file support
|
||||||
|
- [YouTube-style chapter](https://support.google.com/youtube/answer/9884579) saved to a plain text file with `.chp` suffix
|
||||||
|
- PotPlayer `.pbf` file, `[Bookmark]`s as chapters
|
||||||
- Auto-load all audio files in the same folder of the file that you attempted to play, into a playlist
|
- Auto-load all audio files in the same folder of the file that you attempted to play, into a playlist
|
||||||
|
|
||||||
But these features are not available, some of them are TBD and others are not planned:
|
These features are not available, some of them are TBD and others are not planned:
|
||||||
|
|
||||||
- File format support will be limited by the [FFmpeg version that Qt 6 uses](https://doc.qt.io/qt-6/qtmultimedia-attribution-ffmpeg.html).
|
- File format support will be limited by the [FFmpeg version that Qt 6 uses](https://doc.qt.io/qt-6/qtmultimedia-attribution-ffmpeg.html).
|
||||||
- ...which if you use Qt's official binary, only contains the LGPLv2.1+ part. (already good enough, tho)
|
- ...which if you use Qt's official binary, only contains the LGPLv2.1+ part. (already good enough, tho)
|
||||||
|
@ -5,8 +5,8 @@ environment:
|
|||||||
PACKAGE_INSTALL_ROOT: C:\projects\pir
|
PACKAGE_INSTALL_ROOT: C:\projects\pir
|
||||||
matrix:
|
matrix:
|
||||||
- build_name: mingw1120_64_qt6_7
|
- build_name: mingw1120_64_qt6_7
|
||||||
QTPATH: C:\Qt\6.7\mingw_64
|
QTPATH: C:\Qt\6.8\mingw_64
|
||||||
MINGW64: C:\Qt\Tools\mingw1120_64
|
MINGW64: C:\Qt\Tools\mingw1310_64
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- mkdir %CMAKE_INSTALL_ROOT%
|
- mkdir %CMAKE_INSTALL_ROOT%
|
||||||
|
BIN
assets/icons/app-icon.icns
Normal file
BIN
assets/icons/app-icon.icns
Normal file
Binary file not shown.
@ -242,6 +242,12 @@ void MainWindow::dropEvent(QDropEvent *e)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fileName.endsWith(".chp") || fileName.endsWith(".pbf")) {
|
||||||
|
QList<std::pair<qint64, QString>> chapters(PlaybackProgressIndicator::tryLoadSidecarChapterFile(fileName));
|
||||||
|
ui->playbackProgressIndicator->setChapters(chapters);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const QModelIndex & modelIndex = m_playlistManager->loadPlaylist(urls);
|
const QModelIndex & modelIndex = m_playlistManager->loadPlaylist(urls);
|
||||||
if (modelIndex.isValid()) {
|
if (modelIndex.isValid()) {
|
||||||
loadByModelIndex(modelIndex);
|
loadByModelIndex(modelIndex);
|
||||||
@ -264,14 +270,21 @@ void MainWindow::loadFile()
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_playlistManager->loadPlaylist(urlList);
|
m_playlistManager->loadPlaylist(urlList);
|
||||||
m_mediaPlayer->setSource(urlList.first());
|
const QUrl & firstUrl = urlList.first();
|
||||||
m_lrcbar->loadLyrics(urlList.first().toLocalFile());
|
const QString firstFilePath = firstUrl.toLocalFile();
|
||||||
|
m_mediaPlayer->setSource(firstUrl);
|
||||||
|
m_lrcbar->loadLyrics(firstFilePath);
|
||||||
|
QList<std::pair<qint64, QString>> chapters(PlaybackProgressIndicator::tryLoadSidecarChapterFile(firstFilePath));
|
||||||
|
ui->playbackProgressIndicator->setChapters(chapters);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::loadByModelIndex(const QModelIndex & index)
|
void MainWindow::loadByModelIndex(const QModelIndex & index)
|
||||||
{
|
{
|
||||||
m_mediaPlayer->setSource(m_playlistManager->urlByIndex(index));
|
m_mediaPlayer->setSource(m_playlistManager->urlByIndex(index));
|
||||||
m_lrcbar->loadLyrics(m_playlistManager->localFileByIndex(index));
|
QString filePath(m_playlistManager->localFileByIndex(index));
|
||||||
|
m_lrcbar->loadLyrics(filePath);
|
||||||
|
QList<std::pair<qint64, QString>> chapters(PlaybackProgressIndicator::tryLoadSidecarChapterFile(filePath));
|
||||||
|
ui->playbackProgressIndicator->setChapters(chapters);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::play()
|
void MainWindow::play()
|
||||||
@ -359,14 +372,6 @@ void MainWindow::on_stopBtn_clicked()
|
|||||||
m_mediaPlayer->stop();
|
m_mediaPlayer->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::on_playbackSlider_valueChanged(int value)
|
|
||||||
{
|
|
||||||
qint64 currPos = m_mediaPlayer->duration() == 0 ? value : m_mediaPlayer->position() * ui->playbackSlider->maximum() / m_mediaPlayer->duration();
|
|
||||||
if (qAbs(currPos - value) > 2) {
|
|
||||||
m_mediaPlayer->setPosition(ui->playbackSlider->value() * 1.0 / ui->playbackSlider->maximum() * m_mediaPlayer->duration());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::on_prevBtn_clicked()
|
void MainWindow::on_prevBtn_clicked()
|
||||||
{
|
{
|
||||||
QModelIndex index(m_playlistManager->previousIndex());
|
QModelIndex index(m_playlistManager->previousIndex());
|
||||||
@ -414,6 +419,10 @@ void MainWindow::initConnections()
|
|||||||
m_audioOutput->setDevice(m_mediaDevices->defaultAudioOutput());
|
m_audioOutput->setDevice(m_mediaDevices->defaultAudioOutput());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
connect(ui->playbackProgressIndicator, &PlaybackProgressIndicator::seekingRequested, this, [=](qint64 pos){
|
||||||
|
m_mediaPlayer->setPosition(pos);
|
||||||
|
});
|
||||||
|
|
||||||
connect(m_mediaPlayer, &QMediaPlayer::sourceChanged, this, [=](){
|
connect(m_mediaPlayer, &QMediaPlayer::sourceChanged, this, [=](){
|
||||||
QUrl fileUrl(m_mediaPlayer->source());
|
QUrl fileUrl(m_mediaPlayer->source());
|
||||||
|
|
||||||
@ -470,7 +479,7 @@ void MainWindow::initConnections()
|
|||||||
connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, [=](qint64 pos) {
|
connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, [=](qint64 pos) {
|
||||||
ui->nowTimeLabel->setText(ms2str(pos));
|
ui->nowTimeLabel->setText(ms2str(pos));
|
||||||
if (m_mediaPlayer->duration() != 0) {
|
if (m_mediaPlayer->duration() != 0) {
|
||||||
ui->playbackSlider->setSliderPosition(ui->playbackSlider->maximum() * pos / m_mediaPlayer->duration());
|
ui->playbackProgressIndicator->setPosition(pos);
|
||||||
}
|
}
|
||||||
m_lrcbar->playbackPositionChanged(pos, m_mediaPlayer->duration());
|
m_lrcbar->playbackPositionChanged(pos, m_mediaPlayer->duration());
|
||||||
});
|
});
|
||||||
@ -484,6 +493,7 @@ void MainWindow::initConnections()
|
|||||||
});
|
});
|
||||||
|
|
||||||
connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, [=](qint64 dua) {
|
connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, [=](qint64 dua) {
|
||||||
|
ui->playbackProgressIndicator->setDuration(dua);
|
||||||
ui->totalTimeLabel->setText(ms2str(dua));
|
ui->totalTimeLabel->setText(ms2str(dua));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -68,7 +68,6 @@ private slots:
|
|||||||
void on_playBtn_clicked();
|
void on_playBtn_clicked();
|
||||||
void on_volumeSlider_valueChanged(int value);
|
void on_volumeSlider_valueChanged(int value);
|
||||||
void on_stopBtn_clicked();
|
void on_stopBtn_clicked();
|
||||||
void on_playbackSlider_valueChanged(int value);
|
|
||||||
void on_prevBtn_clicked();
|
void on_prevBtn_clicked();
|
||||||
void on_nextBtn_clicked();
|
void on_nextBtn_clicked();
|
||||||
void on_volumeBtn_clicked();
|
void on_volumeBtn_clicked();
|
||||||
|
@ -371,14 +371,7 @@ QListView {
|
|||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="SeekableSlider" name="playbackSlider">
|
<widget class="PlaybackProgressIndicator" name="playbackProgressIndicator" native="true"/>
|
||||||
<property name="maximum">
|
|
||||||
<number>1000</number>
|
|
||||||
</property>
|
|
||||||
<property name="orientation">
|
|
||||||
<enum>Qt::Orientation::Horizontal</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="playbackControlLayout">
|
<layout class="QHBoxLayout" name="playbackControlLayout">
|
||||||
@ -694,6 +687,12 @@ QListView {
|
|||||||
<extends>QSlider</extends>
|
<extends>QSlider</extends>
|
||||||
<header>seekableslider.h</header>
|
<header>seekableslider.h</header>
|
||||||
</customwidget>
|
</customwidget>
|
||||||
|
<customwidget>
|
||||||
|
<class>PlaybackProgressIndicator</class>
|
||||||
|
<extends>QWidget</extends>
|
||||||
|
<header>playbackprogressindicator.h</header>
|
||||||
|
<container>1</container>
|
||||||
|
</customwidget>
|
||||||
</customwidgets>
|
</customwidgets>
|
||||||
<resources>
|
<resources>
|
||||||
<include location="resources.qrc"/>
|
<include location="resources.qrc"/>
|
||||||
|
200
playbackprogressindicator.cpp
Normal file
200
playbackprogressindicator.cpp
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2025 Gary Wang <git@blumia.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
#include "playbackprogressindicator.h"
|
||||||
|
|
||||||
|
#include <QDir>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QPainterPath>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
|
||||||
|
PlaybackProgressIndicator::PlaybackProgressIndicator(QWidget *parent) :
|
||||||
|
QWidget(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaybackProgressIndicator::setPosition(qint64 pos)
|
||||||
|
{
|
||||||
|
m_position = pos;
|
||||||
|
emit positionChanged(m_position);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaybackProgressIndicator::setDuration(qint64 dur)
|
||||||
|
{
|
||||||
|
m_duration = dur;
|
||||||
|
emit durationChanged(m_duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaybackProgressIndicator::setChapters(QList<std::pair<qint64, QString> > chapters)
|
||||||
|
{
|
||||||
|
m_chapterModel.clear();
|
||||||
|
for (const std::pair<qint64, QString> & chapter : chapters) {
|
||||||
|
QStandardItem * chapterItem = new QStandardItem(chapter.second);
|
||||||
|
chapterItem->setData(chapter.first, StartTimeMsRole);
|
||||||
|
m_chapterModel.appendRow(chapterItem);
|
||||||
|
}
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<std::pair<qint64, QString> > PlaybackProgressIndicator::tryLoadSidecarChapterFile(const QString &filePath)
|
||||||
|
{
|
||||||
|
if (filePath.endsWith(".chp", Qt::CaseInsensitive)) {
|
||||||
|
return parseCHPChapterFile(filePath);
|
||||||
|
} else if (filePath.endsWith(".pbf", Qt::CaseInsensitive)) {
|
||||||
|
return parsePBFChapterFile(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
QFileInfo fileInfo(filePath);
|
||||||
|
fileInfo.setFile(fileInfo.dir().filePath(fileInfo.completeBaseName() + ".chp"));
|
||||||
|
if (fileInfo.exists()) {
|
||||||
|
return parseCHPChapterFile(fileInfo.absoluteFilePath());
|
||||||
|
}
|
||||||
|
fileInfo.setFile(fileInfo.dir().filePath(fileInfo.completeBaseName() + ".pbf"));
|
||||||
|
if (fileInfo.exists()) {
|
||||||
|
return parsePBFChapterFile(fileInfo.absoluteFilePath());
|
||||||
|
}
|
||||||
|
fileInfo.setFile(filePath + ".chp");
|
||||||
|
if (fileInfo.exists()) {
|
||||||
|
return parseCHPChapterFile(fileInfo.absoluteFilePath());
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<std::pair<qint64, QString> > PlaybackProgressIndicator::parseCHPChapterFile(const QString &filePath)
|
||||||
|
{
|
||||||
|
QList<std::pair<qint64, QString>> chapters;
|
||||||
|
QFile file(filePath);
|
||||||
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
return chapters;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextStream in(&file);
|
||||||
|
QRegularExpression timeRegex(R"((\d{1,2}):(\d{2})(?::(\d{2}))?(?:\.(\d{1,3}))?)");
|
||||||
|
|
||||||
|
while (!in.atEnd()) {
|
||||||
|
QString line = in.readLine().trimmed();
|
||||||
|
QRegularExpressionMatch match = timeRegex.match(line);
|
||||||
|
if (match.hasMatch()) {
|
||||||
|
int hours = match.capturedView(3).isEmpty() ? 0 : match.capturedView(1).toInt();
|
||||||
|
int minutes = match.capturedView(3).isEmpty() ? match.capturedView(1).toInt() : match.capturedView(2).toInt();
|
||||||
|
int seconds = match.capturedView(3).isEmpty() ? match.capturedView(2).toInt() : match.capturedView(3).toInt();
|
||||||
|
int milliseconds = 0;
|
||||||
|
|
||||||
|
QStringView millisecondsStr(match.capturedView(4));
|
||||||
|
if (!millisecondsStr.isEmpty()) {
|
||||||
|
milliseconds = millisecondsStr.toInt() * pow(10, 3 - millisecondsStr.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
qint64 totalMilliseconds = (hours * 3600 + minutes * 60 + seconds) * 1000 + milliseconds;
|
||||||
|
|
||||||
|
QString chapterTitle = line.mid(match.capturedLength()).trimmed();
|
||||||
|
chapters.append(std::make_pair(totalMilliseconds, chapterTitle));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
return chapters;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<std::pair<qint64, QString> > PlaybackProgressIndicator::parsePBFChapterFile(const QString &filePath)
|
||||||
|
{
|
||||||
|
QList<std::pair<qint64, QString>> chapters;
|
||||||
|
QFile file(filePath);
|
||||||
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
return chapters;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextStream in(&file);
|
||||||
|
QRegularExpression chapterRegex(R"(^\d+=(\d+)\*([^*]*)\*.*$)");
|
||||||
|
|
||||||
|
while (!in.atEnd()) {
|
||||||
|
QString line = in.readLine().trimmed();
|
||||||
|
QRegularExpressionMatch match = chapterRegex.match(line);
|
||||||
|
if (match.hasMatch()) {
|
||||||
|
qint64 timestamp = match.captured(1).toLongLong();
|
||||||
|
QString title = match.captured(2).trimmed();
|
||||||
|
chapters.append(std::make_pair(timestamp, title));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
return chapters;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaybackProgressIndicator::paintEvent(QPaintEvent *event)
|
||||||
|
{
|
||||||
|
constexpr int progressBarHeight = 6;
|
||||||
|
constexpr QColor activeColor = QColor(85, 170, 0);
|
||||||
|
const QPointF topLeft(0, height() / 2.0 - progressBarHeight / 2.0);
|
||||||
|
const QSizeF barSize(width(), progressBarHeight);
|
||||||
|
|
||||||
|
const float currentProgress = m_duration <= 0 ? 0 : (m_seekingPosition >= 0 ? m_seekingPosition : m_position) / (float)m_duration;
|
||||||
|
const QSizeF progressSize(width() * currentProgress, progressBarHeight);
|
||||||
|
|
||||||
|
QPainterPath theProgress;
|
||||||
|
theProgress.addRoundedRect(QRectF(topLeft, progressSize), progressBarHeight / 2, progressBarHeight / 2);
|
||||||
|
|
||||||
|
QPainter painter(this);
|
||||||
|
painter.setRenderHint(QPainter::Antialiasing);
|
||||||
|
painter.save();
|
||||||
|
|
||||||
|
// the bar itself
|
||||||
|
painter.setPen(Qt::gray);
|
||||||
|
painter.drawRoundedRect(QRectF(topLeft, barSize), progressBarHeight / 2, progressBarHeight / 2);
|
||||||
|
painter.fillPath(theProgress, activeColor);
|
||||||
|
|
||||||
|
// progress
|
||||||
|
painter.setPen(activeColor);
|
||||||
|
painter.drawPath(theProgress);
|
||||||
|
|
||||||
|
// chapter markers
|
||||||
|
if (m_duration > 0) {
|
||||||
|
painter.setPen(Qt::lightGray);
|
||||||
|
for (int i = 0; i < m_chapterModel.rowCount(); i++) {
|
||||||
|
qint64 chapterStartTime = m_chapterModel.item(i)->data(StartTimeMsRole).toInt();
|
||||||
|
if (chapterStartTime == 0) continue;
|
||||||
|
if (chapterStartTime > m_duration) break;
|
||||||
|
float chapterPercent = chapterStartTime / (float)m_duration;
|
||||||
|
float chapterPosX = width() * chapterPercent;
|
||||||
|
painter.drawLine(topLeft + QPoint(chapterPosX, 0),
|
||||||
|
topLeft + QPoint(chapterPosX, progressBarHeight));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
painter.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaybackProgressIndicator::mousePressEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
if (m_duration > 0) {
|
||||||
|
event->accept();
|
||||||
|
} else {
|
||||||
|
return QWidget::mousePressEvent(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaybackProgressIndicator::mouseMoveEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
if (m_duration > 0) {
|
||||||
|
m_seekingPosition = event->position().x() * m_duration / width();
|
||||||
|
if (m_seekOnMove) {
|
||||||
|
emit seekingRequested(m_seekingPosition);
|
||||||
|
}
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
return QWidget::mouseMoveEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaybackProgressIndicator::mouseReleaseEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
if (m_duration > 0) {
|
||||||
|
int seekingPosition = event->position().x() * m_duration / width();
|
||||||
|
m_seekingPosition = -1;
|
||||||
|
emit seekingRequested(seekingPosition);
|
||||||
|
}
|
||||||
|
update();
|
||||||
|
return QWidget::mouseReleaseEvent(event);
|
||||||
|
}
|
56
playbackprogressindicator.h
Normal file
56
playbackprogressindicator.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2025 Gary Wang <git@blumia.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QMouseEvent>
|
||||||
|
#include <QStandardItemModel>
|
||||||
|
|
||||||
|
class PlaybackProgressIndicator : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(bool seekOnMove MEMBER m_seekOnMove NOTIFY seekOnMoveChanged)
|
||||||
|
Q_PROPERTY(qint64 position MEMBER m_position NOTIFY positionChanged)
|
||||||
|
Q_PROPERTY(qint64 duration MEMBER m_duration NOTIFY durationChanged)
|
||||||
|
public:
|
||||||
|
enum Roles {
|
||||||
|
ChapterTitleRole = Qt::DisplayRole,
|
||||||
|
StartTimeMsRole = Qt::UserRole + 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
explicit PlaybackProgressIndicator(QWidget *parent = nullptr);
|
||||||
|
~PlaybackProgressIndicator() = default;
|
||||||
|
|
||||||
|
void setPosition(qint64 pos);
|
||||||
|
void setDuration(qint64 dur);
|
||||||
|
void setChapters(QList<std::pair<qint64, QString>> chapters);
|
||||||
|
|
||||||
|
static QList<std::pair<qint64, QString>> tryLoadSidecarChapterFile(const QString & filePath);
|
||||||
|
static QList<std::pair<qint64, QString>> parseCHPChapterFile(const QString & filePath);
|
||||||
|
static QList<std::pair<qint64, QString>> parsePBFChapterFile(const QString & filePath);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void seekOnMoveChanged(bool sow);
|
||||||
|
void positionChanged(qint64 newPosition);
|
||||||
|
void durationChanged(qint64 newDuration);
|
||||||
|
void seekingRequested(qint64 position);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void paintEvent(QPaintEvent *event) override;
|
||||||
|
void mousePressEvent(QMouseEvent *event) override;
|
||||||
|
void mouseMoveEvent(QMouseEvent *event) override;
|
||||||
|
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_seekOnMove = true;
|
||||||
|
qint64 m_position = -1;
|
||||||
|
qint64 m_seekingPosition = -1;
|
||||||
|
qint64 m_duration = -1;
|
||||||
|
QStandardItemModel m_chapterModel;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user