Debian Codegraph @

src/playlisteditor.cpp

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2023
5 * Martin Lambers <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <QGridLayout>
22 #include <QSpacerItem>
23 #include <QPushButton>
24 #include <QLabel>
25 #include <QTableWidget>
26 #include <QHeaderView>
27 #include <QComboBox>
28 #include <QFileDialog>
29 #include <QLineEdit>
30
31 #include "playlist.hpp"
32 #include "playlisteditor.hpp"
33 #include "modes.hpp"
34 #include "metadata.hpp"
35
36
37 PlaylistEntryEditor::PlaylistEntryEditor(const PlaylistEntry& entry, QWidget* parent) :
38 QDialog(parent),
39 entry(entry)
40 {
41 setModal(true);
42 setWindowTitle(tr("Edit Playlist Entry"));
43
44 QGridLayout* layout = new QGridLayout;
45
46 QLabel* urlLabel0 = new QLabel(tr("URL:"));
47 layout->addWidget(urlLabel0, 0, 0);
48 urlLabel = new QLabel(entry.url.toString());
49 layout->addWidget(urlLabel, 0, 1, 1, 2);
50
51 QPushButton* setFileBtn = new QPushButton(tr("Set File..."));
52 connect(setFileBtn, SIGNAL(clicked()), this, SLOT(setFile()));
53 layout->addWidget(setFileBtn, 1, 1);
54 QPushButton* setURLBtn = new QPushButton(tr("Set URL..."));
55 connect(setURLBtn, SIGNAL(clicked()), this, SLOT(setURL()));
56 layout->addWidget(setURLBtn, 1, 2);
57
58 QLabel* inputModeLabel = new QLabel(tr("Input Mode:"));
59 layout->addWidget(inputModeLabel, 2, 0);
60 inputModeBox = new QComboBox(this);
61 for (int i = 0; i < 12; i++)
62 inputModeBox->addItem(inputModeToStringUI(static_cast<InputMode>(i)));
63 connect(inputModeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEntry()));
64 layout->addWidget(inputModeBox, 2, 1, 1, 2);
65
66 QLabel* surroundModeLabel = new QLabel(tr("Surround Mode:"));
67 layout->addWidget(surroundModeLabel, 3, 0);
68 surroundModeBox = new QComboBox(this);
69 for (int i = 0; i < 4; i++)
70 surroundModeBox->addItem(surroundModeToStringUI(static_cast<SurroundMode>(i)));
71 connect(surroundModeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEntry()));
72 layout->addWidget(surroundModeBox, 3, 1, 1, 2);
73
74 QLabel* videoTrackLabel = new QLabel(tr("Video Track:"));
75 layout->addWidget(videoTrackLabel, 4, 0);
76 videoTrackBox = new QComboBox(this);
77 connect(videoTrackBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEntry()));
78 layout->addWidget(videoTrackBox, 4, 1, 1, 2);
79
80 QLabel* audioTrackLabel = new QLabel(tr("Audio Track:"));
81 layout->addWidget(audioTrackLabel, 5, 0);
82 audioTrackBox = new QComboBox(this);
83 connect(audioTrackBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEntry()));
84 layout->addWidget(audioTrackBox, 5, 1, 1, 2);
85
86 QLabel* subtitleTrackLabel = new QLabel(tr("Subtitle Track:"));
87 layout->addWidget(subtitleTrackLabel, 6, 0);
88 subtitleTrackBox = new QComboBox(this);
89 connect(subtitleTrackBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEntry()));
90 layout->addWidget(subtitleTrackBox, 6, 1, 1, 2);
91
92 QPushButton* doneBtn = new QPushButton(tr("Done"), this);
93 doneBtn->setDefault(true);
94 connect(doneBtn, SIGNAL(clicked()), this, SLOT(accept()));
95 layout->addWidget(doneBtn, 7, 0, 1, 3);
96
97 layout->setColumnStretch(1, 1);
98 layout->setColumnStretch(2, 1);
99 layout->setRowStretch(0, 1);
100 setLayout(layout);
101
102 updateBoxStates();
103 }
104
105 void PlaylistEntryEditor::setFile()
106 {
107 QString name = QFileDialog::getOpenFileName(this);
108 if (!name.isEmpty()) {
109 entry.url = QUrl::fromLocalFile(name);
110 urlLabel->setText(entry.url.toString());
111 updateBoxStates();
112 updateEntry();
113 }
114 }
115
116 void PlaylistEntryEditor::setURL()
117 {
118 QDialog *dialog = new QDialog(this);
119 dialog->setWindowTitle(tr("Open URL"));
120 QLabel *label = new QLabel(tr("URL:"));
121 QLineEdit *edit = new QLineEdit("");
122 edit->setMinimumWidth(256);
123 QPushButton *cancelBtn = new QPushButton(tr("Cancel"));
124 QPushButton *okBtn = new QPushButton(tr("OK"));
125 okBtn->setDefault(true);
126 connect(cancelBtn, SIGNAL(clicked()), dialog, SLOT(reject()));
127 connect(okBtn, SIGNAL(clicked()), dialog, SLOT(accept()));
128 QGridLayout *layout = new QGridLayout();
129 layout->addWidget(label, 0, 0);
130 layout->addWidget(edit, 0, 1, 1, 3);
131 layout->addWidget(cancelBtn, 2, 2);
132 layout->addWidget(okBtn, 2, 3);
133 layout->setColumnStretch(1, 1);
134 dialog->setLayout(layout);
135 dialog->exec();
136 if (dialog->result() == QDialog::Accepted && !edit->text().isEmpty()) {
137 entry.url = QUrl::fromUserInput(edit->text());
138 urlLabel->setText(entry.url.toString());
139 updateBoxStates();
140 updateEntry();
141 }
142 }
143
144 void PlaylistEntryEditor::updateEntry()
145 {
146 entry.inputMode = static_cast<InputMode>(inputModeBox->currentIndex());
147 entry.surroundMode = static_cast<SurroundMode>(surroundModeBox->currentIndex());
148 entry.videoTrack = videoTrackBox->currentIndex() >= 0 ? videoTrackBox->currentIndex() - 1 : -1;
149 entry.audioTrack = audioTrackBox->currentIndex() >= 0 ? audioTrackBox->currentIndex() - 1 : -1;
150 entry.subtitleTrack = subtitleTrackBox->currentIndex() >= 0 ? subtitleTrackBox->currentIndex() - 2 : -2;
151 }
152
153 void PlaylistEntryEditor::updateBoxStates()
154 {
155 MetaData metaData;
156 bool haveMetaData = false;
157 if (!entry.url.isEmpty()) {
158 haveMetaData = metaData.detectCached(entry.url);
159 if (haveMetaData && metaData.videoTracks.size() < 1)
160 haveMetaData = false;
161 }
162 inputModeBox->setCurrentIndex(0);
163 inputModeBox->setEnabled(false);
164 surroundModeBox->setCurrentIndex(0);
165 surroundModeBox->setEnabled(false);
166 videoTrackBox->clear();
167 videoTrackBox->addItem(tr("default"));
168 videoTrackBox->setCurrentIndex(0);
169 videoTrackBox->setEnabled(false);
170 audioTrackBox->clear();
171 audioTrackBox->addItem(tr("default"));
172 audioTrackBox->setCurrentIndex(0);
173 audioTrackBox->setEnabled(false);
174 subtitleTrackBox->clear();
175 subtitleTrackBox->addItem(tr("none"));
176 subtitleTrackBox->setCurrentIndex(0);
177 subtitleTrackBox->setEnabled(false);
178 if (haveMetaData) {
179 inputModeBox->setEnabled(true);
180 surroundModeBox->setEnabled(true);
181 for (int i = 0; i < metaData.videoTracks.size(); i++) {
182 QString s = QString::number(i);
183 QLocale::Language l = static_cast<QLocale::Language>(metaData.videoTracks[i].value(QMediaMetaData::Language).toInt());
184 if (l != QLocale::AnyLanguage)
185 s += QString(tr(" (%1)")).arg(QLocale::languageToString(l));
186 videoTrackBox->addItem(s);
187 }
188 videoTrackBox->setEnabled(true);
189 for (int i = 0; i < metaData.audioTracks.size(); i++) {
190 QString s = QString::number(i);
191 QLocale::Language l = static_cast<QLocale::Language>(metaData.audioTracks[i].value(QMediaMetaData::Language).toInt());
192 if (l != QLocale::AnyLanguage)
193 s += QString(tr(" (%1)")).arg(QLocale::languageToString(l));
194 audioTrackBox->addItem(s);
195 }
196 audioTrackBox->setEnabled(true);
197 if (metaData.subtitleTracks.size() > 0)
198 subtitleTrackBox->addItem(tr("default"));
199 for (int i = 0; i < metaData.subtitleTracks.size(); i++) {
200 QString s = QString::number(i);
201 QLocale::Language l = static_cast<QLocale::Language>(metaData.subtitleTracks[i].value(QMediaMetaData::Language).toInt());
202 if (l != QLocale::AnyLanguage)
203 s += QString(tr(" (%1)")).arg(QLocale::languageToString(l));
204 subtitleTrackBox->addItem(s);
205 }
206 subtitleTrackBox->setEnabled(true);
207 }
208 }
209
210
211 PlaylistEditor::PlaylistEditor(QWidget* parent) :
212 QDialog(parent)
213 {
214 setModal(true);
215 setWindowTitle(tr("Edit Playlist"));
216
217 QGridLayout* layout = new QGridLayout;
218
219 table = new QTableWidget(this);
220 table->setColumnCount(6);
221 table->setHorizontalHeaderLabels({
222 tr("URL"),
223 tr("Input Mode"),
224 tr("Surround Mode"),
225 tr("Video Track"),
226 tr("Audio Track"),
227 tr("Subtitle Track") });
228 table->setSelectionMode(QAbstractItemView::SingleSelection);
229 table->setSelectionBehavior(QAbstractItemView::SelectRows);
230 table->horizontalHeader()->setHighlightSections(false);
231 table->verticalHeader()->setHighlightSections(false);
232 table->resizeColumnsToContents();
233 connect(table, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtonState()));
234 layout->addWidget(table, 0, 0, 7, 7);
235
236 upBtn = new QPushButton(tr("Move up"), this);
237 connect(upBtn, SIGNAL(clicked()), this, SLOT(up()));
238 layout->addWidget(upBtn, 0, 7, 1, 1);
239 downBtn = new QPushButton(tr("Move down"), this);
240 connect(downBtn, SIGNAL(clicked()), this, SLOT(down()));
241 layout->addWidget(downBtn, 1, 7, 1, 1);
242 addBtn = new QPushButton(tr("Add..."), this);
243 connect(addBtn, SIGNAL(clicked()), this, SLOT(add()));
244 layout->addWidget(addBtn, 2, 7, 1, 1);
245 delBtn = new QPushButton(tr("Remove"), this);
246 connect(delBtn, SIGNAL(clicked()), this, SLOT(del()));
247 layout->addWidget(delBtn, 3, 7, 1, 1);
248 editBtn = new QPushButton(tr("Edit..."), this);
249 connect(editBtn, SIGNAL(clicked()), this, SLOT(edit()));
250 layout->addWidget(editBtn, 4, 7, 1, 1);
251 QPushButton* doneBtn = new QPushButton(tr("Done"), this);
252 doneBtn->setDefault(true);
253 connect(doneBtn, SIGNAL(clicked()), this, SLOT(accept()));
254 layout->addWidget(doneBtn, 5, 7, 1, 1);
255
256 layout->addItem(new QSpacerItem(0, 0), 6, 7, 1, 1);
257 layout->setColumnStretch(0, 1);
258 layout->setRowStretch(0, 1);
259 setLayout(layout);
260
261 updateTable();
262 updateButtonState();
263 }
264
265 void PlaylistEditor::updateTable()
266 {
267 const Playlist* playlist = Playlist::instance();
268 int row = table->currentRow();
269 table->clearContents();
270 table->setRowCount(playlist->length());
271 for (int i = 0; i < playlist->length(); i++) {
272 const PlaylistEntry& entry = playlist->entries()[i];
273 table->setItem(i, 0, new QTableWidgetItem(entry.url.toString()));
274 table->setItem(i, 1, new QTableWidgetItem(inputModeToStringUI(entry.inputMode)));
275 table->setItem(i, 2, new QTableWidgetItem(surroundModeToStringUI(entry.surroundMode)));
276 table->setItem(i, 3, new QTableWidgetItem(
277 entry.videoTrack < 0 ? tr("default") :
278 QString::number(entry.videoTrack)));
279 table->setItem(i, 4, new QTableWidgetItem(
280 entry.audioTrack < 0 ? tr("default") :
281 QString::number(entry.audioTrack)));
282 table->setItem(i, 5, new QTableWidgetItem(
283 entry.subtitleTrack < -1 ? tr("none") :
284 entry.subtitleTrack < 0 ? tr("default") :
285 QString::number(entry.subtitleTrack)));
286 for (int j = 0; j < 6; j++)
287 table->item(i, j)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemNeverHasChildren);
288 }
289 table->setCurrentCell(row, 0, QItemSelectionModel::Rows);
290 table->resizeColumnsToContents();
291 }
292
293 void PlaylistEditor::updateButtonState()
294 {
295 int row = selectedRow();
296 bool haveSelection = (row >= 0 && row < table->rowCount());
297 upBtn->setEnabled(haveSelection && row > 0);
298 downBtn->setEnabled(haveSelection && row < table->rowCount() - 1);
299 delBtn->setEnabled(haveSelection);
300 editBtn->setEnabled(haveSelection);
301 }
302
303 int PlaylistEditor::selectedRow()
304 {
305 QList<QTableWidgetItem*> selection = table->selectedItems();
306 return (selection.isEmpty() ? -1 : selection[0]->row());
307 }
308
309 void PlaylistEditor::up()
310 {
311 int row = selectedRow();
312 if (row > 0) {
313 Playlist* playlist = Playlist::instance();
314 playlist->insert(row - 1, playlist->entries()[row]);
315 playlist->remove(row + 1);
316 updateTable();
317 table->setCurrentCell(row - 1, 0);
318 updateButtonState();
319 }
320 }
321
322 void PlaylistEditor::down()
323 {
324 int row = selectedRow();
325 if (row < table->rowCount() - 1) {
326 Playlist* playlist = Playlist::instance();
327 playlist->insert(row + 2, playlist->entries()[row]);
328 playlist->remove(row);
329 updateTable();
330 table->setCurrentCell(row + 1, 0);
331 updateButtonState();
332 }
333 }
334
335 void PlaylistEditor::add()
336 {
337 int row = selectedRow();
338 if (row < 0 || row > table->rowCount() - 1)
339 row = table->rowCount() - 1;
340 Playlist* playlist = Playlist::instance();
341 playlist->insert(row + 1, PlaylistEntry(QUrl("")));
342 updateTable();
343 table->setCurrentCell(row + 1, 0);
344 updateButtonState();
345 edit();
346 }
347
348 void PlaylistEditor::del()
349 {
350 int row = selectedRow();
351 if (row >= 0 && row < table->rowCount()) {
352 Playlist* playlist = Playlist::instance();
353 playlist->remove(row);
354 updateTable();
355 table->setCurrentCell(row, 0);
356 updateButtonState();
357 }
358 }
359
360 void PlaylistEditor::edit()
361 {
362 int row = selectedRow();
363 if (row >= 0 && row < table->rowCount()) {
364 Playlist* playlist = Playlist::instance();
365 PlaylistEntryEditor editor(playlist->entries()[row], this);
366 if (editor.exec() == QDialog::Accepted) {
367 playlist->entries()[row] = editor.entry;
368 updateTable();
369 table->setCurrentCell(row, 0);
370 }
371 }
372 }