-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchapterize.py
204 lines (157 loc) · 6.44 KB
/
chapterize.py
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
200
201
202
203
204
from pathlib import Path
import sys
from PyQt5.QtGui import QContextMenuEvent, QRegularExpressionValidator
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QFileSystemModel, QMenu, QAction, \
QStyledItemDelegate, QLineEdit
from PyQt5.QtCore import pyqtSlot, QAbstractTableModel, Qt, QDir, QItemSelection, QModelIndex, QRegularExpression
from Ui_chapterize import Ui_ChapterizeWindow
from mp3file import Mp3File
from chapter import Chapter
from timestamp import Timestamp
class TimestampDelegate(QStyledItemDelegate):
_ts_regex = QRegularExpression(r'\d+:[0-5]\d:[0-5]\d(\.\d{1,3})?')
def __init__(self):
super().__init__()
def createEditor(self, parent, option, index):
editor = QLineEdit(parent)
editor.setFrame(False)
if index.column() in [1, 2]:
editor.setValidator(QRegularExpressionValidator(TimestampDelegate._ts_regex))
return editor
def setEditorData(self, editor, index):
if index.isValid():
data = index.model().data(index, Qt.EditRole)
editor.setText(data)
def setModelData(self, editor, model, index):
data = editor.text()
model.setData(index, data, Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class ChaptersTableModel(QAbstractTableModel):
headers = ["Title", "Start", "End"]
def __init__(self, mp3_filepath=''):
super().__init__()
self.set_file(mp3_filepath)
def set_file(self, mp3_filepath):
mp3 = None
if mp3_filepath != '':
try:
mp3 = Mp3File(mp3_filepath)
except IOError:
return
self.beginResetModel()
self._mp3 = mp3
self.endResetModel()
def data(self, index, role):
if self._mp3 is None:
return None
if role not in (Qt.DisplayRole, Qt.EditRole):
return None
if not index.isValid():
return None
col = index.column()
row = index.row()
ch = self._mp3.chapters[row]
if col == 0:
return ch.title
elif col == 1:
return str(ch.start)
elif col == 2:
return str(ch.end)
return None
def setData(self, index, value, role):
if index.isValid() and role == Qt.EditRole:
row, col = index.row(), index.column()
ch = self._mp3.chapters[row]
if col == 0:
ch.title = value
elif col == 1:
ch.start = Timestamp.from_string(value)
elif col == 2:
ch.end = Timestamp.from_string(value)
self.dataChanged.emit(index, index, [role])
return True
else:
return False
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role != Qt.DisplayRole or orientation != Qt.Horizontal:
return None
return ChaptersTableModel.headers[section]
def rowCount(self, parent=QModelIndex()):
if parent.isValid() or self._mp3 is None:
return 0
return len(self._mp3.chapters)
def columnCount(self, parent=QModelIndex()):
if parent.isValid():
return 0
return len(ChaptersTableModel.headers)
def insertRows(self, row, count, parent=QModelIndex()):
self.beginInsertRows(parent, row, row+count-1)
for i in range(count):
self._mp3.chapters.insert(row + i, Chapter(title='', start=Timestamp(), end=Timestamp()))
self.endInsertRows()
return True
def removeRows(self, row, count, parent=QModelIndex()):
self.beginRemoveRows(parent, row, row+count-1)
for _ in range(count):
self._mp3.chapters.pop(row)
self.endRemoveRows()
return True
def flags(self, index: QModelIndex) -> Qt.ItemFlags:
if index.isValid():
return super().flags(index) | Qt.ItemIsEditable
return super().flags(index)
def save(self):
self._mp3.save()
class Chapterize(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_ChapterizeWindow()
self.ui.setupUi(self)
self.dir = QDir.homePath()
self.chaptersTableModel = ChaptersTableModel()
self.ui.chapterTable.setModel(self.chaptersTableModel)
self.ui.chapterTable.setItemDelegate(TimestampDelegate())
self.ui.chapterTable.contextMenuEvent = self.onChapterContextMenu
self.mp3ListModel = QFileSystemModel()
self.mp3ListModel.setNameFilters(['*.mp3'])
self.mp3ListModel.setFilter(QDir.Files)
self.mp3ListModel.setRootPath(self.dir)
self.mp3ListModel.setNameFilterDisables(False)
self.ui.mp3List.setModel(self.mp3ListModel)
self.ui.mp3List.setRootIndex(self.mp3ListModel.index(self.dir))
self.ui.mp3List.selectionModel().selectionChanged.connect(self.onMp3Select)
self.ui.actionChangeDir.triggered.connect(self.onDirectoryChange)
self.ui.actionExit.triggered.connect(self.close)
self.ui.actionSave.triggered.connect(self.chaptersTableModel.save)
@pyqtSlot()
def onDirectoryChange(self):
chosen_dir = QFileDialog.getExistingDirectory(self, "Change Directory...", self.dir, QFileDialog.ShowDirsOnly)
if chosen_dir != '':
self.dir = chosen_dir
self.mp3ListModel.setRootPath(self.dir)
self.ui.mp3List.setRootIndex(self.mp3ListModel.index(self.dir))
@pyqtSlot(QItemSelection, QItemSelection)
def onMp3Select(self, selected, deselected):
sel_idx = selected.indexes()[0]
pth = self.mp3ListModel.fileInfo(sel_idx).absoluteFilePath()
self.chaptersTableModel.set_file(pth)
@pyqtSlot(QContextMenuEvent)
def onChapterContextMenu(self, e):
idx = self.ui.chapterTable.indexAt(e.pos())
row = idx.row()
if row < 0:
row = self.chaptersTableModel.rowCount()
menu = QMenu(self)
insertAction = QAction('&Insert', menu)
insertAction.triggered.connect(lambda: self.chaptersTableModel.insertRow(row))
menu.addAction(insertAction)
deleteAction = QAction('&Delete', menu)
deleteAction.triggered.connect(lambda: self.chaptersTableModel.removeRow(row))
menu.addAction(deleteAction)
menu.exec_(e.globalPos())
if __name__ == '__main__':
app = QApplication(sys.argv)
wdw = Chapterize()
wdw.show()
sys.exit(app.exec_())