-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtubeview.cpp
executable file
·233 lines (194 loc) · 9.55 KB
/
youtubeview.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
#include "youtubeview.h"
#include "youtubelistmodel.h"
#include "youtubelistitemdelegate.h"
#include "default_values.h"
#include <QApplication>
#include <QKeyEvent>
#include <QSettings>
#include <QMenu>
#include "youtubecommentsdialog.h"
#include "startextplayerhelper.h"
extern QSettings *theSettings;
YoutubeView::YoutubeView(QWidget *parent) : QListView(parent) {
prev_model = model();
prev_sel_model = selectionModel();
setResizeMode(QListView::Adjust);
setItemDelegate(new YoutubeListItemDelegate(this));
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
connect(this,SIGNAL(activated(const QModelIndex &)),this,SLOT(item_activated(const QModelIndex &)));
}
void YoutubeView::setModel(QAbstractItemModel * model) {
QListView::setModel(model);
emit model_changed();
}
void YoutubeView::setData(const QList<Media> & medias) {
setIconSize(theSettings->value("preview_size",QSize(PREVIEW_WIDTH,PREVIEW_HEIGHT)).toSize());
if (prev_model != NULL) prev_model->removeRows(0,model()->rowCount());
reset();
setModel(new YoutubeListModel(medias,this));
if (prev_model != NULL) prev_model->deleteLater();
if (prev_sel_model != NULL) prev_sel_model->deleteLater();
prev_model = model();
prev_sel_model = selectionModel();
connect(selectionModel(),SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection &)),this,SLOT(on_selectionChanged(const QItemSelection &,const QItemSelection &)));
QMetaObject::invokeMethod(this,"_updateGeometry",Qt::QueuedConnection);
}
void YoutubeView::_updateGeometry() {
resize(QSize(size().width(),size().height()-1));
QMetaObject::invokeMethod(this,"_updateGeometry2",Qt::QueuedConnection);
}
void YoutubeView::_updateGeometry2() {
resize(QSize(size().width(),size().height()+1));
}
void YoutubeView::item_activated(const QModelIndex & /*index*/) {
execPlayer();
}
void YoutubeView::resizeEvent(QResizeEvent * event) {
QListView::resizeEvent(event);
emit model_changed();
}
void YoutubeView::execPlayer(const QUrl & video_url,const QUrl & audio_url) {
QAction * source_object = (QAction *)QObject::sender();
QModelIndexList sel_list = selectionModel()->selectedRows();
if (sel_list.count() <= 0) return;
QUrl m_video_url = video_url;
QUrl m_audio_url = audio_url;
Media * media = (Media *)sel_list.at(0).data(Qt::UserRole).value<void *>();
if (!video_url.isValid()) {
QApplication::setOverrideCursor(Qt::WaitCursor);
VideoInfo info_best = media->best_video();
QApplication::restoreOverrideCursor();
m_video_url = info_best.url();
m_audio_url = info_best.audio_url();
}
if (!video_url.isValid() && (source_object != NULL) && source_object->property("video_url").isValid()) {
m_video_url = source_object->property("video_url").toUrl();
m_audio_url = source_object->property("audio_url").toUrl();
}
if (!m_video_url.isValid()) {
m_video_url = media->url();
m_audio_url = QUrl();
}
new StartExtPlayerHelper(m_video_url,m_audio_url,this,media->subtitlesData());
}
void YoutubeView::execPlayer(const QObject *receiver,const char * execute_at_exit) {
if (receiver == NULL || execute_at_exit == NULL) return;
QModelIndexList sel_list = selectionModel()->selectedRows();
if (sel_list.count() <= 0) return;
Media * media = (Media *)sel_list.at(0).data(Qt::UserRole).value<void *>();
QApplication::setOverrideCursor(Qt::WaitCursor);
VideoInfo info_best = media->best_video();
QApplication::restoreOverrideCursor();
new StartExtPlayerHelper(info_best.url(),info_best.audio_url(),this,media->subtitlesData(),receiver,execute_at_exit);
}
void YoutubeView::download(const QUrl & url) {
QAction * source_object = (QAction *)QObject::sender();
QModelIndexList sel_list = selectionModel()->selectedRows();
if (sel_list.count() <= 0) return;
Media * media = (Media *)sel_list.at(0).data(Qt::UserRole).value<void *>();
QApplication::setOverrideCursor(Qt::WaitCursor);
VideoInfo info = media->best_video();
QApplication::restoreOverrideCursor();
QString m_filename = info.filename();
if (source_object->property("subtitles").isValid()) {
emit download_subs_request(source_object->property("subtitles").value<Subtitle>(),QFileInfo(m_filename).baseName() + ".srt");
return;
}
QUrl m_url = url;
if (!url.isValid()) m_url = info.url();
if (!url.isValid() && (source_object != NULL) && source_object->property("video_url").isValid()) {
m_url = source_object->property("video_url").toUrl();
m_filename = source_object->property("video_filename").toString();
}
if (!m_url.isValid()) m_url = media->url();
emit download_request(m_url,m_filename);
}
void YoutubeView::contextMenuEvent(QContextMenuEvent * e) {
QListView::contextMenuEvent(e);
QModelIndexList sel_list = selectionModel()->selectedRows();
if (sel_list.count() <= 0) return;
QMenu menu(this);
QMenu download_menu;
QMenu subtitles_menu;
QMenu playerMenu;
Media * media = (Media *)sel_list.at(0).data(Qt::UserRole).value<void *>();
QApplication::setOverrideCursor(Qt::WaitCursor);
QList<VideoInfo> real_links = media->video_infos();
QList<Subtitle> subtitles = media->subtitles();
QApplication::restoreOverrideCursor();
if (real_links.count() <= 0) {
menu.addAction(QIcon(":/images/res/media-playback-start.png"),tr("Play with external player..."),this,SLOT(execPlayer()));
menu.addAction(QIcon(":/images/res/download.png"),tr("Download..."),this,SLOT(download()));
}
else {
download_menu.setIcon(QIcon(":/images/res/download.png"));
download_menu.setTitle(tr("Download..."));
playerMenu.setIcon(QIcon(":/images/res/media-playback-start.png"));
playerMenu.setTitle(tr("Play with external player..."));
subtitles_menu.setIcon(QIcon(":/images/res/subtitles.png"));
subtitles_menu.setTitle(tr("Subtitles"));
if (subtitles.count() > 0) download_menu.addMenu(&subtitles_menu);
int i;
for (i=0;i<real_links.count();i++) {
if (!real_links[i].isAudioOnly()) {
QAction * action = playerMenu.addAction(QIcon(":/images/res/tool-animator.png"),real_links[i].desc().toString(),this,SLOT(execPlayer()));
action->setProperty("video_url",real_links[i].url());
action->setProperty("audio_url",real_links[i].audio_url());
}
QAction * action = download_menu.addAction(QIcon(!real_links[i].isAudioOnly()?":/images/res/tool-animator.png":":/images/res/audio-volume-high.png"),real_links[i].desc().toStringFull(),this,SLOT(download()));
action->setProperty("video_url",real_links[i].url());
action->setProperty("video_filename",real_links[i].filename());
}
for (i=0;i<subtitles.count();i++) {
QAction * action = subtitles_menu.addAction(QIcon(":/images/res/srt.png"),subtitles[i].toString(),this,SLOT(download()));
action->setProperty("subtitles",QVariant::fromValue<Subtitle>(subtitles[i]));
}
menu.addMenu(&playerMenu);
menu.addMenu(&download_menu);
}
menu.addAction(QIcon(":/images/res/find-user.png"),tr("View channel")+" \""+media->author()+"\"",this,SLOT(view_uploader_channel()));
menu.addAction(QIcon(":/images/res/find-user-video.png"),tr("Search videos on channel")+" \""+media->author()+"\"...",this,SLOT(show_channel_videos_popup()));
menu.addAction(QIcon(":/images/res/help-about.png"),tr("Information..."),this,SLOT(show_info()));
menu.exec(e->globalPos());
}
void YoutubeView::show_channel_videos_popup() {
QModelIndexList sel_list = selectionModel()->selectedRows();
if (sel_list.count() <= 0) return;
Media * media = (Media *)sel_list.at(0).data(Qt::UserRole).value<void *>();
emit channel_videos_popup_requested(media->author());
}
void YoutubeView::view_uploader_channel() {
QModelIndexList sel_list = selectionModel()->selectedRows();
if (sel_list.count() <= 0) return;
Media * media = (Media *)sel_list.at(0).data(Qt::UserRole).value<void *>();
emit search_requested("","",media->author(),"",relevance,YoutubeTime());
}
void YoutubeView::show_info() {
QModelIndexList sel_list = selectionModel()->selectedRows();
if (sel_list.count() <= 0) return;
YoutubeCommentsDialog((Media *)sel_list.at(0).data(Qt::UserRole).value<void *>(),this).exec();
}
void YoutubeView::on_selectionChanged(const QItemSelection & selected,const QItemSelection &) {
if (selected.isEmpty()) emit mediaListIsEmpty();
else emit indexSelected(selected.indexes().at(0));
}
QModelIndex YoutubeView::selectNextIndexAfter(const QModelIndex & index) {
if ((index.row() + 1) >= model()->rowCount()) return QModelIndex();
QModelIndex next = model()->index(index.row()+1,index.column());
if (!next.isValid()) return QModelIndex();
QItemSelectionModel * sel_model = selectionModel();
if (sel_model == NULL) return QModelIndex();
sel_model->select(next,QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Current);
scrollTo(next);
return next;
}
QModelIndex YoutubeView::selectPrevIndexBefore(const QModelIndex & index) {
if ((index.row() - 1) < 0) return QModelIndex();
QModelIndex prev = model()->index(index.row()-1,index.column());
if (!prev.isValid()) return QModelIndex();
QItemSelectionModel * sel_model = selectionModel();
if (sel_model == NULL) return QModelIndex();
sel_model->select(prev,QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Current);
scrollTo(prev);
return prev;
}