1 /*****************************************************************************
2 * main_inteface.cpp : Main interface
3 ****************************************************************************
4 * Copyright (C) 2006 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. *****************************************************************************/
24 #include "main_interface.hpp"
25 #include "input_manager.hpp"
26 #include "util/input_slider.hpp"
27 #include "util/qvlcframe.hpp"
28 #include "util/customwidgets.hpp"
29 #include "dialogs_provider.hpp"
30 #include "components/interface_widgets.hpp"
31 #include "dialogs/playlist.hpp"
35 #include <QCloseEvent>
36 #include <QPushButton>
43 #include <aout_internal.h>
53 #define VISIBLE(i) (i && i->isVisible())
55 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
56 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
57 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
59 #define DS(i) i.width(),i.height()
61 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
64 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
65 int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
67 return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
69 static void DoRelease( intf_thread_t *p_intf, void *p_win )
71 return p_intf->p_sys->p_mi->releaseVideo( p_win );
73 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
75 return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
78 bool embeddedPlaylistWasActive;
82 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
84 settings = new QSettings( "VideoLAN", "VLC" );
85 settings->beginGroup( "MainWindow" );
87 need_components_update = false;
88 bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
89 embeddedPlaylistWasActive = videoIsActive = false;
91 /* Fetch configuration from settings and vlc config */
92 videoEmbeddedFlag = false;
93 if( config_GetInt( p_intf, "embedded-video" ) )
94 videoEmbeddedFlag = true;
96 alwaysVideoFlag = false;
97 if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
98 alwaysVideoFlag = true;
100 playlistEmbeddedFlag = settings->value( "playlist-embedded", true ).
102 advControlsEnabled= settings->value( "adv-controls", false ).toBool();
104 setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
105 handleMainUi( settings );
107 QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
108 advControlsEnabled );
111 timeLabel = new QLabel( 0 );
112 nameLabel = new QLabel( 0 );
113 statusBar()->addWidget( nameLabel, 4 );
114 statusBar()->addPermanentWidget( timeLabel, 1 );
116 setFocusPolicy( Qt::StrongFocus );
118 /* Init input manager */
119 MainInputManager::getInstance( p_intf );
120 ON_TIMEOUT( updateOnTimer() );
123 CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
124 /* Connect the input manager to the GUI elements it manages */
125 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
126 slider, setPosition( float,int, int ) );
127 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
128 this, setDisplay( float, int, int ) );
129 CONNECT( THEMIM->getIM(), nameChanged( QString ), this,setName( QString ) );
130 CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
131 CONNECT( slider, sliderDragged( float ),
132 THEMIM->getIM(), sliderUpdate( float ) );
134 var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
135 var_AddCallback( p_intf, "interaction", InteractCallback, this );
136 p_intf->b_interaction = VLC_TRUE;
139 MainInterface::~MainInterface()
141 settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
142 settings->setValue( "adv-controls", advControlsEnabled );
143 settings->setValue( "pos", pos() );
144 settings->endGroup();
146 p_intf->b_interaction = VLC_FALSE;
147 var_DelCallback( p_intf, "interaction", InteractCallback, this );
149 p_intf->pf_request_window = NULL;
150 p_intf->pf_release_window = NULL;
151 p_intf->pf_control_window = NULL;
154 void MainInterface::handleMainUi( QSettings *settings )
156 QWidget *main = new QWidget( this );
157 setCentralWidget( main );
158 ui.setupUi( centralWidget() );
160 slider = new InputSlider( Qt::Horizontal, NULL );
161 ui.hboxLayout->insertWidget( 0, slider );
163 BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
164 qtr("Previous"), prev() );
165 BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
166 BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
167 BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
168 BUTTON_SET_ACT_I( ui.visualButton, "", stop.png,
169 qtr( "Audio visualizations" ), visual() );
172 ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
173 ui.volumeSlider->setMaximum( 100 );
174 ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
175 VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
176 ui.volMuteLabel->installEventFilter(h);
177 ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
179 BUTTON_SET_IMG( ui.playlistButton, "" ,volume-low.png,
180 playlistEmbeddedFlag ? qtr( "Show playlist" ) :
181 qtr( "Open playlist" ) );
182 BUTTONACT( ui.playlistButton, playlist() );
184 /* Set initial size */
185 resize ( PREF_W, PREF_H );
187 addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
189 advControls = new ControlsWidget( p_intf );
190 ui.vboxLayout->insertWidget( 0, advControls );
191 advControls->updateGeometry();
192 if( !advControlsEnabled ) advControls->hide();
193 need_components_update = true;
195 visualSelector = new VisualSelector( p_intf );
196 ui.vboxLayout->insertWidget( 0, visualSelector );
197 visualSelector->hide();
199 if( alwaysVideoFlag )
201 bgWidget = new BackgroundWidget( p_intf );
202 bgWidget->widgetSize = settings->value( "backgroundSize",
203 QSize( 200, 200 ) ).toSize();
204 bgWidget->resize( bgWidget->widgetSize );
205 bgWidget->updateGeometry();
206 ui.vboxLayout->insertWidget( 0, bgWidget );
209 if( videoEmbeddedFlag )
211 videoWidget = new VideoWidget( p_intf );
212 videoWidget->widgetSize = QSize( 1, 1 );
213 videoWidget->resize( videoWidget->widgetSize );
214 ui.vboxLayout->insertWidget( 0, videoWidget );
216 p_intf->pf_request_window = ::DoRequest;
217 p_intf->pf_release_window = ::DoRelease;
218 p_intf->pf_control_window = ::DoControl;
220 setMinimumSize( PREF_W, addSize.height() );
223 /**********************************************************************
224 * Handling of the components
225 **********************************************************************/
226 void MainInterface::calculateInterfaceSize()
228 int width = 0, height = 0;
229 if( VISIBLE( bgWidget ) )
231 width = bgWidget->widgetSize.width();
232 height = bgWidget->widgetSize.height();
233 assert( !(playlistWidget && playlistWidget->isVisible() ) );
235 else if( VISIBLE( playlistWidget ) )
237 width = playlistWidget->widgetSize.width();
238 height = playlistWidget->widgetSize.height();
239 fprintf( stderr, "Have %ix%i playlist\n", width, height );
241 else if( videoIsActive )
243 width = videoWidget->widgetSize.width() ;
244 height = videoWidget->widgetSize.height();
245 fprintf( stderr, "Video Size %ix%i\n", DS( videoWidget->widgetSize ) );
249 width = PREF_W - addSize.width();
250 height = PREF_H - addSize.height();
252 if( VISIBLE( visualSelector ) )
253 height += visualSelector->height();
254 fprintf( stderr, "Adv %p - visible %i\n", advControls, advControls->isVisible() );
255 if( VISIBLE( advControls) )
257 fprintf( stderr, "visible\n" );
258 height += advControls->sizeHint().height();
261 fprintf( stderr, "Adv height %i\n", advControls->sizeHint().height() );
262 fprintf( stderr, "Setting to %ix%i\n",
263 width + addSize.width() , height + addSize.height() );
265 mainSize = QSize( width + addSize.width(), height + addSize.height() );
268 void MainInterface::resizeEvent( QResizeEvent *e )
270 fprintf( stderr, "Resize event to %ix%i\n", DS( e->size() ) );
271 videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
272 if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
274 SET_WH( videoWidget, e->size().width() - addSize.width(),
275 e->size().height() - addSize.height() );
276 videoWidget->updateGeometry();
278 if( VISIBLE( playlistWidget ) )
280 SET_WH( playlistWidget , e->size().width() - addSize.width(),
281 e->size().height() - addSize.height() );
282 playlistWidget->updateGeometry();
286 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
287 int *pi_y, unsigned int *pi_width,
288 unsigned int *pi_height )
290 void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
293 videoIsActive = true;
294 if( VISIBLE( playlistWidget ) )
296 embeddedPlaylistWasActive = true;
297 playlistWidget->hide();
299 bool bgWasVisible = false;
300 if( VISIBLE( bgWidget) )
305 if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
307 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
309 else /* Background widget available, use its size */
311 /* Ok, our visualizations are bad, so don't do this for the moment
312 * use the requested size anyway */
313 // videoWidget->widgetSize = bgWidget->widgeTSize;
314 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
316 videoWidget->updateGeometry(); /// FIXME: Needed ?
317 need_components_update = true;
322 void MainInterface::releaseVideo( void *p_win )
324 videoWidget->release( p_win );
325 videoWidget->widgetSize = QSize( 0, 0 );
326 videoWidget->resize( videoWidget->widgetSize );
328 if( embeddedPlaylistWasActive )
329 playlistWidget->show();
333 videoIsActive = false;
334 need_components_update = true;
337 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
339 int i_ret = VLC_EGENERIC;
344 unsigned int *pi_width = va_arg( args, unsigned int * );
345 unsigned int *pi_height = va_arg( args, unsigned int * );
346 *pi_width = videoWidget->widgetSize.width();
347 *pi_height = videoWidget->widgetSize.height();
353 unsigned int i_width = va_arg( args, unsigned int );
354 unsigned int i_height = va_arg( args, unsigned int );
355 // if( !i_width && p_vout ) i_width = p_vout->i_window_width;
356 // if( !i_height && p_vout ) i_height = p_vout->i_window_height;
357 videoWidget->widgetSize = QSize( i_width, i_height );
358 videoWidget->updateGeometry();
359 need_components_update = true;
363 case VOUT_SET_STAY_ON_TOP:
365 msg_Warn( p_intf, "unsupported control query" );
371 void MainInterface::advanced()
373 if( !VISIBLE( advControls ) )
376 advControlsEnabled = true;
381 advControlsEnabled = false;
383 doComponentsUpdate();
386 void MainInterface::visual()
388 if( !VISIBLE( visualSelector) )
390 visualSelector->show();
391 if( !THEMIM->getIM()->hasVideo() )
393 /* Show the background widget */
398 /* Stop any currently running visualization */
399 visualSelector->hide();
401 doComponentsUpdate();
404 void MainInterface::playlist()
406 // Toggle the playlist dialog
407 if( !playlistEmbeddedFlag )
413 THEDP->playlistDialog();
417 if( !playlistWidget )
419 PlaylistDialog::killInstance();
420 playlistWidget = new PlaylistWidget( p_intf );
421 ui.vboxLayout->insertWidget( 0, playlistWidget );
422 playlistWidget->widgetSize = settings->value( "playlistSize",
423 QSize( 650, 310 ) ).toSize();
424 playlistWidget->hide();
426 /// Todo, reset its size ?
427 if( VISIBLE( playlistWidget) )
429 playlistWidget->hide();
432 videoWidget->widgetSize = savedVideoSize;
433 videoWidget->resize( videoWidget->widgetSize );
434 videoWidget->updateGeometry();
439 playlistWidget->show();
442 savedVideoSize = videoWidget->widgetSize;
443 videoWidget->widgetSize.setHeight( 0 );
444 videoWidget->resize( videoWidget->widgetSize );
445 videoWidget->updateGeometry();
447 if( VISIBLE( bgWidget ) ) bgWidget->hide();
449 doComponentsUpdate();
452 /* Video widget cannot do this synchronously as it runs in another thread */
453 /* Well, could it, actually ? Probably dangerous ... */
454 void MainInterface::doComponentsUpdate()
456 calculateInterfaceSize();
460 void MainInterface::undockPlaylist()
464 playlistWidget->hide();
465 playlistWidget->deleteLater();
466 ui.vboxLayout->removeWidget( playlistWidget );
467 playlistWidget = NULL;
468 playlistEmbeddedFlag = false;
471 QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled );
475 videoWidget->widgetSize = savedVideoSize;
476 videoWidget->resize( videoWidget->widgetSize );
477 videoWidget->updateGeometry();
480 doComponentsUpdate();
481 THEDP->playlistDialog();
485 void MainInterface::customEvent( QEvent *event )
487 if( event->type() == PLDockEvent_Type )
489 PlaylistDialog::killInstance();
490 playlistEmbeddedFlag = true;
492 QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled );
497 /************************************************************************
499 ************************************************************************/
500 void MainInterface::keyPressEvent( QKeyEvent *e )
502 int i_vlck = qtEventToVLCKey( e );
505 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
512 void MainInterface::stop()
514 playlist_Stop( THEPL );
516 void MainInterface::play()
518 if( !THEPL->i_size || !THEPL->i_enabled )
520 /* The playlist is empty, open a file requester */
521 THEDP->simpleOpenDialog();
525 THEMIM->togglePlayPause();
527 void MainInterface::prev()
529 playlist_Prev( THEPL );
531 void MainInterface::next()
533 playlist_Next( THEPL );
536 void MainInterface::setDisplay( float pos, int time, int length )
538 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
539 secstotimestr( psz_length, length );
540 secstotimestr( psz_time, time );
542 title.sprintf( "%s/%s", psz_time, psz_length );
543 timeLabel->setText( " "+title+" " );
546 void MainInterface::setName( QString name )
548 nameLabel->setText( " " + name+" " );
551 void MainInterface::setStatus( int status )
553 if( status == 1 ) // Playing
554 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
556 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
559 static bool b_my_volume;
561 void MainInterface::updateOnTimer()
563 aout_instance_t *p_aout = (aout_instance_t *)vlc_object_find( p_intf,
564 VLC_OBJECT_AOUT, FIND_ANYWHERE );
565 /* Todo: make this event-driven */
568 ui.visualButton->setEnabled( true );
569 vlc_object_release( p_aout );
572 ui.visualButton->setEnabled( false );
575 advControls->enableInput( THEMIM->getIM()->hasInput() );
576 advControls->enableVideo( THEMIM->getIM()->hasVideo() );
578 if( intf_ShouldDie( p_intf ) )
580 QApplication::closeAllWindows();
581 QApplication::quit();
583 if( need_components_update )
585 doComponentsUpdate();
586 need_components_update = false;
589 audio_volume_t i_volume;
590 aout_VolumeGet( p_intf, &i_volume );
591 i_volume = (i_volume * 200 )/ AOUT_VOLUME_MAX ;
592 int i_gauge = ui.volumeSlider->value();
594 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
597 ui.volumeSlider->setValue( i_volume );
602 void MainInterface::closeEvent( QCloseEvent *e )
605 p_intf->b_die = VLC_TRUE;
608 void MainInterface::updateVolume( int sliderVolume )
612 int i_res = sliderVolume * AOUT_VOLUME_MAX /
613 (2*ui.volumeSlider->maximum() );
614 aout_VolumeSet( p_intf, i_res );
618 static int InteractCallback( vlc_object_t *p_this,
619 const char *psz_var, vlc_value_t old_val,
620 vlc_value_t new_val, void *param )
622 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
623 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
624 DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
625 QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );