]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
Better full screen impl
[minitube] / src / MediaView.cpp
1 #include "MediaView.h"
2 #include "playlist/PrettyItemDelegate.h"
3 #include "networkaccess.h"
4 #include "videowidget.h"
5 #include "minisplitter.h"
6
7 namespace The {
8     QMap<QString, QAction*>* globalActions();
9     QMap<QString, QMenu*>* globalMenus();
10     QNetworkAccessManager* networkAccessManager();
11 }
12
13 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
14
15     QBoxLayout *layout = new QHBoxLayout();
16     layout->setMargin(0);
17
18     splitter = new MiniSplitter(this);
19     splitter->setChildrenCollapsible(false);
20
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);
31
32     listView = new QListView(this);
33     listView->setItemDelegate(new Playlist::PrettyItemDelegate(this));
34     listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
35
36     // dragndrop
37     listView->setDragEnabled(true);
38     listView->setAcceptDrops(true);
39     listView->setDropIndicatorShown(true);
40     listView->setDragDropMode(QAbstractItemView::InternalMove);
41
42     // cosmetics
43     listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
44     listView->setFrameShape( QFrame::NoFrame );
45     listView->setAttribute(Qt::WA_MacShowFocusRect, false);
46     listView->setMinimumSize(320,240);
47
48     // respond to the user doubleclicking a playlist item
49     connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
50
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);
56
57     connect(listView->selectionModel(),
58             SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
59             this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
60
61     playlistWidget = new PlaylistWidget(this, sortBar, listView);
62
63     splitter->addWidget(playlistWidget);
64
65     videoAreaWidget = new VideoAreaWidget(this);
66     videoAreaWidget->setMinimumSize(320,240);
67
68     videoWidget = new Phonon::VideoWidget(this);
69     videoAreaWidget->setVideoWidget(videoWidget);
70
71     loadingWidget = new LoadingWidget(this);
72     videoAreaWidget->setLoadingWidget(loadingWidget);
73
74     splitter->addWidget(videoAreaWidget);
75
76     layout->addWidget(splitter);
77     setLayout(layout);
78
79 }
80
81 MediaView::~MediaView() {
82
83 }
84
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)));
90 }
91
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)));
102 }
103
104 void MediaView::search(SearchParams *searchParams) {
105     this->searchParams = searchParams;
106
107     // this implies that the enum and the bar action order is the same
108     sortBar->setCheckedAction(searchParams->sortBy()-1);
109
110     listModel->search(searchParams);
111     listView->setFocus();
112 }
113
114 void MediaView::disappear() {
115     timerPlayFlag = true;
116 }
117
118 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
119 {
120
121     // qDebug() << "Phonon state: " << newState << oldState;
122
123     switch (newState) {
124
125          case Phonon::ErrorState:
126         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
127         // recover from errors by skipping to the next video
128         skip();
129         break;
130
131          case Phonon::PlayingState:
132         qDebug("playing");
133         videoAreaWidget->showVideo();
134         break;
135
136          case Phonon::StoppedState:
137         qDebug("stopped");
138         // play() has already been called when setting the source
139         // but Phonon on Linux needs a little more help to start playback
140         mediaObject->play();
141         if (!timerPlayFlag) {
142             QTimer::singleShot(1000, this, SLOT(timerPlay()));
143         }
144         break;
145
146          case Phonon::PausedState:
147         qDebug("paused");
148         break;
149
150          case Phonon::BufferingState:
151         qDebug("buffering");
152         break;
153
154          case Phonon::LoadingState:
155         qDebug("loading");
156         break;
157
158          default:
159         ;
160     }
161 }
162
163 void MediaView::pause() {
164     switch( mediaObject->state() ) {
165     case Phonon::PlayingState:
166         mediaObject->pause();
167         break;
168     default:
169         mediaObject->play();
170         break;
171     }
172 }
173
174 void MediaView::stop() {
175     listModel->abortSearch();
176     mediaObject->stop();
177     mediaObject->clear();
178 }
179
180 void MediaView::activeRowChanged(int row) {
181     Video *video = listModel->videoAt(row);
182     if (!video) return;
183
184     // immediately show the loading widget
185     videoAreaWidget->showLoading(video);
186
187     // mediaObject->pause();
188
189     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
190     video->loadStreamUrl();
191
192     // reset the timer flag
193     timerPlayFlag = false;
194
195     // video title in the statusbar
196     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
197     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
198
199     // see you in gotStreamUrl...
200
201 }
202
203 void MediaView::gotStreamUrl(QUrl streamUrl) {
204
205     // go!
206     mediaObject->setCurrentSource(streamUrl);
207     mediaObject->play();
208
209     // ensure we always have 10 videos ahead
210     listModel->searchNeeded();
211
212     // ensure active item is visible
213     int row = listModel->activeRow();
214     if (row != -1) {
215         QModelIndex index = listModel->index(row, 0, QModelIndex());
216         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
217     }
218
219 }
220
221 void MediaView::itemActivated(const QModelIndex &index) {
222     if (listModel->rowExists(index.row()))
223         listModel->setActiveRow(index.row());
224     // the user doucleclicked on the "Search More" item
225     else listModel->searchMore();
226 }
227
228 void MediaView::aboutToFinish() {
229     /*
230     int nextRow = listModel->nextRow();
231     if (nextRow == -1) return;
232     Video* video = listModel->videoAt(nextRow);
233     QUrl streamUrl = video->streamUrl();
234     qDebug() << "Enqueing" << streamUrl;
235     mediaObject->enqueue(streamUrl);
236     */
237 }
238
239 void MediaView::currentSourceChanged(const Phonon::MediaSource source) {
240     qDebug() << "Source changed:" << source.url();
241 }
242
243 void MediaView::skip() {
244     int nextRow = listModel->nextRow();
245     if (nextRow == -1) return;
246     listModel->setActiveRow(nextRow);
247 }
248
249 void MediaView::openWebPage() {
250     Video* video = listModel->activeVideo();
251     if (!video) return;
252     mediaObject->pause();
253     QDesktopServices::openUrl(video->webpage());
254 }
255
256 void MediaView::removeSelected() {
257     if (!listView->selectionModel()->hasSelection()) return;
258     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
259     listModel->removeIndexes(indexes);
260 }
261
262 void MediaView::selectVideos(QList<Video*> videos) {
263     foreach (Video *video, videos) {
264         QModelIndex index = listModel->indexForVideo(video);
265         listView->selectionModel()->select(index, QItemSelectionModel::Select);
266         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
267     }
268 }
269
270 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
271     const bool gotSelection = listView->selectionModel()->hasSelection();
272     The::globalActions()->value("remove")->setEnabled(gotSelection);
273     The::globalActions()->value("moveUp")->setEnabled(gotSelection);
274     The::globalActions()->value("moveDown")->setEnabled(gotSelection);
275 }
276
277 void MediaView::moveUpSelected() {
278     if (!listView->selectionModel()->hasSelection()) return;
279     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
280     listModel->move(indexes, true);
281 }
282
283 void MediaView::moveDownSelected() {
284     if (!listView->selectionModel()->hasSelection()) return;
285     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
286     listModel->move(indexes, false);
287 }
288
289 void MediaView::showVideoContextMenu(QPoint point) {
290     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
291 }
292
293 void MediaView::searchMostRelevant() {
294     searchParams->setSortBy(SearchParams::SortByRelevance);
295     search(searchParams);
296 }
297
298 void MediaView::searchMostRecent() {
299     searchParams->setSortBy(SearchParams::SortByNewest);
300     search(searchParams);
301 }
302
303 void MediaView::searchMostViewed() {
304     searchParams->setSortBy(SearchParams::SortByViewCount);
305     search(searchParams);
306 }
307
308 void MediaView::setPlaylistVisible(bool visible) {
309     playlistWidget->setVisible(visible);
310 }
311
312 void MediaView::timerPlay() {
313     // qDebug() << mediaObject->currentTime();
314     // Workaround Phonon bug on Mac OSX
315     if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
316         mediaObject->pause();
317         mediaObject->play();
318     }
319 }