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 setVLCWindowsTitle();
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::setVLCWindowsTitle( QString aTitle )
186 if( aTitle.isEmpty() )
188 this->setWindowTitle( qtr( "VLC media player" ) );
192 this->setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
196 void MainInterface::handleMainUi( QSettings *settings )
198 QWidget *main = new QWidget( this );
199 setCentralWidget( main );
200 ui.setupUi( centralWidget() );
202 slider = new InputSlider( Qt::Horizontal, NULL );
203 ui.vboxLayout->insertWidget( 0, slider );
204 ui.discFrame->hide();
205 BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
206 BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
207 BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
209 BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
210 qtr("Previous"), prev() );
211 BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
212 BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
213 BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
216 ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
217 ui.volumeSlider->setMaximum( 100 );
218 ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
219 VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
220 ui.volMuteLabel->installEventFilter(h);
221 ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
223 BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
224 playlistEmbeddedFlag ? qtr( "Show playlist" ) :
225 qtr( "Open playlist" ) );
226 BUTTONACT( ui.playlistButton, playlist() );
228 /* Set initial size */
229 resize ( PREF_W, PREF_H );
231 addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
233 advControls = new ControlsWidget( p_intf );
234 ui.vboxLayout->insertWidget( 0, advControls );
235 advControls->updateGeometry();
236 if( !advControlsEnabled ) advControls->hide();
237 need_components_update = true;
239 visualSelector = new VisualSelector( p_intf );
240 ui.vboxLayout->insertWidget( 0, visualSelector );
241 visualSelector->hide();
243 if( alwaysVideoFlag )
245 bgWidget = new BackgroundWidget( p_intf );
246 bgWidget->widgetSize = settings->value( "backgroundSize",
247 QSize( 200, 200 ) ).toSize();
248 bgWidget->resize( bgWidget->widgetSize );
249 bgWidget->updateGeometry();
250 ui.vboxLayout->insertWidget( 0, bgWidget );
253 if( videoEmbeddedFlag )
255 videoWidget = new VideoWidget( p_intf );
256 videoWidget->widgetSize = QSize( 1, 1 );
257 videoWidget->resize( videoWidget->widgetSize );
258 ui.vboxLayout->insertWidget( 0, videoWidget );
260 p_intf->pf_request_window = ::DoRequest;
261 p_intf->pf_release_window = ::DoRelease;
262 p_intf->pf_control_window = ::DoControl;
264 setMinimumSize( PREF_W, addSize.height() );
267 /**********************************************************************
268 * Handling of the components
269 **********************************************************************/
270 void MainInterface::calculateInterfaceSize()
272 int width = 0, height = 0;
273 if( VISIBLE( bgWidget ) )
275 width = bgWidget->widgetSize.width();
276 height = bgWidget->widgetSize.height();
277 assert( !(playlistWidget && playlistWidget->isVisible() ) );
279 else if( VISIBLE( playlistWidget ) )
281 width = playlistWidget->widgetSize.width();
282 height = playlistWidget->widgetSize.height();
284 else if( videoIsActive )
286 width = videoWidget->widgetSize.width() ;
287 height = videoWidget->widgetSize.height();
291 width = PREF_W - addSize.width();
292 height = PREF_H - addSize.height();
294 if( VISIBLE( visualSelector ) )
295 height += visualSelector->height();
296 if( VISIBLE( advControls) )
298 height += advControls->sizeHint().height();
300 mainSize = QSize( width + addSize.width(), height + addSize.height() );
303 void MainInterface::resizeEvent( QResizeEvent *e )
305 videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
306 if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
308 SET_WH( videoWidget, e->size().width() - addSize.width(),
309 e->size().height() - addSize.height() );
310 videoWidget->updateGeometry();
312 if( VISIBLE( playlistWidget ) )
314 SET_WH( playlistWidget , e->size().width() - addSize.width(),
315 e->size().height() - addSize.height() );
316 playlistWidget->updateGeometry();
320 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
321 int *pi_y, unsigned int *pi_width,
322 unsigned int *pi_height )
324 void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
327 videoIsActive = true;
328 if( VISIBLE( playlistWidget ) )
330 embeddedPlaylistWasActive = true;
331 // playlistWidget->hide();
333 bool bgWasVisible = false;
334 if( VISIBLE( bgWidget) )
339 if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
341 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
343 else /* Background widget available, use its size */
345 /* Ok, our visualizations are bad, so don't do this for the moment
346 * use the requested size anyway */
347 // videoWidget->widgetSize = bgWidget->widgeTSize;
348 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
350 // videoWidget->updateGeometry(); /// FIXME: Needed ?
351 need_components_update = true;
356 void MainInterface::releaseVideo( void *p_win )
358 videoWidget->release( p_win );
359 videoWidget->widgetSize = QSize( 0, 0 );
360 videoWidget->resize( videoWidget->widgetSize );
362 if( embeddedPlaylistWasActive )
363 ;//playlistWidget->show();
367 videoIsActive = false;
368 need_components_update = true;
371 class SetVideoOnTopQtEvent : public QEvent
374 SetVideoOnTopQtEvent( bool _onTop ) :
375 QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
388 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
390 int i_ret = VLC_EGENERIC;
395 unsigned int *pi_width = va_arg( args, unsigned int * );
396 unsigned int *pi_height = va_arg( args, unsigned int * );
397 *pi_width = videoWidget->widgetSize.width();
398 *pi_height = videoWidget->widgetSize.height();
404 unsigned int i_width = va_arg( args, unsigned int );
405 unsigned int i_height = va_arg( args, unsigned int );
406 videoWidget->widgetSize = QSize( i_width, i_height );
407 // videoWidget->updateGeometry();
408 need_components_update = true;
412 case VOUT_SET_STAY_ON_TOP:
414 int i_arg = va_arg( args, int );
415 QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
420 msg_Warn( p_intf, "unsupported control query" );
426 void MainInterface::advanced()
428 if( !VISIBLE( advControls ) )
431 advControlsEnabled = true;
436 advControlsEnabled = false;
438 doComponentsUpdate();
441 void MainInterface::visual()
443 if( !VISIBLE( visualSelector) )
445 visualSelector->show();
446 if( !THEMIM->getIM()->hasVideo() )
448 /* Show the background widget */
450 visualSelectorEnabled = true;
454 /* Stop any currently running visualization */
455 visualSelector->hide();
456 visualSelectorEnabled = false;
458 doComponentsUpdate();
461 void MainInterface::playlist()
463 // Toggle the playlist dialog
464 if( !playlistEmbeddedFlag )
470 THEDP->playlistDialog();
474 if( !playlistWidget )
476 PlaylistDialog::killInstance();
477 playlistWidget = new PlaylistWidget( p_intf );
478 ui.vboxLayout->insertWidget( 0, playlistWidget );
479 playlistWidget->widgetSize = settings->value( "playlistSize",
480 QSize( 650, 310 ) ).toSize();
481 playlistWidget->hide();
483 if( VISIBLE( playlistWidget ) )
485 playlistWidget->hide();
488 videoWidget->widgetSize = savedVideoSize;
489 videoWidget->resize( videoWidget->widgetSize );
490 videoWidget->updateGeometry();
495 playlistWidget->show();
498 savedVideoSize = videoWidget->widgetSize;
499 videoWidget->widgetSize.setHeight( 0 );
500 videoWidget->resize( videoWidget->widgetSize );
501 videoWidget->updateGeometry();
503 if( VISIBLE( bgWidget ) ) bgWidget->hide();
505 doComponentsUpdate();
508 /* Video widget cannot do this synchronously as it runs in another thread */
509 /* Well, could it, actually ? Probably dangerous ... */
510 void MainInterface::doComponentsUpdate()
512 calculateInterfaceSize();
516 void MainInterface::undockPlaylist()
520 playlistWidget->hide();
521 playlistWidget->deleteLater();
522 ui.vboxLayout->removeWidget( playlistWidget );
523 playlistWidget = NULL;
524 playlistEmbeddedFlag = false;
527 QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
528 visualSelectorEnabled);
532 videoWidget->widgetSize = savedVideoSize;
533 videoWidget->resize( videoWidget->widgetSize );
534 videoWidget->updateGeometry();
537 doComponentsUpdate();
538 THEDP->playlistDialog();
542 void MainInterface::customEvent( QEvent *event )
544 if( event->type() == PLDockEvent_Type )
546 PlaylistDialog::killInstance();
547 playlistEmbeddedFlag = true;
549 QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
550 visualSelectorEnabled);
553 else if ( event->type() == SetVideoOnTopEvent_Type )
555 SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
556 if( p_event->OnTop() )
557 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
559 setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
560 show(); /* necessary to apply window flags?? */
565 /************************************************************************
567 ************************************************************************/
568 void MainInterface::dropEvent(QDropEvent *event)
570 const QMimeData *mimeData = event->mimeData();
572 /* D&D of a subtitles file, add it on the fly */
573 if( mimeData->urls().size() == 1 )
575 if( THEMIM->getIM()->hasInput() )
577 if( input_AddSubtitles( THEMIM->getInput(),
578 qtu( mimeData->urls()[0].toString() ),
581 event->acceptProposedAction();
587 foreach( QUrl url, mimeData->urls() ) {
588 QString s = url.toString();
589 if( s.length() > 0 ) {
590 playlist_Add( THEPL, qtu(s), NULL,
591 PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
592 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
596 event->acceptProposedAction();
598 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
600 event->acceptProposedAction();
602 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
604 event->acceptProposedAction();
606 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
611 /************************************************************************
613 ************************************************************************/
614 void MainInterface::keyPressEvent( QKeyEvent *e )
616 int i_vlck = qtEventToVLCKey( e );
619 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
626 void MainInterface::wheelEvent( QWheelEvent *e )
628 int i_vlckey = qtWheelEventToVLCKey( e );
629 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
633 void MainInterface::stop()
638 void MainInterface::play()
640 if( playlist_IsEmpty(THEPL) )
642 /* The playlist is empty, open a file requester */
643 THEDP->openFileDialog();
647 THEMIM->togglePlayPause();
650 void MainInterface::prev()
655 void MainInterface::next()
660 void MainInterface::setDisplay( float pos, int time, int length )
662 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
663 secstotimestr( psz_length, length );
664 secstotimestr( psz_time, time );
666 title.sprintf( "%s/%s", psz_time, psz_length );
667 timeLabel->setText( " "+title+" " );
670 void MainInterface::setName( QString name )
672 nameLabel->setText( " " + name+" " );
673 setVLCWindowsTitle( name );
676 void MainInterface::setStatus( int status )
678 if( status == 1 ) // Playing
679 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
681 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
684 #define HELP_MENU N_("Menu")
685 #define HELP_PCH N_("Previous chapter")
686 #define HELP_NCH N_("Next chapter")
687 #define HELP_PTR N_("Previous track")
688 #define HELP_NTR N_("Next track")
690 void MainInterface::setNavigation( int navigation )
692 // 1 = chapter, 2 = title, 0 = no
693 if( navigation == 0 )
695 ui.discFrame->hide();
696 } else if( navigation == 1 ) {
697 ui.prevSectionButton->show();
698 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
699 ui.nextSectionButton->show();
700 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
701 ui.menuButton->show();
702 ui.discFrame->show();
704 ui.prevSectionButton->show();
705 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
706 ui.nextSectionButton->show();
707 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
708 ui.menuButton->hide();
709 ui.discFrame->show();
713 static bool b_my_volume;
715 void MainInterface::updateOnTimer()
717 /* \todo Make this event-driven */
718 advControls->enableInput( THEMIM->getIM()->hasInput() );
719 advControls->enableVideo( THEMIM->getIM()->hasVideo() );
721 if( intf_ShouldDie( p_intf ) )
723 QApplication::closeAllWindows();
724 QApplication::quit();
726 if( need_components_update )
728 doComponentsUpdate();
729 need_components_update = false;
732 audio_volume_t i_volume;
733 aout_VolumeGet( p_intf, &i_volume );
734 i_volume = (i_volume * 200 )/ AOUT_VOLUME_MAX ;
735 int i_gauge = ui.volumeSlider->value();
737 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
740 ui.volumeSlider->setValue( i_volume );
745 void MainInterface::closeEvent( QCloseEvent *e )
748 p_intf->b_die = VLC_TRUE;
751 void MainInterface::updateVolume( int sliderVolume )
755 int i_res = sliderVolume * AOUT_VOLUME_MAX /
756 (2*ui.volumeSlider->maximum() );
757 aout_VolumeSet( p_intf, i_res );
761 static int InteractCallback( vlc_object_t *p_this,
762 const char *psz_var, vlc_value_t old_val,
763 vlc_value_t new_val, void *param )
765 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
766 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
767 DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
768 QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
772 /*****************************************************************************
773 * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
774 * We don't show the menu directly here because we don't want the
775 * caller to block for a too long time.
776 *****************************************************************************/
777 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
778 vlc_value_t old_val, vlc_value_t new_val, void *param )
780 intf_thread_t *p_intf = (intf_thread_t *)param;
782 if( p_intf->pf_show_dialog )
784 p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
791 /*****************************************************************************
792 * IntfShowCB: callback triggered by the intf-show playlist variable.
793 *****************************************************************************/
794 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
795 vlc_value_t old_val, vlc_value_t new_val, void *param )
797 intf_thread_t *p_intf = (intf_thread_t *)param;
798 //p_intf->p_sys->b_intf_show = VLC_TRUE;