Compare commits

..

No commits in common. "f17b722600c17540e22c918aa901f899624d4204" and "e2dd7c93071c8f5a633ecff3d126d5a504e5ae15" have entirely different histories.

10 changed files with 154 additions and 442 deletions

3
.gitignore vendored
View File

@ -6,6 +6,3 @@
# User config file # User config file
CMakeLists.txt.user* CMakeLists.txt.user*
# Why, macOS, why?
.DS_Store

View File

@ -1,156 +1,138 @@
cmake_minimum_required(VERSION 3.12) cmake_minimum_required(VERSION 3.12)
project(pineapple-music LANGUAGES CXX VERSION 0.3.0) project(pineapple-music LANGUAGES CXX)
include (GNUInstallDirs) include (GNUInstallDirs)
include (FeatureSummary) include (FeatureSummary)
include (FetchContent) include (FetchContent)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(USE_QTEXTCODEC "Use QTextCodec instead of QStringConverter, in case Qt is not built with ICU" OFF) option(USE_QTEXTCODEC "Use QTextCodec instead of QStringConverter, in case Qt is not built with ICU" OFF)
find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED) find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED)
find_package(TagLib 2.0.0) find_package(TagLib 2.0.0)
find_package(KF6Codecs 6.1.0) find_package(KF6Codecs 6.1.0)
FetchContent_Declare( FetchContent_Declare(
kissfft kissfft
GIT_REPOSITORY https://github.com/mborgerding/kissfft.git GIT_REPOSITORY https://github.com/mborgerding/kissfft.git
GIT_TAG f5f2a3b2f2cd02bf80639adb12cbeed125bdf420 GIT_TAG f5f2a3b2f2cd02bf80639adb12cbeed125bdf420
) )
set(KISSFFT_PKGCONFIG OFF CACHE BOOL "dep(kissfft): pkgconfig support") set(KISSFFT_PKGCONFIG OFF CACHE BOOL "dep(kissfft): pkgconfig support")
set(KISSFFT_STATIC ON CACHE BOOL "dep(kissfft): static linking") set(KISSFFT_STATIC ON CACHE BOOL "dep(kissfft): static linking")
set(KISSFFT_TEST OFF CACHE BOOL "dep(kissfft): enable testing") set(KISSFFT_TEST OFF CACHE BOOL "dep(kissfft): enable testing")
set(KISSFFT_TOOLS OFF CACHE BOOL "dep(kissfft): build tools") set(KISSFFT_TOOLS OFF CACHE BOOL "dep(kissfft): build tools")
FetchContent_MakeAvailable(kissfft) FetchContent_MakeAvailable(kissfft)
if (USE_QTEXTCODEC) if (USE_QTEXTCODEC)
find_package(Qt6 6.6 COMPONENTS Core5Compat REQUIRED) find_package(Qt6 6.6 COMPONENTS Core5Compat REQUIRED)
endif() endif()
set (PMUSIC_CPP_FILES set (PMUSIC_CPP_FILES
main.cpp main.cpp
mainwindow.cpp mainwindow.cpp
seekableslider.cpp seekableslider.cpp
playlistmanager.cpp playlistmanager.cpp
singleapplicationmanager.cpp singleapplicationmanager.cpp
lrcbar.cpp lrcbar.cpp
lyricsmanager.cpp lyricsmanager.cpp
fftspectrum.cpp fftspectrum.cpp
playbackprogressindicator.cpp )
)
set (PMUSIC_HEADER_FILES
set (PMUSIC_HEADER_FILES mainwindow.h
mainwindow.h seekableslider.h
seekableslider.h playlistmanager.h
playlistmanager.h singleapplicationmanager.h
singleapplicationmanager.h lrcbar.h
lrcbar.h
lyricsmanager.h lyricsmanager.h
fftspectrum.h fftspectrum.h
playbackprogressindicator.h )
)
set (PMUSIC_UI_FILES
set (PMUSIC_UI_FILES mainwindow.ui
mainwindow.ui )
)
set (EXE_NAME pmusic)
set (EXE_NAME pmusic)
# Translation
# Translation 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} resources.qrc
resources.qrc )
)
qt_add_translations(${EXE_NAME}
qt_add_translations(${EXE_NAME} TS_FILES
TS_FILES ${PMUSIC_TS_FILES}
${PMUSIC_TS_FILES} )
)
if (WIN32)
if (WIN32) target_sources(${EXE_NAME} PRIVATE assets/pineapple-music.rc)
target_sources(${EXE_NAME} PRIVATE assets/pineapple-music.rc) endif ()
endif ()
if (NOT TagLib_FOUND)
if (NOT TagLib_FOUND) target_compile_definitions(${EXE_NAME} PRIVATE NO_TAGLIB=1)
target_compile_definitions(${EXE_NAME} PRIVATE NO_TAGLIB=1) else ()
else () target_link_libraries(${EXE_NAME} PRIVATE TagLib::tag)
target_link_libraries(${EXE_NAME} PRIVATE TagLib::tag) endif ()
endif ()
if (TARGET KF6::Codecs)
if (TARGET KF6::Codecs) target_compile_definitions(${EXE_NAME} PRIVATE HAVE_KCODECS=1)
target_compile_definitions(${EXE_NAME} PRIVATE HAVE_KCODECS=1) target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs)
target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs) endif ()
endif ()
target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network kissfft::kissfft)
target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network kissfft::kissfft)
if (USE_QTEXTCODEC)
if (USE_QTEXTCODEC) target_compile_definitions(${EXE_NAME} PRIVATE USE_QTEXTCODEC=1)
target_compile_definitions(${EXE_NAME} PRIVATE USE_QTEXTCODEC=1) target_link_libraries(${EXE_NAME} PRIVATE Qt6::Core5Compat)
target_link_libraries(${EXE_NAME} PRIVATE Qt6::Core5Compat) endif()
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 (UNIX)
) if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
elseif (APPLE) set(CMAKE_INSTALL_PREFIX /usr)
set_source_files_properties(assets/icons/app-icon.icns PROPERTIES endif ()
MACOSX_PACKAGE_LOCATION "Resources"
) # install icon
target_sources(${EXE_NAME} PUBLIC assets/icons/app-icon.icns) install(
# See https://cmake.org/cmake/help/v3.15/prop_tgt/MACOSX_BUNDLE_INFO_PLIST.html FILES icons/app-icon.svg
set_target_properties(${EXE_NAME} PROPERTIES DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/48x48/apps"
MACOSX_BUNDLE TRUE RENAME net.blumia.pineapple-music.svg
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. # install shortcut
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} install(
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} FILES dist/net.blumia.pineapple-music.desktop
) DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications"
elseif (UNIX) )
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) endif()
set(CMAKE_INSTALL_PREFIX /usr)
endif () set (INSTALL_TARGETS_DEFAULT_ARGS
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
# install icon LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
install( ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Devel
FILES icons/app-icon.svg )
DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/48x48/apps"
RENAME net.blumia.pineapple-music.svg install (
) TARGETS ${EXE_NAME}
${INSTALL_TARGETS_DEFAULT_ARGS}
# install shortcut )
install(
FILES dist/net.blumia.pineapple-music.desktop feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications"
)
endif()
set (INSTALL_TARGETS_DEFAULT_ARGS
BUNDLE DESTINATION .
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Devel
)
install (
TARGETS ${EXE_NAME}
${INSTALL_TARGETS_DEFAULT_ARGS}
)
feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)

View File

@ -7,12 +7,9 @@ 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
These features are not available, some of them are TBD and others are not planned: But 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)

View File

@ -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.8\mingw_64 QTPATH: C:\Qt\6.7\mingw_64
MINGW64: C:\Qt\Tools\mingw1310_64 MINGW64: C:\Qt\Tools\mingw1120_64
install: install:
- mkdir %CMAKE_INSTALL_ROOT% - mkdir %CMAKE_INSTALL_ROOT%

Binary file not shown.

View File

@ -242,12 +242,6 @@ 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);
@ -270,21 +264,14 @@ void MainWindow::loadFile()
} }
m_playlistManager->loadPlaylist(urlList); m_playlistManager->loadPlaylist(urlList);
const QUrl & firstUrl = urlList.first(); m_mediaPlayer->setSource(urlList.first());
const QString firstFilePath = firstUrl.toLocalFile(); m_lrcbar->loadLyrics(urlList.first().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));
QString filePath(m_playlistManager->localFileByIndex(index)); m_lrcbar->loadLyrics(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()
@ -372,6 +359,14 @@ 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());
@ -419,10 +414,6 @@ 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());
@ -479,7 +470,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->playbackProgressIndicator->setPosition(pos); ui->playbackSlider->setSliderPosition(ui->playbackSlider->maximum() * pos / m_mediaPlayer->duration());
} }
m_lrcbar->playbackPositionChanged(pos, m_mediaPlayer->duration()); m_lrcbar->playbackPositionChanged(pos, m_mediaPlayer->duration());
}); });
@ -493,7 +484,6 @@ 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));
}); });

View File

@ -68,6 +68,7 @@ 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();

View File

@ -371,7 +371,14 @@ QListView {
</layout> </layout>
</item> </item>
<item> <item>
<widget class="PlaybackProgressIndicator" name="playbackProgressIndicator" native="true"/> <widget class="SeekableSlider" name="playbackSlider">
<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">
@ -687,12 +694,6 @@ 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"/>

View File

@ -1,200 +0,0 @@
// 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);
}

View File

@ -1,56 +0,0 @@
// 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;
};