Skip to content

Commit

Permalink
library and player cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronson Mathews committed Jul 15, 2020
1 parent f2d2384 commit 0c333d9
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 66 deletions.
5 changes: 1 addition & 4 deletions src/book.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ class Book
Book();
QString title;
QString directory;

QStringList chapter_titles;
QStringList chapter_files;
QVector<uint> chapter_times;

uint time;
qint64 time;

bool operator==(const Book& rhs);

};

#endif // BOOK_H
43 changes: 18 additions & 25 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,36 @@ bool Library::isEmpty() const
return mLibraryItems.isEmpty();
}

/*
const Book* Library::findByPath(const QString& dir) {
Book b;
b.directory=dir;
int i = mLibraryItems.indexOf(b);
if (i == -1)
return NULL;
return &mLibraryItems[i];
}
*/

/*
int Library::getBookIndex(const Book& book) {
int i = mLibraryItems.indexOf(book);
return i;
}*/
const Book* Library::getLibraryItem(const QString &xTitle) {
for (int i=0; i < mLibraryItems.size(); ++i) {
const Book &book = mLibraryItems[i];
if (book.title == xTitle) {
return &book;
}
}
return nullptr;
}

void Library::setActiveItem(int xIndex)
void Library::setActiveItem(QString xTitle)
{
if (mActiveItem == xIndex)
if (mActiveItem != nullptr && mActiveItem->title == xTitle)
return;
mActiveItem = xIndex;
emit activeItemChanged();

mActiveItem = getLibraryItem(xTitle);

if (mActiveItem != nullptr)
emit activeItemChanged();
}

int Library::activeItem()
const Book* Library::activeItem()
{
return mActiveItem;
}

const Book* Library::getActiveItem()
{
if (mActiveItem == -1 || mLibraryItems.isEmpty() || mActiveItem >= mLibraryItems.size())
return nullptr;

return &mLibraryItems[mActiveItem];
return mActiveItem;
}


Expand Down
13 changes: 7 additions & 6 deletions src/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Library : public QObject
Q_OBJECT
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
Q_PROPERTY(int size READ size)
Q_PROPERTY(int activeItem READ activeItem WRITE setActiveItem NOTIFY activeItemChanged)
Q_PROPERTY(QString activeItem WRITE setActiveItem NOTIFY activeItemChanged)

public:
Library(const Library&) = delete; // disable copy for singleton
Expand All @@ -30,12 +30,11 @@ class Library : public QObject
bool isEmpty() const;

QVector<Book> getLibraryItems();
//const Book* findByPath(const QString &dir);
//int getBookIndex(const Book &book);
const Book* getLibraryItem(const QString &xTitle);

// active library item
void setActiveItem(int xIndex);
int activeItem();
void setActiveItem(QString xTitle);
const Book* activeItem();
const Book* getActiveItem();

signals:
Expand All @@ -46,9 +45,11 @@ class Library : public QObject
public slots:
void update();

protected:

private:
int mActiveItem = -1;
QVector<Book> mLibraryItems;
const Book *mActiveItem = nullptr;
QString mPath;
explicit Library(QObject *parent = nullptr);

Expand Down
50 changes: 33 additions & 17 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
Player::Player(QMediaPlayer *parent)
: QMediaPlayer(parent)
{
// TODO: use parent playlist? parent->playlist()
QMediaPlaylist *playlist = new QMediaPlaylist;
setPlaylist(playlist);

// connect this playlist to our slots
connect(playlist, &QMediaPlaylist::currentIndexChanged, this, &Player::superCurrentIndexChanged);
// connects
connect(playlist, &QMediaPlaylist::currentIndexChanged, this, &Player::currentIndexChanged);
connect(this, &QMediaPlayer::positionChanged, this, &Player::positionChanged);


// connect library
connect(Library::instance(), &Library::activeItemChanged, this, &Player::libraryItemChanged);

}


