]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
9825010356c1e9813bab978b44f5b1041bfa0184
[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 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::DragDrop);
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     videoAreaWidget->setListModel(listModel);
71
72     loadingWidget = new LoadingWidget(this);
73     videoAreaWidget->setLoadingWidget(loadingWidget);
74
75     splitter->addWidget(videoAreaWidget);
76
77     layout->addWidget(splitter);
78     setLayout(layout);
79
80 }
81
82 MediaView::~MediaView() {
83
84 }
85
86 void MediaView::initialize() {
87     connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
88     videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
89     connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
90             this, SLOT(showVideoContextMenu(QPoint)));
91 }
92
93 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
94     this->mediaObject = mediaObject;
95     Phonon::createPath(this->mediaObject, videoWidget);
96     // connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
97     connect(mediaObject, SIGNAL(finished()), this, SLOT(skip()));
98     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
99             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
100     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
101             this, SLOT(currentSourceChanged(Phonon::MediaSource)));
102     connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
103 }
104
105 void MediaView::search(SearchParams *searchParams) {
106     this->searchParams = searchParams;
107
108     // start serching for videos
109     listModel->search(searchParams);
110
111     // this implies that the enum and the bar action order is the same
112     sortBar->setCheckedAction(searchParams->sortBy()-1);
113
114     listView->setFocus();
115
116     loadingWidget->clear();
117 }
118
119 void MediaView::disappear() {
120     timerPlayFlag = true;
121 }
122
123 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
124 {
125
126     // qDebug() << "Phonon state: " << newState << oldState;
127
128     switch (newState) {
129
130          case Phonon::ErrorState:
131         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
132         // recover from errors by skipping to the next video
133         skip();
134         break;
135
136          case Phonon::PlayingState:
137         qDebug("playing");
138         videoAreaWidget->showVideo();
139         break;
140
141          case Phonon::StoppedState:
142         qDebug("stopped");
143         // play() has already been called when setting the source
144         // but Phonon on Linux needs a little more help to start playback
145         mediaObject->play();
146
147         // Workaround for Mac playback start problem
148         if (!timerPlayFlag) {
149             QTimer::singleShot(1000, this, SLOT(timerPlay()));
150         }
151
152         break;
153
154          case Phonon::PausedState:
155         qDebug("paused");
156         break;
157
158          case Phonon::BufferingState:
159         qDebug("buffering");
160         break;
161
162          case Phonon::LoadingState:
163         qDebug("loading");
164         break;
165
166          default:
167         ;
168     }
169 }
170
171 void MediaView::pause() {
172     // qDebug() << "pause() called" << mediaObject->state();
173     switch( mediaObject->state() ) {
174     case Phonon::PlayingState:
175         mediaObject->pause();
176         break;
177     default:
178         mediaObject->play();
179         break;
180     }
181 }
182
183 void MediaView::stop() {
184     listModel->abortSearch();
185     mediaObject->stop();
186     mediaObject->clear();
187 }
188
189 void MediaView::activeRowChanged(int row) {
190     Video *video = listModel->videoAt(row);
191     if (!video) return;
192
193     // immediately show the loading widget
194     videoAreaWidget->showLoading(video);
195
196     // mediaObject->pause();
197
198     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
199     // TODO handle signal in a proper slot and impl item error status
200     connect(video, SIGNAL(errorStreamUrl()), SLOT(skip()));
201     video->loadStreamUrl();
202
203     // reset the timer flag
204     timerPlayFlag = false;
205
206     // video title in the statusbar
207     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
208     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
209
210     // see you in gotStreamUrl...
211
212 }
213
214 void MediaView::gotStreamUrl(QUrl streamUrl) {
215
216     // go!
217     mediaObject->setCurrentSource(streamUrl);
218     mediaObject->play();
219
220     // ensure we always have 10 videos ahead
221     listModel->searchNeeded();
222
223     // ensure active item is visible
224     int row = listModel->activeRow();
225     if (row != -1) {
226         QModelIndex index = listModel->index(row, 0, QModelIndex());
227         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
228     }
229
230 }
231
232 void MediaView::itemActivated(const QModelIndex &index) {
233     if (listModel->rowExists(index.row()))
234         listModel->setActiveRow(index.row());
235     // the user doucleclicked on the "Search More" item
236     else listModel->searchMore();
237 }
238
239 void MediaView::aboutToFinish() {
240     /*
241     int nextRow = listModel->nextRow();
242     if (nextRow == -1) return;
243     Video* video = listModel->videoAt(nextRow);
244     QUrl streamUrl = video->streamUrl();
245     qDebug() << "Enqueing" << streamUrl;
246     mediaObject->enqueue(streamUrl);
247     */
248 }
249
250 void MediaView::currentSourceChanged(const Phonon::MediaSource source) {
251     qDebug() << "Source changed:" << source.url();
252 }
253
254 void MediaView::skip() {
255     int nextRow = listModel->nextRow();
256     if (nextRow == -1) return;
257     listModel->setActiveRow(nextRow);
258 }
259
260 void MediaView::openWebPage() {
261     Video* video = listModel->activeVideo();
262     if (!video) return;
263     mediaObject->pause();
264     QDesktopServices::openUrl(video->webpage());
265 }
266
267 void MediaView::removeSelected() {
268     if (!listView->selectionModel()->hasSelection()) return;
269     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
270     listModel->removeIndexes(indexes);
271 }
272
273 void MediaView::selectVideos(QList<Video*> videos) {
274     foreach (Video *video, videos) {
275         QModelIndex index = listModel->indexForVideo(video);
276         listView->selectionModel()->select(index, QItemSelectionModel::Select);
277         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
278     }
279 }
280
281 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
282     const bool gotSelection = listView->selectionModel()->hasSelection();
283     The::globalActions()->value("remove")->setEnabled(gotSelection);
284     The::globalActions()->value("moveUp")->setEnabled(gotSelection);
285     The::globalActions()->value("moveDown")->setEnabled(gotSelection);
286 }
287
288 void MediaView::moveUpSelected() {
289     if (!listView->selectionModel()->hasSelection()) return;
290     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
291     listModel->move(indexes, true);
292 }
293
294 void MediaView::moveDownSelected() {
295     if (!listView->selectionModel()->hasSelection()) return;
296     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
297     listModel->move(indexes, false);
298 }
299
300 void MediaView::showVideoContextMenu(QPoint point) {
301     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
302 }
303
304 void MediaView::searchMostRelevant() {
305     searchParams->setSortBy(SearchParams::SortByRelevance);
306     search(searchParams);
307 }
308
309 void MediaView::searchMostRecent() {
310     searchParams->setSortBy(SearchParams::SortByNewest);
311     search(searchParams);
312 }
313
314 void MediaView::searchMostViewed() {
315     searchParams->setSortBy(SearchParams::SortByViewCount);
316     search(searchParams);
317 }
318
319 void MediaView::setPlaylistVisible(bool visible) {
320     playlistWidget->setVisible(visible);
321 }
322
323 void MediaView::timerPlay() {
324     // qDebug() << mediaObject->currentTime();
325     // Workaround Phonon bug on Mac OSX
326     if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
327         mediaObject->pause();
328         mediaObject->play();
329     }
330 }