2 #include "playlist/PrettyItemDelegate.h"
3 #include "networkaccess.h"
4 #include "videowidget.h"
5 #include "minisplitter.h"
8 QMap<QString, QAction*>* globalActions();
9 QMap<QString, QMenu*>* globalMenus();
10 QNetworkAccessManager* networkAccessManager();
13 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
15 QBoxLayout *layout = new QHBoxLayout();
18 splitter = new MiniSplitter(this);
19 splitter->setChildrenCollapsible(false);
21 sortBar = new THBlackBar(this);
22 mostRelevantAction = new THAction(tr("Most relevant"), this);
23 connect(mostRelevantAction, SIGNAL(triggered()), this, SLOT(searchMostRelevant()), Qt::QueuedConnection);
24 sortBar->addAction(mostRelevantAction);
25 mostRecentAction = new THAction(tr("Most recent"), this);
26 connect(mostRecentAction, SIGNAL(triggered()), this, SLOT(searchMostRecent()), Qt::QueuedConnection);
27 sortBar->addAction(mostRecentAction);
28 mostViewedAction = new THAction(tr("Most viewed"), this);
29 connect(mostViewedAction, SIGNAL(triggered()), this, SLOT(searchMostViewed()), Qt::QueuedConnection);
30 sortBar->addAction(mostViewedAction);
32 listView = new QListView(this);
33 listView->setItemDelegate(new Playlist::PrettyItemDelegate(this));
34 listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
37 listView->setDragEnabled(true);
38 listView->setAcceptDrops(true);
39 listView->setDropIndicatorShown(true);
40 listView->setDragDropMode(QAbstractItemView::InternalMove);
43 listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
44 listView->setFrameShape( QFrame::NoFrame );
45 listView->setAttribute(Qt::WA_MacShowFocusRect, false);
46 listView->setMinimumSize(320,240);
48 // respond to the user doubleclicking a playlist item
49 connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
51 listModel = new ListModel(this);
52 connect(listModel, SIGNAL(activeRowChanged(int)), this, SLOT(activeRowChanged(int)));
53 // needed to restore the selection after dragndrop
54 connect(listModel, SIGNAL(needSelectionFor(QList<Video*>)), this, SLOT(selectVideos(QList<Video*>)));
55 listView->setModel(listModel);
57 connect(listView->selectionModel(),
58 SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
59 this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
61 playlistWidget = new PlaylistWidget(this, sortBar, listView);
63 splitter->addWidget(playlistWidget);
65 videoAreaWidget = new VideoAreaWidget(this);
66 videoAreaWidget->setMinimumSize(320,240);
68 videoWidget = new Phonon::VideoWidget(this);
69 videoAreaWidget->setVideoWidget(videoWidget);
71 loadingWidget = new LoadingWidget(this);
72 videoAreaWidget->setLoadingWidget(loadingWidget);
74 splitter->addWidget(videoAreaWidget);
76 layout->addWidget(splitter);
81 MediaView::~MediaView() {
85 void MediaView::initialize() {
86 connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
87 videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
88 connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
89 this, SLOT(showVideoContextMenu(QPoint)));
92 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
93 this->mediaObject = mediaObject;
94 Phonon::createPath(this->mediaObject, videoWidget);
95 // connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
96 connect(mediaObject, SIGNAL(finished()), this, SLOT(skip()));
97 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
98 this, SLOT(stateChanged(Phonon::State, Phonon::State)));
99 connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
100 this, SLOT(currentSourceChanged(Phonon::MediaSource)));
101 connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
104 void MediaView::search(SearchParams *searchParams) {
105 this->searchParams = searchParams;
107 // start serching for videos
108 listModel->search(searchParams);
110 // this implies that the enum and the bar action order is the same
111 sortBar->setCheckedAction(searchParams->sortBy()-1);
113 listView->setFocus();
115 loadingWidget->clear();
118 void MediaView::disappear() {
119 timerPlayFlag = true;
122 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
125 // qDebug() << "Phonon state: " << newState << oldState;
129 case Phonon::ErrorState:
130 qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
131 // recover from errors by skipping to the next video
135 case Phonon::PlayingState:
137 videoAreaWidget->showVideo();
140 case Phonon::StoppedState:
142 // play() has already been called when setting the source
143 // but Phonon on Linux needs a little more help to start playback
146 // Workaround for Mac playback start problem
147 if (!timerPlayFlag) {
148 QTimer::singleShot(1000, this, SLOT(timerPlay()));
153 case Phonon::PausedState:
157 case Phonon::BufferingState:
161 case Phonon::LoadingState:
170 void MediaView::pause() {
171 switch( mediaObject->state() ) {
172 case Phonon::PlayingState:
173 mediaObject->pause();
181 void MediaView::stop() {
182 listModel->abortSearch();
184 mediaObject->clear();
187 void MediaView::activeRowChanged(int row) {
188 Video *video = listModel->videoAt(row);
191 // immediately show the loading widget
192 videoAreaWidget->showLoading(video);
194 // mediaObject->pause();
196 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
197 video->loadStreamUrl();
199 // reset the timer flag
200 timerPlayFlag = false;
202 // video title in the statusbar
203 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
204 if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
206 // see you in gotStreamUrl...
210 void MediaView::gotStreamUrl(QUrl streamUrl) {
213 mediaObject->setCurrentSource(streamUrl);
216 // ensure we always have 10 videos ahead
217 listModel->searchNeeded();
219 // ensure active item is visible
220 int row = listModel->activeRow();
222 QModelIndex index = listModel->index(row, 0, QModelIndex());
223 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
228 void MediaView::itemActivated(const QModelIndex &index) {
229 if (listModel->rowExists(index.row()))
230 listModel->setActiveRow(index.row());
231 // the user doucleclicked on the "Search More" item
232 else listModel->searchMore();
235 void MediaView::aboutToFinish() {
237 int nextRow = listModel->nextRow();
238 if (nextRow == -1) return;
239 Video* video = listModel->videoAt(nextRow);
240 QUrl streamUrl = video->streamUrl();
241 qDebug() << "Enqueing" << streamUrl;
242 mediaObject->enqueue(streamUrl);
246 void MediaView::currentSourceChanged(const Phonon::MediaSource source) {
247 qDebug() << "Source changed:" << source.url();
250 void MediaView::skip() {
251 int nextRow = listModel->nextRow();
252 if (nextRow == -1) return;
253 listModel->setActiveRow(nextRow);
256 void MediaView::openWebPage() {
257 Video* video = listModel->activeVideo();
259 mediaObject->pause();
260 QDesktopServices::openUrl(video->webpage());
263 void MediaView::removeSelected() {
264 if (!listView->selectionModel()->hasSelection()) return;
265 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
266 listModel->removeIndexes(indexes);
269 void MediaView::selectVideos(QList<Video*> videos) {
270 foreach (Video *video, videos) {
271 QModelIndex index = listModel->indexForVideo(video);
272 listView->selectionModel()->select(index, QItemSelectionModel::Select);
273 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
277 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
278 const bool gotSelection = listView->selectionModel()->hasSelection();
279 The::globalActions()->value("remove")->setEnabled(gotSelection);
280 The::globalActions()->value("moveUp")->setEnabled(gotSelection);
281 The::globalActions()->value("moveDown")->setEnabled(gotSelection);
284 void MediaView::moveUpSelected() {
285 if (!listView->selectionModel()->hasSelection()) return;
286 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
287 listModel->move(indexes, true);
290 void MediaView::moveDownSelected() {
291 if (!listView->selectionModel()->hasSelection()) return;
292 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
293 listModel->move(indexes, false);
296 void MediaView::showVideoContextMenu(QPoint point) {
297 The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
300 void MediaView::searchMostRelevant() {
301 searchParams->setSortBy(SearchParams::SortByRelevance);
302 search(searchParams);
305 void MediaView::searchMostRecent() {
306 searchParams->setSortBy(SearchParams::SortByNewest);
307 search(searchParams);
310 void MediaView::searchMostViewed() {
311 searchParams->setSortBy(SearchParams::SortByViewCount);
312 search(searchParams);
315 void MediaView::setPlaylistVisible(bool visible) {
316 playlistWidget->setVisible(visible);
319 void MediaView::timerPlay() {
320 // qDebug() << mediaObject->currentTime();
321 // Workaround Phonon bug on Mac OSX
322 if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
323 mediaObject->pause();