37f11bd6167c3bd81656856e131310d44e4305f5
[vlc.git] / 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 #include <QUrl>
40
41 #include <assert.h>
42 #include <vlc_keys.h>
43 #include <vlc_vout.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     /* Configuration */
85     settings = new QSettings( "VideoLAN", "VLC" );
86     settings->beginGroup( "MainWindow" );
87
88     need_components_update = false;
89     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
90     embeddedPlaylistWasActive = videoIsActive = false;
91
92     videoEmbeddedFlag = false;
93     if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
94
95     alwaysVideoFlag = false;
96     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
97         alwaysVideoFlag = true;
98
99     playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
100     advControlsEnabled= settings->value( "adv-controls", false ).toBool();
101     visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
102
103     /* UI */
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 );
112
113     setFocusPolicy( Qt::StrongFocus );
114     setAcceptDrops(true);
115
116     /* Init input manager */
117     MainInputManager::getInstance( p_intf );
118     ON_TIMEOUT( updateOnTimer() );
119
120     /* Volume control */
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( slider, sliderDragged( float ),
130              THEMIM->getIM(), sliderUpdate( float ) );
131
132     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
133     var_AddCallback( p_intf, "interaction", InteractCallback, this );
134     p_intf->b_interaction = VLC_TRUE;
135 }
136
137 MainInterface::~MainInterface()
138 {
139     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
140     settings->setValue( "adv-controls", advControlsEnabled );
141     settings->setValue( "pos", pos() );
142     settings->endGroup();
143     delete settings;
144     p_intf->b_interaction = VLC_FALSE;
145     var_DelCallback( p_intf, "interaction", InteractCallback, this );
146
147     p_intf->pf_request_window = NULL;
148     p_intf->pf_release_window = NULL;
149     p_intf->pf_control_window = NULL;
150 }
151
152 void MainInterface::handleMainUi( QSettings *settings )
153 {
154     QWidget *main = new QWidget( this );
155     setCentralWidget( main );
156     ui.setupUi( centralWidget() );
157
158     slider = new InputSlider( Qt::Horizontal, NULL );
159     ui.hboxLayout->insertWidget( 0, slider );
160
161     BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
162                       qtr("Previous"), prev() );
163     BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
164     BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
165     BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
166
167     /* Volume */
168     ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
169     ui.volumeSlider->setMaximum( 100 );
170     ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
171     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
172     ui.volMuteLabel->installEventFilter(h);
173     ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
174
175     BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
176                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
177                                                 qtr( "Open playlist" ) );
178     BUTTONACT( ui.playlistButton, playlist() );
179
180     /* Set initial size */
181     resize ( PREF_W, PREF_H );
182
183     addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
184
185     advControls = new ControlsWidget( p_intf );
186     ui.vboxLayout->insertWidget( 0, advControls );
187     advControls->updateGeometry();
188     if( !advControlsEnabled ) advControls->hide();
189     need_components_update = true;
190
191     visualSelector = new VisualSelector( p_intf );
192     ui.vboxLayout->insertWidget( 0, visualSelector );
193     visualSelector->hide();
194
195     if( alwaysVideoFlag )
196     {
197         bgWidget = new BackgroundWidget( p_intf );
198         bgWidget->widgetSize = settings->value( "backgroundSize",
199                                                 QSize( 200, 200 ) ).toSize();
200         bgWidget->resize( bgWidget->widgetSize );
201         bgWidget->updateGeometry();
202         ui.vboxLayout->insertWidget( 0, bgWidget );
203     }
204
205     if( videoEmbeddedFlag )
206     {
207         videoWidget = new VideoWidget( p_intf );
208         videoWidget->widgetSize = QSize( 1, 1 );
209         videoWidget->resize( videoWidget->widgetSize );
210         ui.vboxLayout->insertWidget( 0, videoWidget );
211
212         p_intf->pf_request_window  = ::DoRequest;
213         p_intf->pf_release_window  = ::DoRelease;
214         p_intf->pf_control_window  = ::DoControl;
215     }
216     setMinimumSize( PREF_W, addSize.height() );
217 }
218
219 /**********************************************************************
220  * Handling of the components
221  **********************************************************************/
222 void MainInterface::calculateInterfaceSize()
223 {
224     int width = 0, height = 0;
225     if( VISIBLE( bgWidget ) )
226     {
227         width = bgWidget->widgetSize.width();
228         height = bgWidget->widgetSize.height();
229         assert( !(playlistWidget && playlistWidget->isVisible() ) );
230     }
231     else if( VISIBLE( playlistWidget ) )
232     {
233         width = playlistWidget->widgetSize.width();
234         height = playlistWidget->widgetSize.height();
235     }
236     else if( videoIsActive )
237     {
238         width =  videoWidget->widgetSize.width() ;
239         height = videoWidget->widgetSize.height();
240     }
241     else
242     {
243         width = PREF_W - addSize.width();
244         height = PREF_H - addSize.height();
245     }
246     if( VISIBLE( visualSelector ) )
247         height += visualSelector->height();
248     if( VISIBLE( advControls) )
249     {
250         height += advControls->sizeHint().height();
251     }
252     mainSize = QSize( width + addSize.width(), height + addSize.height() );
253 }
254
255 void MainInterface::resizeEvent( QResizeEvent *e )
256 {
257     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
258     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
259     {
260         SET_WH( videoWidget, e->size().width() - addSize.width(),
261                              e->size().height()  - addSize.height() );
262         videoWidget->updateGeometry();
263     }
264     if( VISIBLE( playlistWidget ) )
265     {
266         SET_WH( playlistWidget , e->size().width() - addSize.width(),
267                                  e->size().height() - addSize.height() );
268         playlistWidget->updateGeometry();
269     }
270 }
271
272 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
273                                    int *pi_y, unsigned int *pi_width,
274                                    unsigned int *pi_height )
275 {
276     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
277     if( ret )
278     {
279         videoIsActive = true;
280         if( VISIBLE( playlistWidget ) )
281         {
282             embeddedPlaylistWasActive = true;
283             playlistWidget->hide();
284         }
285         bool bgWasVisible = false;
286         if( VISIBLE( bgWidget) )
287         {
288             bgWasVisible = true;
289             bgWidget->hide();
290         }
291         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
292         {
293             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
294         }
295         else /* Background widget available, use its size */
296         {
297             /* Ok, our visualizations are bad, so don't do this for the moment
298              * use the requested size anyway */
299             // videoWidget->widgetSize = bgWidget->widgeTSize;
300             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
301         }
302         videoWidget->updateGeometry(); /// FIXME: Needed ?
303         need_components_update = true;
304     }
305     return ret;
306 }
307
308 void MainInterface::releaseVideo( void *p_win )
309 {
310     videoWidget->release( p_win );
311     videoWidget->widgetSize = QSize( 0, 0 );
312     videoWidget->resize( videoWidget->widgetSize );
313
314     if( embeddedPlaylistWasActive )
315         playlistWidget->show();
316     else if( bgWidget )
317         bgWidget->show();
318
319     videoIsActive = false;
320     need_components_update = true;
321 }
322
323 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
324 {
325     int i_ret = VLC_EGENERIC;
326     switch( i_query )
327     {
328         case VOUT_GET_SIZE:
329         {
330             unsigned int *pi_width  = va_arg( args, unsigned int * );
331             unsigned int *pi_height = va_arg( args, unsigned int * );
332             *pi_width = videoWidget->widgetSize.width();
333             *pi_height = videoWidget->widgetSize.height();
334             i_ret = VLC_SUCCESS;
335             break;
336         }
337         case VOUT_SET_SIZE:
338         {
339             unsigned int i_width  = va_arg( args, unsigned int );
340             unsigned int i_height = va_arg( args, unsigned int );
341             videoWidget->widgetSize = QSize( i_width, i_height );
342             videoWidget->updateGeometry();
343             need_components_update = true;
344             i_ret = VLC_SUCCESS;
345             break;
346         }
347         case VOUT_SET_STAY_ON_TOP:
348         default:
349             msg_Warn( p_intf, "unsupported control query" );
350             break;
351     }
352     return i_ret;
353 }
354
355 void MainInterface::advanced()
356 {
357     if( !VISIBLE( advControls ) )
358     {
359         advControls->show();
360         advControlsEnabled = true;
361     }
362     else
363     {
364         advControls->hide();
365         advControlsEnabled = false;
366     }
367     doComponentsUpdate();
368 }
369
370 void MainInterface::visual()
371 {
372     if( !VISIBLE( visualSelector) )
373     {
374         visualSelector->show();
375         if( !THEMIM->getIM()->hasVideo() )
376         {
377             /* Show the background widget */
378         }
379         visualSelectorEnabled = true;
380     }
381     else
382     {
383         /* Stop any currently running visualization */
384         visualSelector->hide();
385         visualSelectorEnabled = false;
386     }
387     doComponentsUpdate();
388 }
389
390 void MainInterface::playlist()
391 {
392     // Toggle the playlist dialog
393     if( !playlistEmbeddedFlag )
394     {
395         if( playlistWidget )
396         {
397             /// \todo Destroy it
398         }
399         THEDP->playlistDialog();
400         return;
401     }
402
403     if( !playlistWidget )
404     {
405         PlaylistDialog::killInstance();
406         playlistWidget = new PlaylistWidget( p_intf );
407         ui.vboxLayout->insertWidget( 0, playlistWidget );
408         playlistWidget->widgetSize = settings->value( "playlistSize",
409                                                QSize( 650, 310 ) ).toSize();
410         playlistWidget->hide();
411     }
412     if( VISIBLE( playlistWidget ) )
413     {
414         playlistWidget->hide();
415         if( videoIsActive )
416         {
417             videoWidget->widgetSize = savedVideoSize;
418             videoWidget->resize( videoWidget->widgetSize );
419             videoWidget->updateGeometry();
420         }
421     }
422     else
423     {
424         playlistWidget->show();
425         if( videoIsActive )
426         {
427             savedVideoSize = videoWidget->widgetSize;
428             videoWidget->widgetSize.setHeight( 0 );
429             videoWidget->resize( videoWidget->widgetSize );
430             videoWidget->updateGeometry();
431         }
432         if( VISIBLE( bgWidget ) ) bgWidget->hide();
433     }
434     doComponentsUpdate();
435 }
436
437 /* Video widget cannot do this synchronously as it runs in another thread */
438 /* Well, could it, actually ? Probably dangerous ... */
439 void MainInterface::doComponentsUpdate()
440 {
441     calculateInterfaceSize();
442     resize( mainSize );
443 }
444
445 void MainInterface::undockPlaylist()
446 {
447     if( playlistWidget )
448     {
449         playlistWidget->hide();
450         playlistWidget->deleteLater();
451         ui.vboxLayout->removeWidget( playlistWidget );
452         playlistWidget = NULL;
453         playlistEmbeddedFlag = false;
454
455         menuBar()->clear();
456         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
457                                  visualSelectorEnabled);
458
459         if( videoIsActive )
460         {
461             videoWidget->widgetSize = savedVideoSize;
462             videoWidget->resize( videoWidget->widgetSize );
463             videoWidget->updateGeometry();
464         }
465
466         doComponentsUpdate();
467         THEDP->playlistDialog();
468     }
469 }
470
471 void MainInterface::customEvent( QEvent *event )
472 {
473     if( event->type() == PLDockEvent_Type )
474     {
475         PlaylistDialog::killInstance();
476         playlistEmbeddedFlag = true;
477         menuBar()->clear();
478         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
479                                 visualSelectorEnabled);
480         playlist();
481     }
482 }
483
484
485 /************************************************************************
486  * D&D
487  ************************************************************************/
488 void MainInterface::dropEvent(QDropEvent *event)
489 {
490      const QMimeData *mimeData = event->mimeData();
491
492      /* D&D of a subtitles file, add it on the fly */
493      if( mimeData->urls().size() == 1 )
494      {
495         if( THEMIM->getIM()->hasInput() )
496         {
497             if( input_AddSubtitles( THEMIM->getInput(),
498                                     qtu( mimeData->urls()[0].toString() ),
499                                     VLC_TRUE ) )
500             {
501                 event->acceptProposedAction();
502                 return;
503             }
504         }
505      }
506      bool first = true;
507      foreach( QUrl url, mimeData->urls() ) {
508         QString s = url.toString();
509         if( s.length() > 0 ) {
510             playlist_Add( THEPL, qtu(s), NULL,
511                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
512                           PLAYLIST_END, VLC_TRUE );
513             first = false;
514         }
515      }
516      event->acceptProposedAction();
517 }
518 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
519 {
520      event->acceptProposedAction();
521 }
522 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
523 {
524      event->acceptProposedAction();
525 }
526 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
527 {
528      event->accept();
529 }
530
531 /************************************************************************
532  * Other stuff
533  ************************************************************************/
534 void MainInterface::keyPressEvent( QKeyEvent *e )
535 {
536     int i_vlck = qtEventToVLCKey( e );
537     if( i_vlck >= 0 )
538     {
539         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
540         e->accept();
541     }
542     else
543         e->ignore();
544 }
545
546 void MainInterface::stop()
547 {
548     playlist_Stop( THEPL );
549 }
550 void MainInterface::play()
551 {
552     if( playlist_IsEmpty(THEPL) )
553     {
554         /* The playlist is empty, open a file requester */
555         THEDP->simpleOpenDialog();
556         setStatus( 0 );
557         return;
558     }
559     THEMIM->togglePlayPause();
560 }
561 void MainInterface::prev()
562 {
563     playlist_Prev( THEPL );
564 }
565 void MainInterface::next()
566 {
567     playlist_Next( THEPL );
568 }
569
570 void MainInterface::setDisplay( float pos, int time, int length )
571 {
572     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
573     secstotimestr( psz_length, length );
574     secstotimestr( psz_time, time );
575     QString title;
576     title.sprintf( "%s/%s", psz_time, psz_length );
577     timeLabel->setText( " "+title+" " );
578 }
579
580 void MainInterface::setName( QString name )
581 {
582     nameLabel->setText( " " + name+" " );
583 }
584
585 void MainInterface::setStatus( int status )
586 {
587     if( status == 1 ) // Playing
588         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
589     else
590         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
591 }
592
593 static bool b_my_volume;
594
595 void MainInterface::updateOnTimer()
596 {
597     /* \todo Make this event-driven */
598     advControls->enableInput( THEMIM->getIM()->hasInput() );
599     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
600
601     if( intf_ShouldDie( p_intf ) )
602     {
603         QApplication::closeAllWindows();
604         QApplication::quit();
605     }
606     if( need_components_update )
607     {
608         doComponentsUpdate();
609         need_components_update = false;
610     }
611
612     audio_volume_t i_volume;
613     aout_VolumeGet( p_intf, &i_volume );
614     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
615     int i_gauge = ui.volumeSlider->value();
616     b_my_volume = false;
617     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
618     {
619         b_my_volume = true;
620         ui.volumeSlider->setValue( i_volume );
621         b_my_volume = false;
622     }
623 }
624
625 void MainInterface::closeEvent( QCloseEvent *e )
626 {
627     hide();
628     p_intf->b_die = VLC_TRUE;
629 }
630
631 void MainInterface::updateVolume( int sliderVolume )
632 {
633     if( !b_my_volume )
634     {
635         int i_res = sliderVolume * AOUT_VOLUME_MAX /
636                             (2*ui.volumeSlider->maximum() );
637         aout_VolumeSet( p_intf, i_res );
638     }
639 }
640
641 static int InteractCallback( vlc_object_t *p_this,
642                              const char *psz_var, vlc_value_t old_val,
643                              vlc_value_t new_val, void *param )
644 {
645     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
646     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
647     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
648     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
649     return VLC_SUCCESS;
650 }