-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
executable file
·275 lines (224 loc) · 9.38 KB
/
mainwindow.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
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
269
270
271
272
273
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// We want all actions be relative to the path where the application was started
QDir::setCurrent(QCoreApplication::applicationDirPath());
// Starts downloading all currently added videos and all videos added while still in download mode
QAction *actionPlay = ui->toolBar->addAction(QIcon(":/toolbar/icons/play.png"), tr("Play"));
connect(actionPlay, SIGNAL(triggered(bool)), this, SLOT(actionPlayTriggered(bool)));
QAction *actionStop = ui->toolBar->addAction(QIcon(":/toolbar/icons/stop.png"), tr("Stop"));
connect(actionStop, SIGNAL(triggered(bool)), this, SLOT(actionStopTriggered(bool)));
// Unused for now. TODO: Implement
//ui->toolBar->addAction(QIcon(":/toolbar/icons/pause.png"), tr("Pause"));
this->setupTableModel();
this->mediaDownloader = new MediaDownloader(this->tableModel);
this->clipboard = QApplication::clipboard();
updateSettingPanel();
// this->tableModel->addLink("Test1");
// this->tableModel->addLink("Test2");
// auto a = this->tableModel->addLink("Test1");
// this->tableModel->addLink("Test2", a);
}
MainWindow::~MainWindow()
{
delete ui;
delete this->mediaDownloader;
delete this->tableModel;
}
void MainWindow::setupTableModel()
{
this->tableModel = new TableModel();
this->ui->treeTrackView->header()->setSectionResizeMode(QHeaderView::Stretch); // This will set all rows to equal length
this->ui->treeTrackView->setModel(this->tableModel);
// Enable drag and drop
ui->treeTrackView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->treeTrackView->setDragEnabled(true);
ui->treeTrackView->viewport()->setAcceptDrops(true);
ui->treeTrackView->setDropIndicatorShown(true);
ui->treeTrackView->setDragDropMode(QAbstractItemView::DragDrop);
ui->treeTrackView->setDefaultDropAction(Qt::MoveAction);
ui->treeTrackView->setContextMenuPolicy(Qt::CustomContextMenu);
// this->tableModel->addLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
// MediaObject *parentLink1 = this->tableModel->addLink("Link1");
// this->tableModel->addLink("Link1.1", parentLink1);
// MediaObject *parentLink11 = this->tableModel->addLink("Link1.2", parentLink1);
// this->tableModel->addLink("Link1.2.1", parentLink11);
// this->tableModel->addLink("Link1.2.2", parentLink11);
// this->tableModel->addLink("Link1.2.3", parentLink11);
// this->tableModel->addLink("Link1.3", parentLink1);
// this->tableModel->addLink("Link2");
// this->tableModel->addLink("Link3");
// this->tableModel->addLink("Link4");
connect(ui->treeTrackView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(tableViewCustomContextMenu(QPoint))); // Custom Context Menu
connect(ui->treeTrackView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(onSelectionChanged(QItemSelection,QItemSelection)));
}
void MainWindow::tableViewCustomContextMenu(QPoint pos)
{
// TODO: We ( usually ) want context actions to apply to all selected items, so we need to iterate over them here and apply the action to each of them
if(!ui->treeTrackView->indexAt(pos).isValid())
{
return;
}
QModelIndex selectedIndex = ui->treeTrackView->indexAt(pos);
MediaObject *link = static_cast<TreeNode*>(selectedIndex.internalPointer())->getLink();
qDebug() << link->getData(MediaObject::DATA_TITLE);
QMenu menu(this);
QAction *deleteRow = new QAction("Delete", &menu); // Deletes an entry
if(!link->getData(MediaObject::DATA_IS_STARTED).toBool()) {
menu.addAction(deleteRow);
}
QAction *retryLink = new QAction("Retry", &menu);
if(link->getData(MediaObject::DATA_IS_FAILED).toBool())
{
menu.addAction(retryLink);
}
QAction *renameContainer = new QAction("Rename", &menu);
if(link->getData(MediaObject::DATA_IS_CONTAINER).toBool())
{
menu.addAction(renameContainer);
}
QAction *selectedAction = menu.exec(ui->treeTrackView->viewport()->mapToGlobal(pos));
if(selectedAction == deleteRow)
{
this->tableModel->deleteIndex(selectedIndex);
return;
}
if(selectedAction == retryLink)
{
link->setData(MediaObject::DATA_IS_FAILED, QVariant(false));
link->setData(MediaObject::DATA_IS_STARTED, QVariant(false));
return;
}
if(selectedAction == renameContainer)
{
bool ok;
QString newName = QInputDialog::getText(this, "Rename Container", "Enter a new name:", QLineEdit::Normal, QString(), &ok);
if(ok && !newName.isEmpty())
{
link->setData(MediaObject::DATA_TITLE, QVariant(newName));
}
}
}
void MainWindow::on_buttonAddTrack_clicked()
{
this->mediaDownloader->addLink(ui->inputUrl->text());
}
void MainWindow::on_actionSettings_triggered()
{
WindowSettings *ws = new WindowSettings(this);
connect(ws, &WindowSettings::destroyed, [=](QObject *obj){
this->updateSettingPanel();
});
int startX = 0, startY = 0;
startX = int (this->geometry().x() + this->geometry().width() / 2 - ws->geometry().width() / 2);
startY = int (this->geometry().y() + this->geometry().height() / 2 - ws->geometry().height() /2);
ws->setGeometry(startX, startY, ws->geometry().width(), ws->geometry().height());
ws->setWindowModality(Qt::WindowModal);
ws->setAttribute(Qt::WA_DeleteOnClose);
ws->show();
}
void MainWindow::actionPlayTriggered(bool checked)
{
this->mediaDownloader->startDownload();
}
void MainWindow::actionStopTriggered(bool checked)
{
this->mediaDownloader->stopDownload();
}
void MainWindow::on_actionClipboard_Watchdog_toggled(bool checked)
{
if(checked)
{
connect(this->clipboard, SIGNAL(dataChanged()), this, SLOT(onClipboardChanged()));
} else
{
disconnect(this->clipboard, SIGNAL(dataChanged()), this, SLOT(onClipboardChanged()));
}
}
void MainWindow::onClipboardChanged()
{
QStringList formats = this->clipboard->mimeData(QClipboard::Clipboard)->formats();
QString text;
if(formats.contains("text/html") || formats.contains("text/plain"))
{
QString type = formats.contains("text/html")?"text/html":"text/plain";
text = QString(this->clipboard->mimeData(QClipboard::Clipboard)->data(type));
if(this->lastClipboard == text)
{
return;
}
this->lastClipboard = text;
this->mediaDownloader->clipboardChanged(text);
}
}
void MainWindow::on_actionAnalyse_clipboard_for_links_triggered()
{
this->onClipboardChanged();
}
void MainWindow::on_actionAbout_triggered()
{
QApplication::aboutQt();
}
void MainWindow::on_actionAbout_2_triggered()
{
QMessageBox::about(this, "MediaDownloader", "<h2>MediaDownloader</h2><p>This is a minimal frontend for the cli of youtube-dl.</p><p>You can find me on Github: <a href='https://github.com/Mydayyy/MediaDownloader'>https://github.com/Mydayyy/MediaDownloader</a></p>");
}
// INDIVIDUAL SONG SETTINGS STARTING HERE
void MainWindow::onSelectionChanged(const QItemSelection &deselected, const QItemSelection &selected)
{
this->updateSettingPanel();
}
void MainWindow::setSettingForCurrentObjects(QString key, QVariant val) {
QModelIndexList selectedIndexes = this->ui->treeTrackView->selectionModel()->selectedIndexes();
QList<TreeNode*> treeNodes = this->tableModel->convertIndexListToTreeNodeList(selectedIndexes);
for(auto it = treeNodes.begin(); it != treeNodes.end(); it++) {
(*it)->getLink()->getSettings()->set(key, val);
this->tableModel->redrawMediaObject((*it)->getLink());
}
}
void MainWindow::updateSettingPanel()
{
QModelIndexList selectedIndexes = this->ui->treeTrackView->selectionModel()->selectedIndexes();
QList<TreeNode*> treeNodes = this->tableModel->convertIndexListToTreeNodeList(selectedIndexes);
if(treeNodes.size() != 1) {
ui->settingsPanel->hide();
return;
}
ui->settingsPanel->show();
TreeNode *tn = treeNodes.at(0);
ui->inputSavePath->setText(tn->getSettingsValue("downloadSavePath").toString());
QString format = tn->getSettingsValue("audioFormat").toString();
int index = ui->audioFormat->findText(format);
ui->audioFormat->setCurrentIndex(index);
ui->extractOnlyAudio->setChecked(tn->getSettingsValue("extractOnlyAudio").toBool());
}
void MainWindow::on_buttonBrowseFiles_clicked()
{
QFileDialog fileBrowser(this, "Choose a download location", Settings::getInstance().get("downloadSavePath").toString());
fileBrowser.setFileMode(QFileDialog::DirectoryOnly);
if(fileBrowser.exec() == QDialog::Accepted)
{
if(fileBrowser.selectedFiles().size() > 0)
{
QString savePath = fileBrowser.selectedFiles()[0];
setSettingForCurrentObjects("downloadSavePath", savePath);
ui->inputSavePath->setText(savePath);
}
}
}
void MainWindow::on_inputSavePath_textEdited(const QString &text)
{
setSettingForCurrentObjects("downloadSavePath", this->ui->inputSavePath->text());
}
void MainWindow::on_extractOnlyAudio_toggled(bool checked)
{
setSettingForCurrentObjects("extractOnlyAudio", QVariant(checked));
}
void MainWindow::on_audioFormat_currentTextChanged(const QString &arg1)
{
setSettingForCurrentObjects("audioFormat", QVariant(arg1));
}