1 #include "MainWindow.h"
4 #include "iconloader/qticonloader.h"
7 MainWindow::MainWindow() {
14 history = new QStack<QWidget*>();
15 views = new QStackedWidget(this);
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);
24 // lazily initialized views
28 toolbarSearch = new SearchLineEdit(this);
29 toolbarSearch->setFont(qApp->font());
30 toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize()*15);
31 connect(toolbarSearch, SIGNAL(search(const QString&)), searchView, SLOT(watch(const QString&)));
39 // remove that useless menu/toolbar context menu
40 this->setContextMenuPolicy(Qt::NoContextMenu);
42 // mediaView init stuff thats needs actions
43 mediaView->initialize();
45 // restore window position
48 // show the initial view
49 showWidget(searchView);
51 setCentralWidget(views);
54 MainWindow::~MainWindow() {
58 void MainWindow::createActions() {
60 QMap<QString, QAction*> *actions = The::globalActions();
63 settingsAct = new QAction(tr("&Preferences..."), this);
64 settingsAct->setStatusTip(tr(QString("Configure ").append(Constants::APP_NAME).toUtf8()));
66 settingsAct->setMenuRole(QAction::PreferencesRole);
67 actions->insert("settings", settingsAct);
68 connect(settingsAct, SIGNAL(triggered()), this, SLOT(showSettings()));
71 backAct = new QAction(QIcon(":/images/go-previous.png"), tr("&Back"), this);
72 backAct->setEnabled(false);
73 backAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Left));
74 backAct->setStatusTip(tr("Go to the previous view"));
75 actions->insert("back", backAct);
76 connect(backAct, SIGNAL(triggered()), this, SLOT(goBack()));
78 stopAct = new QAction(QtIconLoader::icon("media-playback-stop", QIcon(":/images/stop.png")), tr("&Stop"), this);
79 stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
80 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
81 actions->insert("stop", stopAct);
82 connect(stopAct, SIGNAL(triggered()), this, SLOT(stop()));
84 skipAct = new QAction(QtIconLoader::icon("media-skip-forward", QIcon(":/images/skip.png")), tr("S&kip"), this);
85 skipAct->setStatusTip(tr("Skip to the next video"));
86 skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right) << QKeySequence(Qt::Key_MediaNext));
87 skipAct->setEnabled(false);
88 actions->insert("skip", skipAct);
89 connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
91 pauseAct = new QAction(QtIconLoader::icon("media-playback-pause", QIcon(":/images/pause.png")), tr("&Pause"), this);
92 pauseAct->setStatusTip(tr("Pause playback"));
93 pauseAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
94 pauseAct->setEnabled(false);
95 actions->insert("pause", pauseAct);
96 connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
98 fullscreenAct = new QAction(QtIconLoader::icon("view-fullscreen", QIcon(":/images/view-fullscreen.png")), tr("&Full Screen"), this);
99 fullscreenAct->setStatusTip(tr("Go full screen"));
100 fullscreenAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Return));
101 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
102 actions->insert("fullscreen", fullscreenAct);
103 connect(fullscreenAct, SIGNAL(triggered()), this, SLOT(fullscreen()));
105 compactViewAct = new QAction(tr("&Compact mode"), this);
106 compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
107 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
108 compactViewAct->setCheckable(true);
109 compactViewAct->setChecked(false);
110 compactViewAct->setEnabled(false);
111 actions->insert("compactView", compactViewAct);
112 connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
115 // icon should be document-save but it is ugly
116 downloadAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("&Download"), this);
117 downloadAct->setStatusTip(tr("Download this video"));
118 downloadAct->setShortcut(tr("Ctrl+S"));
119 downloadAct->setEnabled(false);
120 actions->insert("download", downloadAct);
121 connect(downloadAct, SIGNAL(triggered()), this, SLOT(download()));
124 webPageAct = new QAction(QtIconLoader::icon("internet-web-browser", QIcon(":/images/internet-web-browser.png")), tr("&YouTube"), this);
125 webPageAct->setStatusTip(tr("Open the YouTube video page"));
126 webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
127 webPageAct->setEnabled(false);
128 actions->insert("webpage", webPageAct);
129 connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
131 removeAct = new QAction(tr("&Remove"), this);
132 removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
133 removeAct->setShortcuts(QList<QKeySequence>() << QKeySequence("Del") << QKeySequence("Backspace"));
134 removeAct->setEnabled(false);
135 actions->insert("remove", removeAct);
136 connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
138 moveUpAct = new QAction(QtIconLoader::icon("go-up", QIcon(":/images/go-up.png")), tr("Move &Up"), this);
139 moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
140 moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
141 moveUpAct->setEnabled(false);
142 actions->insert("moveUp", moveUpAct);
143 connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
145 moveDownAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("Move &Down"), this);
146 moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
147 moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
148 moveDownAct->setEnabled(false);
149 actions->insert("moveDown", moveDownAct);
150 connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
152 quitAct = new QAction(tr("&Quit"), this);
153 quitAct->setMenuRole(QAction::QuitRole);
154 quitAct->setShortcuts(QList<QKeySequence>() << QKeySequence(tr("Ctrl+Q")) << QKeySequence(Qt::CTRL + Qt::Key_W));
155 quitAct->setStatusTip(tr("Bye"));
156 actions->insert("quit", quitAct);
157 connect(quitAct, SIGNAL(triggered()), this, SLOT(quit()));
159 siteAct = new QAction(tr("&Website"), this);
160 siteAct->setShortcut(QKeySequence::HelpContents);
161 siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::APP_NAME));
162 actions->insert("site", siteAct);
163 connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
165 donateAct = new QAction(tr("&Donate via PayPal"), this);
166 donateAct->setStatusTip(tr("Please support the continued development of %1").arg(Constants::APP_NAME));
167 actions->insert("donate", donateAct);
168 connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
170 aboutAct = new QAction(tr("&About"), this);
171 aboutAct->setMenuRole(QAction::AboutRole);
172 aboutAct->setStatusTip(tr("Info about %1").arg(Constants::APP_NAME));
173 actions->insert("about", aboutAct);
174 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
178 searchFocusAct = new QAction(this);
179 searchFocusAct->setShortcut(QKeySequence::Find);
180 searchFocusAct->setStatusTip(tr("Search"));
181 actions->insert("search", searchFocusAct);
182 connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
183 addAction(searchFocusAct);
185 volumeUpAct = new QAction(this);
186 volumeUpAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Plus) << QKeySequence(Qt::Key_VolumeUp));
187 actions->insert("volume-up", volumeUpAct);
188 connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
189 addAction(volumeUpAct);
191 volumeDownAct = new QAction(this);
192 volumeDownAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Minus) << QKeySequence(Qt::Key_VolumeDown));
193 actions->insert("volume-down", volumeDownAct);
194 connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
195 addAction(volumeDownAct);
197 volumeMuteAct = new QAction(this);
198 volumeMuteAct->setStatusTip(tr("Mute volume"));
199 volumeMuteAct->setShortcuts(QList<QKeySequence>() << QKeySequence(tr("Ctrl+M")) << QKeySequence(Qt::Key_VolumeMute));
200 actions->insert("volume-mute", volumeMuteAct);
201 connect(volumeMuteAct, SIGNAL(triggered()), this, SLOT(volumeMute()));
202 addAction(volumeMuteAct);
204 // common action properties
205 foreach (QAction *action, actions->values()) {
207 // add actions to the MainWindow so that they work
208 // when the menu is hidden
212 // unexperienced users tend to keep keys pressed for a "long" time
213 action->setAutoRepeat(false);
214 action->setToolTip(action->statusTip());
216 // show keyboard shortcuts in the status bar
217 if (!action->shortcut().isEmpty())
218 action->setStatusTip(action->statusTip() + " (" + action->shortcut().toString(QKeySequence::NativeText) + ")");
220 // make the actions work when video is fullscreen
221 action->setShortcutContext(Qt::ApplicationShortcut);
224 // OSX does not use icons in menus
225 action->setIconVisibleInMenu(false);
232 void MainWindow::createMenus() {
234 QMap<QString, QMenu*> *menus = The::globalMenus();
237 fileMenu = menuBar()->addMenu(tr("&Application"));
238 // menus->insert("file", fileMenu);
239 // fileMenu->addAction(settingsAct);
240 fileMenu->addSeparator();
241 fileMenu->addAction(quitAct);
244 playlistMenu = menuBar()->addMenu(tr("&Playlist"));
245 menus->insert("playlist", playlistMenu);
246 playlistMenu->addAction(removeAct);
247 playlistMenu->addSeparator();
248 playlistMenu->addAction(moveUpAct);
249 playlistMenu->addAction(moveDownAct);
251 viewMenu = menuBar()->addMenu(tr("&Video"));
252 menus->insert("video", viewMenu);
253 // viewMenu->addAction(backAct);
254 viewMenu->addAction(stopAct);
255 viewMenu->addAction(pauseAct);
256 viewMenu->addAction(skipAct);
257 viewMenu->addSeparator();
258 viewMenu->addAction(webPageAct);
259 viewMenu->addSeparator();
260 // viewMenu->addAction(downloadAct);
261 viewMenu->addAction(compactViewAct);
262 viewMenu->addAction(fullscreenAct);
264 helpMenu = menuBar()->addMenu(tr("&Help"));
265 helpMenu->addAction(siteAct);
266 helpMenu->addAction(donateAct);
267 helpMenu->addAction(aboutAct);
270 void MainWindow::createToolBars() {
272 setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
274 mainToolBar = new QToolBar(this);
275 mainToolBar->setFloatable(false);
276 mainToolBar->setMovable(false);
279 smallerFont.setPointSize(smallerFont.pointSize()*.85);
280 mainToolBar->setFont(smallerFont);
282 mainToolBar->setIconSize(QSize(32,32));
283 // mainToolBar->addAction(backAct);
284 mainToolBar->addAction(stopAct);
285 mainToolBar->addAction(pauseAct);
286 mainToolBar->addAction(skipAct);
287 mainToolBar->addAction(fullscreenAct);
289 seekSlider = new Phonon::SeekSlider(this);
290 seekSlider->setIconVisible(false);
291 Spacer *seekSliderSpacer = new Spacer(mainToolBar, seekSlider);
292 seekSliderSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
293 mainToolBar->addWidget(seekSliderSpacer);
295 volumeSlider = new Phonon::VolumeSlider(this);
296 // qDebug() << volumeSlider->children();
297 // status tip for the volume slider
298 QSlider* volumeQSlider = volumeSlider->findChild<QSlider*>();
300 volumeQSlider->setStatusTip(tr("Press %1 to raise the volume, %2 to lower it").arg(
301 volumeUpAct->shortcut().toString(QKeySequence::NativeText), volumeDownAct->shortcut().toString(QKeySequence::NativeText)));
302 // status tip for the mute button
303 QToolButton* muteToolButton = volumeSlider->findChild<QToolButton*>();
305 muteToolButton->setStatusTip(volumeMuteAct->statusTip());
306 // this makes the volume slider smaller
307 volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
308 mainToolBar->addWidget(new Spacer(mainToolBar, volumeSlider));
310 toolbarSearch->setStatusTip(searchFocusAct->statusTip());
311 mainToolBar->addWidget(new Spacer(mainToolBar, toolbarSearch));
313 addToolBar(mainToolBar);
316 void MainWindow::createStatusBar() {
317 currentTime = new QLabel(this);
318 statusBar()->addPermanentWidget(currentTime);
320 totalTime = new QLabel(this);
321 statusBar()->addPermanentWidget(totalTime);
323 // remove ugly borders on OSX
324 statusBar()->setStyleSheet("::item{border:0 solid}");
329 void MainWindow::readSettings() {
331 restoreGeometry(settings.value("geometry").toByteArray());
334 void MainWindow::writeSettings() {
335 // do not save geometry when in full screen
339 settings.setValue("geometry", saveGeometry());
340 mediaView->saveSplitterState();
343 void MainWindow::goBack() {
344 if ( history->size() > 1 ) {
346 QWidget *widget = history->pop();
351 void MainWindow::showWidget ( QWidget* widget ) {
353 setUpdatesEnabled(false);
355 // call hide method on the current view
356 View* oldView = dynamic_cast<View *> (views->currentWidget());
357 if (oldView != NULL) {
358 oldView->disappear();
361 // call show method on the new view
362 View* newView = dynamic_cast<View *> (widget);
363 if (newView != NULL) {
365 QMap<QString,QVariant> metadata = newView->metadata();
366 QString windowTitle = metadata.value("title").toString();
367 if (windowTitle.length())
368 windowTitle += " - ";
369 setWindowTitle(windowTitle + Constants::APP_NAME);
370 statusBar()->showMessage((metadata.value("description").toString()));
374 // backAct->setEnabled(history->size() > 1);
375 // settingsAct->setEnabled(widget != settingsView);
376 stopAct->setEnabled(widget == mediaView);
377 fullscreenAct->setEnabled(widget == mediaView);
378 compactViewAct->setEnabled(widget == mediaView);
379 webPageAct->setEnabled(widget == mediaView);
380 aboutAct->setEnabled(widget != aboutView);
383 // this is not the best place to enable downloads, but the user is informed
384 // if there really is no video is playing
385 downloadAct->setEnabled(widget == mediaView);
388 // cool toolbar on the Mac
389 // setUnifiedTitleAndToolBarOnMac(widget == mediaView);
391 // toolbar only for the mediaView
392 mainToolBar->setVisible(widget == mediaView && !compactViewAct->isChecked());
394 history->push(widget);
397 // crossfade only on OSX
398 // where we can be sure of video performance
399 fadeInWidget(views->currentWidget(), widget);
402 views->setCurrentWidget(widget);
404 setUpdatesEnabled(true);
407 void MainWindow::fadeInWidget(QWidget *oldWidget, QWidget *newWidget) {
408 if (faderWidget) faderWidget->close();
409 if (oldWidget == mediaView || newWidget == mediaView) return;
410 QPixmap frozenView = QPixmap::grabWidget(oldWidget);
411 faderWidget = new FaderWidget(newWidget);
412 faderWidget->start(frozenView);
415 void MainWindow::about() {
417 aboutView = new AboutView(this);
418 views->addWidget(aboutView);
420 showWidget(aboutView);
423 void MainWindow::visitSite() {
424 QUrl url(Constants::WEBSITE);
425 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
426 QDesktopServices::openUrl(url);
429 void MainWindow::donate() {
430 QUrl url(QString(Constants::WEBSITE) + "#donate");
431 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
432 QDesktopServices::openUrl(url);
435 void MainWindow::quit() {
440 void MainWindow::closeEvent(QCloseEvent *event) {
442 QWidget::closeEvent(event);
445 void MainWindow::showSettings() {
447 settingsView = new SettingsView(this);
448 views->addWidget(settingsView);
450 showWidget(settingsView);
453 void MainWindow::showSearch() {
454 showWidget(searchView);
455 currentTime->clear();
459 void MainWindow::showMedia(QString query) {
461 mediaView->setMediaObject(mediaObject);
462 SearchParams *searchParams = new SearchParams();
463 searchParams->setKeywords(query);
464 mediaView->search(searchParams);
465 showWidget(mediaView);
468 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) {
470 // qDebug() << "Phonon state: " << newState;
474 case Phonon::ErrorState:
475 if (mediaObject->errorType() == Phonon::FatalError) {
476 statusBar()->showMessage(tr("Fatal error: %1").arg(mediaObject->errorString()));
478 statusBar()->showMessage(tr("Error: %1").arg(mediaObject->errorString()));
482 case Phonon::PlayingState:
483 pauseAct->setEnabled(true);
484 pauseAct->setIcon(QtIconLoader::icon("media-playback-pause", QIcon(":/images/pause.png")));
485 pauseAct->setText(tr("&Pause"));
486 pauseAct->setStatusTip(tr("Pause playback") + " (" + pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
487 skipAct->setEnabled(true);
490 case Phonon::StoppedState:
491 pauseAct->setEnabled(false);
492 skipAct->setEnabled(false);
495 case Phonon::PausedState:
496 skipAct->setEnabled(true);
497 pauseAct->setEnabled(true);
498 pauseAct->setIcon(QtIconLoader::icon("media-playback-start", QIcon(":/images/play.png")));
499 pauseAct->setText(tr("&Play"));
500 pauseAct->setStatusTip(tr("Resume playback") + " (" + pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
503 case Phonon::BufferingState:
504 case Phonon::LoadingState:
505 skipAct->setEnabled(true);
506 pauseAct->setEnabled(false);
507 currentTime->clear();
516 void MainWindow::stop() {
521 void MainWindow::fullscreen() {
523 setUpdatesEnabled(false);
526 // use setShortucs instead of setShortcut
527 // the latter seems not to work
528 fullscreenAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::ALT + Qt::Key_Return));
529 fullscreenAct->setText(tr("&Full Screen"));
530 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
531 if (m_maximized) showMaximized();
533 // Make sure the window has focus (Mac)
536 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
537 fullscreenAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::ALT + Qt::Key_Return));
538 fullscreenAct->setText(tr("Exit &Full Screen"));
539 m_maximized = isMaximized();
541 // save geometry now, if the user quits when in full screen
542 // geometry won't be saved
548 // No compact view action when in full screen
549 compactViewAct->setVisible(m_fullscreen);
550 // Also no Youtube action since it opens a new window
551 webPageAct->setVisible(m_fullscreen);
553 // Hide anything but the video
554 mediaView->setPlaylistVisible(m_fullscreen);
555 mainToolBar->setVisible(m_fullscreen);
556 statusBar()->setVisible(m_fullscreen);
557 menuBar()->setVisible(m_fullscreen);
559 // workaround: prevent focus on the search bar
560 // it steals the Space key needed for Play/Pause
561 mainToolBar->setEnabled(m_fullscreen);
563 m_fullscreen = !m_fullscreen;
565 setUpdatesEnabled(true);
568 void MainWindow::compactView(bool enable) {
570 setUpdatesEnabled(false);
572 // setUnifiedTitleAndToolBarOnMac(!enable);
573 mediaView->setPlaylistVisible(!enable);
574 mainToolBar->setVisible(!enable);
575 statusBar()->setVisible(!enable);
579 menuBar()->setVisible(!enable);
582 // ensure focus does not end up to the search box
583 // as it would steal the Space shortcut
584 toolbarSearch->setEnabled(!enable);
587 stopAct->setShortcut(QString(""));
588 QList<QKeySequence> shortcuts;
589 // for some reason it is important that ESC comes first
590 shortcuts << QKeySequence(Qt::CTRL + Qt::Key_Return) << QKeySequence(Qt::Key_Escape);
591 compactViewAct->setShortcuts(shortcuts);
593 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
594 stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
597 setUpdatesEnabled(true);
600 void MainWindow::searchFocus() {
601 QWidget *view = views->currentWidget();
602 if (view == mediaView) {
603 toolbarSearch->setFocus();
607 void MainWindow::initPhonon() {
608 // Phonon initialization
609 if (mediaObject) delete mediaObject;
610 if (audioOutput) delete audioOutput;
611 mediaObject = new Phonon::MediaObject(this);
612 mediaObject->setTickInterval(100);
613 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
614 this, SLOT(stateChanged(Phonon::State, Phonon::State)));
615 connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
616 connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
617 seekSlider->setMediaObject(mediaObject);
618 audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
619 connect(audioOutput, SIGNAL(volumeChanged(qreal)), this, SLOT(volumeChanged(qreal)));
620 connect(audioOutput, SIGNAL(mutedChanged(bool)), this, SLOT(volumeMutedChanged(bool)));
621 volumeSlider->setAudioOutput(audioOutput);
622 Phonon::createPath(mediaObject, audioOutput);
625 void MainWindow::tick(qint64 time) {
627 currentTime->clear();
630 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
631 currentTime->setText(displayTime.toString("mm:ss"));
633 // remaining time tooltip
634 int remainingTimeInt = mediaObject->remainingTime();
635 QTime remainingTime(0, (remainingTimeInt / 60000) % 60, (remainingTimeInt / 1000) % 60);
636 currentTime->setStatusTip(tr("Remaining time: %1").arg(remainingTime.toString("mm:ss")));
638 // qDebug() << "currentTime" << time << displayTime.toString("mm:ss");
641 void MainWindow::totalTimeChanged(qint64 time) {
646 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
647 totalTime->setText(displayTime.toString("/ mm:ss"));
648 // qDebug() << "totalTime" << time << displayTime.toString("mm:ss");
651 void MainWindow::volumeUp() {
652 qreal newVolume = volumeSlider->audioOutput()->volume() + .1;
653 if (newVolume > volumeSlider->maximumVolume())
654 newVolume = volumeSlider->maximumVolume();
655 volumeSlider->audioOutput()->setVolume(newVolume);
658 void MainWindow::volumeDown() {
659 qreal newVolume = volumeSlider->audioOutput()->volume() - .1;
662 volumeSlider->audioOutput()->setVolume(newVolume);
665 void MainWindow::volumeMute() {
666 volumeSlider->audioOutput()->setMuted(!volumeSlider->audioOutput()->isMuted());
669 void MainWindow::volumeChanged(qreal newVolume) {
670 // automatically unmute when volume changes
671 if (volumeSlider->audioOutput()->isMuted())
672 volumeSlider->audioOutput()->setMuted(false);
673 statusBar()->showMessage(tr("Volume at %1%").arg(newVolume*100));
676 void MainWindow::volumeMutedChanged(bool muted) {
678 statusBar()->showMessage(tr("Volume is muted"));
680 statusBar()->showMessage(tr("Volume is unmuted"));
684 void MainWindow::abortDownload() {
685 QProgressDialog* dlg = dynamic_cast<QProgressDialog*>(this->sender());
686 QMap<QNetworkReply*, DownloadResource>::iterator cur;
687 QMap<QNetworkReply*, DownloadResource>::iterator end;
688 // locate the DownloadResource by its dialog address and trigger abortion
689 for(cur=m_downloads.begin(), end=m_downloads.end(); cur!=end; cur++){
690 if(cur.value().dialog == dlg) cur.key()->abort();
694 void MainWindow::download() {
695 if(mediaObject == NULL || mediaObject->currentSource().url().isEmpty()){
696 // complain unless video source apperas to be valid
697 QMessageBox::critical(this, tr("No Video playing"), tr("You must first play the video you intent to download !"));
700 QString filename = QFileDialog::getSaveFileName(this,
701 tr("Save video as..."),
702 tr("minitube video.mp4"),
703 "Video File(*.avi *.mp4)"
705 if(!filename.isNull()) {
706 // open destination file and initialize download
707 DownloadResource res;
708 res.file = new QFile(filename);
709 if(res.file->open(QFile::WriteOnly) == true) {
710 res.dialog = new QProgressDialog(tr("Downloading: ") + res.file->fileName(),
711 tr("Abort Download"), 0, 100, this);
712 connect(res.dialog, SIGNAL(canceled()), this, SLOT(abortDownload()));
713 download(mediaObject->currentSource().url(), res);
715 QMessageBox::critical(this, tr("File creation failed"), res.file->errorString());
721 void MainWindow::download(const QUrl& url, const DownloadResource& res) {
722 // create and store request and connect the reply signals
723 QNetworkReply *r = The::networkAccessManager()->get(QNetworkRequest(url));
724 m_downloads.insert(r, res);
725 connect(r, SIGNAL(finished()), this, SLOT(replyFinished()));
726 connect(r, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
727 connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError)));
728 connect(r, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(replyDownloadProgress(qint64,qint64)));
729 connect(r, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
732 void MainWindow::replyReadyRead() {
733 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
734 m_downloads[r].file->write(r->readAll());
737 void MainWindow::replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
738 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
739 if (bytesTotal > 0 && bytesReceived >0)
740 m_downloads[r].dialog->setValue( double(100.0/bytesTotal)*bytesReceived ); // pssst :-X
743 void MainWindow::replyError(QNetworkReply::NetworkError code) {
744 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
745 QMessageBox::critical(this, tr("Download failed"), r->errorString());
748 void MainWindow::replyFinished() {
749 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
750 m_downloads[r].dialog->close();
751 m_downloads[r].file->close();
752 delete m_downloads[r].file;
753 m_downloads.remove(r);
756 void MainWindow::replyMetaDataChanged() {
757 QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
758 QUrl url = r->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
760 // redirect - request new url, but keep the resources
761 qDebug() << "redirecting to: " << url.toString();
762 download(url, m_downloads[r]);
763 m_downloads.remove(r);