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>
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 )
85 settings = new QSettings( "VideoLAN", "VLC" );
86 settings->beginGroup( "MainWindow" );
88 need_components_update = false;
89 bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
90 embeddedPlaylistWasActive = videoIsActive = false;
92 videoEmbeddedFlag = false;
93 if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
95 alwaysVideoFlag = false;
96 if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
97 alwaysVideoFlag = true;
99 playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
100 advControlsEnabled= settings->value( "adv-controls", false ).toBool();
101 visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
104 setWindowTitle( qtr( "VLC media player" ) );
105 handleMainUi( settings );
106 QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
107 advControlsEnabled, visualSelectorEnabled );
108 timeLabel = new QLabel( 0 );
109 nameLabel = new QLabel( 0 );
110 statusBar()->addWidget( nameLabel, 4 );
111 statusBar()->addPermanentWidget( timeLabel, 1 );
113 setFocusPolicy( Qt::StrongFocus );
114 setAcceptDrops(true);
116 /* Init input manager */
117 MainInputManager::getInstance( p_intf );
118 ON_TIMEOUT( updateOnTimer() );
121 CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
122 /* Connect the input manager to the GUI elements it manages */
123 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
124 slider, setPosition( float,int, int ) );
125 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
126 this, setDisplay( float, int, int ) );
127 CONNECT( THEMIM->getIM(), nameChanged( QString ), this,setName( QString ) );
128 CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
129 CONNECT( THEMIM->getIM(), navigationChanged( int ), this, setNavigation(int) );
130 CONNECT( slider, sliderDragged( float ),
131 THEMIM->getIM(), sliderUpdate( float ) );
133 CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
135 CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
137 CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
140 var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
141 var_AddCallback( p_intf, "interaction", InteractCallback, this );
142 p_intf->b_interaction = VLC_TRUE;
145 MainInterface::~MainInterface()
147 settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
148 settings->setValue( "adv-controls", advControlsEnabled );
149 settings->setValue( "pos", pos() );
150 settings->endGroup();
152 p_intf->b_interaction = VLC_FALSE;
153 var_DelCallback( p_intf, "interaction", InteractCallback, this );
155 p_intf->pf_request_window = NULL;
156 p_intf->pf_release_window = NULL;
157 p_intf->pf_control_window = NULL;
160 void MainInterface::handleMainUi( QSettings *settings )
162 QWidget *main = new QWidget( this );
163 setCentralWidget( main );
164 ui.setupUi( centralWidget() );
166 slider = new InputSlider( Qt::Horizontal, NULL );
167 ui.hboxLayout->insertWidget( 0, slider );
168 ui.discFrame->hide();
169 BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
170 BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
171 BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
173 BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
174 qtr("Previous"), prev() );
175 BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
176 BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
177 BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
180 ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
181 ui.volumeSlider->setMaximum( 100 );
182 ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
183 VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
184 ui.volMuteLabel->installEventFilter(h);
185 ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
187 BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
188 playlistEmbeddedFlag ? qtr( "Show playlist" ) :
189 qtr( "Open playlist" ) );
190 BUTTONACT( ui.playlistButton, playlist() );
192 /* Set initial size */
193 resize ( PREF_W, PREF_H );
195 addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
197 advControls = new ControlsWidget( p_intf );
198 ui.vboxLayout->insertWidget( 0, advControls );
199 advControls->updateGeometry();
200 if( !advControlsEnabled ) advControls->hide();
201 need_components_update = true;
203 visualSelector = new VisualSelector( p_intf );
204 ui.vboxLayout->insertWidget( 0, visualSelector );
205 visualSelector->hide();
207 if( alwaysVideoFlag )
209 bgWidget = new BackgroundWidget( p_intf );
210 bgWidget->widgetSize = settings->value( "backgroundSize",
211 QSize( 200, 200 ) ).toSize();
212 bgWidget->resize( bgWidget->widgetSize );
213 bgWidget->updateGeometry();
214 ui.vboxLayout->insertWidget( 0, bgWidget );
217 if( videoEmbeddedFlag )
219 videoWidget = new VideoWidget( p_intf );
220 videoWidget->widgetSize = QSize( 1, 1 );
221 videoWidget->resize( videoWidget->widgetSize );
222 ui.vboxLayout->insertWidget( 0, videoWidget );
224 p_intf->pf_request_window = ::DoRequest;
225 p_intf->pf_release_window = ::DoRelease;
226 p_intf->pf_control_window = ::DoControl;
228 setMinimumSize( PREF_W, addSize.height() );
231 /**********************************************************************
232 * Handling of the components
233 **********************************************************************/
234 void MainInterface::calculateInterfaceSize()
236 int width = 0, height = 0;
237 if( VISIBLE( bgWidget ) )
239 width = bgWidget->widgetSize.width();
240 height = bgWidget->widgetSize.height();
241 assert( !(playlistWidget && playlistWidget->isVisible() ) );
243 else if( VISIBLE( playlistWidget ) )
245 width = playlistWidget->widgetSize.width();
246 height = playlistWidget->widgetSize.height();
248 else if( videoIsActive )
250 width = videoWidget->widgetSize.width() ;
251 height = videoWidget->widgetSize.height();
255 width = PREF_W - addSize.width();
256 height = PREF_H - addSize.height();
258 if( VISIBLE( visualSelector ) )
259 height += visualSelector->height();
260 if( VISIBLE( advControls) )
262 height += advControls->sizeHint().height();
264 mainSize = QSize( width + addSize.width(), height + addSize.height() );
267 void MainInterface::resizeEvent( QResizeEvent *e )
269 videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
270 if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
272 SET_WH( videoWidget, e->size().width() - addSize.width(),
273 e->size().height() - addSize.height() );
274 videoWidget->updateGeometry();
276 if( VISIBLE( playlistWidget ) )
278 SET_WH( playlistWidget , e->size().width() - addSize.width(),
279 e->size().height() - addSize.height() );
280 playlistWidget->updateGeometry();
284 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
285 int *pi_y, unsigned int *pi_width,
286 unsigned int *pi_height )
288 void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
291 videoIsActive = true;
292 if( VISIBLE( playlistWidget ) )
294 embeddedPlaylistWasActive = true;
295 playlistWidget->hide();
297 bool bgWasVisible = false;
298 if( VISIBLE( bgWidget) )
303 if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
305 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
307 else /* Background widget available, use its size */
309 /* Ok, our visualizations are bad, so don't do this for the moment
310 * use the requested size anyway */
311 // videoWidget->widgetSize = bgWidget->widgeTSize;
312 videoWidget->widgetSize = QSize( *pi_width, *pi_height );
314 videoWidget->updateGeometry(); /// FIXME: Needed ?
315 need_components_update = true;
320 void MainInterface::releaseVideo( void *p_win )
322 videoWidget->release( p_win );
323 videoWidget->widgetSize = QSize( 0, 0 );
324 videoWidget->resize( videoWidget->widgetSize );
326 if( embeddedPlaylistWasActive )
327 playlistWidget->show();
331 videoIsActive = false;
332 need_components_update = true;
335 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
337 int i_ret = VLC_EGENERIC;
342 unsigned int *pi_width = va_arg( args, unsigned int * );
343 unsigned int *pi_height = va_arg( args, unsigned int * );
344 *pi_width = videoWidget->widgetSize.width();
345 *pi_height = videoWidget->widgetSize.height();
351 unsigned int i_width = va_arg( args, unsigned int );
352 unsigned int i_height = va_arg( args, unsigned int );
353 videoWidget->widgetSize = QSize( i_width, i_height );
354 videoWidget->updateGeometry();
355 need_components_update = true;
359 case VOUT_SET_STAY_ON_TOP:
361 msg_Warn( p_intf, "unsupported control query" );
367 void MainInterface::advanced()
369 if( !VISIBLE( advControls ) )
372 advControlsEnabled = true;
377 advControlsEnabled = false;
379 doComponentsUpdate();
382 void MainInterface::visual()
384 if( !VISIBLE( visualSelector) )
386 visualSelector->show();
387 if( !THEMIM->getIM()->hasVideo() )
389 /* Show the background widget */
391 visualSelectorEnabled = true;
395 /* Stop any currently running visualization */
396 visualSelector->hide();
397 visualSelectorEnabled = false;
399 doComponentsUpdate();
402 void MainInterface::playlist()
404 // Toggle the playlist dialog
405 if( !playlistEmbeddedFlag )
411 THEDP->playlistDialog();
415 if( !playlistWidget )
417 PlaylistDialog::killInstance();
418 playlistWidget = new PlaylistWidget( p_intf );
419 ui.vboxLayout->insertWidget( 0, playlistWidget );
420 playlistWidget->widgetSize = settings->value( "playlistSize",
421 QSize( 650, 310 ) ).toSize();
422 playlistWidget->hide();
424 if( VISIBLE( playlistWidget ) )
426 playlistWidget->hide();
429 videoWidget->widgetSize = savedVideoSize;
430 videoWidget->resize( videoWidget->widgetSize );
431 videoWidget->updateGeometry();
436 playlistWidget->show();
439 savedVideoSize = videoWidget->widgetSize;
440 videoWidget->widgetSize.setHeight( 0 );
441 videoWidget->resize( videoWidget->widgetSize );
442 videoWidget->updateGeometry();
444 if( VISIBLE( bgWidget ) ) bgWidget->hide();
446 doComponentsUpdate();
449 /* Video widget cannot do this synchronously as it runs in another thread */
450 /* Well, could it, actually ? Probably dangerous ... */
451 void MainInterface::doComponentsUpdate()
453 calculateInterfaceSize();
457 void MainInterface::undockPlaylist()
461 playlistWidget->hide();
462 playlistWidget->deleteLater();
463 ui.vboxLayout->removeWidget( playlistWidget );
464 playlistWidget = NULL;
465 playlistEmbeddedFlag = false;
468 QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
469 visualSelectorEnabled);
473 videoWidget->widgetSize = savedVideoSize;
474 videoWidget->resize( videoWidget->widgetSize );
475 videoWidget->updateGeometry();
478 doComponentsUpdate();
479 THEDP->playlistDialog();
483 void MainInterface::customEvent( QEvent *event )
485 if( event->type() == PLDockEvent_Type )
487 PlaylistDialog::killInstance();
488 playlistEmbeddedFlag = true;
490 QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
491 visualSelectorEnabled);
497 /************************************************************************
499 ************************************************************************/
500 void MainInterface::dropEvent(QDropEvent *event)
502 const QMimeData *mimeData = event->mimeData();
504 /* D&D of a subtitles file, add it on the fly */
505 if( mimeData->urls().size() == 1 )
507 if( THEMIM->getIM()->hasInput() )
509 if( input_AddSubtitles( THEMIM->getInput(),
510 qtu( mimeData->urls()[0].toString() ),
513 event->acceptProposedAction();
519 foreach( QUrl url, mimeData->urls() ) {
520 QString s = url.toString();
521 if( s.length() > 0 ) {
522 playlist_Add( THEPL, qtu(s), NULL,
523 PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
524 PLAYLIST_END, VLC_TRUE );
528 event->acceptProposedAction();
530 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
532 event->acceptProposedAction();
534 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
536 event->acceptProposedAction();
538 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
543 /************************************************************************
545 ************************************************************************/
546 void MainInterface::keyPressEvent( QKeyEvent *e )
548 int i_vlck = qtEventToVLCKey( e );
551 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
558 void MainInterface::stop()
560 playlist_Stop( THEPL );
562 void MainInterface::play()
564 if( playlist_IsEmpty(THEPL) )
566 /* The playlist is empty, open a file requester */
567 THEDP->simpleOpenDialog();
571 THEMIM->togglePlayPause();
573 void MainInterface::prev()
575 playlist_Prev( THEPL );
577 void MainInterface::next()
579 playlist_Next( THEPL );
582 void MainInterface::setDisplay( float pos, int time, int length )
584 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
585 secstotimestr( psz_length, length );
586 secstotimestr( psz_time, time );
588 title.sprintf( "%s/%s", psz_time, psz_length );
589 timeLabel->setText( " "+title+" " );
592 void MainInterface::setName( QString name )
594 nameLabel->setText( " " + name+" " );
597 void MainInterface::setStatus( int status )
599 if( status == 1 ) // Playing
600 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
602 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
605 #define HELP_MENU N_("Menu")
606 #define HELP_PCH N_("Previous chapter")
607 #define HELP_NCH N_("Next chapter")
608 #define HELP_PTR N_("Previous track")
609 #define HELP_NTR N_("Next track")
611 void MainInterface::setNavigation( int navigation )
613 // 1 = chapter, 2 = title, 0 = no
614 if( navigation == 0 )
616 ui.discFrame->hide();
617 } else if( navigation == 1 ) {
618 ui.prevSectionButton->show();
619 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
620 ui.nextSectionButton->show();
621 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
622 ui.menuButton->show();
623 ui.discFrame->show();
625 ui.prevSectionButton->show();
626 ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
627 ui.nextSectionButton->show();
628 ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
629 ui.menuButton->hide();
630 ui.discFrame->show();
634 static bool b_my_volume;
636 void MainInterface::updateOnTimer()
638 /* \todo Make this event-driven */
639 advControls->enableInput( THEMIM->getIM()->hasInput() );
640 advControls->enableVideo( THEMIM->getIM()->hasVideo() );
642 if( intf_ShouldDie( p_intf ) )
644 QApplication::closeAllWindows();
645 QApplication::quit();
647 if( need_components_update )
649 doComponentsUpdate();
650 need_components_update = false;
653 audio_volume_t i_volume;
654 aout_VolumeGet( p_intf, &i_volume );
655 i_volume = (i_volume * 200 )/ AOUT_VOLUME_MAX ;
656 int i_gauge = ui.volumeSlider->value();
658 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
661 ui.volumeSlider->setValue( i_volume );
666 void MainInterface::closeEvent( QCloseEvent *e )
669 p_intf->b_die = VLC_TRUE;
672 void MainInterface::updateVolume( int sliderVolume )
676 int i_res = sliderVolume * AOUT_VOLUME_MAX /
677 (2*ui.volumeSlider->maximum() );
678 aout_VolumeSet( p_intf, i_res );
682 static int InteractCallback( vlc_object_t *p_this,
683 const char *psz_var, vlc_value_t old_val,
684 vlc_value_t new_val, void *param )
686 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
687 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
688 DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
689 QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );