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
|
||||
CMakeLists.txt.user*
|
||||
|
||||
# Why, macOS, why?
|
||||
.DS_Store
|
||||
|
278
CMakeLists.txt
278
CMakeLists.txt
@ -1,138 +1,156 @@
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
|
||||
project(pineapple-music LANGUAGES CXX)
|
||||
|
||||
include (GNUInstallDirs)
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
|
||||
project(pineapple-music LANGUAGES CXX VERSION 0.3.0)
|
||||
|
||||
include (GNUInstallDirs)
|
||||
include (FeatureSummary)
|
||||
include (FetchContent)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
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(TagLib 2.0.0)
|
||||
include (FetchContent)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
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(TagLib 2.0.0)
|
||||
find_package(KF6Codecs 6.1.0)
|
||||
|
||||
FetchContent_Declare(
|
||||
kissfft
|
||||
GIT_REPOSITORY https://github.com/mborgerding/kissfft.git
|
||||
GIT_TAG f5f2a3b2f2cd02bf80639adb12cbeed125bdf420
|
||||
|
||||
FetchContent_Declare(
|
||||
kissfft
|
||||
GIT_REPOSITORY https://github.com/mborgerding/kissfft.git
|
||||
GIT_TAG f5f2a3b2f2cd02bf80639adb12cbeed125bdf420
|
||||
)
|
||||
set(KISSFFT_PKGCONFIG OFF CACHE BOOL "dep(kissfft): pkgconfig support")
|
||||
set(KISSFFT_STATIC ON CACHE BOOL "dep(kissfft): static linking")
|
||||
set(KISSFFT_TEST OFF CACHE BOOL "dep(kissfft): enable testing")
|
||||
set(KISSFFT_TOOLS OFF CACHE BOOL "dep(kissfft): build tools")
|
||||
FetchContent_MakeAvailable(kissfft)
|
||||
|
||||
if (USE_QTEXTCODEC)
|
||||
find_package(Qt6 6.6 COMPONENTS Core5Compat REQUIRED)
|
||||
endif()
|
||||
|
||||
set (PMUSIC_CPP_FILES
|
||||
main.cpp
|
||||
mainwindow.cpp
|
||||
seekableslider.cpp
|
||||
playlistmanager.cpp
|
||||
singleapplicationmanager.cpp
|
||||
lrcbar.cpp
|
||||
set(KISSFFT_TOOLS OFF CACHE BOOL "dep(kissfft): build tools")
|
||||
FetchContent_MakeAvailable(kissfft)
|
||||
|
||||
if (USE_QTEXTCODEC)
|
||||
find_package(Qt6 6.6 COMPONENTS Core5Compat REQUIRED)
|
||||
endif()
|
||||
|
||||
set (PMUSIC_CPP_FILES
|
||||
main.cpp
|
||||
mainwindow.cpp
|
||||
seekableslider.cpp
|
||||
playlistmanager.cpp
|
||||
singleapplicationmanager.cpp
|
||||
lrcbar.cpp
|
||||
lyricsmanager.cpp
|
||||
fftspectrum.cpp
|
||||
)
|
||||
|
||||
set (PMUSIC_HEADER_FILES
|
||||
mainwindow.h
|
||||
seekableslider.h
|
||||
playlistmanager.h
|
||||
singleapplicationmanager.h
|
||||
lrcbar.h
|
||||
fftspectrum.cpp
|
||||
playbackprogressindicator.cpp
|
||||
)
|
||||
|
||||
set (PMUSIC_HEADER_FILES
|
||||
mainwindow.h
|
||||
seekableslider.h
|
||||
playlistmanager.h
|
||||
singleapplicationmanager.h
|
||||
lrcbar.h
|
||||
lyricsmanager.h
|
||||
fftspectrum.h
|
||||
)
|
||||
|
||||
set (PMUSIC_UI_FILES
|
||||
mainwindow.ui
|
||||
)
|
||||
|
||||
set (EXE_NAME pmusic)
|
||||
|
||||
# Translation
|
||||
file (GLOB PMUSIC_TS_FILES languages/*.ts)
|
||||
set (PMUSIC_CPP_FILES_FOR_I18N ${PMUSIC_CPP_FILES} ${PMUSIC_UI_FILES})
|
||||
|
||||
add_executable(${EXE_NAME} WIN32
|
||||
${PMUSIC_HEADER_FILES}
|
||||
${PMUSIC_CPP_FILES}
|
||||
${PMUSIC_UI_FILES}
|
||||
resources.qrc
|
||||
)
|
||||
|
||||
qt_add_translations(${EXE_NAME}
|
||||
TS_FILES
|
||||
${PMUSIC_TS_FILES}
|
||||
)
|
||||
|
||||
if (WIN32)
|
||||
target_sources(${EXE_NAME} PRIVATE assets/pineapple-music.rc)
|
||||
endif ()
|
||||
|
||||
if (NOT TagLib_FOUND)
|
||||
target_compile_definitions(${EXE_NAME} PRIVATE NO_TAGLIB=1)
|
||||
else ()
|
||||
target_link_libraries(${EXE_NAME} PRIVATE TagLib::tag)
|
||||
endif ()
|
||||
|
||||
if (TARGET KF6::Codecs)
|
||||
target_compile_definitions(${EXE_NAME} PRIVATE HAVE_KCODECS=1)
|
||||
target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs)
|
||||
endif ()
|
||||
|
||||
target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network kissfft::kissfft)
|
||||
|
||||
if (USE_QTEXTCODEC)
|
||||
target_compile_definitions(${EXE_NAME} PRIVATE USE_QTEXTCODEC=1)
|
||||
target_link_libraries(${EXE_NAME} PRIVATE Qt6::Core5Compat)
|
||||
endif()
|
||||
|
||||
# Install settings
|
||||
if (WIN32)
|
||||
# FIXME: try to avoid install to a "bin" subfolder under windows...
|
||||
# when fixed, don't forget to update the CI config file...
|
||||
elseif (UNIX)
|
||||
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||
set(CMAKE_INSTALL_PREFIX /usr)
|
||||
endif ()
|
||||
|
||||
# install icon
|
||||
install(
|
||||
FILES icons/app-icon.svg
|
||||
DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/48x48/apps"
|
||||
RENAME net.blumia.pineapple-music.svg
|
||||
)
|
||||
|
||||
# install shortcut
|
||||
install(
|
||||
FILES dist/net.blumia.pineapple-music.desktop
|
||||
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications"
|
||||
)
|
||||
endif()
|
||||
|
||||
set (INSTALL_TARGETS_DEFAULT_ARGS
|
||||
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)
|
||||
fftspectrum.h
|
||||
playbackprogressindicator.h
|
||||
)
|
||||
|
||||
set (PMUSIC_UI_FILES
|
||||
mainwindow.ui
|
||||
)
|
||||
|
||||
set (EXE_NAME pmusic)
|
||||
|
||||
# Translation
|
||||
file (GLOB PMUSIC_TS_FILES languages/*.ts)
|
||||
set (PMUSIC_CPP_FILES_FOR_I18N ${PMUSIC_CPP_FILES} ${PMUSIC_UI_FILES})
|
||||
|
||||
add_executable(${EXE_NAME}
|
||||
${PMUSIC_HEADER_FILES}
|
||||
${PMUSIC_CPP_FILES}
|
||||
${PMUSIC_UI_FILES}
|
||||
resources.qrc
|
||||
)
|
||||
|
||||
qt_add_translations(${EXE_NAME}
|
||||
TS_FILES
|
||||
${PMUSIC_TS_FILES}
|
||||
)
|
||||
|
||||
if (WIN32)
|
||||
target_sources(${EXE_NAME} PRIVATE assets/pineapple-music.rc)
|
||||
endif ()
|
||||
|
||||
if (NOT TagLib_FOUND)
|
||||
target_compile_definitions(${EXE_NAME} PRIVATE NO_TAGLIB=1)
|
||||
else ()
|
||||
target_link_libraries(${EXE_NAME} PRIVATE TagLib::tag)
|
||||
endif ()
|
||||
|
||||
if (TARGET KF6::Codecs)
|
||||
target_compile_definitions(${EXE_NAME} PRIVATE HAVE_KCODECS=1)
|
||||
target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs)
|
||||
endif ()
|
||||
|
||||
target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network kissfft::kissfft)
|
||||
|
||||
if (USE_QTEXTCODEC)
|
||||
target_compile_definitions(${EXE_NAME} PRIVATE USE_QTEXTCODEC=1)
|
||||
target_link_libraries(${EXE_NAME} PRIVATE Qt6::Core5Compat)
|
||||
endif()
|
||||
|
||||
# Install settings
|
||||
if (WIN32)
|
||||
set_target_properties(${EXE_NAME} PROPERTIES
|
||||
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)
|
||||
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||
set(CMAKE_INSTALL_PREFIX /usr)
|
||||
endif ()
|
||||
|
||||
# install icon
|
||||
install(
|
||||
FILES icons/app-icon.svg
|
||||
DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/48x48/apps"
|
||||
RENAME net.blumia.pineapple-music.svg
|
||||
)
|
||||
|
||||
# install shortcut
|
||||
install(
|
||||
FILES dist/net.blumia.pineapple-music.desktop
|
||||
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)
|
||||
|
@ -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:
|
||||
|
||||
- [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
|
||||
|
||||
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).
|
||||
- ...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
|
||||
matrix:
|
||||
- build_name: mingw1120_64_qt6_7
|
||||
QTPATH: C:\Qt\6.7\mingw_64
|
||||
MINGW64: C:\Qt\Tools\mingw1120_64
|
||||
QTPATH: C:\Qt\6.8\mingw_64
|
||||
MINGW64: C:\Qt\Tools\mingw1310_64
|
||||
|
||||
install:
|
||||
- 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;
|
||||
}
|
||||
|
||||
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);
|
||||
if (modelIndex.isValid()) {
|
||||
loadByModelIndex(modelIndex);
|
||||
@ -264,14 +270,21 @@ void MainWindow::loadFile()
|
||||
}
|
||||
|
||||
m_playlistManager->loadPlaylist(urlList);
|
||||
m_mediaPlayer->setSource(urlList.first());
|
||||
m_lrcbar->loadLyrics(urlList.first().toLocalFile());
|
||||
const QUrl & firstUrl = urlList.first();
|
||||
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)
|
||||
{
|
||||
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()
|
||||
@ -359,14 +372,6 @@ void MainWindow::on_stopBtn_clicked()
|
||||
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()
|
||||
{
|
||||
QModelIndex index(m_playlistManager->previousIndex());
|
||||
@ -414,6 +419,10 @@ void MainWindow::initConnections()
|
||||
m_audioOutput->setDevice(m_mediaDevices->defaultAudioOutput());
|
||||
});
|
||||
|
||||
connect(ui->playbackProgressIndicator, &PlaybackProgressIndicator::seekingRequested, this, [=](qint64 pos){
|
||||
m_mediaPlayer->setPosition(pos);
|
||||
});
|
||||
|
||||
connect(m_mediaPlayer, &QMediaPlayer::sourceChanged, this, [=](){
|
||||
QUrl fileUrl(m_mediaPlayer->source());
|
||||
|
||||
@ -470,7 +479,7 @@ void MainWindow::initConnections()
|
||||
connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, [=](qint64 pos) {
|
||||
ui->nowTimeLabel->setText(ms2str(pos));
|
||||
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());
|
||||
});
|
||||
@ -484,6 +493,7 @@ void MainWindow::initConnections()
|
||||
});
|
||||
|
||||
connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, [=](qint64 dua) {
|
||||
ui->playbackProgressIndicator->setDuration(dua);
|
||||
ui->totalTimeLabel->setText(ms2str(dua));
|
||||
});
|
||||
|
||||
|
@ -68,7 +68,6 @@ private slots:
|
||||
void on_playBtn_clicked();
|
||||
void on_volumeSlider_valueChanged(int value);
|
||||
void on_stopBtn_clicked();
|
||||
void on_playbackSlider_valueChanged(int value);
|
||||
void on_prevBtn_clicked();
|
||||
void on_nextBtn_clicked();
|
||||
void on_volumeBtn_clicked();
|
||||
|
@ -371,14 +371,7 @@ QListView {
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="SeekableSlider" name="playbackSlider">
|
||||
<property name="maximum">
|
||||
<number>1000</number>
|
||||
</property>
|
||||
<property name="orientation">
|
||||
<enum>Qt::Orientation::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="PlaybackProgressIndicator" name="playbackProgressIndicator" native="true"/>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="playbackControlLayout">
|
||||
@ -694,6 +687,12 @@ QListView {
|
||||
<extends>QSlider</extends>
|
||||
<header>seekableslider.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>PlaybackProgressIndicator</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>playbackprogressindicator.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources>
|
||||
<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