]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Hotkey selector widget. Save not implemented yet
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_inteface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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
23 #include "qt4.hpp"
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"
32 #include "menus.hpp"
33
34 #include <QMenuBar>
35 #include <QCloseEvent>
36 #include <QPushButton>
37 #include <QStatusBar>
38 #include <QKeyEvent>
39
40 #include <assert.h>
41 #include <vlc_keys.h>
42 #include <vlc/vout.h>
43 #include <aout_internal.h>
44
45 #ifdef WIN32
46     #define PREF_W 410
47     #define PREF_H 121
48 #else
49     #define PREF_W 450
50     #define PREF_H 125
51 #endif
52
53 #define VISIBLE(i) (i && i->isVisible())
54
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);
58
59 #define DS(i) i.width(),i.height()
60
61 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
62                              vlc_value_t, void *);
63 /* Video handling */
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)
66 {
67     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
68 }
69 static void DoRelease( intf_thread_t *p_intf, void *p_win )
70 {
71     return p_intf->p_sys->p_mi->releaseVideo( p_win );
72 }
73 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
74 {
75     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
76 }
77
78 bool embeddedPlaylistWasActive;
79 bool videoIsActive;
80 QSize savedVideoSize;
81
82 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
83 {
84     settings = new QSettings( "VideoLAN", "VLC" );
85     settings->beginGroup( "MainWindow" );
86
87     need_components_update = false;
88     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
89     embeddedPlaylistWasActive = videoIsActive = false;
90
91     /* Fetch configuration from settings and vlc config */
92     videoEmbeddedFlag = false;
93     if( config_GetInt( p_intf, "embedded-video" ) )
94         videoEmbeddedFlag = true;
95
96     alwaysVideoFlag = false;
97     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
98         alwaysVideoFlag = true;
99
100     playlistEmbeddedFlag = settings->value( "playlist-embedded", true ).
101                                                                     toBool();
102     advControlsEnabled= settings->value( "adv-controls", false ).toBool();
103
104     setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
105     handleMainUi( settings );
106
107     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
108                              advControlsEnabled );
109
110     /* Status bar */
111     timeLabel = new QLabel( 0 );
112     nameLabel = new QLabel( 0 );
113     statusBar()->addWidget( nameLabel, 4 );
114     statusBar()->addPermanentWidget( timeLabel, 1 );
115
116     setFocusPolicy( Qt::StrongFocus );
117
118     /* Init input manager */
119     MainInputManager::getInstance( p_intf );
120     ON_TIMEOUT( updateOnTimer() );
121
122     /* Volume control */
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 ) );
133
134     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
135     var_AddCallback( p_intf, "interaction", InteractCallback, this );
136     p_intf->b_interaction = VLC_TRUE;
137 }
138
139 MainInterface::~MainInterface()
140 {
141     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
142     settings->setValue( "adv-controls", advControlsEnabled );
143     settings->setValue( "pos", pos() );
144     settings->endGroup();
145     delete settings;
146     p_intf->b_interaction = VLC_FALSE;
147     var_DelCallback( p_intf, "interaction", InteractCallback, this );
148
149     p_intf->pf_request_window = NULL;
150     p_intf->pf_release_window = NULL;
151     p_intf->pf_control_window = NULL;
152 }
153
154 void MainInterface::handleMainUi( QSettings *settings )
155 {
156     QWidget *main = new QWidget( this );
157     setCentralWidget( main );
158     ui.setupUi( centralWidget() );
159
160     slider = new InputSlider( Qt::Horizontal, NULL );
161     ui.hboxLayout->insertWidget( 0, slider );
162
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() );
170
171     /* Volume */
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 );
178
179     BUTTON_SET_IMG( ui.playlistButton, "" ,volume-low.png,
180                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
181                                                 qtr( "Open playlist" ) );
182     BUTTONACT( ui.playlistButton, playlist() );
183
184     /* Set initial size */
185     resize ( PREF_W, PREF_H );
186
187     addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
188
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;
194
195     visualSelector = new VisualSelector( p_intf );
196     ui.vboxLayout->insertWidget( 0, visualSelector );
197     visualSelector->hide();
198
199     if( alwaysVideoFlag )
200     {
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 );
207     }
208
209     if( videoEmbeddedFlag )
210     {
211         videoWidget = new VideoWidget( p_intf );
212         videoWidget->widgetSize = QSize( 1, 1 );
213         videoWidget->resize( videoWidget->widgetSize );
214         ui.vboxLayout->insertWidget( 0, videoWidget );
215
216         p_intf->pf_request_window  = ::DoRequest;
217         p_intf->pf_release_window  = ::DoRelease;
218         p_intf->pf_control_window  = ::DoControl;
219     }
220     setMinimumSize( PREF_W, addSize.height() );
221 }
222
223 /**********************************************************************
224  * Handling of the components
225  **********************************************************************/
226 void MainInterface::calculateInterfaceSize()
227 {
228     int width = 0, height = 0;
229     if( VISIBLE( bgWidget ) )
230     {
231         width = bgWidget->widgetSize.width();
232         height = bgWidget->widgetSize.height();
233         assert( !(playlistWidget && playlistWidget->isVisible() ) );
234     }
235     else if( VISIBLE( playlistWidget ) )
236     {
237         width = playlistWidget->widgetSize.width();
238         height = playlistWidget->widgetSize.height();
239         fprintf( stderr, "Have %ix%i playlist\n", width, height );
240     }
241     else if( videoIsActive )
242     {
243         width =  videoWidget->widgetSize.width() ;
244         height = videoWidget->widgetSize.height();
245         fprintf( stderr, "Video Size %ix%i\n", DS( videoWidget->widgetSize ) );
246     }
247     else
248     {
249         width = PREF_W - addSize.width();
250         height = PREF_H - addSize.height();
251     }
252     if( VISIBLE( visualSelector ) )
253         height += visualSelector->height();
254     fprintf( stderr, "Adv %p - visible %i\n", advControls, advControls->isVisible() );
255     if( VISIBLE( advControls) )
256     {
257         fprintf( stderr, "visible\n" );
258         height += advControls->sizeHint().height();
259     }
260
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() );
264
265     mainSize = QSize( width + addSize.width(), height + addSize.height() );
266 }
267
268 void MainInterface::resizeEvent( QResizeEvent *e )
269 {
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 )
273     {
274         SET_WH( videoWidget, e->size().width() - addSize.width(),
275                              e->size().height()  - addSize.height() );
276         videoWidget->updateGeometry();
277     }
278     if( VISIBLE( playlistWidget ) )
279     {
280         SET_WH( playlistWidget , e->size().width() - addSize.width(),
281                                  e->size().height() - addSize.height() );
282         playlistWidget->updateGeometry();
283     }
284 }
285
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 )
289 {
290     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
291     if( ret )
292     {
293         videoIsActive = true;
294         if( VISIBLE( playlistWidget ) )
295         {
296             embeddedPlaylistWasActive = true;
297             playlistWidget->hide();
298         }
299         bool bgWasVisible = false;
300         if( VISIBLE( bgWidget) )
301         {
302             bgWasVisible = true;
303             bgWidget->hide();
304         }
305         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
306         {
307             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
308         }
309         else /* Background widget available, use its size */
310         {
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 );
315         }
316         videoWidget->updateGeometry(); /// FIXME: Needed ?
317         need_components_update = true;
318     }
319     return ret;
320 }
321
322 void MainInterface::releaseVideo( void *p_win )
323 {
324     videoWidget->release( p_win );
325     videoWidget->widgetSize = QSize( 0, 0 );
326     videoWidget->resize( videoWidget->widgetSize );
327
328     if( embeddedPlaylistWasActive )
329         playlistWidget->show();
330     else if( bgWidget )
331         bgWidget->show();
332
333     videoIsActive = false;
334     need_components_update = true;
335 }
336
337 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
338 {
339     int i_ret = VLC_EGENERIC;
340     switch( i_query )
341     {
342         case VOUT_GET_SIZE:
343         {
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();
348             i_ret = VLC_SUCCESS;
349             break;
350         }
351         case VOUT_SET_SIZE:
352         {
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;
360             i_ret = VLC_SUCCESS;
361             break;
362         }
363         case VOUT_SET_STAY_ON_TOP:
364         default:
365             msg_Warn( p_intf, "unsupported control query" );
366             break;
367     }
368     return i_ret;
369 }
370
371 void MainInterface::advanced()
372 {
373     if( !VISIBLE( advControls ) )
374     {
375         advControls->show();
376         advControlsEnabled = true;
377     }
378     else
379     {
380         advControls->hide();
381         advControlsEnabled = false;
382     }
383     doComponentsUpdate();
384 }
385
386 void MainInterface::visual()
387 {
388     if( !VISIBLE( visualSelector) )
389     {
390         visualSelector->show();
391         if( !THEMIM->getIM()->hasVideo() )
392         {
393             /* Show the background widget */
394         }
395     }
396     else
397     {
398         /* Stop any currently running visualization */
399         visualSelector->hide();
400     }
401     doComponentsUpdate();
402 }
403
404 void MainInterface::playlist()
405 {
406     // Toggle the playlist dialog
407     if( !playlistEmbeddedFlag )
408     {
409         if( playlistWidget )
410         {
411             /// \todo Destroy it
412         }
413         THEDP->playlistDialog();
414         return;
415     }
416
417     if( !playlistWidget )
418     {
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();
425     }
426     /// Todo, reset its size ?
427     if( VISIBLE( playlistWidget) )
428     {
429         playlistWidget->hide();
430         if( videoIsActive )
431         {
432             videoWidget->widgetSize = savedVideoSize;
433             videoWidget->resize( videoWidget->widgetSize );
434             videoWidget->updateGeometry();
435         }
436     }
437     else
438     {
439         playlistWidget->show();
440         if( videoIsActive )
441         {
442             savedVideoSize = videoWidget->widgetSize;
443             videoWidget->widgetSize.setHeight( 0 );
444             videoWidget->resize( videoWidget->widgetSize );
445             videoWidget->updateGeometry();
446         }
447         if( VISIBLE( bgWidget ) ) bgWidget->hide();
448     }
449     doComponentsUpdate();
450 }
451
452 /* Video widget cannot do this synchronously as it runs in another thread */
453 /* Well, could it, actually ? Probably dangerous ... */
454 void MainInterface::doComponentsUpdate()
455 {
456     calculateInterfaceSize();
457     resize( mainSize );
458 }
459
460 void MainInterface::undockPlaylist()
461 {
462     if( playlistWidget )
463     {
464         playlistWidget->hide();
465         playlistWidget->deleteLater();
466         ui.vboxLayout->removeWidget( playlistWidget );
467         playlistWidget = NULL;
468         playlistEmbeddedFlag = false;
469
470         menuBar()->clear();
471         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled );
472
473         if( videoIsActive )
474         {
475             videoWidget->widgetSize = savedVideoSize;
476             videoWidget->resize( videoWidget->widgetSize );
477             videoWidget->updateGeometry();
478         }
479
480         doComponentsUpdate();
481         THEDP->playlistDialog();
482     }
483 }
484
485 void MainInterface::customEvent( QEvent *event )
486 {
487     if( event->type() == PLDockEvent_Type )
488     {
489         PlaylistDialog::killInstance();
490         playlistEmbeddedFlag = true;
491         menuBar()->clear();
492         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled );
493         playlist();
494     }
495 }
496
497 /************************************************************************
498  * Other stuff
499  ************************************************************************/
500 void MainInterface::keyPressEvent( QKeyEvent *e )
501 {
502     int i_vlck = qtEventToVLCKey( e );
503     if( i_vlck >= 0 )
504     {
505         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
506         e->accept();
507     }
508     else
509         e->ignore();
510 }
511
512 void MainInterface::stop()
513 {
514     playlist_Stop( THEPL );
515 }
516 void MainInterface::play()
517 {
518     if( !THEPL->i_size || !THEPL->i_enabled )
519     {
520         /* The playlist is empty, open a file requester */
521         THEDP->simpleOpenDialog();
522         setStatus( 0 );
523         return;
524     }
525     THEMIM->togglePlayPause();
526 }
527 void MainInterface::prev()
528 {
529     playlist_Prev( THEPL );
530 }
531 void MainInterface::next()
532 {
533     playlist_Next( THEPL );
534 }
535
536 void MainInterface::setDisplay( float pos, int time, int length )
537 {
538     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
539     secstotimestr( psz_length, length );
540     secstotimestr( psz_time, time );
541     QString title;
542     title.sprintf( "%s/%s", psz_time, psz_length );
543     timeLabel->setText( " "+title+" " );
544 }
545
546 void MainInterface::setName( QString name )
547 {
548     nameLabel->setText( " " + name+" " );
549 }
550
551 void MainInterface::setStatus( int status )
552 {
553     if( status == 1 ) // Playing
554         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
555     else
556         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
557 }
558
559 static bool b_my_volume;
560
561 void MainInterface::updateOnTimer()
562 {
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 */
566     if( p_aout )
567     {
568         ui.visualButton->setEnabled( true );
569         vlc_object_release( p_aout );
570     }
571     else
572         ui.visualButton->setEnabled( false );
573
574     /* And this too */
575     advControls->enableInput( THEMIM->getIM()->hasInput() );
576     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
577
578     if( intf_ShouldDie( p_intf ) )
579     {
580         QApplication::closeAllWindows();
581         QApplication::quit();
582     }
583     if( need_components_update )
584     {
585         doComponentsUpdate();
586         need_components_update = false;
587     }
588
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();
593     b_my_volume = false;
594     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
595     {
596         b_my_volume = true;
597         ui.volumeSlider->setValue( i_volume );
598         b_my_volume = false;
599     }
600 }
601
602 void MainInterface::closeEvent( QCloseEvent *e )
603 {
604     hide();
605     p_intf->b_die = VLC_TRUE;
606 }
607
608 void MainInterface::updateVolume( int sliderVolume )
609 {
610     if( !b_my_volume )
611     {
612         int i_res = sliderVolume * AOUT_VOLUME_MAX /
613                             (2*ui.volumeSlider->maximum() );
614         aout_VolumeSet( p_intf, i_res );
615     }
616 }
617
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 )
621 {
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) );
626     return VLC_SUCCESS;
627 }