1 /*****************************************************************************
2 * main_interface.cpp : Main interface
3 ****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #include "main_interface.hpp"
26 #include "input_manager.hpp"
27 #include "util/input_slider.hpp"
28 #include "util/qvlcframe.hpp"
29 #include "util/customwidgets.hpp"
30 #include "dialogs_provider.hpp"
31 #include "components/interface_widgets.hpp"
32 #include "dialogs/playlist.hpp"
36 #include <QCloseEvent>
37 #include <QPushButton>
54 #define VISIBLE(i) (i && i->isVisible())
56 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
57 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
58 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
60 #define DS(i) i.width(),i.height()
62 /* Callback prototypes */
63 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
64 vlc_value_t old_val, vlc_value_t new_val, void *param );
65 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
66 vlc_value_t old_val, vlc_value_t new_val, void *param );
67 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
70 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
71 int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
73 return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
75 static void DoRelease( intf_thread_t *p_intf, void *p_win )
77 return p_intf->p_sys->p_mi->releaseVideo( p_win );
79 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
81 return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
84 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
87 settings = new QSettings( "VideoLAN", "VLC" );
88 settings->beginGroup( "MainWindow" );
90 setWindowIcon( QApplication::windowIcon() );
92 need_components_update = false;
93 bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
94 embeddedPlaylistWasActive = videoIsActive = false;
96 videoEmbeddedFlag = false;
97 if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
99 alwaysVideoFlag = false;
100 if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
101 alwaysVideoFlag = true;
103 playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
104 advControlsEnabled= settings->value( "adv-controls", false ).toBool();
105 visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
108 setWindowTitle( qtr( "VLC media player" ) );
109 handleMainUi( settings );
110 QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
111 advControlsEnabled, visualSelectorEnabled );
112 timeLabel = new QLabel( 0 );
113 nameLabel = new QLabel( 0 );
114 statusBar()->addWidget( nameLabel, 4 );
115 statusBar()->addPermanentWidget( timeLabel, 1 );
117 setFocusPolicy( Qt::StrongFocus );
118 setAcceptDrops(true);
120 /* Init input manager */
121 MainInputManager::getInstance( p_intf );
122 ON_TIMEOUT( updateOnTimer() );
125 CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
126 /* Connect the input manager to the GUI elements it manages */
127 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
128 slider, setPosition( float,int, int ) );
129 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
130 this, setDisplay( float, int, int ) );
131 CONNECT( THEMIM->getIM(), nameChanged( QString ), this,setName( QString ) );
132 CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
133 CONNECT( THEMIM->getIM(), navigationChanged( int ), this, setNavigation(int) );
134 CONNECT( slider, sliderDragged( float ),
135 THEMIM->getIM(), sliderUpdate( float ) );
137 CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
139 CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
141 CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
144 var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
145 var_AddCallback( p_intf, "interaction", InteractCallback, this );
146 p_intf->b_interaction = VLC_TRUE;
148 /* Register callback for the intf-popupmenu variable */
149 playlist_t *p_playlist =
150 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
151 if( p_playlist != NULL )
153 var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
154 var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
155 vlc_object_release( p_playlist );
159 MainInterface::~MainInterface()
161 /* Unregister callback for the intf-popupmenu variable */
162 playlist_t *p_playlist =
163 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
164 if( p_playlist != NULL )
166 var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
167 var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
168 vlc_object_release( p_playlist );
171 settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
172 settings->setValue( "adv-controls", advControlsEnabled );
173 settings->setValue( "pos", pos() );
174 settings->endGroup();
176 p_intf->b_interaction = VLC_FALSE;
177 var_DelCallback( p_intf, "interaction", InteractCallback, this );
179 p_intf->pf_request_window = NULL;
180 p_intf->pf_release_window = NULL;
181 p_intf->pf_control_window = NULL;
184 void MainInterface::handleMainUi( QSettings *settings )
186 QWidget *main = new QWidget( this );
187 setCentralWidget( main );
188 ui.setupUi( centralWidget() );
190 slider = new InputSlider( Qt::Horizontal, NULL );
191 ui.vboxLayout->insertWidget( 0, slider );
192 ui.discFrame->hide();
193 BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
194 BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
195 BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
197 BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
198 qtr("Previous"), prev() );
199 BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
200 BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
201 BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
204 ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
205 ui.volumeSlider->setMaximum( 100 );
206 ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
207 VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
208 ui.volMuteLabel->installEventFilter(h);
209 ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
211 BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
212 playlistEmbeddedFlag ? qtr( "Show playlist" ) :
213 qtr( "Open playlist" ) );
214 BUTTONACT( ui.playlistButton, playlist() );
216 /* Set initial size */
217 resize ( PREF_W, PREF_H );
219 addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
221 advControls = new ControlsWidget( p_intf );
222 ui.vboxLayout->insertWidget( 0, advControls );
223 advControls->updateGeometry();
224 if( !advControlsEnabled ) advControls->hide();
225 need_components_update = true;
227 visualSelector = new VisualSelector( p_intf );
228 ui.vboxLayout->insertWidget( 0, visualSelector );
229 visualSelector->hide();
231 if( alwaysVideoFlag )
233 bgWidget = new BackgroundWidget( p_intf );
234 bgWidget->widgetSize = settings->value( "backgroundSize",
235 QSize( 200, 200 ) ).toSize();
236 bgWidget->resize( bgWidget->widgetSize );
237 bgWidget->updateGeometry();
238 ui.vboxLayout->insertWidget( 0, bgWidget );
241 if( videoEmbeddedFlag )
243 videoWidget = new VideoWidget( p_intf );
244 videoWidget->widgetSize = QSize( 1, 1 );
245 videoWidget->resize( videoWidget->widgetSize );
246 ui.vboxLayout->insertWidget( 0, videoWidget );
248 p_intf->pf_request_window = ::DoRequest;
249 p_intf->pf_release_window = ::DoRelease;
250 p_intf->pf_control_window = ::DoControl;
252 setMinimumSize( PREF_W, addSize.height() );
255 /**********************************************************************
256 * Handling of the components
257 **********************************************************************/
258 void MainInterface::calculateInterfaceSize()
260 int width = 0, height = 0;
261 if( VISIBLE( bgWidget ) )
263 width = bgWidget->widgetSize.width();
264 height = bgWidget->widgetSize.height();
265 assert( !(playlistWidget && playlistWidget->isVisible() ) );
267 else if( VISIBLE( playlistWidget ) )
269 width = playlistWidget->widgetSize.width();
270 height = playlistWidget->widgetSize.height();
272 else if( videoIsActive )
274 width = videoWidget->widgetSize.width() ;
275 height = videoWidget->widgetSize.height();
279 width = PREF_W - addSize.width();
280 height = PREF_H - addSize.height();
282 if( VISIBLE( visualSelector ) )
283 height += visualSelector->height();
284 if( VISIBLE( advControls) )
286 height += advControls->sizeHint().height();
288 mainSize = QSize( width + addSize.width(), height + addSize.height() );
291 void MainInterface::resizeEvent( QResizeEvent *e )
293 videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
294 if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
296 SET_WH( videoWidget, e->size().width() - addSize.width(),
297 e->size().height() - addSize.height() );
298 videoWidget->updateGeometry();
300 if( VISIBLE( playlistWidget ) )
302 SET_WH( playlistWidget , e->size().width() - addSize.width(),
303 e->size().height() - addSize.height() );
304 playlistWidget->updateGeometry();
308 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
309 int *pi_y, unsigned int *pi_width,
310 unsigned int *pi_height )
312 void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
315 videoIsActive = true;
316 if( VISIBLE( playlistWidget ) )
318 embeddedPlaylistWasActive = true;
319 // playlistWidget->hide();
321 bool bgWasVisible = false;
322 if( VISIBLE( bgWidget) )
327 if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
329 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
331 else /* Background widget available, use its size */
333 /* Ok, our visualizations are bad, so don't do this for the moment
334 * use the requested size anyway */
335 // videoWidget->widgetSize = bgWidget->widgeTSize;
336 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
338 // videoWidget->updateGeometry(); /// FIXME: Needed ?
339 need_components_update = true;
344 void MainInterface::releaseVideo( void *p_win )
346 videoWidget->release( p_win );
347 videoWidget->widgetSize = QSize( 0, 0 );
348 videoWidget->resize( videoWidget->widgetSize );
350 if( embeddedPlaylistWasActive )
351 ;//playlistWidget->show();
355 videoIsActive = false;
356 need_components_update = true;
359 class SetVideoOnTopQtEvent : public QEvent
362 SetVideoOnTopQtEvent( bool _onTop ) :
363 QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
376 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
378 int i_ret = VLC_EGENERIC;
383 unsigned int *pi_width = va_arg( args, unsigned int * );
384 unsigned int *pi_height = va_arg( args, unsigned int * );
385 *pi_width = videoWidget->widgetSize.width();
386 *pi_height = videoWidget->widgetSize.height();
392 unsigned int i_width = va_arg( args, unsigned int );
393 unsigned int i_height = va_arg( args, unsigned int );
394 videoWidget->widgetSize = QSize( i_width, i_height );
395 // videoWidget->updateGeometry();
396 need_components_update = true;
400 case VOUT_SET_STAY_ON_TOP:
402 int i_arg = va_arg( args, int );
403 QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
408 msg_Warn( p_intf, "unsupported control query" );
414 void MainInterface::advanced()
416 if( !VISIBLE( advControls ) )
419 advControlsEnabled = true;
424 advControlsEnabled = false;
426 doComponentsUpdate();
429 void MainInterface::visual()
431 if( !VISIBLE( visualSelector) )
433 visualSelector->show();
434 if( !THEMIM->getIM()->hasVideo() )
436 /* Show the background widget */
438 visualSelectorEnabled = true;
442 /* Stop any currently running visualization */
443 visualSelector->hide();
444 visualSelectorEnabled = false;
446 doComponentsUpdate();
449 void MainInterface::playlist()
451 // Toggle the playlist dialog
452 if( !playlistEmbeddedFlag )
458 THEDP->playlistDialog();
462 if( !playlistWidget )
464 PlaylistDialog::killInstance();
465 playlistWidget = new PlaylistWidget( p_intf );
466 ui.vboxLayout->insertWidget( 0, playlistWidget );
467 playlistWidget->widgetSize = settings->value( "playlistSize",
468 QSize( 650, 310 ) ).toSize();
469 playlistWidget->hide();
471 if( VISIBLE( playlistWidget ) )
473 playlistWidget->hide();
476 videoWidget->widgetSize = savedVideoSize;
477 videoWidget->resize( videoWidget->widgetSize );
478 videoWidget->updateGeometry();
483 playlistWidget->show();
486 savedVideoSize = videoWidget->widgetSize;
487 videoWidget->widgetSize.setHeight( 0 );
488 videoWidget->resize( videoWidget->widgetSize );
489 videoWidget->updateGeometry();
491 if( VISIBLE( bgWidget ) ) bgWidget->hide();
493 doComponentsUpdate();
496 /* Video widget cannot do this synchronously as it runs in another thread */
497 /* Well, could it, actually ? Probably dangerous ... */
498 void MainInterface::doComponentsUpdate()
500 calculateInterfaceSize();
504 void MainInterface::undockPlaylist()
508 playlistWidget->hide();
509 playlistWidget->deleteLater();
510 ui.vboxLayout->removeWidget( playlistWidget );
511 playlistWidget = NULL;
512 playlistEmbeddedFlag = false;
515 QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
516 visualSelectorEnabled);
520 videoWidget->widgetSize = savedVideoSize;
521 videoWidget->resize( videoWidget->widgetSize );
522 videoWidget->updateGeometry();
525 doComponentsUpdate();
526 THEDP->playlistDialog();
530 void MainInterface::customEvent( QEvent *event )
532 if( event->type() == PLDockEvent_Type )
534 PlaylistDialog::killInstance();
535 playlistEmbeddedFlag = true;
537 QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
538 visualSelectorEnabled);
541 else if ( event->type() == SetVideoOnTopEvent_Type )
543 SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
544 if( p_event->OnTop() )
545 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
547 setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
548 show(); /* necessary to apply window flags?? */
553 /************************************************************************
555 ************************************************************************/
556 void MainInterface::dropEvent(QDropEvent *event)
558 const QMimeData *mimeData = event->mimeData();
560 /* D&D of a subtitles file, add it on the fly */
561 if( mimeData->urls().size() == 1 )
563 if( THEMIM->getIM()->hasInput() )
565 if( input_AddSubtitles( THEMIM->getInput(),
566 qtu( mimeData->urls()[0].toString() ),
569 event->acceptProposedAction();
575 foreach( QUrl url, mimeData->urls() ) {
576 QString s = url.toString();
577 if( s.length() > 0 ) {
578 playlist_Add( THEPL, qtu(s), NULL,
579 PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
580 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
584 event->acceptProposedAction();
586 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
588 event->acceptProposedAction();
590 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
592 event->acceptProposedAction();
594 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
599 /************************************************************************
601 ************************************************************************/
602 void MainInterface::keyPressEvent( QKeyEvent *e )
604 int i_vlck = qtEventToVLCKey( e );
607 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
614 void MainInterface::wheelEvent( QWheelEvent *e )
616 int i_vlckey = qtWheelEventToVLCKey( e );
617 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
621 void MainInterface::stop()
626 void MainInterface::play()
628 if( playlist_IsEmpty(THEPL) )
630 /* The playlist is empty, open a file requester */
631 THEDP->openFileDialog();
635 THEMIM->togglePlayPause();
638 void MainInterface::prev()
643 void MainInterface::next()
648 void MainInterface::setDisplay( float pos, int time, int length )
650 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
651 secstotimestr( psz_length, length );
652 secstotimestr( psz_time, time );
654 title.sprintf( "%s/%s", psz_time, psz_length );
655 timeLabel->setText( " "+title+" " );
658 void MainInterface::setName( QString name )
660 nameLabel->setText( " " + name+" " );
663 void MainInterface::setStatus( int status )
665 if( status == 1 ) // Playing
666 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
668 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
671 #define HELP_MENU N_("Menu")
672 #define HELP_PCH N_("Previous chapter")
673 #define HELP_NCH N_("Next chapter")
674 #define HELP_PTR N_("Previous track")
675 #define HELP_NTR N_("Next track")
677 void MainInterface::setNavigation( int navigation )
679 // 1 = chapter, 2 = title, 0 = no
680 if( navigation == 0 )
682 ui.discFrame->hide();
683 } else if( navigation == 1 ) {
684 ui.prevSectionButton->show();
685 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
686 ui.nextSectionButton->show();
687 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
688 ui.menuButton->show();
689 ui.discFrame->show();
691 ui.prevSectionButton->show();
692 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
693 ui.nextSectionButton->show();
694 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
695 ui.menuButton->hide();
696 ui.discFrame->show();
700 static bool b_my_volume;
702 void MainInterface::updateOnTimer()
704 /* \todo Make this event-driven */
705 advControls->enableInput( THEMIM->getIM()->hasInput() );
706 advControls->enableVideo( THEMIM->getIM()->hasVideo() );
708 if( intf_ShouldDie( p_intf ) )
710 QApplication::closeAllWindows();
711 QApplication::quit();
713 if( need_components_update )
715 doComponentsUpdate();
716 need_components_update = false;
719 audio_volume_t i_volume;
720 aout_VolumeGet( p_intf, &i_volume );
721 i_volume = (i_volume * 200 )/ AOUT_VOLUME_MAX ;
722 int i_gauge = ui.volumeSlider->value();
724 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
727 ui.volumeSlider->setValue( i_volume );
732 void MainInterface::closeEvent( QCloseEvent *e )
735 p_intf->b_die = VLC_TRUE;
738 void MainInterface::updateVolume( int sliderVolume )
742 int i_res = sliderVolume * AOUT_VOLUME_MAX /
743 (2*ui.volumeSlider->maximum() );
744 aout_VolumeSet( p_intf, i_res );
748 static int InteractCallback( vlc_object_t *p_this,
749 const char *psz_var, vlc_value_t old_val,
750 vlc_value_t new_val, void *param )
752 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
753 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
754 DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
755 QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
759 /*****************************************************************************
760 * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
761 * We don't show the menu directly here because we don't want the
762 * caller to block for a too long time.
763 *****************************************************************************/
764 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
765 vlc_value_t old_val, vlc_value_t new_val, void *param )
767 intf_thread_t *p_intf = (intf_thread_t *)param;
769 if( p_intf->pf_show_dialog )
771 p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
778 /*****************************************************************************
779 * IntfShowCB: callback triggered by the intf-show playlist variable.
780 *****************************************************************************/
781 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
782 vlc_value_t old_val, vlc_value_t new_val, void *param )
784 intf_thread_t *p_intf = (intf_thread_t *)param;
785 //p_intf->p_sys->b_intf_show = VLC_TRUE;