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 QAction(tr("Most relevant"), this);
23 mostRelevantAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_1));
24 addAction(mostRelevantAction);
25 connect(mostRelevantAction, SIGNAL(triggered()), this, SLOT(searchMostRelevant()), Qt::QueuedConnection);
26 sortBar->addAction(mostRelevantAction);
27 mostRecentAction = new QAction(tr("Most recent"), this);
28 mostRecentAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_2));
29 addAction(mostRecentAction);
30 connect(mostRecentAction, SIGNAL(triggered()), this, SLOT(searchMostRecent()), Qt::QueuedConnection);
31 sortBar->addAction(mostRecentAction);
32 mostViewedAction = new QAction(tr("Most viewed"), this);
33 mostViewedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_3));
34 addAction(mostViewedAction);
35 connect(mostViewedAction, SIGNAL(triggered()), this, SLOT(searchMostViewed()), Qt::QueuedConnection);
36 sortBar->addAction(mostViewedAction);
38 listView = new QListView(this);
39 listView->setItemDelegate(new PrettyItemDelegate(this));
40 listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
43 listView->setDragEnabled(true);
44 listView->setAcceptDrops(true);
45 listView->setDropIndicatorShown(true);
46 listView->setDragDropMode(QAbstractItemView::DragDrop);
49 listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
50 listView->setFrameShape( QFrame::NoFrame );
51 listView->setAttribute(Qt::WA_MacShowFocusRect, false);
52 listView->setMinimumSize(320,240);
54 // respond to the user doubleclicking a playlist item
55 connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
57 listModel = new ListModel(this);
58 connect(listModel, SIGNAL(activeRowChanged(int)), this, SLOT(activeRowChanged(int)));
59 // needed to restore the selection after dragndrop
60 connect(listModel, SIGNAL(needSelectionFor(QList<Video*>)), this, SLOT(selectVideos(QList<Video*>)));
61 listView->setModel(listModel);
63 connect(listView->selectionModel(),
64 SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
65 this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
67 playlistWidget = new PlaylistWidget(this, sortBar, listView);
69 splitter->addWidget(playlistWidget);
71 videoAreaWidget = new VideoAreaWidget(this);
72 videoAreaWidget->setMinimumSize(320,240);
74 videoWidget = new Phonon::VideoWidget(this);
75 videoAreaWidget->setVideoWidget(videoWidget);
76 videoAreaWidget->setListModel(listModel);
78 loadingWidget = new LoadingWidget(this);
79 videoAreaWidget->setLoadingWidget(loadingWidget);
81 splitter->addWidget(videoAreaWidget);
83 layout->addWidget(splitter);
88 MediaView::~MediaView() {
92 void MediaView::initialize() {
93 connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
94 videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
95 connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
96 this, SLOT(showVideoContextMenu(QPoint)));
99 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
100 this->mediaObject = mediaObject;
101 Phonon::createPath(this->mediaObject, videoWidget);
102 // connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
103 connect(mediaObject, SIGNAL(finished()), this, SLOT(skip()));
104 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
105 this, SLOT(stateChanged(Phonon::State, Phonon::State)));
106 connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
107 this, SLOT(currentSourceChanged(Phonon::MediaSource)));
108 connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
111 void MediaView::search(SearchParams *searchParams) {
112 this->searchParams = searchParams;
114 // start serching for videos
115 listModel->search(searchParams);
117 // this implies that the enum and the bar action order is the same
118 sortBar->setCheckedAction(searchParams->sortBy()-1);
120 listView->setFocus();
122 loadingWidget->clear();
125 void MediaView::disappear() {
126 timerPlayFlag = true;
129 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
132 // qDebug() << "Phonon state: " << newState << oldState;
136 case Phonon::ErrorState:
137 qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
138 videoAreaWidget->showError(mediaObject->errorString());
139 // recover from errors by skipping to the next video
143 case Phonon::PlayingState:
145 videoAreaWidget->showVideo();
148 case Phonon::StoppedState:
150 // play() has already been called when setting the source
151 // but Phonon on Linux needs a little more help to start playback
154 // Workaround for Mac playback start problem
155 if (!timerPlayFlag) {
156 QTimer::singleShot(1000, this, SLOT(timerPlay()));
161 case Phonon::PausedState:
165 case Phonon::BufferingState:
169 case Phonon::LoadingState:
178 void MediaView::pause() {
179 // qDebug() << "pause() called" << mediaObject->state();
180 switch( mediaObject->state() ) {
181 case Phonon::PlayingState:
182 mediaObject->pause();
190 void MediaView::stop() {
191 listModel->abortSearch();
193 mediaObject->clear();
196 void MediaView::activeRowChanged(int row) {
197 Video *video = listModel->videoAt(row);
200 // immediately show the loading widget
201 videoAreaWidget->showLoading(video);
203 // mediaObject->pause();
205 connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
206 // TODO handle signal in a proper slot and impl item error status
207 connect(video, SIGNAL(errorStreamUrl()), SLOT(skip()));
208 video->loadStreamUrl();
210 // reset the timer flag
211 timerPlayFlag = false;
213 // video title in the statusbar
214 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
215 if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
217 // see you in gotStreamUrl...
221 void MediaView::gotStreamUrl(QUrl streamUrl) {
224 mediaObject->setCurrentSource(streamUrl);
227 // ensure we always have 10 videos ahead
228 listModel->searchNeeded();
230 // ensure active item is visible
231 int row = listModel->activeRow();
233 QModelIndex index = listModel->index(row, 0, QModelIndex());
234 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
239 void MediaView::itemActivated(const QModelIndex &index) {
240 if (listModel->rowExists(index.row()))
241 listModel->setActiveRow(index.row());
242 // the user doucleclicked on the "Search More" item
243 else listModel->searchMore();
246 void MediaView::aboutToFinish() {
248 int nextRow = listModel->nextRow();
249 if (nextRow == -1) return;
250 Video* video = listModel->videoAt(nextRow);
251 QUrl streamUrl = video->streamUrl();
252 qDebug() << "Enqueing" << streamUrl;
253 mediaObject->enqueue(streamUrl);
257 void MediaView::currentSourceChanged(const Phonon::MediaSource source) {
258 qDebug() << "Source changed:" << source.url();
261 void MediaView::skip() {
262 int nextRow = listModel->nextRow();
263 if (nextRow == -1) return;
264 listModel->setActiveRow(nextRow);
267 void MediaView::openWebPage() {
268 Video* video = listModel->activeVideo();
270 mediaObject->pause();
271 QDesktopServices::openUrl(video->webpage());
274 void MediaView::removeSelected() {
275 if (!listView->selectionModel()->hasSelection()) return;
276 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
277 listModel->removeIndexes(indexes);
280 void MediaView::selectVideos(QList<Video*> videos) {
281 foreach (Video *video, videos) {
282 QModelIndex index = listModel->indexForVideo(video);
283 listView->selectionModel()->select(index, QItemSelectionModel::Select);
284 listView->scrollTo(index, QAbstractItemView::EnsureVisible);
288 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
289 const bool gotSelection = listView->selectionModel()->hasSelection();
290 The::globalActions()->value("remove")->setEnabled(gotSelection);
291 The::globalActions()->value("moveUp")->setEnabled(gotSelection);
292 The::globalActions()->value("moveDown")->setEnabled(gotSelection);
295 void MediaView::moveUpSelected() {
296 if (!listView->selectionModel()->hasSelection()) return;
297 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
298 listModel->move(indexes, true);
301 void MediaView::moveDownSelected() {
302 if (!listView->selectionModel()->hasSelection()) return;
303 QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
304 listModel->move(indexes, false);
307 void MediaView::showVideoContextMenu(QPoint point) {
308 The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
311 void MediaView::searchMostRelevant() {
312 searchParams->setSortBy(SearchParams::SortByRelevance);
313 search(searchParams);
316 void MediaView::searchMostRecent() {
317 searchParams->setSortBy(SearchParams::SortByNewest);
318 search(searchParams);
321 void MediaView::searchMostViewed() {
322 searchParams->setSortBy(SearchParams::SortByViewCount);
323 search(searchParams);
326 void MediaView::setPlaylistVisible(bool visible) {
327 playlistWidget->setVisible(visible);
330 void MediaView::timerPlay() {
331 // qDebug() << mediaObject->currentTime();
332 // Workaround Phonon bug on Mac OSX
333 if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
334 mediaObject->pause();