]> git.sur5r.net Git - minitube/blob - src/MainWindow.cpp
f4fff9633e85cc710321bb5e2f97e3029d810a89
[minitube] / src / MainWindow.cpp
1 #include "MainWindow.h"
2 #include "spacer.h"
3 #include "Constants.h"
4 #include "iconloader/qticonloader.h"
5 #include "global.h"
6
7 MainWindow::MainWindow() {
8
9     m_fullscreen = false;
10     mediaObject = 0;
11     audioOutput = 0;
12
13     // views mechanism
14     history = new QStack<QWidget*>();
15     views = new QStackedWidget(this);
16
17     // views
18     searchView = new SearchView(this);
19     connect(searchView, SIGNAL(search(QString)), this, SLOT(showMedia(QString)));
20     views->addWidget(searchView);
21     mediaView = new MediaView(this);
22     views->addWidget(mediaView);
23
24     // lazy initialized views
25     aboutView = 0;
26     settingsView = 0;
27
28     toolbarSearch = new SearchLineEdit(this);
29     toolbarSearch->setFont(qApp->font());
30     // toolbarSearch->setMinimumWidth(200);
31     connect(toolbarSearch, SIGNAL(search(const QString&)), searchView, SLOT(watch(const QString&)));
32
33     // build ui
34     createActions();
35     createMenus();
36     createToolBars();
37     createStatusBar();
38
39     // remove that useless menu/toolbar context menu
40     this->setContextMenuPolicy(Qt::NoContextMenu);
41
42     // mediaView init stuff thats needs actions
43     mediaView->initialize();
44
45     // restore window position
46     readSettings();
47
48     // show the initial view
49     showWidget(searchView);
50
51     setCentralWidget(views);
52
53     // top dock widget
54     /*
55     QLabel* message = new QLabel(this);
56     message->setText(QString("A new version of %1 is available.").arg(Constants::APP_NAME));
57     message->setMargin(10);
58     message->setAlignment(Qt::AlignCenter);
59     QPalette palette;
60     message->setBackgroundRole(QPalette::ToolTipBase);
61     message->setForegroundRole(QPalette::ToolTipText);
62     message->setAutoFillBackground(true);
63     QDockWidget *dockWidget = new QDockWidget("", this, 0);
64     dockWidget->setTitleBarWidget(0);
65     dockWidget->setWidget(message);
66     dockWidget->setFeatures(QDockWidget::DockWidgetClosable);
67     addDockWidget(Qt::TopDockWidgetArea, dockWidget);
68     */
69
70 }
71
72 MainWindow::~MainWindow() {
73     delete history;
74 }
75
76 void MainWindow::createActions() {
77
78     QMap<QString, QAction*> *actions = The::globalActions();
79
80     /*
81     settingsAct = new QAction(tr("&Preferences..."), this);
82     settingsAct->setStatusTip(tr(QString("Configure ").append(Constants::APP_NAME).toUtf8()));
83     // Mac integration
84     settingsAct->setMenuRole(QAction::PreferencesRole);
85     actions->insert("settings", settingsAct);
86     connect(settingsAct, SIGNAL(triggered()), this, SLOT(showSettings()));
87     */
88     
89     backAct = new QAction(QIcon(":/images/go-previous.png"), tr("&Back"), this);
90     backAct->setEnabled(false);
91     backAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Left));
92     backAct->setStatusTip(tr("Go to the previous view"));
93     actions->insert("back", backAct);
94     connect(backAct, SIGNAL(triggered()), this, SLOT(goBack()));
95
96     stopAct = new QAction(QtIconLoader::icon("media-stop", QIcon(":/images/stop.png")), tr("&Stop"), this);
97     stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
98     stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
99     actions->insert("stop", stopAct);
100     connect(stopAct, SIGNAL(triggered()), this, SLOT(stop()));
101
102     skipAct = new QAction(QtIconLoader::icon("media-skip-forward", QIcon(":/images/skip.png")), tr("S&kip"), this);
103     skipAct->setStatusTip(tr("Skip to the next video"));
104     skipAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
105     skipAct->setEnabled(false);
106     actions->insert("skip", skipAct);
107     connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
108
109     pauseAct = new QAction(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")), tr("&Pause"), this);
110     pauseAct->setStatusTip(tr("Pause playback"));
111     pauseAct->setShortcut(QKeySequence(Qt::Key_Space));
112     pauseAct->setEnabled(false);
113     actions->insert("pause", pauseAct);
114     connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
115
116     fullscreenAct = new QAction(QtIconLoader::icon("view-fullscreen", QIcon(":/images/view-fullscreen.png")), tr("&Full Screen"), this);
117     fullscreenAct->setStatusTip(tr("Go full screen"));
118     fullscreenAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Return));
119     fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
120     actions->insert("fullscreen", fullscreenAct);
121     connect(fullscreenAct, SIGNAL(triggered()), this, SLOT(fullscreen()));
122
123     compactViewAct = new QAction(tr("&Compact mode"), this);
124     compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
125     compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
126     compactViewAct->setCheckable(true);
127     compactViewAct->setChecked(false);
128     compactViewAct->setEnabled(false);
129     actions->insert("compactView", compactViewAct);
130     connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
131
132     /*
133     // icon should be document-save but it is ugly
134     downloadAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("&Download"), this);
135     downloadAct->setStatusTip(tr("Download this video"));
136     downloadAct->setShortcut(tr("Ctrl+S"));
137     actions.insert("download", downloadAct);
138     connect(downloadAct, SIGNAL(triggered()), this, SLOT(download()));
139     */
140
141     webPageAct = new QAction(QtIconLoader::icon("internet-web-browser", QIcon(":/images/internet-web-browser.png")), tr("&YouTube"), this);
142     webPageAct->setStatusTip(tr("Open the YouTube video page"));
143     webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
144     webPageAct->setEnabled(false);
145     actions->insert("webpage", webPageAct);
146     connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
147
148     removeAct = new QAction(tr("&Remove"), this);
149     removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
150     QList<QKeySequence> shortcuts;
151     shortcuts << QKeySequence("Del") << QKeySequence("Backspace");
152     removeAct->setShortcuts(shortcuts);
153     removeAct->setEnabled(false);
154     actions->insert("remove", removeAct);
155     connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
156
157     moveUpAct = new QAction(QtIconLoader::icon("go-up", QIcon(":/images/go-up.png")), tr("Move &Up"), this);
158     moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
159     moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
160     moveUpAct->setEnabled(false);
161     actions->insert("moveUp", moveUpAct);
162     connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
163
164     moveDownAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("Move &Down"), this);
165     moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
166     moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
167     moveDownAct->setEnabled(false);
168     actions->insert("moveDown", moveDownAct);
169     connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
170
171     quitAct = new QAction(tr("&Quit"), this);
172     quitAct->setMenuRole(QAction::QuitRole);
173     quitAct->setShortcut(tr("Ctrl+Q"));
174     quitAct->setStatusTip(tr("Bye"));
175     actions->insert("quit", quitAct);
176     connect(quitAct, SIGNAL(triggered()), this, SLOT(quit()));
177
178     siteAct = new QAction(tr("&Website"), this);
179     siteAct->setShortcut(QKeySequence::HelpContents);
180     siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::APP_NAME));
181     actions->insert("site", siteAct);
182     connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
183
184     donateAct = new QAction(tr("&Donate via PayPal"), this);
185     donateAct->setStatusTip(tr("Please support the continued development of %1").arg(Constants::APP_NAME));
186     actions->insert("donate", donateAct);
187     connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
188
189     aboutAct = new QAction(tr("&About"), this);
190     aboutAct->setMenuRole(QAction::AboutRole);
191     aboutAct->setStatusTip(tr("Info about %1").arg(Constants::APP_NAME));
192     actions->insert("about", aboutAct);
193     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
194
195     searchFocusAct = new QAction(tr("&Search"), this);
196     searchFocusAct->setShortcut(QKeySequence::Find);
197     actions->insert("search", searchFocusAct);
198     connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
199     addAction(searchFocusAct);
200
201     // common action properties
202     foreach (QAction *action, actions->values()) {
203         // never autorepeat.
204         // unexperienced users tend to keep keys pressed for a "long" time
205         action->setAutoRepeat(false);
206         action->setToolTip(action->statusTip());
207
208         // make the actions work when video is fullscreen
209         action->setShortcutContext(Qt::ApplicationShortcut);
210
211 #ifdef Q_WS_MAC
212         // OSX does not use icons in menus
213         action->setIconVisibleInMenu(false);
214 #endif
215
216     }
217
218 }
219
220 void MainWindow::createMenus() {
221
222     QMap<QString, QMenu*> *menus = The::globalMenus();
223
224     fileMenu = menuBar()->addMenu(tr("&Application"));
225     // menus->insert("file", fileMenu);
226     /*
227     fileMenu->addAction(settingsAct);
228     fileMenu->addSeparator();
229     */
230     fileMenu->addAction(quitAct);
231
232     playlistMenu = menuBar()->addMenu(tr("&Playlist"));
233     menus->insert("playlist", playlistMenu);
234     playlistMenu->addAction(removeAct);
235     playlistMenu->addSeparator();
236     playlistMenu->addAction(moveUpAct);
237     playlistMenu->addAction(moveDownAct);
238
239     viewMenu = menuBar()->addMenu(tr("&Video"));
240     menus->insert("video", viewMenu);
241     // viewMenu->addAction(backAct);
242     viewMenu->addAction(stopAct);
243     viewMenu->addAction(pauseAct);
244     viewMenu->addAction(skipAct);
245     viewMenu->addSeparator();
246     viewMenu->addAction(webPageAct);
247     viewMenu->addSeparator();
248     viewMenu->addAction(compactViewAct);
249     viewMenu->addAction(fullscreenAct);
250
251     helpMenu = menuBar()->addMenu(tr("&Help"));
252     helpMenu->addAction(siteAct);
253     helpMenu->addAction(donateAct);
254     helpMenu->addAction(aboutAct);
255 }
256
257 void MainWindow::createToolBars() {
258
259     setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
260
261     mainToolBar = new QToolBar(this);
262     mainToolBar->setFloatable(false);
263     mainToolBar->setMovable(false);
264
265     QFont smallerFont;
266     smallerFont.setPointSize(smallerFont.pointSize()*.85);
267     mainToolBar->setFont(smallerFont);
268
269     mainToolBar->setIconSize(QSize(32,32));
270     // mainToolBar->addAction(backAct);
271     mainToolBar->addAction(stopAct);
272     mainToolBar->addAction(pauseAct);
273     mainToolBar->addAction(skipAct);
274     mainToolBar->addAction(fullscreenAct);
275
276     seekSlider = new Phonon::SeekSlider(this);
277     seekSlider->setIconVisible(false);
278     Spacer *seekSliderSpacer = new Spacer(mainToolBar, seekSlider);
279     seekSliderSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
280     mainToolBar->addWidget(seekSliderSpacer);
281
282     volumeSlider = new Phonon::VolumeSlider(this);
283     // this makes the volume slider smaller
284     volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
285     mainToolBar->addWidget(new Spacer(mainToolBar, volumeSlider));
286
287     mainToolBar->addWidget(new Spacer(mainToolBar, toolbarSearch));
288
289     addToolBar(mainToolBar);
290 }
291
292 void MainWindow::createStatusBar() {
293     currentTime = new QLabel(this);
294     statusBar()->addPermanentWidget(currentTime);
295
296     totalTime = new QLabel(this);
297     statusBar()->addPermanentWidget(totalTime);
298
299     // remove ugly borders on OSX
300     statusBar()->setStyleSheet("::item{border:0 solid}");
301
302     statusBar()->show();
303 }
304
305 void MainWindow::readSettings() {
306     QSettings settings;
307     restoreGeometry(settings.value("geometry").toByteArray());
308 }
309
310 void MainWindow::writeSettings() {
311     QSettings settings;
312     settings.setValue("geometry", saveGeometry());
313 }
314
315 void MainWindow::goBack() {
316     if ( history->size() > 1 ) {
317         history->pop();
318         QWidget *widget = history->pop();
319         showWidget(widget);
320     }
321 }
322
323 void MainWindow::showWidget ( QWidget* widget ) {
324
325     // call hide method on the current view
326     View* oldView = dynamic_cast<View *> (views->currentWidget());
327     if (oldView != NULL) {
328         oldView->disappear();
329     }
330
331     // call show method on the new view
332     View* newView = dynamic_cast<View *> (widget);
333     if (newView != NULL) {
334         newView->appear();
335         QMap<QString,QVariant> metadata = newView->metadata();
336         QString windowTitle = metadata.value("title").toString();
337         if (windowTitle.length())
338             windowTitle += " - ";
339         setWindowTitle(windowTitle + Constants::APP_NAME);
340         statusBar()->showMessage((metadata.value("description").toString()));
341
342     }
343
344     // backAct->setEnabled(history->size() > 1);
345     // settingsAct->setEnabled(widget != settingsView);
346     stopAct->setEnabled(widget == mediaView);
347     fullscreenAct->setEnabled(widget == mediaView);
348     compactViewAct->setEnabled(widget == mediaView);
349     webPageAct->setEnabled(widget == mediaView);
350     aboutAct->setEnabled(widget != aboutView);
351
352     // cool toolbar on the Mac
353     // setUnifiedTitleAndToolBarOnMac(widget == mediaView);
354
355     // toolbar only for the mediaView
356     mainToolBar->setVisible(widget == mediaView && !compactViewAct->isChecked());
357
358     history->push(widget);
359     fadeInWidget(views->currentWidget(), widget);
360     views->setCurrentWidget(widget);
361
362 }
363
364 void MainWindow::fadeInWidget(QWidget *oldWidget, QWidget *newWidget) {
365     if (faderWidget) faderWidget->close();
366     if (!oldWidget || !newWidget || oldWidget == mediaView || newWidget == mediaView) return;
367     QPixmap frozenView = QPixmap::grabWidget(oldWidget);
368     faderWidget = new FaderWidget(newWidget);
369     faderWidget->start(frozenView);
370 }
371
372 void MainWindow::about() {
373     if (!aboutView) {
374         aboutView = new AboutView(this);
375         views->addWidget(aboutView);
376     }
377     showWidget(aboutView);
378 }
379
380 void MainWindow::visitSite() {
381     QUrl url(Constants::WEBSITE);
382     statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
383     QDesktopServices::openUrl(url);
384 }
385
386 void MainWindow::donate() {
387     QUrl url(QString(Constants::WEBSITE) + "#donate");
388     statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
389     QDesktopServices::openUrl(url);
390 }
391
392 void MainWindow::quit() {
393     writeSettings();
394     qApp->quit();
395 }
396
397 void MainWindow::closeEvent(QCloseEvent *event) {
398     quit();
399     QWidget::closeEvent(event);
400 }
401
402 void MainWindow::showSettings() {
403     if (!settingsView) {
404         settingsView = new SettingsView(this);
405         views->addWidget(settingsView);
406     }
407     showWidget(settingsView);
408 }
409
410 void MainWindow::showSearch() {
411     showWidget(searchView);
412     currentTime->clear();
413     totalTime->clear();
414 }
415
416 void MainWindow::showMedia(QString query) {
417     initPhonon();
418     mediaView->setMediaObject(mediaObject);
419     SearchParams *searchParams = new SearchParams();
420     searchParams->setKeywords(query);
421     mediaView->search(searchParams);
422     showWidget(mediaView);
423 }
424
425 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) {
426
427     // qDebug() << "Phonon state: " << newState;
428
429     switch (newState) {
430
431          case Phonon::ErrorState:
432         if (mediaObject->errorType() == Phonon::FatalError) {
433             statusBar()->showMessage(tr("Fatal error: %1").arg(mediaObject->errorString()));
434         } else {
435             statusBar()->showMessage(tr("Error: %1").arg(mediaObject->errorString()));
436         }
437         break;
438
439          case Phonon::PlayingState:
440         pauseAct->setEnabled(true);
441         pauseAct->setIcon(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")));
442         pauseAct->setText(tr("&Pause"));
443         pauseAct->setStatusTip(tr("Pause playback"));
444         skipAct->setEnabled(true);
445         break;
446
447          case Phonon::StoppedState:
448         pauseAct->setEnabled(false);
449         skipAct->setEnabled(false);
450         break;
451
452          case Phonon::PausedState:
453         skipAct->setEnabled(true);
454         pauseAct->setEnabled(true);
455         pauseAct->setIcon(QtIconLoader::icon("media-play", QIcon(":/images/play.png")));
456         pauseAct->setText(tr("&Play"));
457         pauseAct->setStatusTip(tr("Resume playback"));
458         break;
459
460          case Phonon::BufferingState:
461          case Phonon::LoadingState:
462         skipAct->setEnabled(true);
463         pauseAct->setEnabled(false);
464         currentTime->clear();
465         totalTime->clear();
466         break;
467
468          default:
469         ;
470     }
471 }
472
473 void MainWindow::stop() {
474     mediaView->stop();
475     showSearch();
476 }
477
478 void MainWindow::fullscreen() {
479
480     if (m_fullscreen) {
481         mediaView->exitFullscreen();
482         fullscreenAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Return));
483         fullscreenAct->setText(tr("&Full Screen"));
484         stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
485     } else {
486         mediaView->fullscreen();
487         stopAct->setShortcut(QString(""));
488         QList<QKeySequence> shortcuts;
489         // for some reason it is importante that ESC comes first
490         shortcuts << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::ALT + Qt::Key_Return);
491         fullscreenAct->setShortcuts(shortcuts);
492         fullscreenAct->setText(tr("Exit &Full Screen"));
493     }
494     compactViewAct->setVisible(m_fullscreen);
495
496     m_fullscreen = !m_fullscreen;
497
498 }
499
500 void MainWindow::compactView(bool enable) {
501
502     // setUnifiedTitleAndToolBarOnMac(!enable);
503     mediaView->setPlaylistVisible(!enable);
504     mainToolBar->setVisible(!enable);
505     statusBar()->setVisible(!enable);
506
507     // ensure focus does not end up to the search box
508     // as it would steal the Space shortcut
509     toolbarSearch->setEnabled(!enable);
510
511     if (enable) {
512         stopAct->setShortcut(QString(""));
513         QList<QKeySequence> shortcuts;
514         // for some reason it is important that ESC comes first
515         shortcuts << QKeySequence(Qt::CTRL + Qt::Key_Return) << QKeySequence(Qt::Key_Escape);
516         compactViewAct->setShortcuts(shortcuts);
517     } else {
518         compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
519         stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
520     }
521
522 }
523
524 void MainWindow::searchFocus() {
525     QWidget *view = views->currentWidget();
526     if (view == mediaView) {
527         toolbarSearch->setFocus();
528     }
529 }
530
531 void MainWindow::initPhonon() {
532     // Phonon initialization
533     if (mediaObject) delete mediaObject;
534     if (audioOutput) delete audioOutput;
535     mediaObject = new Phonon::MediaObject(this);
536     mediaObject->setTickInterval(100);
537     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
538             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
539     connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
540     connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
541     seekSlider->setMediaObject(mediaObject);
542     audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
543     volumeSlider->setAudioOutput(audioOutput);
544     Phonon::createPath(mediaObject, audioOutput);
545 }
546
547 void MainWindow::tick(qint64 time) {
548     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
549     currentTime->setText(displayTime.toString("mm:ss"));
550     // qDebug() << "currentTime" << time << displayTime.toString("mm:ss");
551 }
552
553 void MainWindow::totalTimeChanged(qint64 time) {
554     if (time <= 0) {
555         totalTime->clear();
556         return;
557     }
558     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
559     totalTime->setText(displayTime.toString("/ mm:ss"));
560     // qDebug() << "totalTime" << time << displayTime.toString("mm:ss");
561 }
562