-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchaptertreemodel.cpp
More file actions
199 lines (165 loc) · 5.87 KB
/
chaptertreemodel.cpp
File metadata and controls
199 lines (165 loc) · 5.87 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "chaptertreemodel.h"
#include "filehandlermanager.h"
#include "filehandlerinterface.h"
#include "taglibutils_p.h"
#include <QDebug>
#include <QItemSelectionModel>
#include <QMimeDatabase>
#include <QSaveFile>
#include <QTime>
bool ChapterTreeModel::loadFromFile(const QString &pathToFile)
{
clear();
QMimeDatabase db;
QMimeType mimeType = db.mimeTypeForFile(pathToFile);
FileHandlerInterface * handler = FileHandlerManager::instance()->createReadHandlerByMimeType(mimeType);
if (handler) {
handler->setFile(pathToFile);
handler->importFromFile();
ChapterItem * created = handler->createChapterTree();
if (created) {
appendRow(created);
}
return true;
}
qDebug() << mimeType;
return false;
}
bool ChapterTreeModel::saveToFile(const QString &pathToFile)
{
QMimeDatabase db;
QMimeType mimeType = db.mimeTypeForFile(pathToFile);
FileHandlerInterface * handler = FileHandlerManager::instance()->createWriteHandlerByMimeType(mimeType);
if (handler) {
QStandardItem * item = invisibleRootItem()->child(0);
ChapterItem * chapterItem = static_cast<ChapterItem *>(item);
handler->setFile(pathToFile);
handler->writeToFile(chapterItem);
return true;
}
return false;
}
bool ChapterTreeModel::exportToFile(const QString &pathToFile, const QString & suffix)
{
QStandardItem * item = invisibleRootItem()->child(0);
ChapterItem * chapterItem = static_cast<ChapterItem *>(item);
FileHandlerInterface * handler = FileHandlerManager::instance()->createExportHandlerBySuffix(suffix);
handler->setFile(pathToFile);
handler->exportToFile(chapterItem);
return true;
}
void ChapterTreeModel::ensureHaveTOC()
{
QStandardItem * parentItem = invisibleRootItem()->child(0);
if (!parentItem) {
ChapterItem * tocItem = new ChapterItem("presudoTOC");
tocItem->setColumnCount(3);
tocItem->setItemProperty(FrameId, "CTOC");
appendRow(tocItem);
}
}
bool ChapterTreeModel::clearChapterTreeButKeepTOC()
{
QStandardItem * parentItem = invisibleRootItem()->child(0);
if (parentItem) {
QModelIndex tocIndex(indexFromItem(parentItem));
removeRows(0, parentItem->rowCount(), tocIndex);
return true;
}
return false;
}
// actually only one item can be selected..
QModelIndex ChapterTreeModel::appendChapter(QItemSelectionModel * selectionModel)
{
ensureHaveTOC();
QModelIndex parent;
int row = -1;
int startTimeMs = 0;
QString chapterName(QStringLiteral("New Chapter"));
ChapterItem * selectedChapter = nullptr;
if (selectionModel && !selectionModel->selectedIndexes().isEmpty()) {
QModelIndexList selectedIndexes(selectionModel->selectedIndexes());
parent = selectedIndexes[0].parent();
row = selectedIndexes[0].row();
selectedChapter = static_cast<ChapterItem *>(itemFromIndex(selectedIndexes[0]));
}
QStandardItem * parentItem = parent.isValid() ? itemFromIndex(parent)
: invisibleRootItem()->child(0);
if (selectedChapter) {
startTimeMs = selectedChapter->data(ChapterStartTimeMs).toInt();
}
return appendChapter(parentItem, chapterName, startTimeMs, row);
}
QModelIndex ChapterTreeModel::appendChapter(int startTimeMs, const QString &title)
{
QStandardItem * parentItem = invisibleRootItem()->child(0);
if (!parentItem) {
ChapterItem * tocItem = new ChapterItem("presudoTOC");
tocItem->setColumnCount(3);
tocItem->setItemProperty(FrameId, "CTOC");
appendRow(tocItem);
parentItem = tocItem;
}
return appendChapter(parentItem, title, startTimeMs);
}
QVariant ChapterTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("Name");
case 1:
return tr("Start Time");
case 2:
return tr("End Time");
default:
return QVariant();
}
}
return QVariant();
}
QVariant ChapterTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QStandardItemModel::data(index, role);
if (role != Qt::DisplayRole) return QStandardItemModel::data(index, role);
// only affect text display
switch (index.column()) {
case 1: {
QTime time(QTime::fromMSecsSinceStartOfDay(itemFromIndex(index.siblingAtColumn(0))->data(ChapterStartTimeMs).toInt()));
return time.toString();
}
case 2: {
QVariant data(itemFromIndex(index.siblingAtColumn(0))->data(ChapterEndTimeMs));
if (data.isNull()) {
return QStringLiteral("N/A");
}
QTime time(QTime::fromMSecsSinceStartOfDay(data.toInt()));
return time.toString();
}
default:
break;
}
return QStandardItemModel::data(index, role);
}
int ChapterTreeModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QModelIndex ChapterTreeModel::appendChapter(QStandardItem *parentItem, const QString &title, int startTimeMs, int rowAt)
{
Q_CHECK_PTR(parentItem);
ChapterItem * newChapter = new ChapterItem();
newChapter->setColumnCount(3);
newChapter->setItemProperty(FrameId, "CHAP");
newChapter->setItemProperty(ChapterTitle, title);
newChapter->setItemProperty(ChapterStartTimeMs, startTimeMs);
if (rowAt != -1) {
parentItem->insertRow(rowAt + 1, newChapter);
} else {
parentItem->appendRow(newChapter);
}
return indexFromItem(newChapter);
}