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 // lazy initialized views
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&)));
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);
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);
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);
72 MainWindow::~MainWindow() {
76 void MainWindow::createActions() {
78 QMap<QString, QAction*> *actions = The::globalActions();
81 settingsAct = new QAction(tr("&Preferences..."), this);
82 settingsAct->setStatusTip(tr(QString("Configure ").append(Constants::APP_NAME).toUtf8()));
84 settingsAct->setMenuRole(QAction::PreferencesRole);
85 actions->insert("settings", settingsAct);
86 connect(settingsAct, SIGNAL(triggered()), this, SLOT(showSettings()));
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()));
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()));
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()));
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()));
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_Enter));
119 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
120 actions->insert("fullscreen", fullscreenAct);
121 connect(fullscreenAct, SIGNAL(triggered()), this, SLOT(fullscreen()));
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_M));
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)));
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()));
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()));
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()));
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()));
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()));
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()));
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()));
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()));
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()));
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);
201 // common action properties
202 foreach (QAction *action, actions->values()) {
204 // unexperienced users tend to keep keys pressed for a "long" time
205 action->setAutoRepeat(false);
206 action->setToolTip(action->statusTip());
208 // make the actions work when video is fullscreen
209 action->setShortcutContext(Qt::ApplicationShortcut);
212 // OSX does not use icons in menus
213 action->setIconVisibleInMenu(false);
220 void MainWindow::createMenus() {
222 QMap<QString, QMenu*> *menus = The::globalMenus();
224 fileMenu = menuBar()->addMenu(tr("&Application"));
225 // menus->insert("file", fileMenu);
227 fileMenu->addAction(settingsAct);
228 fileMenu->addSeparator();
230 fileMenu->addAction(quitAct);
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);
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(fullscreenAct);
249 viewMenu->addAction(compactViewAct);
251 helpMenu = menuBar()->addMenu(tr("&Help"));
252 helpMenu->addAction(siteAct);
253 helpMenu->addAction(donateAct);
254 helpMenu->addAction(aboutAct);
257 void MainWindow::createToolBars() {
259 setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
261 mainToolBar = new QToolBar(this);
262 mainToolBar->setFloatable(false);
263 mainToolBar->setMovable(false);
266 smallerFont.setPointSize(smallerFont.pointSize()*.85);
267 mainToolBar->setFont(smallerFont);
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);
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);
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));
287 mainToolBar->addWidget(new Spacer(mainToolBar, toolbarSearch));
289 addToolBar(mainToolBar);
292 void MainWindow::createStatusBar() {
293 currentTime = new QLabel(this);
294 currentTime->setFrameShape(QFrame::NoFrame);
295 statusBar()->addPermanentWidget(currentTime);
297 totalTime = new QLabel(this);
298 totalTime->setFrameShape(QFrame::NoFrame);
299 statusBar()->addPermanentWidget(totalTime);
304 void MainWindow::readSettings() {
306 restoreGeometry(settings.value("geometry").toByteArray());
309 void MainWindow::writeSettings() {
311 settings.setValue("geometry", saveGeometry());
314 void MainWindow::goBack() {
315 if ( history->size() > 1 ) {
317 QWidget *widget = history->pop();
322 void MainWindow::showWidget ( QWidget* widget ) {
324 // call hide method on the current view
325 View* oldView = dynamic_cast<View *> (views->currentWidget());
326 if (oldView != NULL) {
327 oldView->disappear();
330 // call show method on the new view
331 View* newView = dynamic_cast<View *> (widget);
332 if (newView != NULL) {
334 QMap<QString,QVariant> metadata = newView->metadata();
335 QString windowTitle = metadata.value("title").toString();
336 if (windowTitle.length())
337 windowTitle += " - ";
338 setWindowTitle(windowTitle + Constants::APP_NAME);
339 statusBar()->showMessage((metadata.value("description").toString()));
343 // backAct->setEnabled(history->size() > 1);
344 // settingsAct->setEnabled(widget != settingsView);
345 stopAct->setEnabled(widget == mediaView);
346 fullscreenAct->setEnabled(widget == mediaView);
347 compactViewAct->setEnabled(widget == mediaView);
348 webPageAct->setEnabled(widget == mediaView);
349 aboutAct->setEnabled(widget != aboutView);
351 // cool toolbar on the Mac
352 setUnifiedTitleAndToolBarOnMac(widget == mediaView);
354 // toolbar only for the mediaView
355 mainToolBar->setVisible(widget == mediaView);
357 history->push(widget);
358 fadeInWidget(views->currentWidget(), widget);
359 views->setCurrentWidget(widget);
363 void MainWindow::fadeInWidget(QWidget *oldWidget, QWidget *newWidget) {
364 if (faderWidget) faderWidget->close();
365 if (!oldWidget || !newWidget || oldWidget == mediaView || newWidget == mediaView) return;
366 QPixmap frozenView = QPixmap::grabWidget(oldWidget);
367 faderWidget = new FaderWidget(newWidget);
368 faderWidget->start(frozenView);
371 void MainWindow::about() {
373 aboutView = new AboutView(this);
374 views->addWidget(aboutView);
376 showWidget(aboutView);
379 void MainWindow::visitSite() {
380 QUrl url(Constants::WEBSITE);
381 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
382 QDesktopServices::openUrl(url);
385 void MainWindow::donate() {
386 QUrl url(QString(Constants::WEBSITE) + "#donate");
387 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
388 QDesktopServices::openUrl(url);
391 void MainWindow::quit() {
396 void MainWindow::closeEvent(QCloseEvent *event) {
398 QWidget::closeEvent(event);
401 void MainWindow::showSettings() {
403 settingsView = new SettingsView(this);
404 views->addWidget(settingsView);
406 showWidget(settingsView);
409 void MainWindow::showSearch() {
410 showWidget(searchView);
411 currentTime->clear();
415 void MainWindow::showMedia(QString query) {
417 mediaView->setMediaObject(mediaObject);
418 SearchParams *searchParams = new SearchParams();
419 searchParams->setKeywords(query);
420 mediaView->search(searchParams);
421 showWidget(mediaView);
424 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) {
426 // qDebug() << "Phonon state: " << newState;
430 case Phonon::ErrorState:
431 if (mediaObject->errorType() == Phonon::FatalError) {
432 statusBar()->showMessage(tr("Fatal error: %1").arg(mediaObject->errorString()));
434 statusBar()->showMessage(tr("Error: %1").arg(mediaObject->errorString()));
438 case Phonon::PlayingState:
439 pauseAct->setEnabled(true);
440 pauseAct->setIcon(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")));
441 pauseAct->setText(tr("&Pause"));
442 pauseAct->setStatusTip(tr("Pause playback"));
443 skipAct->setEnabled(true);
446 case Phonon::StoppedState:
447 pauseAct->setEnabled(false);
448 skipAct->setEnabled(false);
451 case Phonon::PausedState:
452 skipAct->setEnabled(true);
453 pauseAct->setEnabled(true);
454 pauseAct->setIcon(QtIconLoader::icon("media-play", QIcon(":/images/play.png")));
455 pauseAct->setText(tr("&Play"));
456 pauseAct->setStatusTip(tr("Resume playback"));
459 case Phonon::BufferingState:
460 case Phonon::LoadingState:
461 skipAct->setEnabled(true);
462 pauseAct->setEnabled(false);
463 currentTime->clear();
472 void MainWindow::stop() {
477 void MainWindow::fullscreen() {
479 mediaView->exitFullscreen();
480 fullscreenAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Enter));
481 fullscreenAct->setText(tr("&Full Screen"));
482 stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
484 mediaView->fullscreen();
485 stopAct->setShortcut(QString(""));
486 fullscreenAct->setShortcut(QKeySequence(Qt::Key_Escape));
487 fullscreenAct->setText(tr("Exit &Full Screen"));
489 m_fullscreen = !m_fullscreen;
492 void MainWindow::compactView(bool enable) {
493 if (m_fullscreen) fullscreen();
494 mediaView->setPlaylistVisible(!enable);
495 mainToolBar->setVisible(!enable);
498 void MainWindow::searchFocus() {
499 QWidget *view = views->currentWidget();
500 if (view == mediaView) {
501 toolbarSearch->setFocus();
505 void MainWindow::initPhonon() {
506 // Phonon initialization
507 if (mediaObject) delete mediaObject;
508 if (audioOutput) delete audioOutput;
509 mediaObject = new Phonon::MediaObject(this);
510 mediaObject->setTickInterval(100);
511 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
512 this, SLOT(stateChanged(Phonon::State, Phonon::State)));
513 connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
514 connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
515 seekSlider->setMediaObject(mediaObject);
516 audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
517 volumeSlider->setAudioOutput(audioOutput);
518 Phonon::createPath(mediaObject, audioOutput);
521 void MainWindow::tick(qint64 time) {
522 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
523 currentTime->setText(displayTime.toString("mm:ss"));
524 // qDebug() << "currentTime" << time << displayTime.toString("mm:ss");
527 void MainWindow::totalTimeChanged(qint64 time) {
532 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
533 totalTime->setText(displayTime.toString("/ mm:ss"));
534 // qDebug() << "totalTime" << time << displayTime.toString("mm:ss");