-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchaptermodel.cpp
54 lines (43 loc) · 1.28 KB
/
chaptermodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <QDebug>
#include "chaptermodel.h"
#include "player.h"
#include "util.h"
ChapterModel::ChapterModel(QObject *parent)
: QAbstractListModel(parent)
{
// connect library
connect(Player::instance(), &Player::currentIndexChanged, this, &ChapterModel::refresh);
}
void ChapterModel::refresh() {
beginResetModel();
endResetModel();
}
int ChapterModel::rowCount(const QModelIndex & /*parent*/) const
{
Book * book = Player::instance()->getCurrentItem();
if (book == nullptr)
return 0;
return book->chapters.size();
}
QVariant ChapterModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
const Book *book = Player::instance()->getCurrentItem();
if (book == nullptr)
return QVariant();
const Chapter c = book->chapters.at(index.row());
switch (role) {
case ChapterIndexRole:
return QVariant(index.row());
case TitleRole:
return QVariant(c.title);
case FileNameRole:
return QVariant(c.path);
case DurationRole:
return QVariant(Util::getDisplayTime(c.duration));
case CurrentItemRole:
return QVariant(index.row() == Player::instance()->chapterIndex());
}
return QVariant();
}