]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
Keyboard shortcuts for the sortBar
[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 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);
37
38     listView = new QListView(this);
39     listView->setItemDelegate(new PrettyItemDelegate(this));
40     listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
41
42     // dragndrop
43     listView->setDragEnabled(true);
44     listView->setAcceptDrops(true);
45     listView->setDropIndicatorShown(true);
46     listView->setDragDropMode(QAbstractItemView::DragDrop);
47
48     // cosmetics
49     listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
50     listView->setFrameShape( QFrame::NoFrame );
51     listView->setAttribute(Qt::WA_MacShowFocusRect, false);
52     listView->setMinimumSize(320,240);
53
54     // respond to the user doubleclicking a playlist item
55     connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
56
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);
62
63     connect(listView->selectionModel(),
64             SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
65             this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
66
67     playlistWidget = new PlaylistWidget(this, sortBar, listView);
68
69     splitter->addWidget(playlistWidget);
70
71     videoAreaWidget = new VideoAreaWidget(this);
72     videoAreaWidget->setMinimumSize(320,240);
73
74     videoWidget = new Phonon::VideoWidget(this);
75     videoAreaWidget->setVideoWidget(videoWidget);
76     videoAreaWidget->setListModel(listModel);
77
78     loadingWidget = new LoadingWidget(this);
79     videoAreaWidget->setLoadingWidget(loadingWidget);
80
81     splitter->addWidget(videoAreaWidget);
82
83     layout->addWidget(splitter);
84     setLayout(layout);
85
86 }
87
88 MediaView::~MediaView() {
89
90 }
91
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)));
97 }
98
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)));
109 }
110
111 void MediaView::search(SearchParams *searchParams) {
112     this->searchParams = searchParams;
113
114     // start serching for videos
115     listModel->search(searchParams);
116
117     // this implies that the enum and the bar action order is the same
118     sortBar->setCheckedAction(searchParams->sortBy()-1);
119
120     listView->setFocus();
121
122     loadingWidget->clear();
123 }
124
125 void MediaView::disappear() {
126     timerPlayFlag = true;
127 }
128
129 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
130 {
131
132     // qDebug() << "Phonon state: " << newState << oldState;
133
134     switch (newState) {
135
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
140         skip();
141         break;
142
143          case Phonon::PlayingState:
144         qDebug("playing");
145         videoAreaWidget->showVideo();
146         break;
147
148          case Phonon::StoppedState:
149         qDebug("stopped");
150         // play() has already been called when setting the source
151         // but Phonon on Linux needs a little more help to start playback
152         mediaObject->play();
153
154         // Workaround for Mac playback start problem
155         if (!timerPlayFlag) {
156             QTimer::singleShot(1000, this, SLOT(timerPlay()));
157         }
158
159         break;
160
161          case Phonon::PausedState:
162         qDebug("paused");
163         break;
164
165          case Phonon::BufferingState:
166         qDebug("buffering");
167         break;
168
169          case Phonon::LoadingState:
170         qDebug("loading");
171         break;
172
173          default:
174         ;
175     }
176 }
177
178 void MediaView::pause() {
179     // qDebug() << "pause() called" << mediaObject->state();
180     switch( mediaObject->state() ) {
181     case Phonon::PlayingState:
182         mediaObject->pause();
183         break;
184     default:
185         mediaObject->play();
186         break;
187     }
188 }
189
190 void MediaView::stop() {
191     listModel->abortSearch();
192     mediaObject->stop();
193     mediaObject->clear();
194 }
195
196 void MediaView::activeRowChanged(int row) {
197     Video *video = listModel->videoAt(row);
198     if (!video) return;
199
200     // immediately show the loading widget
201     videoAreaWidget->showLoading(video);
202
203     // mediaObject->pause();
204
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();
209
210     // reset the timer flag
211     timerPlayFlag = false;
212
213     // video title in the statusbar
214     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
215     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
216
217     // see you in gotStreamUrl...
218
219 }
220
221 void MediaView::gotStreamUrl(QUrl streamUrl) {
222
223     // go!
224     mediaObject->setCurrentSource(streamUrl);
225     mediaObject->play();
226
227     // ensure we always have 10 videos ahead
228     listModel->searchNeeded();
229
230     // ensure active item is visible
231     int row = listModel->activeRow();
232     if (row != -1) {
233         QModelIndex index = listModel->index(row, 0, QModelIndex());
234         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
235     }
236
237 }
238
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();
244 }
245
246 void MediaView::aboutToFinish() {
247     /*
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);
254     */
255 }
256
257 void MediaView::currentSourceChanged(const Phonon::MediaSource source) {
258     qDebug() << "Source changed:" << source.url();
259 }
260
261 void MediaView::skip() {
262     int nextRow = listModel->nextRow();
263     if (nextRow == -1) return;
264     listModel->setActiveRow(nextRow);
265 }
266
267 void MediaView::openWebPage() {
268     Video* video = listModel->activeVideo();
269     if (!video) return;
270     mediaObject->pause();
271     QDesktopServices::openUrl(video->webpage());
272 }
273
274 void MediaView::removeSelected() {
275     if (!listView->selectionModel()->hasSelection()) return;
276     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
277     listModel->removeIndexes(indexes);
278 }
279
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);
285     }
286 }
287
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);
293 }
294
295 void MediaView::moveUpSelected() {
296     if (!listView->selectionModel()->hasSelection()) return;
297     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
298     listModel->move(indexes, true);
299 }
300
301 void MediaView::moveDownSelected() {
302     if (!listView->selectionModel()->hasSelection()) return;
303     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
304     listModel->move(indexes, false);
305 }
306
307 void MediaView::showVideoContextMenu(QPoint point) {
308     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
309 }
310
311 void MediaView::searchMostRelevant() {
312     searchParams->setSortBy(SearchParams::SortByRelevance);
313     search(searchParams);
314 }
315
316 void MediaView::searchMostRecent() {
317     searchParams->setSortBy(SearchParams::SortByNewest);
318     search(searchParams);
319 }
320
321 void MediaView::searchMostViewed() {
322     searchParams->setSortBy(SearchParams::SortByViewCount);
323     search(searchParams);
324 }
325
326 void MediaView::setPlaylistVisible(bool visible) {
327     playlistWidget->setVisible(visible);
328 }
329
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();
335         mediaObject->play();
336     }
337 }