1 /*****************************************************************************
2 * main_interface.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>
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 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
81 settings = new QSettings( "VideoLAN", "VLC" );
82 settings->beginGroup( "MainWindow" );
84 need_components_update = false;
85 bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
86 embeddedPlaylistWasActive = videoIsActive = false;
88 videoEmbeddedFlag = false;
89 if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
91 alwaysVideoFlag = false;
92 if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
93 alwaysVideoFlag = true;
95 playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
96 advControlsEnabled= settings->value( "adv-controls", false ).toBool();
97 visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
100 setWindowTitle( qtr( "VLC media player" ) );
101 handleMainUi( settings );
102 QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
103 advControlsEnabled, visualSelectorEnabled );
104 timeLabel = new QLabel( 0 );
105 nameLabel = new QLabel( 0 );
106 statusBar()->addWidget( nameLabel, 4 );
107 statusBar()->addPermanentWidget( timeLabel, 1 );
109 setFocusPolicy( Qt::StrongFocus );
110 setAcceptDrops(true);
112 /* Init input manager */
113 MainInputManager::getInstance( p_intf );
114 ON_TIMEOUT( updateOnTimer() );
117 CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
118 /* Connect the input manager to the GUI elements it manages */
119 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
120 slider, setPosition( float,int, int ) );
121 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
122 this, setDisplay( float, int, int ) );
123 CONNECT( THEMIM->getIM(), nameChanged( QString ), this,setName( QString ) );
124 CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
125 CONNECT( THEMIM->getIM(), navigationChanged( int ), this, setNavigation(int) );
126 CONNECT( slider, sliderDragged( float ),
127 THEMIM->getIM(), sliderUpdate( float ) );
129 CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
131 CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
133 CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
136 var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
137 var_AddCallback( p_intf, "interaction", InteractCallback, this );
138 p_intf->b_interaction = VLC_TRUE;
141 MainInterface::~MainInterface()
143 settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
144 settings->setValue( "adv-controls", advControlsEnabled );
145 settings->setValue( "pos", pos() );
146 settings->endGroup();
148 p_intf->b_interaction = VLC_FALSE;
149 var_DelCallback( p_intf, "interaction", InteractCallback, this );
151 p_intf->pf_request_window = NULL;
152 p_intf->pf_release_window = NULL;
153 p_intf->pf_control_window = NULL;
156 void MainInterface::handleMainUi( QSettings *settings )
158 QWidget *main = new QWidget( this );
159 setCentralWidget( main );
160 ui.setupUi( centralWidget() );
162 slider = new InputSlider( Qt::Horizontal, NULL );
163 ui.vboxLayout->insertWidget( 0, slider );
164 ui.discFrame->hide();
165 BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
166 BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
167 BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
169 BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
170 qtr("Previous"), prev() );
171 BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
172 BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
173 BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
176 ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
177 ui.volumeSlider->setMaximum( 100 );
178 ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
179 VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
180 ui.volMuteLabel->installEventFilter(h);
181 ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
183 BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
184 playlistEmbeddedFlag ? qtr( "Show playlist" ) :
185 qtr( "Open playlist" ) );
186 BUTTONACT( ui.playlistButton, playlist() );
188 /* Set initial size */
189 resize ( PREF_W, PREF_H );
191 addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
193 advControls = new ControlsWidget( p_intf );
194 ui.vboxLayout->insertWidget( 0, advControls );
195 advControls->updateGeometry();
196 if( !advControlsEnabled ) advControls->hide();
197 need_components_update = true;
199 visualSelector = new VisualSelector( p_intf );
200 ui.vboxLayout->insertWidget( 0, visualSelector );
201 visualSelector->hide();
203 if( alwaysVideoFlag )
205 bgWidget = new BackgroundWidget( p_intf );
206 bgWidget->widgetSize = settings->value( "backgroundSize",
207 QSize( 200, 200 ) ).toSize();
208 bgWidget->resize( bgWidget->widgetSize );
209 bgWidget->updateGeometry();
210 ui.vboxLayout->insertWidget( 0, bgWidget );
213 if( videoEmbeddedFlag )
215 videoWidget = new VideoWidget( p_intf );
216 videoWidget->widgetSize = QSize( 1, 1 );
217 videoWidget->resize( videoWidget->widgetSize );
218 ui.vboxLayout->insertWidget( 0, videoWidget );
220 p_intf->pf_request_window = ::DoRequest;
221 p_intf->pf_release_window = ::DoRelease;
222 p_intf->pf_control_window = ::DoControl;
224 setMinimumSize( PREF_W, addSize.height() );
227 /**********************************************************************
228 * Handling of the components
229 **********************************************************************/
230 void MainInterface::calculateInterfaceSize()
232 int width = 0, height = 0;
233 if( VISIBLE( bgWidget ) )
235 width = bgWidget->widgetSize.width();
236 height = bgWidget->widgetSize.height();
237 assert( !(playlistWidget && playlistWidget->isVisible() ) );
239 else if( VISIBLE( playlistWidget ) )
241 width = playlistWidget->widgetSize.width();
242 height = playlistWidget->widgetSize.height();
244 else if( videoIsActive )
246 width = videoWidget->widgetSize.width() ;
247 height = videoWidget->widgetSize.height();
251 width = PREF_W - addSize.width();
252 height = PREF_H - addSize.height();
254 if( VISIBLE( visualSelector ) )
255 height += visualSelector->height();
256 if( VISIBLE( advControls) )
258 height += advControls->sizeHint().height();
260 mainSize = QSize( width + addSize.width(), height + addSize.height() );
263 void MainInterface::resizeEvent( QResizeEvent *e )
265 videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
266 if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
268 SET_WH( videoWidget, e->size().width() - addSize.width(),
269 e->size().height() - addSize.height() );
270 videoWidget->updateGeometry();
272 if( VISIBLE( playlistWidget ) )
274 SET_WH( playlistWidget , e->size().width() - addSize.width(),
275 e->size().height() - addSize.height() );
276 playlistWidget->updateGeometry();
280 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
281 int *pi_y, unsigned int *pi_width,
282 unsigned int *pi_height )
284 void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
287 videoIsActive = true;
288 if( VISIBLE( playlistWidget ) )
290 embeddedPlaylistWasActive = true;
291 // playlistWidget->hide();
293 bool bgWasVisible = false;
294 if( VISIBLE( bgWidget) )
299 if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
301 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
303 else /* Background widget available, use its size */
305 /* Ok, our visualizations are bad, so don't do this for the moment
306 * use the requested size anyway */
307 // videoWidget->widgetSize = bgWidget->widgeTSize;
308 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
310 // videoWidget->updateGeometry(); /// FIXME: Needed ?
311 need_components_update = true;
316 void MainInterface::releaseVideo( void *p_win )
318 videoWidget->release( p_win );
319 videoWidget->widgetSize = QSize( 0, 0 );
320 videoWidget->resize( videoWidget->widgetSize );
322 if( embeddedPlaylistWasActive )
323 ;//playlistWidget->show();
327 videoIsActive = false;
328 need_components_update = true;
331 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
333 int i_ret = VLC_EGENERIC;
338 unsigned int *pi_width = va_arg( args, unsigned int * );
339 unsigned int *pi_height = va_arg( args, unsigned int * );
340 *pi_width = videoWidget->widgetSize.width();
341 *pi_height = videoWidget->widgetSize.height();
347 unsigned int i_width = va_arg( args, unsigned int );
348 unsigned int i_height = va_arg( args, unsigned int );
349 videoWidget->widgetSize = QSize( i_width, i_height );
350 // videoWidget->updateGeometry();
351 need_components_update = true;
355 case VOUT_SET_STAY_ON_TOP:
357 msg_Warn( p_intf, "unsupported control query" );
363 void MainInterface::advanced()
365 if( !VISIBLE( advControls ) )
368 advControlsEnabled = true;
373 advControlsEnabled = false;
375 doComponentsUpdate();
378 void MainInterface::visual()
380 if( !VISIBLE( visualSelector) )
382 visualSelector->show();
383 if( !THEMIM->getIM()->hasVideo() )
385 /* Show the background widget */
387 visualSelectorEnabled = true;
391 /* Stop any currently running visualization */
392 visualSelector->hide();
393 visualSelectorEnabled = false;
395 doComponentsUpdate();
398 void MainInterface::playlist()
400 // Toggle the playlist dialog
401 if( !playlistEmbeddedFlag )
407 THEDP->playlistDialog();
411 if( !playlistWidget )
413 PlaylistDialog::killInstance();
414 playlistWidget = new PlaylistWidget( p_intf );
415 ui.vboxLayout->insertWidget( 0, playlistWidget );
416 playlistWidget->widgetSize = settings->value( "playlistSize",
417 QSize( 650, 310 ) ).toSize();
418 playlistWidget->hide();
420 if( VISIBLE( playlistWidget ) )
422 playlistWidget->hide();
425 videoWidget->widgetSize = savedVideoSize;
426 videoWidget->resize( videoWidget->widgetSize );
427 videoWidget->updateGeometry();
432 playlistWidget->show();
435 savedVideoSize = videoWidget->widgetSize;
436 videoWidget->widgetSize.setHeight( 0 );
437 videoWidget->resize( videoWidget->widgetSize );
438 videoWidget->updateGeometry();
440 if( VISIBLE( bgWidget ) ) bgWidget->hide();
442 doComponentsUpdate();
445 /* Video widget cannot do this synchronously as it runs in another thread */
446 /* Well, could it, actually ? Probably dangerous ... */
447 void MainInterface::doComponentsUpdate()
449 calculateInterfaceSize();
453 void MainInterface::undockPlaylist()
457 playlistWidget->hide();
458 playlistWidget->deleteLater();
459 ui.vboxLayout->removeWidget( playlistWidget );
460 playlistWidget = NULL;
461 playlistEmbeddedFlag = false;
464 QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
465 visualSelectorEnabled);
469 videoWidget->widgetSize = savedVideoSize;
470 videoWidget->resize( videoWidget->widgetSize );
471 videoWidget->updateGeometry();
474 doComponentsUpdate();
475 THEDP->playlistDialog();
479 void MainInterface::customEvent( QEvent *event )
481 if( event->type() == PLDockEvent_Type )
483 PlaylistDialog::killInstance();
484 playlistEmbeddedFlag = true;
486 QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
487 visualSelectorEnabled);
493 /************************************************************************
495 ************************************************************************/
496 void MainInterface::dropEvent(QDropEvent *event)
498 const QMimeData *mimeData = event->mimeData();
500 /* D&D of a subtitles file, add it on the fly */
501 if( mimeData->urls().size() == 1 )
503 if( THEMIM->getIM()->hasInput() )
505 if( input_AddSubtitles( THEMIM->getInput(),
506 qtu( mimeData->urls()[0].toString() ),
509 event->acceptProposedAction();
515 foreach( QUrl url, mimeData->urls() ) {
516 QString s = url.toString();
517 if( s.length() > 0 ) {
518 playlist_Add( THEPL, qtu(s), NULL,
519 PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
520 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
524 event->acceptProposedAction();
526 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
528 event->acceptProposedAction();
530 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
532 event->acceptProposedAction();
534 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
539 /************************************************************************
541 ************************************************************************/
542 void MainInterface::keyPressEvent( QKeyEvent *e )
544 int i_vlck = qtEventToVLCKey( e );
547 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
554 void MainInterface::stop()
556 playlist_Stop( THEPL );
558 void MainInterface::play()
560 if( playlist_IsEmpty(THEPL) )
562 /* The playlist is empty, open a file requester */
563 THEDP->openFileDialog();
567 THEMIM->togglePlayPause();
569 void MainInterface::prev()
571 playlist_Prev( THEPL );
573 void MainInterface::next()
575 playlist_Next( THEPL );
578 void MainInterface::setDisplay( float pos, int time, int length )
580 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
581 secstotimestr( psz_length, length );
582 secstotimestr( psz_time, time );
584 title.sprintf( "%s/%s", psz_time, psz_length );
585 timeLabel->setText( " "+title+" " );
588 void MainInterface::setName( QString name )
590 nameLabel->setText( " " + name+" " );
593 void MainInterface::setStatus( int status )
595 if( status == 1 ) // Playing
596 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
598 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
601 #define HELP_MENU N_("Menu")
602 #define HELP_PCH N_("Previous chapter")
603 #define HELP_NCH N_("Next chapter")
604 #define HELP_PTR N_("Previous track")
605 #define HELP_NTR N_("Next track")
607 void MainInterface::setNavigation( int navigation )
609 // 1 = chapter, 2 = title, 0 = no
610 if( navigation == 0 )
612 ui.discFrame->hide();
613 } else if( navigation == 1 ) {
614 ui.prevSectionButton->show();
615 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
616 ui.nextSectionButton->show();
617 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
618 ui.menuButton->show();
619 ui.discFrame->show();
621 ui.prevSectionButton->show();
622 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
623 ui.nextSectionButton->show();
624 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
625 ui.menuButton->hide();
626 ui.discFrame->show();
630 static bool b_my_volume;
632 void MainInterface::updateOnTimer()
634 /* \todo Make this event-driven */
635 advControls->enableInput( THEMIM->getIM()->hasInput() );
636 advControls->enableVideo( THEMIM->getIM()->hasVideo() );
638 if( intf_ShouldDie( p_intf ) )
640 QApplication::closeAllWindows();
641 QApplication::quit();
643 if( need_components_update )
645 doComponentsUpdate();
646 need_components_update = false;
649 audio_volume_t i_volume;
650 aout_VolumeGet( p_intf, &i_volume );
651 i_volume = (i_volume * 200 )/ AOUT_VOLUME_MAX ;
652 int i_gauge = ui.volumeSlider->value();
654 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
657 ui.volumeSlider->setValue( i_volume );
662 void MainInterface::closeEvent( QCloseEvent *e )
665 p_intf->b_die = VLC_TRUE;
668 void MainInterface::updateVolume( int sliderVolume )
672 int i_res = sliderVolume * AOUT_VOLUME_MAX /
673 (2*ui.volumeSlider->maximum() );
674 aout_VolumeSet( p_intf, i_res );
678 static int InteractCallback( vlc_object_t *p_this,
679 const char *psz_var, vlc_value_t old_val,
680 vlc_value_t new_val, void *param )
682 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
683 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
684 DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
685 QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );