-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
268 lines (221 loc) · 8.31 KB
/
mainwindow.cpp
File metadata and controls
268 lines (221 loc) · 8.31 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
SPDX-FileCopyrightText: 2021 Gary Wang <wzc782970009@gmail.com>
SPDX-License-Identifier: GPL-2.0-only
*/
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "chaptertreemodel.h"
#include "filehandlermanager.h"
#include "filehandlerinterface.h"
#include "chapterinfowidget.h"
#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QMimeData>
#include <QMouseEvent>
#include <QRegularExpression>
#include <QTime>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->chapterInfoWidget->setVisible(false);
// Due to we have action that doesn't really match to a freedesktop icon theme
// action type, using theme icon will cause style doesn't match. Before we figure
// out some better solution, we use action icons inside the resource file...
// ui->actionOpen->setIcon(qApp->style()->standardIcon(QStyle::SP_DialogOpenButton));
// ui->actionSave->setIcon(qApp->style()->standardIcon(QStyle::SP_DialogSaveButton));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loadFile(QUrl url)
{
QString filePath(url.toLocalFile());
if (QFile::exists(filePath)) {
m_filePath = filePath;
}
loadFile();
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
} else {
event->ignore();
}
return QMainWindow::dragEnterEvent(event);
}
void MainWindow::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();
const QMimeData * mimeData = event->mimeData();
if (mimeData->hasUrls()) {
const QList<QUrl> &urls = mimeData->urls();
if (!urls.isEmpty()) {
loadFile(urls.first());
}
}
}
void MainWindow::loadFile()
{
QFileInfo fileInfo(m_filePath);
if (!fileInfo.exists()) return;
ChapterTreeModel * model = new ChapterTreeModel;
bool succ = model->loadFromFile(m_filePath);
if (succ) {
QAbstractItemModel * oldModel = ui->treeView->model();
ui->treeView->setModel(model);
ui->treeView->expandAll();
uiAdjustHeaderSelectionResizeMode();
if (oldModel) {
delete oldModel;
}
QFileInfo fileInfo(m_filePath);
setWindowTitle(fileInfo.fileName());
ui->actionApply->setEnabled(true);
} else {
QMessageBox::information(
this, tr("Load failed"),
tr("Unsupported file type.\nThe supported file types are: %1.")
.arg("mp3, ogg, opus"));
}
}
QTime timeFromString(const QString & timeStr)
{
switch (timeStr.count(':')) {
case 0:
return QTime::fromString(timeStr, "s");
case 1:
return QTime::fromString(timeStr, "m:s");
default:
return QTime::fromString(timeStr, "h:m:s");
}
}
// We use a very flexible rule similar to the one used on YouTube
// https://support.google.com/youtube/answer/9884579
// YouTube require the first chapter marker as 0:00, and a chapter line
// must start with a chapter marker, we don't require this.
void MainWindow::importFromLines(ChapterTreeModel * model, const QStringList &lines)
{
Q_CHECK_PTR(model);
bool hasValidLine = false;
model->clearChapterTreeButKeepTOC();
// This regex see: https://stackoverflow.com/questions/8318236/
QRegularExpression timeRegex(QStringLiteral(R"((?:([01]?\d|2[0-3]):)?([0-5]?\d):([0-5]?\d))"));
for (QString line : lines) {
QRegularExpressionMatch match(timeRegex.match(line));
qDebug() << match.capturedTexts() << match.hasMatch();
if (match.hasMatch()) {
const QString timeStr(match.capturedTexts()[0]);
line.remove(timeStr);
line = line.trimmed();
QTime startTime(timeFromString(timeStr));
qDebug() << startTime << startTime.msecsSinceStartOfDay();
model->appendChapter(startTime.msecsSinceStartOfDay(), line);
hasValidLine = true;
}
}
if (hasValidLine) {
uiAdjustHeaderSelectionResizeMode();
}
}
void MainWindow::uiAdjustHeaderSelectionResizeMode()
{
ui->treeView->expandAll();
ui->treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->treeView->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
ui->treeView->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
}
void MainWindow::on_actionOpen_triggered()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Open Audio File"),
QDir::homePath(),
tr("Audio Files (*.mp3 *.ogg *.opus)"));
if (!filePath.isEmpty()) {
m_filePath = filePath;
loadFile();
}
}
void MainWindow::on_actionApply_triggered()
{
ChapterTreeModel * chapterModel = ui->treeView->model();
QFileInfo currentFile(m_filePath);
if (!chapterModel) return;
if (!currentFile.exists()) return;
QString filePath = QFileDialog::getSaveFileName(this, tr("Save Audio File with Chapters"),
currentFile.absolutePath(),
tr("Audio Files (*.mp3 *.ogg *.opus, *.m4a, *.cue, *.info.json)"));
chapterModel->saveToFile(filePath);
}
void MainWindow::on_actionExport_triggered()
{
ChapterTreeModel * chapterModel = ui->treeView->model();
QFileInfo currentFile(m_filePath);
QString defaultSavePath;
QStringList filters; // Copywritings
QMap<QString, QString> filterExporterMap; // <Filter Copywriting, Suffix>
QList<QPair<QString, QString> > exporters = FileHandlerManager::instance()->exporterList();
for (const QPair<QString, QString> & exporter : std::as_const(exporters)) {
filterExporterMap[exporter.second] = exporter.first;
filters.append(exporter.second);
}
if (!chapterModel) return;
defaultSavePath = currentFile.exists() ? currentFile.absolutePath() : QDir::homePath();
const QStringList schemes = QStringList(QStringLiteral("file"));
QFileDialog dlg(this, tr("Export Chapter Data to File"), defaultSavePath,
filters.join(QLatin1String(";;")));
dlg.setSupportedSchemes(schemes);
dlg.setAcceptMode(QFileDialog::AcceptSave);
if (dlg.exec() == QDialog::Accepted) {
QString selectedFilter = dlg.selectedNameFilter();
if (filterExporterMap.contains(selectedFilter)) {
QString filePath(dlg.selectedUrls().value(0).toLocalFile());
chapterModel->exportToFile(filePath, filterExporterMap[selectedFilter]);
}
}
}
void MainWindow::on_treeView_viewSelectionChanged()
{
bool canDelete = false;
QModelIndexList selectedIndex(ui->treeView->selectionModel()->selectedIndexes());
if (!selectedIndex.isEmpty()) {
canDelete = true;
ui->chapterInfoWidget->setCurrentChapter(ui->treeView->model(), selectedIndex.first());
ui->chapterInfoWidget->setVisible(true);
} else {
ui->chapterInfoWidget->setVisible(false);
}
ui->removeBtn->setEnabled(canDelete);
}
void MainWindow::on_appendChapterBtn_clicked()
{
ChapterTreeModel * model = ui->treeView->model();
if (model) {
model->appendChapter(ui->treeView->selectionModel());
uiAdjustHeaderSelectionResizeMode();
}
}
void MainWindow::on_removeBtn_clicked()
{
QItemSelectionModel * selectionModel = ui->treeView->selectionModel();
if (!selectionModel) return;
QModelIndexList selectedIndex(ui->treeView->selectionModel()->selectedIndexes());
if (!selectedIndex.isEmpty()) {
ui->treeView->model()->removeRow(selectedIndex[0].row(), selectedIndex[0].parent());
}
}
void MainWindow::on_importBtn_clicked()
{
ChapterTreeModel * model = ui->treeView->model();
if (model) {
QString text = QInputDialog::getMultiLineText(this, tr("Single line chapters"), tr("Sample:\n1:23 Chapter 1 title\n1:23:45 Chapter 2 title"));
if (!text.isEmpty()) {
importFromLines(model, text.split('\n'));
}
}
//
}