Expand Down Expand Up @@ -53,6 +57,7 @@ void Player::setPlayingBook(const Book &p_book) {
book = p_book;
mPlayListTime = book.time;
mProgressScale = 10000.0/mPlayListTime;
mProgress = 0;

playlist()->clear();

Expand Down Expand Up @@ -86,19 +91,24 @@ const QString Player::getPlayingChapterTitle() {
return book.chapter_titles[getPlayingChapterIndex()];
}

void Player::positionChanged(qint64 xPosition)
{
// parent position has changed
// account for chapters before current chapter
qint64 start_pos = 0;
int idx = playlist()->currentIndex();
for(int i = 0; i < idx ; ++i) {
start_pos += book.chapter_times[i];
}
mProgress = start_pos + xPosition;
emit progressChanged();
}

void Player::playUrl(const QUrl &url) {
setMedia(url);
play();
}

void Player::setPlayingBook(int xLibraryIndex)
{
/*
const Book& book = Library::instance()->getLibraryItems().at(idx.row());
setPlayingBook(book);
play();
*/
}

void Player::togglePlayPause() {
if (state() != QMediaPlayer::PlayingState)
Expand All @@ -115,18 +125,25 @@ uint Player::getPlaylistLength() {
return mPlayListTime;
}

uint Player::getProgress() {
return mProgressScale * getPosition();
uint Player::progress() const {
// multiply by scale of the slider
return mProgress * mProgressScale;
}

uint Player::getPosition() {
/*
uint Player::getPosition() const {
// account for chapters before current chapter
int start_pos = 0;
int idx = playlist()->currentIndex();
for(int i = 0; i < idx ; ++i) {
start_pos += book.chapter_times[i];
}
return start_pos + position();
}*/

void Player::setProgress(qint64 xPosition)
{
qDebug() << xPosition;
}

void Player::setPosition(uint p_position) {
Expand All @@ -151,14 +168,13 @@ void Player::setPosition(uint p_position) {


void Player::skipForward() {
int current_position = getPosition();
current_position += 30000; // 30s
qint64 current_position = mProgress + mSkip;
//if (current_position >= mPlayListTime) // TODO: check skip end of book
setPosition(current_position);
}

void Player::skipBackward() {
int current_position = getPosition();
current_position -= 30000; // 30s
qint64 current_position = mProgress - mSkip;
if (current_position < 0)
current_position = 0;
setPosition(current_position);
Expand Down
22 changes: 16 additions & 6 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Player : public QMediaPlayer
{
Q_OBJECT
//Q_PROPERTY(QString play READ play WRITE setPlay NOTIFY playChanged)
Q_PROPERTY(uint progress READ progress WRITE setProgress NOTIFY progressChanged)

public:
Player(const Player&) = delete; // disable copy for singleton
Expand All @@ -22,9 +23,12 @@ class Player : public QMediaPlayer
static QStringList supportedMimeTypes();
static QStringList supportedSuffixes();

uint progress() const;
uint getPosition() const;
void setProgress(qint64 xPosition);

uint getPlaylistLength();
uint getProgress();
uint getPosition();

const Book& getPlayingItem();
int getPlayingChapterIndex();
const QString getPlayingChapterTitle();
Expand All @@ -33,8 +37,11 @@ class Player : public QMediaPlayer
Book book;

public slots:
void positionChanged(qint64 xPosition);
void libraryItemChanged();

// old
void playUrl(const QUrl& url);
void setPlayingBook(int xLibraryIndex);
void setPlayingBook(const Book &book);
void setPlayingChapter(QString p_chapter);
void setPosition(uint p_position);
Expand All @@ -45,14 +52,17 @@ public slots:
void setPlaybackMode(QMediaPlaylist::PlaybackMode mode);
void togglePlayPause();
void superCurrentIndexChanged();
void libraryItemChanged();


signals:
void currentIndexChanged();
void progressChanged();

private:
uint mPlayListTime;
float mProgressScale;
qint64 mProgress = 0;
int mSkip = 30000; // 30 sec
qint64 mPlayListTime;
qint64 mProgressScale;
explicit Player(QMediaPlayer *parent = nullptr);

};
Expand Down
41 changes: 36 additions & 5 deletions src/qml/ControlPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,45 @@ Page {
color: Style.app.color
}

Slider {
id: progress
from: 0
value: Player.position
to: 10000
Item {
id: timeline
height: progress.height
anchors.right: parent.right
anchors.rightMargin: 0
anchors.left: parent.left
anchors.leftMargin: 0
anchors.top: parent.top
anchors.topMargin: 0

Label {
id: track_position
text: "00:00:00"
}

Slider {
id: progress
width: parent.width
anchors.top: parent.top
anchors.right: parent.right
anchors.left: parent.left
value: Player.progress
from: 0
to: 10000
}

Label {
id: track_time
text: "00:00:00"
anchors.right: parent.right
}
}



Grid {
x: 0
anchors.top: timeline.bottom
anchors.topMargin: 6
columns: 3
spacing: Style.control.spacing

Expand Down
4 changes: 2 additions & 2 deletions src/qml/LibraryPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ Page {
onClicked: {
library_list.currentIndex = index;
console.log(model.title)
Library.activeItem = model.libraryIndex;
Library.activeItem = model.title;
}
onDoubleClicked: {
library_list.currentIndex = index;
console.log('double')
Library.activeItem = model.libraryIndex;
Library.activeItem = model.title;
swipe_view.currentIndex = 0; // TODO: not working?!
Player.play();
}
Expand Down
5 changes: 5 additions & 0 deletions src/taglib/taglib_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#ifndef TAGLIB_EXPORT_H
#define TAGLIB_EXPORT_H

// Note: Brons set this value for windows to build
#define TAGLIB_STATIC 1



#if defined(TAGLIB_STATIC)
#define TAGLIB_EXPORT
#elif (defined(_WIN32) || defined(_WIN64))
Expand Down
2 changes: 1 addition & 1 deletion src/util.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "util.h"
#include <taglib/fileref.h>
#include "taglib/fileref.h"


QString Util::getDisplayTime(const QString &xFileName)
Expand Down

0 comments on commit 0c333d9

Please sign in to comment.